@ainc/fs 0.1.11 → 0.1.15

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 (186) 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} +145 -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 +21 -21
  86. package/dist/jsonc.js +40 -40
  87. package/dist/jsonc.js.map +1 -1
  88. package/dist/lstat.d.ts +22 -22
  89. package/dist/lstat.js +30 -30
  90. package/dist/mapdir.d.ts +22 -22
  91. package/dist/mapdir.js +42 -42
  92. package/dist/mapdir.js.map +1 -1
  93. package/dist/mkdir.d.ts +6 -6
  94. package/dist/mkdir.js +37 -37
  95. package/dist/mkdir.js.map +1 -1
  96. package/dist/parseJSON.d.ts +16 -0
  97. package/dist/parseJSON.d.ts.map +1 -0
  98. package/dist/parseJSON.js +29 -0
  99. package/dist/parseJSON.js.map +1 -0
  100. package/dist/readFile.d.ts +29 -29
  101. package/dist/readFile.js +26 -26
  102. package/dist/readFile.js.map +1 -1
  103. package/dist/readStream.d.ts +36 -36
  104. package/dist/readStream.d.ts.map +1 -1
  105. package/dist/readStream.js +19 -19
  106. package/dist/readStream.js.map +1 -1
  107. package/dist/readdir.d.ts +16 -16
  108. package/dist/readdir.js +34 -34
  109. package/dist/readdir.js.map +1 -1
  110. package/dist/readline.d.ts +16 -16
  111. package/dist/readline.d.ts.map +1 -1
  112. package/dist/readline.js +24 -24
  113. package/dist/readline.js.map +1 -1
  114. package/dist/realpath.d.ts +12 -12
  115. package/dist/realpath.js +32 -32
  116. package/dist/realpath.js.map +1 -1
  117. package/dist/relative.d.ts +6 -6
  118. package/dist/relative.js +38 -38
  119. package/dist/resolve.d.ts +26 -26
  120. package/dist/resolve.js +53 -53
  121. package/dist/resolve.js.map +1 -1
  122. package/dist/resolvePath.d.ts +27 -0
  123. package/dist/resolvePath.d.ts.map +1 -0
  124. package/dist/resolvePath.js +178 -0
  125. package/dist/resolvePath.js.map +1 -0
  126. package/dist/rmdir.d.ts +6 -6
  127. package/dist/rmdir.js +40 -40
  128. package/dist/rmdir.js.map +1 -1
  129. package/dist/stat.d.ts +21 -21
  130. package/dist/stat.js +30 -30
  131. package/dist/supdir.d.ts +21 -15
  132. package/dist/supdir.d.ts.map +1 -1
  133. package/dist/supdir.js +62 -35
  134. package/dist/supdir.js.map +1 -1
  135. package/dist/symlink.d.ts +22 -22
  136. package/dist/symlink.js +39 -39
  137. package/dist/symlink.js.map +1 -1
  138. package/dist/unlink.d.ts +6 -6
  139. package/dist/unlink.js +28 -28
  140. package/dist/unlink.js.map +1 -1
  141. package/dist/usedir.d.ts +6 -6
  142. package/dist/usedir.js +24 -24
  143. package/dist/writeFile.d.ts +33 -33
  144. package/dist/writeFile.js +49 -49
  145. package/dist/writeFile.js.map +1 -1
  146. package/dist/writeStream.d.ts +35 -35
  147. package/dist/writeStream.d.ts.map +1 -1
  148. package/dist/writeStream.js +19 -19
  149. package/dist/writeStream.js.map +1 -1
  150. package/package.json +5 -5
  151. package/src/helpers/FileResolver.ts +282 -0
  152. package/src/helpers/compose.ts +49 -0
  153. package/src/helpers/each.ts +19 -0
  154. package/src/helpers/match.ts +31 -0
  155. package/src/helpers/normalizePath.ts +51 -0
  156. package/src/helpers/safeCall.ts +21 -0
  157. package/src/helpers/split.ts +25 -0
  158. package/src/helpers/stripBOM.ts +17 -0
  159. package/src/{normalizeJSON.ts → helpers/stripComments.ts} +1 -1
  160. package/src/index.ts +2 -3
  161. package/src/json.ts +2 -1
  162. package/src/jsonc.ts +2 -2
  163. package/src/parseJSON.ts +40 -0
  164. package/src/readStream.ts +2 -2
  165. package/src/readline.ts +2 -2
  166. package/src/resolvePath.ts +234 -0
  167. package/src/supdir.ts +31 -0
  168. package/src/writeStream.ts +2 -2
  169. package/dist/normalizeJSON.d.ts +0 -7
  170. package/dist/normalizeJSON.d.ts.map +0 -1
  171. package/dist/normalizeJSON.js.map +0 -1
  172. package/dist/resolveFile.d.ts +0 -22
  173. package/dist/resolveFile.d.ts.map +0 -1
  174. package/dist/resolveFile.js +0 -80
  175. package/dist/resolveFile.js.map +0 -1
  176. package/dist/resolveModule.d.ts +0 -41
  177. package/dist/resolveModule.d.ts.map +0 -1
  178. package/dist/resolveModule.js +0 -119
  179. package/dist/resolveModule.js.map +0 -1
  180. package/dist/resolvePaths.d.ts +0 -22
  181. package/dist/resolvePaths.d.ts.map +0 -1
  182. package/dist/resolvePaths.js +0 -233
  183. package/dist/resolvePaths.js.map +0 -1
  184. package/src/resolveFile.ts +0 -103
  185. package/src/resolveModule.ts +0 -157
  186. package/src/resolvePaths.ts +0 -320
@@ -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
- }