@ainc/fs 0.1.15 → 0.1.17

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 (255) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +18 -42
  3. package/dist/findUp.d.ts +6 -0
  4. package/dist/findUp.js +36 -0
  5. package/dist/index.d.ts +7 -38
  6. package/dist/index.js +21 -80
  7. package/dist/jsonc.d.ts +4 -14
  8. package/dist/jsonc.js +19 -27
  9. package/dist/lookup.d.ts +18 -0
  10. package/dist/lookup.js +64 -0
  11. package/dist/readFile.d.ts +1 -25
  12. package/dist/readFile.js +9 -12
  13. package/dist/relative.d.ts +12 -1
  14. package/dist/relative.js +47 -16
  15. package/dist/resolveFile.d.ts +6 -0
  16. package/dist/resolveFile.js +41 -0
  17. package/dist/{extname.d.ts → rm.d.ts} +2 -3
  18. package/dist/rm.js +35 -0
  19. package/dist/stat.d.ts +1 -16
  20. package/dist/stat.js +4 -11
  21. package/dist/{helpers → utils}/stripComments.d.ts +1 -2
  22. package/dist/utils/stripComments.js +120 -0
  23. package/package.json +33 -30
  24. package/bin/clean.js +0 -45
  25. package/bin/cli.js +0 -42
  26. package/bin/git.js +0 -40
  27. package/bin/run.js +0 -41
  28. package/dist/basename.d.ts +0 -7
  29. package/dist/basename.d.ts.map +0 -1
  30. package/dist/basename.js +0 -17
  31. package/dist/basename.js.map +0 -1
  32. package/dist/copy.d.ts +0 -41
  33. package/dist/copy.d.ts.map +0 -1
  34. package/dist/copy.js +0 -90
  35. package/dist/copy.js.map +0 -1
  36. package/dist/cpdir.d.ts +0 -20
  37. package/dist/cpdir.d.ts.map +0 -1
  38. package/dist/cpdir.js +0 -29
  39. package/dist/cpdir.js.map +0 -1
  40. package/dist/cwd.d.ts +0 -7
  41. package/dist/cwd.d.ts.map +0 -1
  42. package/dist/cwd.js +0 -17
  43. package/dist/cwd.js.map +0 -1
  44. package/dist/dirname.d.ts +0 -7
  45. package/dist/dirname.d.ts.map +0 -1
  46. package/dist/dirname.js +0 -31
  47. package/dist/dirname.js.map +0 -1
  48. package/dist/extname.d.ts.map +0 -1
  49. package/dist/extname.js +0 -17
  50. package/dist/extname.js.map +0 -1
  51. package/dist/filename.d.ts +0 -7
  52. package/dist/filename.d.ts.map +0 -1
  53. package/dist/filename.js +0 -25
  54. package/dist/filename.js.map +0 -1
  55. package/dist/find.d.ts +0 -27
  56. package/dist/find.d.ts.map +0 -1
  57. package/dist/find.js +0 -73
  58. package/dist/find.js.map +0 -1
  59. package/dist/findFile.d.ts +0 -21
  60. package/dist/findFile.d.ts.map +0 -1
  61. package/dist/findFile.js +0 -40
  62. package/dist/findFile.js.map +0 -1
  63. package/dist/finddir.d.ts +0 -21
  64. package/dist/finddir.d.ts.map +0 -1
  65. package/dist/finddir.js +0 -40
  66. package/dist/finddir.js.map +0 -1
  67. package/dist/helpers/FileResolver.d.ts +0 -54
  68. package/dist/helpers/FileResolver.d.ts.map +0 -1
  69. package/dist/helpers/FileResolver.js +0 -184
  70. package/dist/helpers/FileResolver.js.map +0 -1
  71. package/dist/helpers/compose.d.ts +0 -14
  72. package/dist/helpers/compose.d.ts.map +0 -1
  73. package/dist/helpers/compose.js +0 -38
  74. package/dist/helpers/compose.js.map +0 -1
  75. package/dist/helpers/each.d.ts +0 -7
  76. package/dist/helpers/each.d.ts.map +0 -1
  77. package/dist/helpers/each.js +0 -21
  78. package/dist/helpers/each.js.map +0 -1
  79. package/dist/helpers/match.d.ts +0 -7
  80. package/dist/helpers/match.d.ts.map +0 -1
  81. package/dist/helpers/match.js +0 -30
  82. package/dist/helpers/match.js.map +0 -1
  83. package/dist/helpers/normalizePath.d.ts +0 -23
  84. package/dist/helpers/normalizePath.d.ts.map +0 -1
  85. package/dist/helpers/normalizePath.js +0 -37
  86. package/dist/helpers/normalizePath.js.map +0 -1
  87. package/dist/helpers/safeCall.d.ts +0 -7
  88. package/dist/helpers/safeCall.d.ts.map +0 -1
  89. package/dist/helpers/safeCall.js +0 -24
  90. package/dist/helpers/safeCall.js.map +0 -1
  91. package/dist/helpers/split.d.ts +0 -7
  92. package/dist/helpers/split.d.ts.map +0 -1
  93. package/dist/helpers/split.js +0 -25
  94. package/dist/helpers/split.js.map +0 -1
  95. package/dist/helpers/stripBOM.d.ts +0 -7
  96. package/dist/helpers/stripBOM.d.ts.map +0 -1
  97. package/dist/helpers/stripBOM.js +0 -19
  98. package/dist/helpers/stripBOM.js.map +0 -1
  99. package/dist/helpers/stripComments.d.ts.map +0 -1
  100. package/dist/helpers/stripComments.js +0 -145
  101. package/dist/helpers/stripComments.js.map +0 -1
  102. package/dist/homedir.d.ts +0 -7
  103. package/dist/homedir.d.ts.map +0 -1
  104. package/dist/homedir.js +0 -32
  105. package/dist/homedir.js.map +0 -1
  106. package/dist/includePaths.d.ts +0 -7
  107. package/dist/includePaths.d.ts.map +0 -1
  108. package/dist/includePaths.js +0 -54
  109. package/dist/includePaths.js.map +0 -1
  110. package/dist/index.d.ts.map +0 -1
  111. package/dist/index.js.map +0 -1
  112. package/dist/indir.d.ts +0 -14
  113. package/dist/indir.d.ts.map +0 -1
  114. package/dist/indir.js +0 -27
  115. package/dist/indir.js.map +0 -1
  116. package/dist/isAbsolutePath.d.ts +0 -7
  117. package/dist/isAbsolutePath.d.ts.map +0 -1
  118. package/dist/isAbsolutePath.js +0 -25
  119. package/dist/isAbsolutePath.js.map +0 -1
  120. package/dist/isDirectory.d.ts +0 -13
  121. package/dist/isDirectory.d.ts.map +0 -1
  122. package/dist/isDirectory.js +0 -27
  123. package/dist/isDirectory.js.map +0 -1
  124. package/dist/isRelativePath.d.ts +0 -7
  125. package/dist/isRelativePath.d.ts.map +0 -1
  126. package/dist/isRelativePath.js +0 -45
  127. package/dist/isRelativePath.js.map +0 -1
  128. package/dist/json.d.ts +0 -22
  129. package/dist/json.d.ts.map +0 -1
  130. package/dist/json.js +0 -35
  131. package/dist/json.js.map +0 -1
  132. package/dist/jsonc.d.ts.map +0 -1
  133. package/dist/jsonc.js.map +0 -1
  134. package/dist/lstat.d.ts +0 -23
  135. package/dist/lstat.d.ts.map +0 -1
  136. package/dist/lstat.js +0 -31
  137. package/dist/lstat.js.map +0 -1
  138. package/dist/mapdir.d.ts +0 -23
  139. package/dist/mapdir.d.ts.map +0 -1
  140. package/dist/mapdir.js +0 -43
  141. package/dist/mapdir.js.map +0 -1
  142. package/dist/mkdir.d.ts +0 -7
  143. package/dist/mkdir.d.ts.map +0 -1
  144. package/dist/mkdir.js +0 -38
  145. package/dist/mkdir.js.map +0 -1
  146. package/dist/parseJSON.d.ts +0 -16
  147. package/dist/parseJSON.d.ts.map +0 -1
  148. package/dist/parseJSON.js +0 -29
  149. package/dist/parseJSON.js.map +0 -1
  150. package/dist/readFile.d.ts.map +0 -1
  151. package/dist/readFile.js.map +0 -1
  152. package/dist/readStream.d.ts +0 -37
  153. package/dist/readStream.d.ts.map +0 -1
  154. package/dist/readStream.js +0 -20
  155. package/dist/readStream.js.map +0 -1
  156. package/dist/readdir.d.ts +0 -17
  157. package/dist/readdir.d.ts.map +0 -1
  158. package/dist/readdir.js +0 -35
  159. package/dist/readdir.js.map +0 -1
  160. package/dist/readline.d.ts +0 -17
  161. package/dist/readline.d.ts.map +0 -1
  162. package/dist/readline.js +0 -25
  163. package/dist/readline.js.map +0 -1
  164. package/dist/realpath.d.ts +0 -13
  165. package/dist/realpath.d.ts.map +0 -1
  166. package/dist/realpath.js +0 -33
  167. package/dist/realpath.js.map +0 -1
  168. package/dist/relative.d.ts.map +0 -1
  169. package/dist/relative.js.map +0 -1
  170. package/dist/resolve.d.ts +0 -27
  171. package/dist/resolve.d.ts.map +0 -1
  172. package/dist/resolve.js +0 -54
  173. package/dist/resolve.js.map +0 -1
  174. package/dist/resolvePath.d.ts +0 -27
  175. package/dist/resolvePath.d.ts.map +0 -1
  176. package/dist/resolvePath.js +0 -178
  177. package/dist/resolvePath.js.map +0 -1
  178. package/dist/rmdir.d.ts +0 -7
  179. package/dist/rmdir.d.ts.map +0 -1
  180. package/dist/rmdir.js +0 -41
  181. package/dist/rmdir.js.map +0 -1
  182. package/dist/stat.d.ts.map +0 -1
  183. package/dist/stat.js.map +0 -1
  184. package/dist/supdir.d.ts +0 -22
  185. package/dist/supdir.d.ts.map +0 -1
  186. package/dist/supdir.js +0 -63
  187. package/dist/supdir.js.map +0 -1
  188. package/dist/symlink.d.ts +0 -23
  189. package/dist/symlink.d.ts.map +0 -1
  190. package/dist/symlink.js +0 -40
  191. package/dist/symlink.js.map +0 -1
  192. package/dist/unlink.d.ts +0 -7
  193. package/dist/unlink.d.ts.map +0 -1
  194. package/dist/unlink.js +0 -29
  195. package/dist/unlink.js.map +0 -1
  196. package/dist/usedir.d.ts +0 -7
  197. package/dist/usedir.d.ts.map +0 -1
  198. package/dist/usedir.js +0 -25
  199. package/dist/usedir.js.map +0 -1
  200. package/dist/writeFile.d.ts +0 -34
  201. package/dist/writeFile.d.ts.map +0 -1
  202. package/dist/writeFile.js +0 -50
  203. package/dist/writeFile.js.map +0 -1
  204. package/dist/writeStream.d.ts +0 -36
  205. package/dist/writeStream.d.ts.map +0 -1
  206. package/dist/writeStream.js +0 -20
  207. package/dist/writeStream.js.map +0 -1
  208. package/src/basename.ts +0 -15
  209. package/src/copy.ts +0 -141
  210. package/src/cpdir.ts +0 -47
  211. package/src/cwd.ts +0 -15
  212. package/src/dirname.ts +0 -34
  213. package/src/extname.ts +0 -15
  214. package/src/filename.ts +0 -25
  215. package/src/find.ts +0 -106
  216. package/src/findFile.ts +0 -61
  217. package/src/finddir.ts +0 -61
  218. package/src/helpers/FileResolver.ts +0 -282
  219. package/src/helpers/compose.ts +0 -49
  220. package/src/helpers/each.ts +0 -19
  221. package/src/helpers/match.ts +0 -31
  222. package/src/helpers/normalizePath.ts +0 -51
  223. package/src/helpers/safeCall.ts +0 -21
  224. package/src/helpers/split.ts +0 -25
  225. package/src/helpers/stripBOM.ts +0 -17
  226. package/src/helpers/stripComments.ts +0 -167
  227. package/src/homedir.ts +0 -34
  228. package/src/includePaths.ts +0 -62
  229. package/src/index.ts +0 -52
  230. package/src/indir.ts +0 -45
  231. package/src/isAbsolutePath.ts +0 -25
  232. package/src/isDirectory.ts +0 -28
  233. package/src/isRelativePath.ts +0 -52
  234. package/src/json.ts +0 -62
  235. package/src/jsonc.ts +0 -70
  236. package/src/lstat.ts +0 -40
  237. package/src/mapdir.ts +0 -59
  238. package/src/mkdir.ts +0 -43
  239. package/src/parseJSON.ts +0 -40
  240. package/src/readFile.ts +0 -64
  241. package/src/readStream.ts +0 -53
  242. package/src/readdir.ts +0 -51
  243. package/src/readline.ts +0 -42
  244. package/src/realpath.ts +0 -35
  245. package/src/relative.ts +0 -43
  246. package/src/resolve.ts +0 -83
  247. package/src/resolvePath.ts +0 -234
  248. package/src/rmdir.ts +0 -44
  249. package/src/stat.ts +0 -40
  250. package/src/supdir.ts +0 -79
  251. package/src/symlink.ts +0 -67
  252. package/src/unlink.ts +0 -31
  253. package/src/usedir.ts +0 -25
  254. package/src/writeFile.ts +0 -86
  255. package/src/writeStream.ts +0 -52
package/src/readdir.ts DELETED
@@ -1,51 +0,0 @@
1
- /**
2
- *****************************************
3
- * Created by edonet@163.com
4
- * Created on 2019-11-07 16:20:26
5
- *****************************************
6
- */
7
- 'use strict';
8
-
9
-
10
- /**
11
- *****************************************
12
- * 加载依赖
13
- *****************************************
14
- */
15
- import * as fs from 'fs';
16
- import * as path from 'path';
17
- import { isDirectory } from './isDirectory';
18
-
19
-
20
- /**
21
- *****************************************
22
- * 状态
23
- *****************************************
24
- */
25
- interface State {
26
- name: string;
27
- path: string;
28
- }
29
-
30
-
31
- /**
32
- *****************************************
33
- * 读取目录
34
- *****************************************
35
- */
36
- export function readdir(dir: string): State[] {
37
- let stats = isDirectory(dir);
38
-
39
- // 存在目录
40
- if (stats) {
41
- dir = stats.path;
42
-
43
- // 读取文件夹
44
- return fs.readdirSync(dir).map(
45
- name => ({ name, path: path.join(dir, name) })
46
- );
47
- }
48
-
49
- // 返回对象
50
- return [];
51
- }
package/src/readline.ts DELETED
@@ -1,42 +0,0 @@
1
- /**
2
- *****************************************
3
- * Created by edonet@163.com
4
- * Created on 2021-05-22 11:53:49
5
- *****************************************
6
- */
7
- 'use strict';
8
-
9
-
10
- /**
11
- *****************************************
12
- * 加载依赖
13
- *****************************************
14
- */
15
- import * as fs from 'fs';
16
- import { Readable } from 'stream';
17
- import { createInterface } from 'readline';
18
- import { Options } from './readStream';
19
-
20
-
21
- /**
22
- *****************************************
23
- * 逐行读取
24
- *****************************************
25
- */
26
- function readline(path: string, encoding?: BufferEncoding): Readable;
27
- function readline(path: string, options?: Options): Readable;
28
- function readline(path: string, options?: BufferEncoding | Options): Readable {
29
- const input = fs.createReadStream(path, options);
30
- const readable = Readable.from(createInterface({ input, crlfDelay: Infinity }));
31
-
32
- // 返回可读流
33
- return readable;
34
- }
35
-
36
-
37
- /**
38
- *****************************************
39
- * 抛出接口
40
- *****************************************
41
- */
42
- export { readline };
package/src/realpath.ts DELETED
@@ -1,35 +0,0 @@
1
- /**
2
- *****************************************
3
- * Created by edonet@163.com
4
- * Created on 2021-07-17 17:42:34
5
- *****************************************
6
- */
7
- 'use strict';
8
-
9
-
10
- /**
11
- *****************************************
12
- * 加载依赖
13
- *****************************************
14
- */
15
- import { realpathSync } from 'fs';
16
- import { resolve } from 'path';
17
- import { isAbsolutePath } from './isAbsolutePath';
18
-
19
-
20
- /**
21
- *****************************************
22
- * 获取基准路径
23
- *****************************************
24
- */
25
- export const baseDir = realpathSync(process.cwd());
26
-
27
-
28
- /**
29
- *****************************************
30
- * 解析真实地址
31
- *****************************************
32
- */
33
- export function realpath(path: string): string {
34
- return isAbsolutePath(path) ? realpathSync(path) : resolve(baseDir, path);
35
- }
package/src/relative.ts DELETED
@@ -1,43 +0,0 @@
1
- /**
2
- *****************************************
3
- * Created by edonet@163.com
4
- * Created on 2021-04-18 15:00:57
5
- *****************************************
6
- */
7
- 'use strict';
8
-
9
-
10
- /**
11
- *****************************************
12
- * 获取相对路径
13
- *****************************************
14
- */
15
- import * as path from 'path';
16
-
17
-
18
- /**
19
- *****************************************
20
- * 获取相对路径
21
- *****************************************
22
- */
23
- export function relative(from: string, to: string): string {
24
- let result = path.relative(from, to);
25
-
26
- // 替换分隔符
27
- if (path.sep === '\\') {
28
- result = result.split('\\').join('/');
29
- }
30
-
31
- // 补全相对路径
32
- if (result.charAt(0) !== '.') {
33
- return './' + result;
34
- }
35
-
36
- // 纯目录
37
- if (['..', '.'].includes(result) || result.endsWith('/..')) {
38
- return result + '/';
39
- }
40
-
41
- // 返回结果
42
- return result;
43
- }
package/src/resolve.ts DELETED
@@ -1,83 +0,0 @@
1
- /**
2
- *****************************************
3
- * Created by edonet@163.com
4
- * Created on 2021-04-18 12:10:46
5
- *****************************************
6
- */
7
- 'use strict';
8
-
9
-
10
- /**
11
- *****************************************
12
- * 加载依赖
13
- *****************************************
14
- */
15
- import { stat } from './stat';
16
- import { dirname } from './dirname';
17
- import { findFile } from './findFile';
18
-
19
-
20
- /**
21
- *****************************************
22
- * 解析配置
23
- *****************************************
24
- */
25
- export interface Options {
26
- ext?: string;
27
- test?: RegExp;
28
- exclude?: string[];
29
- }
30
-
31
-
32
- /**
33
- *****************************************
34
- * 资源对象
35
- *****************************************
36
- */
37
- export interface Asset {
38
- type: 'dir' | 'file';
39
- dir: string;
40
- files: string[];
41
- }
42
-
43
-
44
- /**
45
- *****************************************
46
- * 解析文件
47
- *****************************************
48
- */
49
- export function resolve(dir: string | string[], options: Options = {}): Asset[] {
50
- const input = Array.isArray(dir) ? dir : [dir];
51
- const name = options.ext ? options.ext + '$' : options.test || '*';
52
- const ignore = options.exclude ? ['node_modules', ...options.exclude] : ['node_modules'];
53
- const assets: Asset[] = [];
54
-
55
- // 解析文件
56
- input.forEach(str => {
57
- const stats = stat(str);
58
-
59
- // 不存在目录
60
- if (!stats) {
61
- return;
62
- }
63
-
64
- // 文件入口
65
- if (!stats.isDirectory()) {
66
- return assets.push({
67
- type: 'file',
68
- dir: dirname(stats.path),
69
- files: [stats.path],
70
- });
71
- }
72
-
73
- // 处理文件夹
74
- assets.push({
75
- type: 'dir',
76
- dir: stats.path,
77
- files: findFile(name, stats.path, { ignore }),
78
- });
79
- });
80
-
81
- // 返回结果
82
- return assets;
83
- }
@@ -1,234 +0,0 @@
1
- /**
2
- *****************************************
3
- * Created by edonet@163.com
4
- * Created on 2021-06-19 00:16:40
5
- *****************************************
6
- */
7
- 'use strict';
8
-
9
-
10
- /**
11
- *****************************************
12
- * 加载依赖
13
- *****************************************
14
- */
15
- import { createFileResolver, FileResolver, Handler, Options as FileOptions } from './helpers/FileResolver';
16
- import { split } from './helpers/split';
17
- import { each } from './helpers/each';
18
- import { compose } from './helpers/compose';
19
- import { cwd } from './cwd';
20
-
21
-
22
- /**
23
- *****************************************
24
- * 格式化别名
25
- *****************************************
26
- */
27
- function normalizePaths(value: string | string[]): void | string[] {
28
-
29
- // 校验值
30
- if (!value) {
31
- return;
32
- }
33
-
34
- // 处理字符串配置
35
- if (typeof value === 'string') {
36
- return [value];
37
- }
38
-
39
- // 校验数组配置参数
40
- if (!Array.isArray(value) || !value.length) {
41
- return;
42
- }
43
-
44
- // 校验参数
45
- if (value.find(path => !path || typeof path !== 'string')) {
46
- throw new Error('expect paths value to be valid string[]!');
47
- }
48
-
49
- // 返回结果
50
- return value;
51
- }
52
-
53
-
54
- /**
55
- *****************************************
56
- * 创建路径解析器
57
- *****************************************
58
- */
59
- function createPathsResolver(resolver: FileResolver, options: Options): Handler {
60
- const rules: Handler[] = [];
61
- const baseURL = cwd(options.context || process.cwd(), options.baseUrl || './');
62
- const resolvePath = resolver.resolvePath;
63
-
64
- // 解析路径
65
- function resolvePaths(paths: string[]): Handler {
66
- return compose(paths.map(value => {
67
- const [start, end] = split(value, '*');
68
-
69
- // 处理绝对匹配
70
- if (end === undefined) {
71
- return (_, context) => resolvePath(cwd(baseURL, start), context);
72
- } else {
73
- return (id, context) => resolvePath(cwd(baseURL, start + id + end), context);
74
- }
75
- }));
76
- }
77
-
78
- // 校验存在路径映射配置
79
- each(options.paths, (value, key) => {
80
- const paths = normalizePaths(value);
81
-
82
- // 校验配置
83
- if (!paths) {
84
- return;
85
- }
86
-
87
- // 生成解析函数
88
- const resolve = resolvePaths(paths);
89
-
90
- // 解析匹配项
91
- const [start, end] = split(key, '*');
92
-
93
- // 完成匹配
94
- if (end === undefined) {
95
- return rules.push((id, context) => {
96
- return id === start ? resolve('', context) : undefined;
97
- });
98
- }
99
-
100
- // 获取前/后缀长度
101
- const startLength = start.length;
102
- const endLength = end.length;
103
- const idx = (startLength ? 1 : 0) + (endLength ? 2 : 0);
104
-
105
- // 匹配前缀
106
- if (idx === 1) {
107
- return rules.push((id, context) => {
108
- if (id.startsWith(start)) {
109
- return resolve(id.slice(startLength), context);
110
- }
111
- });
112
- }
113
-
114
- // 匹配后缀
115
- if (idx === 2) {
116
- return rules.push((id, context) => {
117
- if (id.endsWith(end)) {
118
- return resolve(id.slice(0, - endLength), context);
119
- }
120
- });
121
- }
122
-
123
- // 匹配前缀和后缀
124
- if (idx === 3) {
125
- return rules.push((id, context) => {
126
- if (id.startsWith(start) && id.endsWith(end)) {
127
- return resolve(id.slice(startLength, - endLength), context);
128
- }
129
- });
130
- }
131
-
132
- // 默认完全匹配
133
- return rules.push((id, context) => resolve(id, context));
134
- });
135
-
136
- // 组件规则解析方法
137
- if (rules.length) {
138
- return resolver.wrapHandler(compose([...rules, resolver.resolveModule]));
139
- } else {
140
- return resolvePath;
141
- }
142
- }
143
-
144
-
145
- /**
146
- *****************************************
147
- * 创建别名解析器
148
- *****************************************
149
- */
150
- function createAliasResolver(resolver: FileResolver, options: Options): Handler {
151
- const resolvePaths = createPathsResolver(resolver, options);
152
- const rules: Handler[] = [];
153
-
154
- // 解析别名
155
- each(options.alias, (value, key) => {
156
- const paths = normalizePaths(value);
157
-
158
- // 校验参数
159
- if (!paths) {
160
- return;
161
- }
162
-
163
- // 生成解析函数
164
- const resolve: Handler = compose(
165
- paths.map(start => (id, context) => resolvePaths(start + id, context))
166
- );
167
-
168
- // 绝对匹配
169
- if (key.endsWith('$')) {
170
-
171
- // 去除匹配符
172
- key = key.slice(0, -1);
173
-
174
- // 添加匹配规则
175
- return rules.push(
176
- (id, context) => (id === key ? resolve('', context) : undefined)
177
- );
178
- }
179
-
180
- // 生成配置前缀
181
- const prefix = key + '/';
182
- const prefixLength = key.length;
183
-
184
- // 添加匹配规则
185
- return rules.push(
186
- (id, context) => (id === key ? resolve('', context) : undefined),
187
- (id, context) => (id.startsWith(prefix) ? resolve(id.slice(prefixLength), context) : undefined)
188
- );
189
- });
190
-
191
- // 返回解析函数
192
- if (rules.length) {
193
- return resolver.wrapHandler(compose([...rules, resolvePaths]));
194
- } else {
195
- return resolvePaths;
196
- }
197
- }
198
-
199
-
200
- /**
201
- *****************************************
202
- * 选项
203
- *****************************************
204
- */
205
- export interface Options extends FileOptions {
206
- baseUrl?: string;
207
- paths?: {
208
- [key: string]: string[];
209
- };
210
- alias?: {
211
- [key: string]: string | string[];
212
- };
213
- }
214
-
215
-
216
- /**
217
- *****************************************
218
- * 解析文件
219
- *****************************************
220
- */
221
- export function resolvePath(options: Options = {}): Handler {
222
- const modules = options.modules || ['node_modules'];
223
-
224
- // 添加基准模块解析
225
- if (options.baseUrl || options.paths) {
226
- modules.unshift(options.baseUrl || '.');
227
- }
228
-
229
- // 创建文件解析器
230
- const resolver = createFileResolver({ ...options, modules });
231
-
232
- // 返回解析函数
233
- return createAliasResolver(resolver, options);
234
- }
package/src/rmdir.ts DELETED
@@ -1,44 +0,0 @@
1
- /**
2
- *****************************************
3
- * Created by edonet@163.com
4
- * Created on 2019-11-07 17:33:20
5
- *****************************************
6
- */
7
- 'use strict';
8
-
9
-
10
- /**
11
- *****************************************
12
- * 加载依赖
13
- *****************************************
14
- */
15
- import * as fs from 'fs';
16
- import { lstat } from './lstat';
17
- import { readdir } from './readdir';
18
-
19
-
20
- /**
21
- *****************************************
22
- * 移除目录
23
- *****************************************
24
- */
25
- export function rmdir(dir: string): boolean {
26
- let stats = lstat(dir);
27
-
28
- // 存在目录
29
- if (stats) {
30
- if (stats.isDirectory()) {
31
-
32
- // 移除子目录
33
- readdir(dir).forEach(({ path }) => rmdir(path));
34
-
35
- // 移除目录
36
- fs.rmdirSync(dir);
37
- } else {
38
- fs.unlinkSync(dir);
39
- }
40
- }
41
-
42
- // 返回成功
43
- return true;
44
- }
package/src/stat.ts DELETED
@@ -1,40 +0,0 @@
1
- /**
2
- *****************************************
3
- * Created by edonet@163.com
4
- * Created on 2019-11-07 16:06:57
5
- *****************************************
6
- */
7
- 'use strict';
8
-
9
-
10
- /**
11
- *****************************************
12
- * 加载依赖
13
- *****************************************
14
- */
15
- import * as fs from 'fs';
16
- import * as path from 'path';
17
-
18
-
19
- /**
20
- *****************************************
21
- * 状态
22
- *****************************************
23
- */
24
- export interface State extends fs.Stats {
25
- path: string;
26
- }
27
-
28
-
29
- /**
30
- *****************************************
31
- * 获取文件状态
32
- *****************************************
33
- */
34
- export function stat(dir: string): State | null {
35
- try {
36
- return Object.assign(fs.statSync(dir), { path: path.resolve(dir) });
37
- } catch (err) {
38
- return null;
39
- }
40
- }
package/src/supdir.ts DELETED
@@ -1,79 +0,0 @@
1
- /**
2
- *****************************************
3
- * Created by edonet@163.com
4
- * Created on 2019-11-08 18:50:54
5
- *****************************************
6
- */
7
- 'use strict';
8
-
9
-
10
- /**
11
- *****************************************
12
- * 加载依赖
13
- *****************************************
14
- */
15
- import * as path from 'path';
16
- import { dirname } from './dirname';
17
-
18
-
19
- /**
20
- *****************************************
21
- * 遍历目录
22
- *****************************************
23
- */
24
- export function mapdir(dir: string, handler: (dir: string) => void): void {
25
- const resolveFrom = path.resolve(dir);
26
- const sep = path.sep;
27
- const end = resolveFrom.indexOf(sep);
28
-
29
- // 不存在结束符
30
- if (end < 0) {
31
- return handler(resolveFrom);
32
- }
33
-
34
- // 遍历目录
35
- for (let idx = resolveFrom.length; idx >= end; ) {
36
-
37
- // 执行回调
38
- if (idx === 0) {
39
- return handler('/');
40
- } else {
41
- handler(resolveFrom.slice(0, idx));
42
- }
43
-
44
- // 更新索引
45
- idx = resolveFrom.lastIndexOf(sep, idx - 1);
46
- }
47
- }
48
-
49
-
50
- /**
51
- *****************************************
52
- * 查找上一级目录回调
53
- *****************************************
54
- */
55
- interface Handler<T> {
56
- (dir: string, next: () => T | undefined): T;
57
- }
58
-
59
-
60
- /**
61
- *****************************************
62
- * 抛出回调
63
- *****************************************
64
- */
65
- function invoke<T>(dir: string, handler: Handler<T>): T | undefined {
66
- if (dir) {
67
- return handler(dir, () => invoke(dirname(dir), handler));
68
- }
69
- }
70
-
71
-
72
- /**
73
- *****************************************
74
- * 查找上级目录
75
- *****************************************
76
- */
77
- export function supdir<T>(dir: string, handler: Handler<T>): T | undefined {
78
- return invoke(path.resolve(dir), handler);
79
- }