@zwa73/utils 1.0.108 → 1.0.110
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/UtilFileTools.d.ts +7 -1
- package/dist/UtilFileTools.js +22 -3
- package/package.json +1 -1
- package/src/UtilFileTools.ts +27 -5
package/dist/UtilFileTools.d.ts
CHANGED
|
@@ -17,11 +17,15 @@ type EnsurePathExistsOpt = Partial<{
|
|
|
17
17
|
type FileSearchGlobOpt = Partial<{
|
|
18
18
|
/**忽略的文件 默认 undefined */
|
|
19
19
|
ingore: string | string[];
|
|
20
|
+
/**输出的路径风格 默认跟随系统 */
|
|
21
|
+
normalize: "posix" | "win32";
|
|
20
22
|
}>;
|
|
21
23
|
/**regex搜索选项 */
|
|
22
24
|
type FileSearchRegexOpt = Partial<{
|
|
23
25
|
/**搜索子目录 默认 true */
|
|
24
26
|
relative: boolean;
|
|
27
|
+
/**输出的路径风格 默认跟随系统 */
|
|
28
|
+
normalize: "posix" | "win32";
|
|
25
29
|
}>;
|
|
26
30
|
/**文件工具 */
|
|
27
31
|
export declare namespace UtilFT {
|
|
@@ -102,6 +106,7 @@ export declare namespace UtilFT {
|
|
|
102
106
|
* @param traitRegex - 正则表达式
|
|
103
107
|
* @param opt - 可选参数
|
|
104
108
|
* @param opt.relative - 搜索子目录
|
|
109
|
+
* @param opt.normalize - 输出的路径风格 默认跟随系统
|
|
105
110
|
* @returns 文件名路径数组
|
|
106
111
|
*/
|
|
107
112
|
function fileSearchRegex(dir: string, traitRegex: string, opt?: FileSearchRegexOpt): string[];
|
|
@@ -109,7 +114,8 @@ export declare namespace UtilFT {
|
|
|
109
114
|
* @param dir - 起始目录
|
|
110
115
|
* @param globPattern - glob匹配
|
|
111
116
|
* @param opt - 可选参数
|
|
112
|
-
* @param opt.ignore
|
|
117
|
+
* @param opt.ignore - 忽略的文件
|
|
118
|
+
* @param opt.normalize - 输出的路径风格 默认跟随系统
|
|
113
119
|
* @returns 文件绝对路径数组
|
|
114
120
|
*/
|
|
115
121
|
function fileSearchGlob(dir: string, globPattern: string | string[], opt?: FileSearchGlobOpt): string[];
|
package/dist/UtilFileTools.js
CHANGED
|
@@ -30,6 +30,7 @@ const UtilLogger_1 = require("./UtilLogger");
|
|
|
30
30
|
const JSON5 = __importStar(require("json5"));
|
|
31
31
|
const glob_1 = require("glob");
|
|
32
32
|
const UtilFunctions_1 = require("./UtilFunctions");
|
|
33
|
+
const QuickFunction_1 = require("./QuickFunction");
|
|
33
34
|
/**文件工具 */
|
|
34
35
|
var UtilFT;
|
|
35
36
|
(function (UtilFT) {
|
|
@@ -198,12 +199,14 @@ var UtilFT;
|
|
|
198
199
|
* @param traitRegex - 正则表达式
|
|
199
200
|
* @param opt - 可选参数
|
|
200
201
|
* @param opt.relative - 搜索子目录
|
|
202
|
+
* @param opt.normalize - 输出的路径风格 默认跟随系统
|
|
201
203
|
* @returns 文件名路径数组
|
|
202
204
|
*/
|
|
203
205
|
function fileSearchRegex(dir, traitRegex, opt) {
|
|
204
206
|
const relative = opt?.relative ?? true;
|
|
205
207
|
const outArray = [];
|
|
206
208
|
const subFiles = fs.readdirSync(dir, { withFileTypes: true });
|
|
209
|
+
//如果使用 g 会导致 RegExp.lastIndex 更改, 将会导致匹配错误
|
|
207
210
|
const regex = new RegExp(traitRegex);
|
|
208
211
|
for (const subFile of subFiles) {
|
|
209
212
|
const subFilePath = path.join(dir, subFile.name);
|
|
@@ -216,21 +219,37 @@ var UtilFT;
|
|
|
216
219
|
if (regex.test(subFilePath))
|
|
217
220
|
outArray.push(subFilePath);
|
|
218
221
|
}
|
|
219
|
-
return outArray
|
|
222
|
+
return outArray.map((filePath) => {
|
|
223
|
+
if (opt?.normalize === undefined)
|
|
224
|
+
return filePath;
|
|
225
|
+
return (0, QuickFunction_1.matchProc)(opt.normalize, {
|
|
226
|
+
posix: (nor) => path[nor].normalize(filePath.replaceAll("\\", "/")),
|
|
227
|
+
win32: (nor) => path[nor].normalize(filePath.replaceAll("/", "\\")),
|
|
228
|
+
});
|
|
229
|
+
});
|
|
220
230
|
}
|
|
221
231
|
UtilFT.fileSearchRegex = fileSearchRegex;
|
|
222
232
|
/**搜索符合Glob匹配的文件
|
|
223
233
|
* @param dir - 起始目录
|
|
224
234
|
* @param globPattern - glob匹配
|
|
225
235
|
* @param opt - 可选参数
|
|
226
|
-
* @param opt.ignore
|
|
236
|
+
* @param opt.ignore - 忽略的文件
|
|
237
|
+
* @param opt.normalize - 输出的路径风格 默认跟随系统
|
|
227
238
|
* @returns 文件绝对路径数组
|
|
228
239
|
*/
|
|
229
240
|
function fileSearchGlob(dir, globPattern, opt) {
|
|
230
241
|
const fixedPath = typeof globPattern === "string"
|
|
231
242
|
? path.join(dir, path.posix.normalize(globPattern))
|
|
232
243
|
: globPattern.map((p) => path.join(dir, path.posix.normalize(p)));
|
|
233
|
-
return (0, glob_1.globSync)(fixedPath, { ignore: opt?.ingore, absolute: true })
|
|
244
|
+
return (0, glob_1.globSync)(fixedPath, { ignore: opt?.ingore, absolute: true })
|
|
245
|
+
.map((filePath) => {
|
|
246
|
+
if (opt?.normalize === undefined)
|
|
247
|
+
return filePath;
|
|
248
|
+
return (0, QuickFunction_1.matchProc)(opt.normalize, {
|
|
249
|
+
posix: (nor) => path[nor].normalize(filePath.replaceAll("\\", "/")),
|
|
250
|
+
win32: (nor) => path[nor].normalize(filePath.replaceAll("/", "\\")),
|
|
251
|
+
});
|
|
252
|
+
});
|
|
234
253
|
}
|
|
235
254
|
UtilFT.fileSearchGlob = fileSearchGlob;
|
|
236
255
|
/**
|
package/package.json
CHANGED
package/src/UtilFileTools.ts
CHANGED
|
@@ -5,6 +5,7 @@ import { SLogger } from "@src/UtilLogger";
|
|
|
5
5
|
import * as JSON5 from 'json5';
|
|
6
6
|
import { globSync } from "glob";
|
|
7
7
|
import { UtilFunc } from "./UtilFunctions";
|
|
8
|
+
import { matchProc } from "./QuickFunction";
|
|
8
9
|
|
|
9
10
|
/**创建文件选项 */
|
|
10
11
|
type CreatePathOpt = Partial<{
|
|
@@ -25,12 +26,16 @@ type EnsurePathExistsOpt = Partial<{
|
|
|
25
26
|
type FileSearchGlobOpt = Partial<{
|
|
26
27
|
/**忽略的文件 默认 undefined */
|
|
27
28
|
ingore:string|string[];
|
|
29
|
+
/**输出的路径风格 默认跟随系统 */
|
|
30
|
+
normalize:"posix"|"win32";
|
|
28
31
|
}>
|
|
29
32
|
|
|
30
33
|
/**regex搜索选项 */
|
|
31
34
|
type FileSearchRegexOpt = Partial<{
|
|
32
35
|
/**搜索子目录 默认 true */
|
|
33
36
|
relative:boolean;
|
|
37
|
+
/**输出的路径风格 默认跟随系统 */
|
|
38
|
+
normalize:"posix"|"win32";
|
|
34
39
|
}>
|
|
35
40
|
|
|
36
41
|
/**文件工具 */
|
|
@@ -232,36 +237,53 @@ export async function writeJSONFile(
|
|
|
232
237
|
* @param traitRegex - 正则表达式
|
|
233
238
|
* @param opt - 可选参数
|
|
234
239
|
* @param opt.relative - 搜索子目录
|
|
240
|
+
* @param opt.normalize - 输出的路径风格 默认跟随系统
|
|
235
241
|
* @returns 文件名路径数组
|
|
236
242
|
*/
|
|
237
243
|
export function fileSearchRegex(dir: string, traitRegex: string, opt?:FileSearchRegexOpt) {
|
|
238
244
|
const relative = opt?.relative ?? true;
|
|
239
245
|
const outArray: string[] = [];
|
|
240
|
-
const subFiles = fs.readdirSync(dir,{withFileTypes:true});
|
|
246
|
+
const subFiles = fs.readdirSync(dir, { withFileTypes: true });
|
|
247
|
+
//如果使用 g 会导致 RegExp.lastIndex 更改, 将会导致匹配错误
|
|
241
248
|
const regex = new RegExp(traitRegex);
|
|
242
249
|
for (const subFile of subFiles) {
|
|
243
250
|
const subFilePath = path.join(dir, subFile.name);
|
|
244
251
|
//判断是否是文件夹,递归调用
|
|
245
252
|
if (subFile.isDirectory()) {
|
|
246
|
-
if(relative)
|
|
253
|
+
if (relative)
|
|
254
|
+
outArray.push(...fileSearchRegex(path.join(subFilePath, path.sep), traitRegex));
|
|
247
255
|
continue;
|
|
248
256
|
}
|
|
249
257
|
if (regex.test(subFilePath)) outArray.push(subFilePath);
|
|
250
258
|
}
|
|
251
|
-
return outArray
|
|
259
|
+
return outArray.map((filePath) => {
|
|
260
|
+
if (opt?.normalize === undefined) return filePath;
|
|
261
|
+
return matchProc(opt.normalize, {
|
|
262
|
+
posix: (nor) => path[nor].normalize(filePath.replaceAll("\\", "/")),
|
|
263
|
+
win32: (nor) => path[nor].normalize(filePath.replaceAll("/", "\\")),
|
|
264
|
+
});
|
|
265
|
+
});
|
|
252
266
|
}
|
|
253
267
|
/**搜索符合Glob匹配的文件
|
|
254
268
|
* @param dir - 起始目录
|
|
255
269
|
* @param globPattern - glob匹配
|
|
256
270
|
* @param opt - 可选参数
|
|
257
|
-
* @param opt.ignore
|
|
271
|
+
* @param opt.ignore - 忽略的文件
|
|
272
|
+
* @param opt.normalize - 输出的路径风格 默认跟随系统
|
|
258
273
|
* @returns 文件绝对路径数组
|
|
259
274
|
*/
|
|
260
275
|
export function fileSearchGlob(dir: string, globPattern:string|string[],opt?:FileSearchGlobOpt){
|
|
261
276
|
const fixedPath = typeof globPattern === "string"
|
|
262
277
|
? path.join(dir,path.posix.normalize(globPattern))
|
|
263
278
|
: globPattern.map((p)=>path.join(dir,path.posix.normalize(p)));
|
|
264
|
-
return globSync(fixedPath,{ignore:opt?.ingore,absolute:true})
|
|
279
|
+
return globSync(fixedPath, { ignore: opt?.ingore, absolute: true })
|
|
280
|
+
.map((filePath) => {
|
|
281
|
+
if (opt?.normalize === undefined) return filePath;
|
|
282
|
+
return matchProc(opt.normalize, {
|
|
283
|
+
posix: (nor) => path[nor].normalize(filePath.replaceAll("\\", "/")),
|
|
284
|
+
win32: (nor) => path[nor].normalize(filePath.replaceAll("/", "\\")),
|
|
285
|
+
});
|
|
286
|
+
});
|
|
265
287
|
}
|
|
266
288
|
/**
|
|
267
289
|
* @deprecated 请使用 fileSearchRegex 或 fileSearchGlob
|