@zwa73/utils 1.0.108 → 1.0.109

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.
@@ -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[];
@@ -198,12 +198,14 @@ var UtilFT;
198
198
  * @param traitRegex - 正则表达式
199
199
  * @param opt - 可选参数
200
200
  * @param opt.relative - 搜索子目录
201
+ * @param opt.normalize - 输出的路径风格 默认跟随系统
201
202
  * @returns 文件名路径数组
202
203
  */
203
204
  function fileSearchRegex(dir, traitRegex, opt) {
204
205
  const relative = opt?.relative ?? true;
205
206
  const outArray = [];
206
207
  const subFiles = fs.readdirSync(dir, { withFileTypes: true });
208
+ //如果使用 g 会导致 RegExp.lastIndex 更改, 将会导致匹配错误
207
209
  const regex = new RegExp(traitRegex);
208
210
  for (const subFile of subFiles) {
209
211
  const subFilePath = path.join(dir, subFile.name);
@@ -216,21 +218,31 @@ var UtilFT;
216
218
  if (regex.test(subFilePath))
217
219
  outArray.push(subFilePath);
218
220
  }
219
- return outArray;
221
+ return outArray.map((filePath) => {
222
+ if (opt?.normalize === undefined)
223
+ return filePath;
224
+ return path[opt.normalize].normalize(filePath);
225
+ });
226
+ ;
220
227
  }
221
228
  UtilFT.fileSearchRegex = fileSearchRegex;
222
229
  /**搜索符合Glob匹配的文件
223
230
  * @param dir - 起始目录
224
231
  * @param globPattern - glob匹配
225
232
  * @param opt - 可选参数
226
- * @param opt.ignore - 忽略的文件
233
+ * @param opt.ignore - 忽略的文件
234
+ * @param opt.normalize - 输出的路径风格 默认跟随系统
227
235
  * @returns 文件绝对路径数组
228
236
  */
229
237
  function fileSearchGlob(dir, globPattern, opt) {
230
238
  const fixedPath = typeof globPattern === "string"
231
239
  ? path.join(dir, path.posix.normalize(globPattern))
232
240
  : globPattern.map((p) => path.join(dir, path.posix.normalize(p)));
233
- return (0, glob_1.globSync)(fixedPath, { ignore: opt?.ingore, absolute: true });
241
+ return (0, glob_1.globSync)(fixedPath, { ignore: opt?.ingore, absolute: true }).map((filePath) => {
242
+ if (opt?.normalize === undefined)
243
+ return filePath;
244
+ return path[opt.normalize].normalize(filePath);
245
+ });
234
246
  }
235
247
  UtilFT.fileSearchGlob = fileSearchGlob;
236
248
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zwa73/utils",
3
- "version": "1.0.108",
3
+ "version": "1.0.109",
4
4
  "description": "my utils",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -25,12 +25,16 @@ type EnsurePathExistsOpt = Partial<{
25
25
  type FileSearchGlobOpt = Partial<{
26
26
  /**忽略的文件 默认 undefined */
27
27
  ingore:string|string[];
28
+ /**输出的路径风格 默认跟随系统 */
29
+ normalize:"posix"|"win32";
28
30
  }>
29
31
 
30
32
  /**regex搜索选项 */
31
33
  type FileSearchRegexOpt = Partial<{
32
34
  /**搜索子目录 默认 true */
33
35
  relative:boolean;
36
+ /**输出的路径风格 默认跟随系统 */
37
+ normalize:"posix"|"win32";
34
38
  }>
35
39
 
36
40
  /**文件工具 */
@@ -232,12 +236,14 @@ export async function writeJSONFile(
232
236
  * @param traitRegex - 正则表达式
233
237
  * @param opt - 可选参数
234
238
  * @param opt.relative - 搜索子目录
239
+ * @param opt.normalize - 输出的路径风格 默认跟随系统
235
240
  * @returns 文件名路径数组
236
241
  */
237
242
  export function fileSearchRegex(dir: string, traitRegex: string, opt?:FileSearchRegexOpt) {
238
243
  const relative = opt?.relative ?? true;
239
244
  const outArray: string[] = [];
240
245
  const subFiles = fs.readdirSync(dir,{withFileTypes:true});
246
+ //如果使用 g 会导致 RegExp.lastIndex 更改, 将会导致匹配错误
241
247
  const regex = new RegExp(traitRegex);
242
248
  for (const subFile of subFiles) {
243
249
  const subFilePath = path.join(dir, subFile.name);
@@ -248,20 +254,27 @@ export function fileSearchRegex(dir: string, traitRegex: string, opt?:FileSearch
248
254
  }
249
255
  if (regex.test(subFilePath)) outArray.push(subFilePath);
250
256
  }
251
- return outArray;
257
+ return outArray.map((filePath)=>{
258
+ if(opt?.normalize===undefined) return filePath;
259
+ return path[opt.normalize].normalize(filePath);
260
+ });;
252
261
  }
253
262
  /**搜索符合Glob匹配的文件
254
263
  * @param dir - 起始目录
255
264
  * @param globPattern - glob匹配
256
265
  * @param opt - 可选参数
257
- * @param opt.ignore - 忽略的文件
266
+ * @param opt.ignore - 忽略的文件
267
+ * @param opt.normalize - 输出的路径风格 默认跟随系统
258
268
  * @returns 文件绝对路径数组
259
269
  */
260
270
  export function fileSearchGlob(dir: string, globPattern:string|string[],opt?:FileSearchGlobOpt){
261
271
  const fixedPath = typeof globPattern === "string"
262
272
  ? path.join(dir,path.posix.normalize(globPattern))
263
273
  : globPattern.map((p)=>path.join(dir,path.posix.normalize(p)));
264
- return globSync(fixedPath,{ignore:opt?.ingore,absolute:true});
274
+ return globSync(fixedPath,{ignore:opt?.ingore,absolute:true}).map((filePath)=>{
275
+ if(opt?.normalize===undefined) return filePath;
276
+ return path[opt.normalize].normalize(filePath);
277
+ });
265
278
  }
266
279
  /**
267
280
  * @deprecated 请使用 fileSearchRegex 或 fileSearchGlob