@zwa73/utils 1.0.127 → 1.0.128

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.
@@ -136,7 +136,15 @@ export declare namespace UtilFT {
136
136
  * @param opt.relative - 搜索子目录
137
137
  * @returns 文件名路径数组
138
138
  */
139
- function fileSearchRegex(dir: string, traitRegex: string, opt?: FileSearchRegexOpt): string[];
139
+ function fileSearchRegex(dir: string, traitRegex: string, opt?: FileSearchRegexOpt): Promise<string[]>;
140
+ /**搜索路径符合正则表达式的文件 同步版本
141
+ * @param dir - 起始目录
142
+ * @param traitRegex - 正则表达式
143
+ * @param opt - 可选参数
144
+ * @param opt.relative - 搜索子目录
145
+ * @returns 文件名路径数组
146
+ */
147
+ function fileSearchRegexSync(dir: string, traitRegex: string, opt?: FileSearchRegexOpt): string[];
140
148
  /**搜索符合Glob匹配的文件
141
149
  * @param dir - 起始目录
142
150
  * @param globPattern - glob匹配
@@ -144,7 +152,15 @@ export declare namespace UtilFT {
144
152
  * @param opt.ignore - 忽略的文件
145
153
  * @returns 文件绝对路径数组
146
154
  */
147
- function fileSearchGlob(dir: string, globPattern: string | string[], opt?: FileSearchGlobOpt): string[];
155
+ function fileSearchGlob(dir: string, globPattern: string | string[], opt?: FileSearchGlobOpt): Promise<string[]>;
156
+ /**搜索符合Glob匹配的文件 同步版本
157
+ * @param dir - 起始目录
158
+ * @param globPattern - glob匹配
159
+ * @param opt - 可选参数
160
+ * @param opt.ignore - 忽略的文件
161
+ * @returns 文件绝对路径数组
162
+ */
163
+ function fileSearchGlobSync(dir: string, globPattern: string | string[], opt?: FileSearchGlobOpt): string[];
148
164
  /**是一个有效的文件路径
149
165
  * @param filePath - 需要验证的文件路径
150
166
  */
@@ -248,7 +248,34 @@ var UtilFT;
248
248
  * @param opt.relative - 搜索子目录
249
249
  * @returns 文件名路径数组
250
250
  */
251
- function fileSearchRegex(dir, traitRegex, opt) {
251
+ async function fileSearchRegex(dir, traitRegex, opt) {
252
+ const relative = opt?.relative ?? true;
253
+ const outArray = [];
254
+ const subFiles = await fs.promises.readdir(dir, { withFileTypes: true });
255
+ //如果使用 g 会导致 RegExp.lastIndex 更改, 将会导致匹配错误
256
+ const regex = new RegExp(traitRegex);
257
+ await Promise.all(subFiles.map(async (subFile) => {
258
+ const subFilePath = pathe_1.default.join(dir, subFile.name);
259
+ //判断是否是文件夹,递归调用
260
+ if (subFile.isDirectory()) {
261
+ if (relative)
262
+ outArray.push(...await fileSearchRegex(pathe_1.default.join(subFilePath, pathe_1.default.sep), traitRegex));
263
+ return;
264
+ }
265
+ if (regex.test(subFilePath))
266
+ outArray.push(subFilePath);
267
+ }));
268
+ return outArray.map((filePath) => pathe_1.default.normalize(filePath));
269
+ }
270
+ UtilFT.fileSearchRegex = fileSearchRegex;
271
+ /**搜索路径符合正则表达式的文件 同步版本
272
+ * @param dir - 起始目录
273
+ * @param traitRegex - 正则表达式
274
+ * @param opt - 可选参数
275
+ * @param opt.relative - 搜索子目录
276
+ * @returns 文件名路径数组
277
+ */
278
+ function fileSearchRegexSync(dir, traitRegex, opt) {
252
279
  const relative = opt?.relative ?? true;
253
280
  const outArray = [];
254
281
  const subFiles = fs.readdirSync(dir, { withFileTypes: true });
@@ -259,7 +286,7 @@ var UtilFT;
259
286
  //判断是否是文件夹,递归调用
260
287
  if (subFile.isDirectory()) {
261
288
  if (relative)
262
- outArray.push(...fileSearchRegex(pathe_1.default.join(subFilePath, pathe_1.default.sep), traitRegex));
289
+ outArray.push(...fileSearchRegexSync(pathe_1.default.join(subFilePath, pathe_1.default.sep), traitRegex));
263
290
  continue;
264
291
  }
265
292
  if (regex.test(subFilePath))
@@ -267,7 +294,7 @@ var UtilFT;
267
294
  }
268
295
  return outArray.map((filePath) => pathe_1.default.normalize(filePath));
269
296
  }
270
- UtilFT.fileSearchRegex = fileSearchRegex;
297
+ UtilFT.fileSearchRegexSync = fileSearchRegexSync;
271
298
  /**搜索符合Glob匹配的文件
272
299
  * @param dir - 起始目录
273
300
  * @param globPattern - glob匹配
@@ -275,14 +302,29 @@ var UtilFT;
275
302
  * @param opt.ignore - 忽略的文件
276
303
  * @returns 文件绝对路径数组
277
304
  */
278
- function fileSearchGlob(dir, globPattern, opt) {
305
+ async function fileSearchGlob(dir, globPattern, opt) {
279
306
  const fixedPath = typeof globPattern === "string"
280
307
  ? pathe_1.default.join(dir, globPattern)
281
308
  : globPattern.map((p) => pathe_1.default.join(dir, p));
282
- return (0, glob_1.globSync)(fixedPath, { ignore: opt?.ingore, absolute: true })
309
+ return (await (0, glob_1.glob)(fixedPath, { ignore: opt?.ingore, absolute: true }))
283
310
  .map((filePath) => pathe_1.default.normalize(filePath));
284
311
  }
285
312
  UtilFT.fileSearchGlob = fileSearchGlob;
313
+ /**搜索符合Glob匹配的文件 同步版本
314
+ * @param dir - 起始目录
315
+ * @param globPattern - glob匹配
316
+ * @param opt - 可选参数
317
+ * @param opt.ignore - 忽略的文件
318
+ * @returns 文件绝对路径数组
319
+ */
320
+ function fileSearchGlobSync(dir, globPattern, opt) {
321
+ const fixedPath = typeof globPattern === "string"
322
+ ? pathe_1.default.join(dir, globPattern)
323
+ : globPattern.map((p) => pathe_1.default.join(dir, p));
324
+ return (0, glob_1.globSync)(fixedPath, { ignore: opt?.ingore, absolute: true })
325
+ .map((filePath) => pathe_1.default.normalize(filePath));
326
+ }
327
+ UtilFT.fileSearchGlobSync = fileSearchGlobSync;
286
328
  /**是一个有效的文件路径
287
329
  * @param filePath - 需要验证的文件路径
288
330
  */
@@ -71,7 +71,7 @@ export type AnyString = (string & {});
71
71
  * 将联合类型映射为联合函数
72
72
  * 再取得可以传递给任意联合函数的类型
73
73
  */
74
- export type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never;
74
+ export type UnionToIntersection<U> = ((k: U) => void) extends ((k: infer I) => void) ? I : never;
75
75
  /**创建一个新的类型,这个新的类型包含了基础类型 B 的所有属性,
76
76
  * 以及一个名为 K[N] 类型为 T 的新属性。
77
77
  * 所有 K 中非 K[N] 的其他属性都是可选的并且不能被赋值(因为它们的类型是 never)。
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zwa73/utils",
3
- "version": "1.0.127",
3
+ "version": "1.0.128",
4
4
  "description": "my utils",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -13,7 +13,8 @@ const INFO_TABLE = {
13
13
  "UtilFT.createPath 更改参数 dir 移入 可选参数对象 \n" +
14
14
  "UtilFT.createPathSync 更改参数 dir 移入 可选参数对象 \n" +
15
15
  "UtilFT.ensurePathExists 更改参数 dir 移入 可选参数对象 \n" +
16
- "UtilFT.ensurePathExistsSync 更改参数 dir 移入 可选参数对象 \n"
16
+ "UtilFT.ensurePathExistsSync 更改参数 dir 移入 可选参数对象 \n",
17
+ "1.0.128":"fileSearch 添加Sync版本, 默认改为异步函数",
17
18
  }
18
19
 
19
20
  // 将版本号转换为可以比较的数字
@@ -4,7 +4,7 @@ import * as os from "os";
4
4
  import { JObject, JToken } from "@src/UtilInterfaces";
5
5
  import { SLogger } from "@src/UtilLogger";
6
6
  import * as JSON5 from 'json5';
7
- import { globSync } from "glob";
7
+ import { globSync, glob } from "glob";
8
8
  import { UtilFunc } from "./UtilFunctions";
9
9
 
10
10
  /**创建文件选项 */
@@ -291,7 +291,32 @@ export function currosizePath(filePath: string): string {
291
291
  * @param opt.relative - 搜索子目录
292
292
  * @returns 文件名路径数组
293
293
  */
294
- export function fileSearchRegex(dir: string, traitRegex: string, opt?:FileSearchRegexOpt) {
294
+ export async function fileSearchRegex(dir: string, traitRegex: string, opt?:FileSearchRegexOpt) {
295
+ const relative = opt?.relative ?? true;
296
+ const outArray: string[] = [];
297
+ const subFiles = await fs.promises.readdir(dir, { withFileTypes: true });
298
+ //如果使用 g 会导致 RegExp.lastIndex 更改, 将会导致匹配错误
299
+ const regex = new RegExp(traitRegex);
300
+ await Promise.all(subFiles.map(async (subFile)=>{
301
+ const subFilePath = path.join(dir, subFile.name);
302
+ //判断是否是文件夹,递归调用
303
+ if (subFile.isDirectory()) {
304
+ if (relative)
305
+ outArray.push(... await fileSearchRegex(path.join(subFilePath, path.sep), traitRegex));
306
+ return;
307
+ }
308
+ if (regex.test(subFilePath)) outArray.push(subFilePath);
309
+ }));
310
+ return outArray.map((filePath) => path.normalize(filePath));
311
+ }
312
+ /**搜索路径符合正则表达式的文件 同步版本
313
+ * @param dir - 起始目录
314
+ * @param traitRegex - 正则表达式
315
+ * @param opt - 可选参数
316
+ * @param opt.relative - 搜索子目录
317
+ * @returns 文件名路径数组
318
+ */
319
+ export function fileSearchRegexSync(dir: string, traitRegex: string, opt?:FileSearchRegexOpt) {
295
320
  const relative = opt?.relative ?? true;
296
321
  const outArray: string[] = [];
297
322
  const subFiles = fs.readdirSync(dir, { withFileTypes: true });
@@ -302,7 +327,7 @@ export function fileSearchRegex(dir: string, traitRegex: string, opt?:FileSearch
302
327
  //判断是否是文件夹,递归调用
303
328
  if (subFile.isDirectory()) {
304
329
  if (relative)
305
- outArray.push(...fileSearchRegex(path.join(subFilePath, path.sep), traitRegex));
330
+ outArray.push(...fileSearchRegexSync(path.join(subFilePath, path.sep), traitRegex));
306
331
  continue;
307
332
  }
308
333
  if (regex.test(subFilePath)) outArray.push(subFilePath);
@@ -316,7 +341,21 @@ export function fileSearchRegex(dir: string, traitRegex: string, opt?:FileSearch
316
341
  * @param opt.ignore - 忽略的文件
317
342
  * @returns 文件绝对路径数组
318
343
  */
319
- export function fileSearchGlob(dir: string, globPattern:string|string[],opt?:FileSearchGlobOpt){
344
+ export async function fileSearchGlob(dir: string, globPattern:string|string[],opt?:FileSearchGlobOpt){
345
+ const fixedPath = typeof globPattern === "string"
346
+ ? path.join(dir,globPattern)
347
+ : globPattern.map((p)=>path.join(dir,p));
348
+ return (await glob(fixedPath, { ignore: opt?.ingore, absolute: true }))
349
+ .map((filePath) => path.normalize(filePath));
350
+ }
351
+ /**搜索符合Glob匹配的文件 同步版本
352
+ * @param dir - 起始目录
353
+ * @param globPattern - glob匹配
354
+ * @param opt - 可选参数
355
+ * @param opt.ignore - 忽略的文件
356
+ * @returns 文件绝对路径数组
357
+ */
358
+ export function fileSearchGlobSync(dir: string, globPattern:string|string[],opt?:FileSearchGlobOpt){
320
359
  const fixedPath = typeof globPattern === "string"
321
360
  ? path.join(dir,globPattern)
322
361
  : globPattern.map((p)=>path.join(dir,p));
@@ -101,11 +101,8 @@ export type AnyString = (string&{});
101
101
  * 再取得可以传递给任意联合函数的类型
102
102
  */
103
103
  export type UnionToIntersection<U> =
104
- (U extends any
105
- ? (k: U)=>void
106
- : never) extends ((k: infer I)=>void)
107
- ? I
108
- : never
104
+ ((k: U)=>void) extends ((k: infer I)=>void)
105
+ ? I : never
109
106
 
110
107
  /**创建一个新的类型,这个新的类型包含了基础类型 B 的所有属性,
111
108
  * 以及一个名为 K[N] 类型为 T 的新属性。