@nu-art/ts-common 0.400.3 → 0.400.4

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nu-art/ts-common",
3
- "version": "0.400.3",
3
+ "version": "0.400.4",
4
4
  "description": "js and ts infra",
5
5
  "type": "module",
6
6
  "keywords": [
@@ -2,7 +2,9 @@ import { StringMap } from './types.js';
2
2
  export declare const DEFAULT_TEMPLATE_PATTERN: RegExp;
3
3
  export declare const DEFAULT_OLD_TEMPLATE_PATTERN: RegExp;
4
4
  export declare const FileSystemUtils: {
5
+ exists: (pathToFile: string) => Promise<boolean>;
5
6
  file: {
7
+ isFile: (pathToFile: string) => Promise<boolean>;
6
8
  exists: (pathToFile: string) => Promise<boolean>;
7
9
  delete: (pathToFile: string, mustExist?: boolean) => Promise<void>;
8
10
  write: (pathToFile: string, content: string) => Promise<void>;
@@ -16,15 +18,17 @@ export declare const FileSystemUtils: {
16
18
  };
17
19
  };
18
20
  folder: {
21
+ isFolder: (pathToFile: string) => Promise<boolean>;
19
22
  delete: (pathToFolder: string, mustExist?: boolean) => Promise<void>;
20
23
  empty: (pathToFolder: string, mustExist?: boolean) => Promise<void>;
21
24
  create: (pathToFolder: string) => Promise<string | void>;
22
25
  list: ((pathToFolder: string) => Promise<string[]>) & {
23
- forEach: ((pathToFolder: string, callback: (path: string) => Promise<any>) => Promise<void>) & {
26
+ forEach: ((pathToFolder: string, callback: (path: string) => Promise<any>) => Promise<any[]>) & {
24
27
  file: (pathToFolder: string, callback: (path: string) => Promise<any>) => Promise<void>;
25
28
  folder: (pathToFolder: string, callback: (path: string) => Promise<any>) => Promise<void>;
26
29
  };
27
30
  };
31
+ iterate: (pathToDir: string, options: FileIteratorOptions) => Promise<void>;
28
32
  };
29
33
  symlink: {
30
34
  create: (targetPath: string, linkPath: string) => Promise<void>;
@@ -32,3 +36,9 @@ export declare const FileSystemUtils: {
32
36
  read: (pathToLink: string) => Promise<string>;
33
37
  };
34
38
  };
39
+ type FileIteratorOptions = {
40
+ filter: (path: string) => Promise<boolean>;
41
+ processor: (path: string) => Promise<any>;
42
+ followSymlinks?: boolean;
43
+ };
44
+ export {};
@@ -2,6 +2,7 @@ import { promises as _fs } from 'fs';
2
2
  import { resolve } from 'path';
3
3
  import { BadImplementationException } from '../core/exceptions/exceptions.js';
4
4
  import { exists } from './tools.js';
5
+ import path from 'node:path';
5
6
  async function isFile(path) {
6
7
  return (await _fs.stat(path)).isFile();
7
8
  }
@@ -34,11 +35,16 @@ async function assertExists(path, mustExist, type) {
34
35
  }
35
36
  return true;
36
37
  }
37
- const escapeRegExp = (string) => string.replace(/[\\^$.*+?()[\]{}|]/g, '\\$&');
38
- export const DEFAULT_TEMPLATE_PATTERN = new RegExp(`\{\{([a-zA-Z]\\w{2,}?)\}\}`);
39
- export const DEFAULT_OLD_TEMPLATE_PATTERN = new RegExp(`(?<!\\\\)\\$([a-zA-Z]\\w{2,})`);
38
+ export const DEFAULT_TEMPLATE_PATTERN = new RegExp(`\{\{(\\S*?)\}\}`);
39
+ export const DEFAULT_OLD_TEMPLATE_PATTERN = new RegExp(`(?<!\\\\)\\$([a-zA-Z][\\w-_]{2,})`);
40
40
  export const FileSystemUtils = {
41
+ exists: async (pathToFile) => {
42
+ return await fileExists(pathToFile);
43
+ },
41
44
  file: {
45
+ isFile: async (pathToFile) => {
46
+ return isFile(pathToFile);
47
+ },
42
48
  exists: async (pathToFile) => {
43
49
  return await fileExists(pathToFile);
44
50
  },
@@ -73,8 +79,7 @@ export const FileSystemUtils = {
73
79
  const value = params[match[1]];
74
80
  if (!exists(value))
75
81
  throw new BadImplementationException(`Missing template param: ${match[1]}`);
76
- const fullMatchRegex = new RegExp(escapeRegExp(match[0]), 'g');
77
- input = input.replace(fullMatchRegex, value);
82
+ input = input.replace(match[0], value);
78
83
  }
79
84
  return input;
80
85
  },
@@ -90,6 +95,9 @@ export const FileSystemUtils = {
90
95
  },
91
96
  },
92
97
  folder: {
98
+ isFolder: async (pathToFile) => {
99
+ return isFolder(pathToFile);
100
+ },
93
101
  delete: async (pathToFolder, mustExist = false) => {
94
102
  if (!await assertExists(pathToFolder, mustExist, 'Folder'))
95
103
  return;
@@ -113,8 +121,7 @@ export const FileSystemUtils = {
113
121
  }, {
114
122
  forEach: Object.assign(async (pathToFolder, callback) => {
115
123
  const entries = await _fs.readdir(pathToFolder);
116
- for (const entry of entries)
117
- await callback(resolve(pathToFolder, entry));
124
+ return Promise.all(entries.map(entry => callback(resolve(pathToFolder, entry))));
118
125
  }, {
119
126
  file: async (pathToFolder, callback) => {
120
127
  await FileSystemUtils.folder.list.forEach(pathToFolder, async (entry) => {
@@ -131,7 +138,21 @@ export const FileSystemUtils = {
131
138
  });
132
139
  }
133
140
  })
134
- })
141
+ }),
142
+ iterate: async (pathToDir, options) => {
143
+ const pathToEntry = path.resolve(pathToDir);
144
+ if (await FileSystemUtils.file.isFile(pathToEntry)) {
145
+ (await options.filter(pathToEntry)) && (await options.processor(pathToEntry));
146
+ return;
147
+ }
148
+ if (await FileSystemUtils.folder.isFolder(pathToEntry)) {
149
+ await FileSystemUtils.folder.list.forEach(pathToEntry, async (entry) => {
150
+ return (await options.filter(entry)) && (await FileSystemUtils.folder.iterate(entry, options));
151
+ });
152
+ return;
153
+ }
154
+ throw new BadImplementationException(`Expected file or folder but found something else: ${pathToEntry}`);
155
+ }
135
156
  },
136
157
  symlink: {
137
158
  create: async (targetPath, linkPath) => {