@zwa73/utils 1.0.118 → 1.0.120
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/QuickFunction.d.ts +1 -1
- package/dist/QuickFunction.js +2 -2
- package/dist/UtilFfmpegTools.js +3 -3
- package/dist/UtilFileTools.d.ts +15 -4
- package/dist/UtilFileTools.js +47 -22
- package/dist/UtilLogger.js +2 -2
- package/package.json +2 -1
- package/src/QuickFunction.ts +4 -1
- package/src/UtilFfmpegTools.ts +1 -1
- package/src/UtilFileTools.ts +37 -19
- package/src/UtilLogger.ts +1 -1
package/dist/QuickFunction.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import { UtilFT } from "./UtilFileTools";
|
|
2
2
|
import { UtilFunc } from "./UtilFunctions";
|
|
3
3
|
export declare const outcome: typeof UtilFunc.outcome, matchProc: typeof UtilFunc.matchProc, sucesProc: typeof UtilFunc.sucesProc, isFailed: typeof UtilFunc.isFailed, isSafeNumber: typeof UtilFunc.isSafeNumber, isSuccess: typeof UtilFunc.isSuccess, likeNone: typeof UtilFunc.likeNone, expect: typeof UtilFunc.expect, assertType: typeof UtilFunc.assertType, assertLiteral: typeof UtilFunc.assertLiteral, deepClone: typeof UtilFunc.deepClone, sleep: typeof UtilFunc.sleep, stringifyJToken: typeof UtilFunc.stringifyJToken, getTime: typeof UtilFunc.getTime, mapEntries: typeof UtilFunc.mapEntries, dedent: typeof UtilFunc.dedent, throwError: typeof UtilFunc.throwError;
|
|
4
|
-
export declare const stylizePath: typeof UtilFT.stylizePath;
|
|
4
|
+
export declare const stylizePath: typeof UtilFT.stylizePath, posixizePath: typeof UtilFT.posixizePath, win32izePath: typeof UtilFT.win32izePath, currosizePath: typeof UtilFT.currosizePath;
|
package/dist/QuickFunction.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.stylizePath = exports.throwError = exports.dedent = exports.mapEntries = exports.getTime = exports.stringifyJToken = exports.sleep = exports.deepClone = exports.assertLiteral = exports.assertType = exports.expect = exports.likeNone = exports.isSuccess = exports.isSafeNumber = exports.isFailed = exports.sucesProc = exports.matchProc = exports.outcome = void 0;
|
|
3
|
+
exports.currosizePath = exports.win32izePath = exports.posixizePath = exports.stylizePath = exports.throwError = exports.dedent = exports.mapEntries = exports.getTime = exports.stringifyJToken = exports.sleep = exports.deepClone = exports.assertLiteral = exports.assertType = exports.expect = exports.likeNone = exports.isSuccess = exports.isSafeNumber = exports.isFailed = exports.sucesProc = exports.matchProc = exports.outcome = void 0;
|
|
4
4
|
const UtilFileTools_1 = require("./UtilFileTools");
|
|
5
5
|
const UtilFunctions_1 = require("./UtilFunctions");
|
|
6
6
|
const UtilSymbol_1 = require("./UtilSymbol");
|
|
7
7
|
exports.outcome = UtilFunctions_1.UtilFunc.outcome, exports.matchProc = UtilFunctions_1.UtilFunc.matchProc, exports.sucesProc = UtilFunctions_1.UtilFunc.sucesProc, exports.isFailed = UtilFunctions_1.UtilFunc.isFailed, exports.isSafeNumber = UtilFunctions_1.UtilFunc.isSafeNumber, exports.isSuccess = UtilFunctions_1.UtilFunc.isSuccess, exports.likeNone = UtilFunctions_1.UtilFunc.likeNone, exports.expect = UtilFunctions_1.UtilFunc.expect, exports.assertType = UtilFunctions_1.UtilFunc.assertType, exports.assertLiteral = UtilFunctions_1.UtilFunc.assertLiteral, exports.deepClone = UtilFunctions_1.UtilFunc.deepClone, exports.sleep = UtilFunctions_1.UtilFunc.sleep, exports.stringifyJToken = UtilFunctions_1.UtilFunc.stringifyJToken, exports.getTime = UtilFunctions_1.UtilFunc.getTime, exports.mapEntries = UtilFunctions_1.UtilFunc.mapEntries, exports.dedent = UtilFunctions_1.UtilFunc.dedent, exports.throwError = UtilFunctions_1.UtilFunc.throwError;
|
|
8
|
-
exports.stylizePath = UtilFileTools_1.UtilFT.stylizePath;
|
|
8
|
+
exports.stylizePath = UtilFileTools_1.UtilFT.stylizePath, exports.posixizePath = UtilFileTools_1.UtilFT.posixizePath, exports.win32izePath = UtilFileTools_1.UtilFT.win32izePath, exports.currosizePath = UtilFileTools_1.UtilFT.currosizePath;
|
|
9
9
|
if (false) {
|
|
10
10
|
let aaser = (0, exports.assertLiteral)({ a: 1 });
|
|
11
11
|
let a = null;
|
package/dist/UtilFfmpegTools.js
CHANGED
|
@@ -28,7 +28,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
28
28
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
29
|
exports.SFfmpegTool = void 0;
|
|
30
30
|
const fluent_ffmpeg_1 = __importDefault(require("fluent-ffmpeg"));
|
|
31
|
-
const
|
|
31
|
+
const pathe_1 = __importDefault(require("pathe"));
|
|
32
32
|
const fs = __importStar(require("fs"));
|
|
33
33
|
const UtilLogger_1 = require("./UtilLogger");
|
|
34
34
|
const UtilClass_1 = require("./UtilClass");
|
|
@@ -39,7 +39,7 @@ class SFfmpegTool {
|
|
|
39
39
|
static init() {
|
|
40
40
|
const ffmpegPath = process.env.FFMPEG_PATH;
|
|
41
41
|
if (ffmpegPath != null) {
|
|
42
|
-
const exepath =
|
|
42
|
+
const exepath = pathe_1.default.join(ffmpegPath, "ffmpeg.exe");
|
|
43
43
|
SFfmpegTool.setFfmpegPath(exepath);
|
|
44
44
|
}
|
|
45
45
|
}
|
|
@@ -70,7 +70,7 @@ class SFfmpegTool {
|
|
|
70
70
|
* @param quality - 质量
|
|
71
71
|
*/
|
|
72
72
|
static async flac2ogg(inputFlacFile, outputOggPath, quality = 10) {
|
|
73
|
-
let wavPath =
|
|
73
|
+
let wavPath = pathe_1.default.join(pathe_1.default.dirname(inputFlacFile), `tmp_${pathe_1.default.basename(inputFlacFile, ".flac")}.wav`);
|
|
74
74
|
await new Promise((resolve, reject) => {
|
|
75
75
|
(0, fluent_ffmpeg_1.default)(inputFlacFile)
|
|
76
76
|
.audioCodec("pcm_s16le")
|
package/dist/UtilFileTools.d.ts
CHANGED
|
@@ -17,15 +17,11 @@ type EnsurePathExistsOpt = Partial<{
|
|
|
17
17
|
type FileSearchGlobOpt = Partial<{
|
|
18
18
|
/**忽略的文件 默认 undefined */
|
|
19
19
|
ingore: string | string[];
|
|
20
|
-
/**输出的路径风格 默认跟随系统 */
|
|
21
|
-
style: "posix" | "win32";
|
|
22
20
|
}>;
|
|
23
21
|
/**regex搜索选项 */
|
|
24
22
|
type FileSearchRegexOpt = Partial<{
|
|
25
23
|
/**搜索子目录 默认 true */
|
|
26
24
|
relative: boolean;
|
|
27
|
-
/**输出的路径风格 默认跟随系统 */
|
|
28
|
-
style: "posix" | "win32";
|
|
29
25
|
}>;
|
|
30
26
|
/**文件工具 */
|
|
31
27
|
export declare namespace UtilFT {
|
|
@@ -107,6 +103,21 @@ export declare namespace UtilFT {
|
|
|
107
103
|
* @returns 新的符合目标风格的路径
|
|
108
104
|
*/
|
|
109
105
|
function stylizePath(filePath: string, style?: 'win32' | 'posix'): string;
|
|
106
|
+
/**保证路径为POSIX风格
|
|
107
|
+
* @param filePath - 输入路径
|
|
108
|
+
* @returns 新的符合POSIX风格的路径
|
|
109
|
+
*/
|
|
110
|
+
function posixizePath(filePath: string): string;
|
|
111
|
+
/**保证路径为Win32风格
|
|
112
|
+
* @param filePath - 输入路径
|
|
113
|
+
* @returns 新的符合Win32风格的路径
|
|
114
|
+
*/
|
|
115
|
+
function win32izePath(filePath: string): string;
|
|
116
|
+
/**保证路径为当前系统风格
|
|
117
|
+
* @param filePath - 输入路径
|
|
118
|
+
* @returns 新的符合当前系统风格的路径
|
|
119
|
+
*/
|
|
120
|
+
function currosizePath(filePath: string): string;
|
|
110
121
|
/**搜索路径符合正则表达式的文件
|
|
111
122
|
* @param dir - 起始目录
|
|
112
123
|
* @param traitRegex - 正则表达式
|
package/dist/UtilFileTools.js
CHANGED
|
@@ -22,10 +22,14 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
22
22
|
__setModuleDefault(result, mod);
|
|
23
23
|
return result;
|
|
24
24
|
};
|
|
25
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
+
};
|
|
25
28
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
29
|
exports.UtilFT = void 0;
|
|
27
30
|
const fs = __importStar(require("fs"));
|
|
28
|
-
const
|
|
31
|
+
const pathe_1 = __importDefault(require("pathe"));
|
|
32
|
+
const os = __importStar(require("os"));
|
|
29
33
|
const UtilLogger_1 = require("./UtilLogger");
|
|
30
34
|
const JSON5 = __importStar(require("json5"));
|
|
31
35
|
const glob_1 = require("glob");
|
|
@@ -72,13 +76,13 @@ var UtilFT;
|
|
|
72
76
|
*/
|
|
73
77
|
async function createPath(filePath, opt) {
|
|
74
78
|
if (opt?.dir == true)
|
|
75
|
-
filePath =
|
|
79
|
+
filePath = pathe_1.default.join(filePath, pathe_1.default.sep);
|
|
76
80
|
try {
|
|
77
|
-
if (filePath.endsWith(
|
|
81
|
+
if (filePath.endsWith(pathe_1.default.sep)) {
|
|
78
82
|
await fs.promises.mkdir(filePath, { recursive: true });
|
|
79
83
|
return true;
|
|
80
84
|
}
|
|
81
|
-
await fs.promises.mkdir(
|
|
85
|
+
await fs.promises.mkdir(pathe_1.default.dirname(filePath), { recursive: true });
|
|
82
86
|
const filehandle = await fs.promises.open(filePath, 'w');
|
|
83
87
|
await filehandle.close();
|
|
84
88
|
return true;
|
|
@@ -97,13 +101,13 @@ var UtilFT;
|
|
|
97
101
|
*/
|
|
98
102
|
function createPathSync(filePath, opt) {
|
|
99
103
|
if (opt?.dir == true)
|
|
100
|
-
filePath =
|
|
104
|
+
filePath = pathe_1.default.join(filePath, pathe_1.default.sep);
|
|
101
105
|
try {
|
|
102
|
-
if (filePath.endsWith(
|
|
106
|
+
if (filePath.endsWith(pathe_1.default.sep)) {
|
|
103
107
|
fs.mkdirSync(filePath, { recursive: true });
|
|
104
108
|
return true;
|
|
105
109
|
}
|
|
106
|
-
fs.mkdirSync(
|
|
110
|
+
fs.mkdirSync(pathe_1.default.dirname(filePath), { recursive: true });
|
|
107
111
|
fs.openSync(filePath, 'w');
|
|
108
112
|
return true;
|
|
109
113
|
}
|
|
@@ -139,7 +143,7 @@ var UtilFT;
|
|
|
139
143
|
}
|
|
140
144
|
UtilFT.ensurePathExistsSync = ensurePathExistsSync;
|
|
141
145
|
function loadJSONFileSync(filePath, def) {
|
|
142
|
-
if (
|
|
146
|
+
if (pathe_1.default.extname(filePath) !== '.json')
|
|
143
147
|
filePath += '.json';
|
|
144
148
|
let str = "";
|
|
145
149
|
// 判断文件路径是否存在
|
|
@@ -155,7 +159,7 @@ var UtilFT;
|
|
|
155
159
|
}
|
|
156
160
|
UtilFT.loadJSONFileSync = loadJSONFileSync;
|
|
157
161
|
async function loadJSONFile(filePath, def) {
|
|
158
|
-
if (
|
|
162
|
+
if (pathe_1.default.extname(filePath) !== '.json')
|
|
159
163
|
filePath += '.json';
|
|
160
164
|
let str = "";
|
|
161
165
|
// 判断文件路径是否存在
|
|
@@ -178,7 +182,7 @@ var UtilFT;
|
|
|
178
182
|
*/
|
|
179
183
|
async function writeJSONFile(filePath, token) {
|
|
180
184
|
let str = UtilFunctions_1.UtilFunc.stringifyJToken(token);
|
|
181
|
-
if (
|
|
185
|
+
if (pathe_1.default.extname(filePath) !== '.json')
|
|
182
186
|
filePath += '.json';
|
|
183
187
|
// 判断文件路径是否存在 不存在则创建
|
|
184
188
|
if (!(await pathExists(filePath)))
|
|
@@ -207,6 +211,33 @@ var UtilFT;
|
|
|
207
211
|
});
|
|
208
212
|
}
|
|
209
213
|
UtilFT.stylizePath = stylizePath;
|
|
214
|
+
/**保证路径为POSIX风格
|
|
215
|
+
* @param filePath - 输入路径
|
|
216
|
+
* @returns 新的符合POSIX风格的路径
|
|
217
|
+
*/
|
|
218
|
+
function posixizePath(filePath) {
|
|
219
|
+
return stylizePath(filePath, 'posix');
|
|
220
|
+
}
|
|
221
|
+
UtilFT.posixizePath = posixizePath;
|
|
222
|
+
/**保证路径为Win32风格
|
|
223
|
+
* @param filePath - 输入路径
|
|
224
|
+
* @returns 新的符合Win32风格的路径
|
|
225
|
+
*/
|
|
226
|
+
function win32izePath(filePath) {
|
|
227
|
+
return stylizePath(filePath, 'win32');
|
|
228
|
+
}
|
|
229
|
+
UtilFT.win32izePath = win32izePath;
|
|
230
|
+
/**保证路径为当前系统风格
|
|
231
|
+
* @param filePath - 输入路径
|
|
232
|
+
* @returns 新的符合当前系统风格的路径
|
|
233
|
+
*/
|
|
234
|
+
function currosizePath(filePath) {
|
|
235
|
+
const system = os.platform();
|
|
236
|
+
if (system === 'win32')
|
|
237
|
+
return win32izePath(filePath);
|
|
238
|
+
return posixizePath(filePath);
|
|
239
|
+
}
|
|
240
|
+
UtilFT.currosizePath = currosizePath;
|
|
210
241
|
/**搜索路径符合正则表达式的文件
|
|
211
242
|
* @param dir - 起始目录
|
|
212
243
|
* @param traitRegex - 正则表达式
|
|
@@ -216,26 +247,23 @@ var UtilFT;
|
|
|
216
247
|
* @returns 文件名路径数组
|
|
217
248
|
*/
|
|
218
249
|
function fileSearchRegex(dir, traitRegex, opt) {
|
|
219
|
-
dir = stylizePath(dir, opt?.style);
|
|
220
|
-
const fixpath = opt?.style === undefined ? path :
|
|
221
|
-
opt.style === 'posix' ? path.posix : path.win32;
|
|
222
250
|
const relative = opt?.relative ?? true;
|
|
223
251
|
const outArray = [];
|
|
224
252
|
const subFiles = fs.readdirSync(dir, { withFileTypes: true });
|
|
225
253
|
//如果使用 g 会导致 RegExp.lastIndex 更改, 将会导致匹配错误
|
|
226
254
|
const regex = new RegExp(traitRegex);
|
|
227
255
|
for (const subFile of subFiles) {
|
|
228
|
-
const subFilePath =
|
|
256
|
+
const subFilePath = pathe_1.default.join(dir, subFile.name);
|
|
229
257
|
//判断是否是文件夹,递归调用
|
|
230
258
|
if (subFile.isDirectory()) {
|
|
231
259
|
if (relative)
|
|
232
|
-
outArray.push(...fileSearchRegex(
|
|
260
|
+
outArray.push(...fileSearchRegex(pathe_1.default.join(subFilePath, pathe_1.default.sep), traitRegex));
|
|
233
261
|
continue;
|
|
234
262
|
}
|
|
235
263
|
if (regex.test(subFilePath))
|
|
236
264
|
outArray.push(subFilePath);
|
|
237
265
|
}
|
|
238
|
-
return outArray.map((filePath) =>
|
|
266
|
+
return outArray.map((filePath) => pathe_1.default.normalize(filePath));
|
|
239
267
|
}
|
|
240
268
|
UtilFT.fileSearchRegex = fileSearchRegex;
|
|
241
269
|
/**搜索符合Glob匹配的文件
|
|
@@ -247,14 +275,11 @@ var UtilFT;
|
|
|
247
275
|
* @returns 文件绝对路径数组
|
|
248
276
|
*/
|
|
249
277
|
function fileSearchGlob(dir, globPattern, opt) {
|
|
250
|
-
dir = stylizePath(dir, opt?.style);
|
|
251
|
-
const fixpath = opt?.style === undefined ? path :
|
|
252
|
-
opt.style === 'posix' ? path.posix : path.win32;
|
|
253
278
|
const fixedPath = typeof globPattern === "string"
|
|
254
|
-
?
|
|
255
|
-
: globPattern.map((p) =>
|
|
279
|
+
? pathe_1.default.join(dir, globPattern)
|
|
280
|
+
: globPattern.map((p) => pathe_1.default.join(dir, p));
|
|
256
281
|
return (0, glob_1.globSync)(fixedPath, { ignore: opt?.ingore, absolute: true })
|
|
257
|
-
.map((filePath) =>
|
|
282
|
+
.map((filePath) => pathe_1.default.normalize(filePath));
|
|
258
283
|
}
|
|
259
284
|
UtilFT.fileSearchGlob = fileSearchGlob;
|
|
260
285
|
/**
|
package/dist/UtilLogger.js
CHANGED
|
@@ -27,7 +27,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
27
27
|
};
|
|
28
28
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
29
|
exports.SLogger = void 0;
|
|
30
|
-
const
|
|
30
|
+
const pathe_1 = __importDefault(require("pathe"));
|
|
31
31
|
const winston = __importStar(require("winston"));
|
|
32
32
|
const winston_daily_rotate_file_1 = __importDefault(require("winston-daily-rotate-file"));
|
|
33
33
|
const util_1 = require("util");
|
|
@@ -83,7 +83,7 @@ class SLogger {
|
|
|
83
83
|
return `[${info.timestamp}] [${level.toUpperCase()}]: ${messageList.join("\n")}`;
|
|
84
84
|
}));
|
|
85
85
|
transports.push(new winston_daily_rotate_file_1.default({
|
|
86
|
-
filename:
|
|
86
|
+
filename: pathe_1.default.join(outFloder, 'log-%DATE%.txt'),
|
|
87
87
|
datePattern: 'YYYY-MM-DD',
|
|
88
88
|
level: fileLevel,
|
|
89
89
|
format: fileFormat,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zwa73/utils",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.120",
|
|
4
4
|
"description": "my utils",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -25,6 +25,7 @@
|
|
|
25
25
|
"http-proxy-agent": "^5.0.0",
|
|
26
26
|
"https-proxy-agent": "^5.0.1",
|
|
27
27
|
"json5": "^2.2.3",
|
|
28
|
+
"pathe": "^1.1.2",
|
|
28
29
|
"tiktoken": "^1.0.7",
|
|
29
30
|
"winston": "^3.10.0",
|
|
30
31
|
"winston-daily-rotate-file": "^4.7.1"
|
package/src/QuickFunction.ts
CHANGED
package/src/UtilFfmpegTools.ts
CHANGED
package/src/UtilFileTools.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import * as fs from "fs";
|
|
2
|
-
import
|
|
2
|
+
import path from 'pathe';
|
|
3
|
+
import * as os from "os";
|
|
3
4
|
import { JObject, JToken } from "@src/UtilInterfaces";
|
|
4
5
|
import { SLogger } from "@src/UtilLogger";
|
|
5
6
|
import * as JSON5 from 'json5';
|
|
@@ -26,16 +27,12 @@ type EnsurePathExistsOpt = Partial<{
|
|
|
26
27
|
type FileSearchGlobOpt = Partial<{
|
|
27
28
|
/**忽略的文件 默认 undefined */
|
|
28
29
|
ingore:string|string[];
|
|
29
|
-
/**输出的路径风格 默认跟随系统 */
|
|
30
|
-
style:"posix"|"win32";
|
|
31
30
|
}>
|
|
32
31
|
|
|
33
32
|
/**regex搜索选项 */
|
|
34
33
|
type FileSearchRegexOpt = Partial<{
|
|
35
34
|
/**搜索子目录 默认 true */
|
|
36
35
|
relative:boolean;
|
|
37
|
-
/**输出的路径风格 默认跟随系统 */
|
|
38
|
-
style:"posix"|"win32";
|
|
39
36
|
}>
|
|
40
37
|
|
|
41
38
|
/**文件工具 */
|
|
@@ -242,6 +239,35 @@ export function stylizePath(filePath:string,style?:'win32'|'posix'){
|
|
|
242
239
|
win32: (nor) => filePath.replaceAll("/", "\\"),
|
|
243
240
|
});
|
|
244
241
|
}
|
|
242
|
+
/**保证路径为POSIX风格
|
|
243
|
+
* @param filePath - 输入路径
|
|
244
|
+
* @returns 新的符合POSIX风格的路径
|
|
245
|
+
*/
|
|
246
|
+
export function posixizePath(filePath: string): string {
|
|
247
|
+
return stylizePath(filePath, 'posix');
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/**保证路径为Win32风格
|
|
251
|
+
* @param filePath - 输入路径
|
|
252
|
+
* @returns 新的符合Win32风格的路径
|
|
253
|
+
*/
|
|
254
|
+
export function win32izePath(filePath: string): string {
|
|
255
|
+
return stylizePath(filePath, 'win32');
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/**保证路径为当前系统风格
|
|
259
|
+
* @param filePath - 输入路径
|
|
260
|
+
* @returns 新的符合当前系统风格的路径
|
|
261
|
+
*/
|
|
262
|
+
export function currosizePath(filePath: string): string {
|
|
263
|
+
const system = os.platform();
|
|
264
|
+
if (system === 'win32')
|
|
265
|
+
return win32izePath(filePath);
|
|
266
|
+
return posixizePath(filePath);
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
|
|
270
|
+
|
|
245
271
|
|
|
246
272
|
/**搜索路径符合正则表达式的文件
|
|
247
273
|
* @param dir - 起始目录
|
|
@@ -252,26 +278,22 @@ export function stylizePath(filePath:string,style?:'win32'|'posix'){
|
|
|
252
278
|
* @returns 文件名路径数组
|
|
253
279
|
*/
|
|
254
280
|
export function fileSearchRegex(dir: string, traitRegex: string, opt?:FileSearchRegexOpt) {
|
|
255
|
-
dir = stylizePath(dir,opt?.style);
|
|
256
|
-
const fixpath = opt?.style === undefined ? path :
|
|
257
|
-
opt.style ==='posix' ? path.posix : path.win32;
|
|
258
|
-
|
|
259
281
|
const relative = opt?.relative ?? true;
|
|
260
282
|
const outArray: string[] = [];
|
|
261
283
|
const subFiles = fs.readdirSync(dir, { withFileTypes: true });
|
|
262
284
|
//如果使用 g 会导致 RegExp.lastIndex 更改, 将会导致匹配错误
|
|
263
285
|
const regex = new RegExp(traitRegex);
|
|
264
286
|
for (const subFile of subFiles) {
|
|
265
|
-
const subFilePath =
|
|
287
|
+
const subFilePath = path.join(dir, subFile.name);
|
|
266
288
|
//判断是否是文件夹,递归调用
|
|
267
289
|
if (subFile.isDirectory()) {
|
|
268
290
|
if (relative)
|
|
269
|
-
outArray.push(...fileSearchRegex(
|
|
291
|
+
outArray.push(...fileSearchRegex(path.join(subFilePath, path.sep), traitRegex));
|
|
270
292
|
continue;
|
|
271
293
|
}
|
|
272
294
|
if (regex.test(subFilePath)) outArray.push(subFilePath);
|
|
273
295
|
}
|
|
274
|
-
return outArray.map((filePath) =>
|
|
296
|
+
return outArray.map((filePath) => path.normalize(filePath));
|
|
275
297
|
}
|
|
276
298
|
/**搜索符合Glob匹配的文件
|
|
277
299
|
* @param dir - 起始目录
|
|
@@ -282,15 +304,11 @@ export function fileSearchRegex(dir: string, traitRegex: string, opt?:FileSearch
|
|
|
282
304
|
* @returns 文件绝对路径数组
|
|
283
305
|
*/
|
|
284
306
|
export function fileSearchGlob(dir: string, globPattern:string|string[],opt?:FileSearchGlobOpt){
|
|
285
|
-
dir = stylizePath(dir,opt?.style);
|
|
286
|
-
const fixpath = opt?.style === undefined ? path :
|
|
287
|
-
opt.style ==='posix' ? path.posix : path.win32;
|
|
288
|
-
|
|
289
307
|
const fixedPath = typeof globPattern === "string"
|
|
290
|
-
?
|
|
291
|
-
: globPattern.map((p)=>
|
|
308
|
+
? path.join(dir,globPattern)
|
|
309
|
+
: globPattern.map((p)=>path.join(dir,p));
|
|
292
310
|
return globSync(fixedPath, { ignore: opt?.ingore, absolute: true })
|
|
293
|
-
.map((filePath) =>
|
|
311
|
+
.map((filePath) => path.normalize(filePath));
|
|
294
312
|
}
|
|
295
313
|
/**
|
|
296
314
|
* @deprecated 请使用 fileSearchRegex 或 fileSearchGlob
|
package/src/UtilLogger.ts
CHANGED