@ainc/fs 0.1.12 → 0.1.16

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.
Files changed (187) hide show
  1. package/README.md +1 -0
  2. package/dist/basename.d.ts +6 -6
  3. package/dist/basename.js +16 -16
  4. package/dist/copy.d.ts +40 -40
  5. package/dist/copy.js +89 -89
  6. package/dist/copy.js.map +1 -1
  7. package/dist/cpdir.d.ts +19 -19
  8. package/dist/cpdir.js +28 -28
  9. package/dist/cpdir.js.map +1 -1
  10. package/dist/cwd.d.ts +6 -6
  11. package/dist/cwd.js +16 -16
  12. package/dist/dirname.d.ts +6 -6
  13. package/dist/dirname.js +30 -30
  14. package/dist/extname.d.ts +6 -6
  15. package/dist/extname.js +16 -16
  16. package/dist/filename.d.ts +6 -6
  17. package/dist/filename.js +24 -24
  18. package/dist/find.d.ts +26 -26
  19. package/dist/find.js +72 -72
  20. package/dist/find.js.map +1 -1
  21. package/dist/findFile.d.ts +20 -20
  22. package/dist/findFile.js +39 -39
  23. package/dist/findFile.js.map +1 -1
  24. package/dist/finddir.d.ts +20 -20
  25. package/dist/finddir.js +39 -39
  26. package/dist/finddir.js.map +1 -1
  27. package/dist/helpers/FileResolver.d.ts +54 -0
  28. package/dist/helpers/FileResolver.d.ts.map +1 -0
  29. package/dist/helpers/FileResolver.js +184 -0
  30. package/dist/helpers/FileResolver.js.map +1 -0
  31. package/dist/helpers/compose.d.ts +14 -0
  32. package/dist/helpers/compose.d.ts.map +1 -0
  33. package/dist/helpers/compose.js +38 -0
  34. package/dist/helpers/compose.js.map +1 -0
  35. package/dist/helpers/each.d.ts +7 -0
  36. package/dist/helpers/each.d.ts.map +1 -0
  37. package/dist/helpers/each.js +21 -0
  38. package/dist/helpers/each.js.map +1 -0
  39. package/dist/helpers/match.d.ts +7 -0
  40. package/dist/helpers/match.d.ts.map +1 -0
  41. package/dist/helpers/match.js +30 -0
  42. package/dist/helpers/match.js.map +1 -0
  43. package/dist/helpers/normalizePath.d.ts +23 -0
  44. package/dist/helpers/normalizePath.d.ts.map +1 -0
  45. package/dist/helpers/normalizePath.js +37 -0
  46. package/dist/helpers/normalizePath.js.map +1 -0
  47. package/dist/helpers/safeCall.d.ts +7 -0
  48. package/dist/helpers/safeCall.d.ts.map +1 -0
  49. package/dist/helpers/safeCall.js +24 -0
  50. package/dist/helpers/safeCall.js.map +1 -0
  51. package/dist/helpers/split.d.ts +7 -0
  52. package/dist/helpers/split.d.ts.map +1 -0
  53. package/dist/helpers/split.js +25 -0
  54. package/dist/helpers/split.js.map +1 -0
  55. package/dist/helpers/stripBOM.d.ts +7 -0
  56. package/dist/helpers/stripBOM.d.ts.map +1 -0
  57. package/dist/helpers/stripBOM.js +19 -0
  58. package/dist/helpers/stripBOM.js.map +1 -0
  59. package/dist/helpers/stripComments.d.ts +7 -0
  60. package/dist/helpers/stripComments.d.ts.map +1 -0
  61. package/dist/{normalizeJSON.js → helpers/stripComments.js} +147 -145
  62. package/dist/helpers/stripComments.js.map +1 -0
  63. package/dist/homedir.d.ts +6 -6
  64. package/dist/homedir.js +31 -31
  65. package/dist/includePaths.d.ts +6 -6
  66. package/dist/includePaths.js +53 -53
  67. package/dist/includePaths.js.map +1 -1
  68. package/dist/index.d.ts +43 -44
  69. package/dist/index.d.ts.map +1 -1
  70. package/dist/index.js +90 -92
  71. package/dist/index.js.map +1 -1
  72. package/dist/indir.d.ts +13 -13
  73. package/dist/indir.js +26 -26
  74. package/dist/isAbsolutePath.d.ts +6 -6
  75. package/dist/isAbsolutePath.js +24 -24
  76. package/dist/isDirectory.d.ts +12 -12
  77. package/dist/isDirectory.js +26 -26
  78. package/dist/isDirectory.js.map +1 -1
  79. package/dist/isRelativePath.d.ts +6 -6
  80. package/dist/isRelativePath.js +44 -44
  81. package/dist/json.d.ts +21 -21
  82. package/dist/json.d.ts.map +1 -1
  83. package/dist/json.js +34 -33
  84. package/dist/json.js.map +1 -1
  85. package/dist/jsonc.d.ts +27 -21
  86. package/dist/jsonc.d.ts.map +1 -1
  87. package/dist/jsonc.js +49 -40
  88. package/dist/jsonc.js.map +1 -1
  89. package/dist/lstat.d.ts +22 -22
  90. package/dist/lstat.js +30 -30
  91. package/dist/mapdir.d.ts +22 -22
  92. package/dist/mapdir.js +42 -42
  93. package/dist/mapdir.js.map +1 -1
  94. package/dist/mkdir.d.ts +6 -6
  95. package/dist/mkdir.js +37 -37
  96. package/dist/mkdir.js.map +1 -1
  97. package/dist/parseJSON.d.ts +16 -0
  98. package/dist/parseJSON.d.ts.map +1 -0
  99. package/dist/parseJSON.js +29 -0
  100. package/dist/parseJSON.js.map +1 -0
  101. package/dist/readFile.d.ts +29 -29
  102. package/dist/readFile.js +26 -26
  103. package/dist/readFile.js.map +1 -1
  104. package/dist/readStream.d.ts +36 -36
  105. package/dist/readStream.d.ts.map +1 -1
  106. package/dist/readStream.js +19 -19
  107. package/dist/readStream.js.map +1 -1
  108. package/dist/readdir.d.ts +16 -16
  109. package/dist/readdir.js +34 -34
  110. package/dist/readdir.js.map +1 -1
  111. package/dist/readline.d.ts +16 -16
  112. package/dist/readline.d.ts.map +1 -1
  113. package/dist/readline.js +24 -24
  114. package/dist/readline.js.map +1 -1
  115. package/dist/realpath.d.ts +12 -12
  116. package/dist/realpath.js +32 -32
  117. package/dist/realpath.js.map +1 -1
  118. package/dist/relative.d.ts +6 -6
  119. package/dist/relative.js +38 -38
  120. package/dist/resolve.d.ts +26 -26
  121. package/dist/resolve.js +53 -53
  122. package/dist/resolve.js.map +1 -1
  123. package/dist/resolvePath.d.ts +27 -0
  124. package/dist/resolvePath.d.ts.map +1 -0
  125. package/dist/resolvePath.js +178 -0
  126. package/dist/resolvePath.js.map +1 -0
  127. package/dist/rmdir.d.ts +6 -6
  128. package/dist/rmdir.js +40 -40
  129. package/dist/rmdir.js.map +1 -1
  130. package/dist/stat.d.ts +21 -21
  131. package/dist/stat.js +30 -30
  132. package/dist/supdir.d.ts +21 -15
  133. package/dist/supdir.d.ts.map +1 -1
  134. package/dist/supdir.js +62 -35
  135. package/dist/supdir.js.map +1 -1
  136. package/dist/symlink.d.ts +22 -22
  137. package/dist/symlink.js +39 -39
  138. package/dist/symlink.js.map +1 -1
  139. package/dist/unlink.d.ts +6 -6
  140. package/dist/unlink.js +28 -28
  141. package/dist/unlink.js.map +1 -1
  142. package/dist/usedir.d.ts +6 -6
  143. package/dist/usedir.js +24 -24
  144. package/dist/writeFile.d.ts +33 -33
  145. package/dist/writeFile.js +49 -49
  146. package/dist/writeFile.js.map +1 -1
  147. package/dist/writeStream.d.ts +35 -35
  148. package/dist/writeStream.d.ts.map +1 -1
  149. package/dist/writeStream.js +19 -19
  150. package/dist/writeStream.js.map +1 -1
  151. package/package.json +4 -4
  152. package/src/helpers/FileResolver.ts +282 -0
  153. package/src/helpers/compose.ts +49 -0
  154. package/src/helpers/each.ts +19 -0
  155. package/src/helpers/match.ts +31 -0
  156. package/src/helpers/normalizePath.ts +51 -0
  157. package/src/helpers/safeCall.ts +21 -0
  158. package/src/helpers/split.ts +25 -0
  159. package/src/helpers/stripBOM.ts +17 -0
  160. package/src/{normalizeJSON.ts → helpers/stripComments.ts} +58 -52
  161. package/src/index.ts +2 -3
  162. package/src/json.ts +2 -1
  163. package/src/jsonc.ts +22 -11
  164. package/src/parseJSON.ts +40 -0
  165. package/src/readStream.ts +2 -2
  166. package/src/readline.ts +2 -2
  167. package/src/resolvePath.ts +234 -0
  168. package/src/supdir.ts +31 -0
  169. package/src/writeStream.ts +2 -2
  170. package/dist/normalizeJSON.d.ts +0 -7
  171. package/dist/normalizeJSON.d.ts.map +0 -1
  172. package/dist/normalizeJSON.js.map +0 -1
  173. package/dist/resolveFile.d.ts +0 -22
  174. package/dist/resolveFile.d.ts.map +0 -1
  175. package/dist/resolveFile.js +0 -80
  176. package/dist/resolveFile.js.map +0 -1
  177. package/dist/resolveModule.d.ts +0 -41
  178. package/dist/resolveModule.d.ts.map +0 -1
  179. package/dist/resolveModule.js +0 -119
  180. package/dist/resolveModule.js.map +0 -1
  181. package/dist/resolvePaths.d.ts +0 -22
  182. package/dist/resolvePaths.d.ts.map +0 -1
  183. package/dist/resolvePaths.js +0 -233
  184. package/dist/resolvePaths.js.map +0 -1
  185. package/src/resolveFile.ts +0 -103
  186. package/src/resolveModule.ts +0 -157
  187. package/src/resolvePaths.ts +0 -320
@@ -1,157 +0,0 @@
1
- /**
2
- *****************************************
3
- * Created by edonet@163.com
4
- * Created on 2021-07-10 17:01:38
5
- *****************************************
6
- */
7
- 'use strict';
8
-
9
-
10
- /**
11
- *****************************************
12
- * 加载依赖
13
- *****************************************
14
- */
15
- import * as Module from 'module';
16
- import { cwd } from './cwd';
17
- import { supdir } from './supdir';
18
- import { relative } from './relative';
19
- import { isDirectory } from './isDirectory';
20
- import { isRelativePath } from './isRelativePath';
21
- import { isAbsolutePath } from './isAbsolutePath';
22
- import { match, resolveFile, Options as ResolveFileOptions } from './resolveFile';
23
-
24
-
25
- /**
26
- *****************************************
27
- * 解析函数
28
- *****************************************
29
- */
30
- export type Handler = (id: string, context?: string) => void | string;
31
-
32
-
33
- /**
34
- *****************************************
35
- * 格式化路径
36
- *****************************************
37
- */
38
- export function normalizePaths(paths: string | string[]): string[] {
39
-
40
- // 重载参数
41
- if (typeof paths === 'string') {
42
- paths = [paths];
43
- }
44
-
45
- // 校验参数
46
- if (!Array.isArray(paths)) {
47
- throw new Error('expect paths to be path string or string[]!');
48
- }
49
-
50
- // 生成处理函数
51
- return paths.map(name => {
52
-
53
- // 校验路径
54
- if (!name || typeof name !== 'string') {
55
- throw new Error('expect paths to be path string or string[]!');
56
- }
57
-
58
- // 解析路径
59
- return isRelativePath(name) ? cwd(name) : name;
60
- });
61
- }
62
-
63
-
64
- /**
65
- *****************************************
66
- * 解析内置模块
67
- *****************************************
68
- */
69
- export function resolveBuiltinModule(name: string): void | string {
70
- return Module.builtinModules.includes(name) ? name : undefined;
71
- }
72
-
73
-
74
- /**
75
- *****************************************
76
- * 第三方模块路径
77
- *****************************************
78
- */
79
- const nodeModulesRegExp = /^(\.?\.\/)*node_modules\//i;
80
-
81
-
82
- /**
83
- *****************************************
84
- * 解析模块目录
85
- *****************************************
86
- */
87
- export function resolveModulePaths(options: Options): Handler {
88
- const baseDir = process.cwd();
89
- const cached = new Map<string, Handler>();
90
- const modules = normalizePaths(options.modules || ['node_modules']);
91
- const useRelativePath = !!options.useRelativePath;
92
-
93
- // 解析模块上下文
94
- function resolveModuleContext(context: string): Handler {
95
- const paths: string[] = [];
96
-
97
- // 遍历路径
98
- modules.forEach(path => {
99
- if (isAbsolutePath(path)) {
100
- paths.push(path);
101
- } else {
102
- supdir(context, (dir, next) => (paths.push(cwd(dir, path)), next()));
103
- }
104
- });
105
-
106
- // 生成解析函数
107
- const resolve: Handler = id => match(paths, dir => {
108
- return isDirectory(dir) ? resolveFile(cwd(dir, id), options) : undefined;
109
- });
110
-
111
- // 缓存函数
112
- cached.set(context, resolve);
113
-
114
- // 返回函数函数
115
- return resolve;
116
- }
117
-
118
- // 返回结果
119
- return (id, context) => {
120
- const dir = context || baseDir;
121
- const resolve = cached.get(dir) || resolveModuleContext(dir);
122
- const modulePath = resolve(id);
123
-
124
- // 解析失败
125
- if (!modulePath) {
126
- return;
127
- }
128
-
129
- // 解析返回路径
130
- return (
131
- useRelativePath ?
132
- relative(dir, modulePath).replace(nodeModulesRegExp, '') :
133
- modulePath
134
- );
135
- };
136
- }
137
-
138
-
139
- /**
140
- *****************************************
141
- * 解析配置
142
- *****************************************
143
- */
144
- export interface Options extends ResolveFileOptions {
145
- modules?: string[];
146
- useRelativePath?: boolean;
147
- }
148
-
149
-
150
- /**
151
- *****************************************
152
- * 解析模块
153
- *****************************************
154
- */
155
- export function resolveModule(id: string, context?: string, options?: Options): void | string {
156
- return resolveModulePaths(options || {})(id, context);
157
- }
@@ -1,320 +0,0 @@
1
- /**
2
- *****************************************
3
- * Created by edonet@163.com
4
- * Created on 2021-07-10 14:04:38
5
- *****************************************
6
- */
7
- 'use strict';
8
-
9
-
10
- /**
11
- *****************************************
12
- * 加载依赖
13
- *****************************************
14
- */
15
- import { cwd } from './cwd';
16
- import { isRelativePath } from './isRelativePath';
17
- import { isAbsolutePath } from './isAbsolutePath';
18
- import { match, resolveFile } from './resolveFile';
19
- import { normalizePaths, resolveModulePaths, resolveBuiltinModule, Handler, Options as ResolveModuleOptions } from './resolveModule';
20
-
21
-
22
- /**
23
- *****************************************
24
- * 组合规则
25
- *****************************************
26
- */
27
- function compose(rules: Handler[]): Handler {
28
-
29
- // 无处理函数
30
- if (rules.length === 0) {
31
- return () => undefined;
32
- }
33
-
34
- // 单一处理函数
35
- if (rules.length === 1) {
36
- return rules[0];
37
- }
38
-
39
- // 复合处理函数
40
- return (id, context) => {
41
- for (let idx = 0, len = rules.length; idx < len; idx++) {
42
- const result = rules[idx](id, context);
43
-
44
- // 解析成功
45
- if (result) {
46
- return result;
47
- }
48
- }
49
- };
50
- }
51
-
52
-
53
- /**
54
- *****************************************
55
- * 解析文件
56
- *****************************************
57
- */
58
- function resolvePath(name: string, options: Options): void | string {
59
-
60
- // 处理绝对路径
61
- if (isAbsolutePath(name)) {
62
- return resolveFile(name, options);
63
- }
64
-
65
- // 处理相对路径
66
- if (isRelativePath(name)) {
67
- return resolveFile(cwd(name), options);
68
- }
69
- }
70
-
71
-
72
- /**
73
- *****************************************
74
- * 匹配函数
75
- *****************************************
76
- */
77
- type AliasRule = (id: string) => void | string[];
78
- type AliasHandler = (id: string, next: Handler) => void | string;
79
-
80
-
81
- /**
82
- *****************************************
83
- * 解析别名
84
- *****************************************
85
- */
86
- function resolveAliasPaths(options: Options): AliasHandler {
87
- const { alias } = options;
88
-
89
- // 不存在别名映射
90
- if (!alias) {
91
- return (id, next) => resolvePath(id, options) || next(id);
92
- }
93
-
94
- // 定义规则列表
95
- const rules: AliasRule[] = [];
96
-
97
- // 解析别名
98
- Object.keys(alias).forEach(key => {
99
- const paths = normalizePaths(alias[key]);
100
-
101
- // 绝对匹配
102
- if (key.endsWith('$')) {
103
-
104
- // 去除匹配符
105
- key = key.slice(0, -1);
106
-
107
- // 添加匹配规则
108
- return rules.push(id => id === key ? paths : undefined);
109
- }
110
-
111
- // 生成配置前缀
112
- const prefix = key + '/';
113
- const prefixLength = key.length;
114
- const join = (suffix: string): string[] => paths.map(id => id + suffix);
115
-
116
- // 添加匹配规则
117
- rules.push(
118
- id => id === key ? paths : undefined,
119
- id => id.startsWith(prefix) ? join(id.slice(prefixLength)) : undefined,
120
- );
121
- });
122
-
123
- // 解析规则
124
- function resolveRules(id: string, next: Handler, rules: AliasRule[]): void | string {
125
-
126
- // 处理绝对路径
127
- if (isAbsolutePath(id)) {
128
- return resolveFile(id, options);
129
- }
130
-
131
- // 处理相对路径
132
- if (isRelativePath(id)) {
133
- return resolveFile(cwd(id), options);
134
- }
135
-
136
- // 解析模块
137
- const result = match(rules, handler => {
138
- const paths = handler(id);
139
-
140
- // 存在解析路径
141
- if (paths) {
142
- return match(paths, id => resolveRules(id, next, rules.filter(fn => fn !== handler)));
143
- }
144
- });
145
-
146
- // 执行解析回调
147
- return result || next(id);
148
- }
149
-
150
- // 返回解析函数
151
- return (id, next) => resolveRules(id, next, [...rules]);
152
- }
153
-
154
-
155
- /**
156
- *****************************************
157
- * 解析基准路径模块
158
- *****************************************
159
- */
160
- function resolveBaseUrlPaths(baseDir: string, paths: Record<string, string[]>, options: Options): Handler {
161
- const rules: Handler[] = [];
162
-
163
- // 解析映射路径
164
- function resolvePaths(paths: string[], isExactMatch: boolean): Handler {
165
-
166
- // 校验参数
167
- if (!Array.isArray(paths) || paths.find(path => !path || typeof path !== 'string')) {
168
- throw new Error('expect paths value to bo string[]!');
169
- }
170
-
171
- // 生成完全配置函数
172
- if (isExactMatch) {
173
- return compose(
174
- paths.map(dir => () => resolveFile(cwd(baseDir, dir), options))
175
- );
176
- }
177
-
178
- // 生成部分配置函数
179
- return compose(paths.map(dir => {
180
- const idx = dir.indexOf('*');
181
-
182
- // 不存在通配符
183
- if (idx === -1) {
184
- return () => resolveFile(cwd(baseDir, dir), options);
185
- }
186
-
187
- // 获取前、后缀
188
- const prefix = dir.slice(0, idx);
189
- const suffix = dir.slice(idx + 1);
190
-
191
- // 返回解析函数
192
- return (id) => resolveFile(cwd(baseDir, prefix + id + suffix), options);
193
- }));
194
- }
195
-
196
- // 遍历映射
197
- Object.keys(paths).forEach(key => {
198
-
199
- // 无效路径
200
- if (!key) {
201
- return;
202
- }
203
-
204
- // 获取匹配符
205
- const idx = key.indexOf('*');
206
-
207
- // 完全匹配
208
- if (idx === -1) {
209
- const resolve = resolvePaths(paths[key], true);
210
-
211
- // 添加处理函数
212
- return rules.push((id) => id === key ? resolve('') : undefined);
213
- }
214
-
215
- // 获取解析项
216
- const prefix = key.slice(0, idx);
217
- const suffix = key.slice(idx + 1);
218
- const prefixLength = prefix.length;
219
- const suffixLength = - suffix.length;
220
- const resolve = resolvePaths(paths[key], false);
221
-
222
- // 生成函数
223
- if (prefix) {
224
- rules.push(
225
- suffix ?
226
- id => {
227
- if (id.startsWith(prefix) && id.endsWith(suffix)) {
228
- return resolve(id.slice(prefixLength, suffixLength));
229
- }
230
- } :
231
- id => {
232
- if (id.startsWith(prefix)) {
233
- return resolve(id.slice(prefixLength));
234
- }
235
- }
236
- );
237
- } else {
238
- rules.push(
239
- suffix ?
240
- id => id.endsWith(suffix) ? resolve(id.slice(0, suffixLength)) : undefined :
241
- id => resolve(id)
242
- );
243
- }
244
- });
245
-
246
- // 返回结果
247
- return compose(rules);
248
- }
249
-
250
-
251
- /**
252
- *****************************************
253
- * 解析基准路径模块
254
- *****************************************
255
- */
256
- function resolveBaseUrlModule(baseDir: string, options: Options): Handler {
257
- return (id) => {
258
- return resolveFile(cwd(baseDir, id), options);
259
- };
260
- }
261
-
262
-
263
- /**
264
- *****************************************
265
- * 别名配置
266
- *****************************************
267
- */
268
- export interface Options extends ResolveModuleOptions {
269
- baseUrl?: string;
270
- paths?: {
271
- [key: string]: string[];
272
- };
273
- alias?: {
274
- [key: string]: string | string[];
275
- };
276
- }
277
-
278
-
279
- /**
280
- *****************************************
281
- * 解析模块路径
282
- *****************************************
283
- */
284
- export function resolvePaths(options: Options): Handler {
285
- const { baseUrl, paths } = options;
286
- const rules: Handler[] = [];
287
- const baseDir = cwd(baseUrl || './');
288
-
289
- // 解析内置模块
290
- rules.push(resolveBuiltinModule);
291
-
292
- // 解析模块映射
293
- if (paths) {
294
- rules.push(resolveBaseUrlPaths(baseDir, paths, options));
295
- }
296
-
297
- // 解析基准路径模块
298
- if (baseUrl) {
299
- rules.push(resolveBaseUrlModule(baseDir, options));
300
- }
301
-
302
- // 解析第三方模块
303
- rules.push(resolveModulePaths(options));
304
-
305
- // 生成解析函数
306
- const resolveAlias = resolveAliasPaths(options);
307
- const resolveModule = compose(rules);
308
-
309
- // 返回解析函数
310
- return (id, context) => {
311
-
312
- // 不解析相对路径
313
- if (isRelativePath(id)) {
314
- return;
315
- }
316
-
317
- // 解析结果
318
- return resolveAlias(id, name => resolveModule(name, context));
319
- };
320
- }