@ainc/fs 0.1.9 → 0.1.13

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 (128) hide show
  1. package/README.md +1 -0
  2. package/bin/cli.js +0 -0
  3. package/dist/copy.d.ts +1 -1
  4. package/dist/copy.d.ts.map +1 -1
  5. package/dist/copy.js +6 -3
  6. package/dist/copy.js.map +1 -1
  7. package/dist/find.d.ts +2 -2
  8. package/dist/find.d.ts.map +1 -1
  9. package/dist/find.js +12 -8
  10. package/dist/find.js.map +1 -1
  11. package/dist/findFile.d.ts +1 -1
  12. package/dist/findFile.d.ts.map +1 -1
  13. package/dist/findFile.js.map +1 -1
  14. package/dist/finddir.d.ts +1 -1
  15. package/dist/finddir.d.ts.map +1 -1
  16. package/dist/finddir.js.map +1 -1
  17. package/dist/helpers/FileResolver.d.ts +54 -0
  18. package/dist/helpers/FileResolver.d.ts.map +1 -0
  19. package/dist/helpers/FileResolver.js +184 -0
  20. package/dist/helpers/FileResolver.js.map +1 -0
  21. package/dist/helpers/compose.d.ts +14 -0
  22. package/dist/helpers/compose.d.ts.map +1 -0
  23. package/dist/helpers/compose.js +38 -0
  24. package/dist/helpers/compose.js.map +1 -0
  25. package/dist/helpers/each.d.ts +7 -0
  26. package/dist/helpers/each.d.ts.map +1 -0
  27. package/dist/helpers/each.js +21 -0
  28. package/dist/helpers/each.js.map +1 -0
  29. package/dist/helpers/match.d.ts +7 -0
  30. package/dist/helpers/match.d.ts.map +1 -0
  31. package/dist/helpers/match.js +30 -0
  32. package/dist/helpers/match.js.map +1 -0
  33. package/dist/helpers/normalizePath.d.ts +23 -0
  34. package/dist/helpers/normalizePath.d.ts.map +1 -0
  35. package/dist/helpers/normalizePath.js +37 -0
  36. package/dist/helpers/normalizePath.js.map +1 -0
  37. package/dist/helpers/safeCall.d.ts +7 -0
  38. package/dist/helpers/safeCall.d.ts.map +1 -0
  39. package/dist/helpers/safeCall.js +24 -0
  40. package/dist/helpers/safeCall.js.map +1 -0
  41. package/dist/helpers/split.d.ts +7 -0
  42. package/dist/helpers/split.d.ts.map +1 -0
  43. package/dist/helpers/split.js +25 -0
  44. package/dist/helpers/split.js.map +1 -0
  45. package/dist/includePaths.js +1 -1
  46. package/dist/includePaths.js.map +1 -1
  47. package/dist/index.d.ts +2 -1
  48. package/dist/index.d.ts.map +1 -1
  49. package/dist/index.js +75 -73
  50. package/dist/index.js.map +1 -1
  51. package/dist/isRelativePath.d.ts.map +1 -1
  52. package/dist/isRelativePath.js +27 -1
  53. package/dist/isRelativePath.js.map +1 -1
  54. package/dist/json.d.ts +2 -2
  55. package/dist/json.d.ts.map +1 -1
  56. package/dist/json.js.map +1 -1
  57. package/dist/jsonc.d.ts +2 -2
  58. package/dist/jsonc.d.ts.map +1 -1
  59. package/dist/jsonc.js +2 -3
  60. package/dist/jsonc.js.map +1 -1
  61. package/dist/{stripComments.d.ts → normalizeJSON.d.ts} +2 -2
  62. package/dist/normalizeJSON.d.ts.map +1 -0
  63. package/dist/normalizeJSON.js +145 -0
  64. package/dist/normalizeJSON.js.map +1 -0
  65. package/dist/realpath.d.ts +13 -0
  66. package/dist/realpath.d.ts.map +1 -0
  67. package/dist/realpath.js +33 -0
  68. package/dist/realpath.js.map +1 -0
  69. package/dist/relative.d.ts.map +1 -1
  70. package/dist/relative.js +5 -1
  71. package/dist/relative.js.map +1 -1
  72. package/dist/resolve.d.ts +1 -0
  73. package/dist/resolve.d.ts.map +1 -1
  74. package/dist/resolve.js +1 -1
  75. package/dist/resolve.js.map +1 -1
  76. package/dist/resolvePath.d.ts +27 -0
  77. package/dist/resolvePath.d.ts.map +1 -0
  78. package/dist/resolvePath.js +178 -0
  79. package/dist/resolvePath.js.map +1 -0
  80. package/dist/stat.d.ts +1 -2
  81. package/dist/stat.d.ts.map +1 -1
  82. package/dist/supdir.d.ts +6 -0
  83. package/dist/supdir.d.ts.map +1 -1
  84. package/dist/supdir.js +28 -1
  85. package/dist/supdir.js.map +1 -1
  86. package/dist/writeFile.d.ts +8 -2
  87. package/dist/writeFile.d.ts.map +1 -1
  88. package/dist/writeFile.js +6 -3
  89. package/dist/writeFile.js.map +1 -1
  90. package/package.json +5 -5
  91. package/src/copy.ts +10 -4
  92. package/src/find.ts +16 -12
  93. package/src/findFile.ts +1 -1
  94. package/src/finddir.ts +1 -1
  95. package/src/helpers/FileResolver.ts +282 -0
  96. package/src/helpers/compose.ts +49 -0
  97. package/src/helpers/each.ts +19 -0
  98. package/src/helpers/match.ts +31 -0
  99. package/src/helpers/normalizePath.ts +51 -0
  100. package/src/helpers/safeCall.ts +21 -0
  101. package/src/helpers/split.ts +25 -0
  102. package/src/includePaths.ts +1 -1
  103. package/src/index.ts +2 -1
  104. package/src/isRelativePath.ts +36 -1
  105. package/src/json.ts +3 -3
  106. package/src/jsonc.ts +5 -6
  107. package/src/normalizeJSON.ts +167 -0
  108. package/src/realpath.ts +35 -0
  109. package/src/relative.ts +6 -1
  110. package/src/resolve.ts +2 -1
  111. package/src/resolvePath.ts +234 -0
  112. package/src/stat.ts +1 -1
  113. package/src/supdir.ts +31 -0
  114. package/src/writeFile.ts +11 -8
  115. package/dist/resolveAlias.d.ts +0 -21
  116. package/dist/resolveAlias.d.ts.map +0 -1
  117. package/dist/resolveAlias.js +0 -108
  118. package/dist/resolveAlias.js.map +0 -1
  119. package/dist/stripBOM.d.ts +0 -7
  120. package/dist/stripBOM.d.ts.map +0 -1
  121. package/dist/stripBOM.js +0 -24
  122. package/dist/stripBOM.js.map +0 -1
  123. package/dist/stripComments.d.ts.map +0 -1
  124. package/dist/stripComments.js +0 -100
  125. package/dist/stripComments.js.map +0 -1
  126. package/src/resolveAlias.ts +0 -153
  127. package/src/stripBOM.ts +0 -24
  128. package/src/stripComments.ts +0 -112
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ainc/fs",
3
- "version": "0.1.9",
3
+ "version": "0.1.13",
4
4
  "description": "file system",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -13,15 +13,15 @@
13
13
  "dist"
14
14
  ],
15
15
  "scripts": {
16
- "start": "esc ./src",
17
- "build": "yarn clean && yarn compile",
18
- "watch": "yarn compile -watch",
16
+ "start": "yarn watch",
17
+ "build": "yarn compile",
18
+ "watch": "yarn compile --watch",
19
19
  "compile": "esc -p ./src",
20
20
  "prepublishOnly": "yarn build",
21
21
  "test": "jest",
22
22
  "clean": "rm -rf ./dist"
23
23
  },
24
- "repository": "lifx@edonet:repo/ainc/fs.git",
24
+ "repository": "git@absock.net:repo/ainc/fs.git",
25
25
  "author": "edonet <edonet@163.com>",
26
26
  "license": "MIT",
27
27
  "devDependencies": {
package/src/copy.ts CHANGED
@@ -73,19 +73,25 @@ function doCopy(src: string, dest: string, options: Required<Options>): void {
73
73
  }
74
74
  }
75
75
 
76
- // 复制文件或目录
76
+ // 复制目录
77
77
  if (isDirectory) {
78
78
 
79
79
  // 创建目标目录
80
80
  mkdir(dest);
81
81
 
82
82
  // 复制子文件
83
- fs.readdirSync(src).forEach(name => {
83
+ return fs.readdirSync(src).forEach(name => {
84
84
  if (!ignore.includes(name)) {
85
85
  doCopy(path.join(src, name), path.join(dest, name), options);
86
86
  }
87
87
  });
88
- } else if (transform) {
88
+ }
89
+
90
+ // 创建目标目录
91
+ mkdir(path.dirname(dest));
92
+
93
+ // 复制文件
94
+ if (transform) {
89
95
  fs.writeFileSync(dest, transform(fs.readFileSync(src, 'utf8'), srcStats.path, srcStats));
90
96
  } else {
91
97
  fs.copyFileSync(src, dest);
@@ -100,7 +106,7 @@ function doCopy(src: string, dest: string, options: Required<Options>): void {
100
106
  *****************************************
101
107
  */
102
108
  function copy(src: string, dest: string): void;
103
- function copy(src: string, dest: string, mode: string): void;
109
+ function copy(src: string, dest: string, mode: Options['mode']): void;
104
110
  function copy(src: string, dest: string, transform: Transform): void;
105
111
  function copy(src: string, dest: string, { mode, ignore, transform }: Options): void;
106
112
  function copy(src: string, dest: string, options: Options | Transform | string = {}): void {
package/src/find.ts CHANGED
@@ -31,31 +31,35 @@ interface Options {
31
31
  * 创建匹配函数
32
32
  *****************************************
33
33
  */
34
- export function matchsWith(name: string): (str: string) => boolean {
34
+ export function matchsWith(name: string | RegExp): (str: string) => boolean {
35
35
 
36
- // 匹配部
37
- if (name === '*') {
36
+ // 匹配正则
37
+ if (name instanceof RegExp) {
38
+ const reg = name;
39
+
40
+ // 返回函数
41
+ return (str: string) => reg.test(str);
42
+ }
43
+
44
+ // 匹配全部
45
+ if (name === '*' || !name) {
38
46
  return () => true;
39
47
  }
40
48
 
41
49
  // 匹配开头
42
50
  if (name.charAt(0) === '^') {
43
-
44
- // 获取匹配项
45
- name = name.slice(1);
51
+ const prefix = name.slice(1);
46
52
 
47
53
  // 返回匹配函数
48
- return (str: string) => str.startsWith(name);
54
+ return (str: string) => str.startsWith(prefix);
49
55
  }
50
56
 
51
57
  // 匹配结束
52
58
  if (name.charAt(name.length - 1) === '$') {
53
-
54
- // 获取匹配项
55
- name = name.slice(0, -1);
59
+ const suffix = name.slice(0, -1);
56
60
 
57
61
  // 返回匹配函数
58
- return (str: string) => str.endsWith(name);
62
+ return (str: string) => str.endsWith(suffix);
59
63
  }
60
64
 
61
65
  // 完全匹配
@@ -68,7 +72,7 @@ export function matchsWith(name: string): (str: string) => boolean {
68
72
  * 查找目录
69
73
  *****************************************
70
74
  */
71
- function find(name: string, dir = '.', options: Options = {}): string[] {
75
+ function find(name: string | RegExp, dir = '.', options: Options = {}): string[] {
72
76
  let match = matchsWith(name),
73
77
  result: string[] = [],
74
78
  ignore: string[] = options.ignore || ['node_modules', '.DS_Store'];
package/src/findFile.ts CHANGED
@@ -32,7 +32,7 @@ interface Options {
32
32
  * 查找目录
33
33
  *****************************************
34
34
  */
35
- function findFile(name: string, dir = '.', options: Options = {}): string[] {
35
+ function findFile(name: string | RegExp, dir = '.', options: Options = {}): string[] {
36
36
  let match = matchsWith(name),
37
37
  result: string[] = [],
38
38
  ignore: string[] = options.ignore || ['node_modules', '.DS_Store'];
package/src/finddir.ts CHANGED
@@ -32,7 +32,7 @@ interface Options {
32
32
  * 查找目录
33
33
  *****************************************
34
34
  */
35
- function finddir(name: string, dir = '.', options: Options = {}): string[] {
35
+ function finddir(name: string | RegExp, dir = '.', options: Options = {}): string[] {
36
36
  let match = matchsWith(name),
37
37
  result: string[] = [],
38
38
  ignore: string[] = options.ignore || ['node_modules', '.DS_Store'];
@@ -0,0 +1,282 @@
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 * as path from 'path';
16
+ import * as Module from 'module';
17
+ import { relative } from '@/relative';
18
+ import { mapdir } from '@/supdir';
19
+ import { json } from '@/json';
20
+ import { isRelativePath } from '@/isRelativePath';
21
+ import { safeCall } from './safeCall';
22
+ import { match } from './match';
23
+ import { normalizePath } from './normalizePath';
24
+
25
+
26
+ /**
27
+ *****************************************
28
+ * 声明属性
29
+ *****************************************
30
+ */
31
+ declare module 'module' {
32
+ export const _extensions: Extensions;
33
+ export function _nodeModulePaths(basePath: string): string[];
34
+ export function _resolveFilename(id: string, parent?: Module, isMain?: boolean): void | string;
35
+ }
36
+
37
+
38
+ /**
39
+ *****************************************
40
+ * 扩展解析函数
41
+ *****************************************
42
+ */
43
+ type Extensions = Record<string, () => void>;
44
+
45
+
46
+ /**
47
+ *****************************************
48
+ * 解析模块目录
49
+ *****************************************
50
+ */
51
+ function resolveModules(context: string, modules: string[]): (resolveFrom: string, paths: string[]) => string[] {
52
+ const rules: [number, string][] = [];
53
+
54
+ // 生成解析规则
55
+ modules.forEach(value => {
56
+
57
+ // 处理绝对路径
58
+ if (path.isAbsolute(value)) {
59
+ return rules.push([0, value]);
60
+ }
61
+
62
+ // 处理相对路径
63
+ if (isRelativePath(value)) {
64
+ return rules.push([0, path.resolve(context, value)]);
65
+ }
66
+
67
+ // 处理模块路径
68
+ if (value) {
69
+ rules.push([value === 'node_modules' ? 1 : 2, value]);
70
+ }
71
+ });
72
+
73
+ // 返回解析函数
74
+ return (resolveFrom, paths) => {
75
+ const result: string[] = [];
76
+
77
+ // 添加路径
78
+ function add(value: string): void {
79
+ result.includes(value) || result.push(value);
80
+ }
81
+
82
+ // 遍历规则
83
+ rules.forEach(([type, value]) => {
84
+ switch (type) {
85
+ case 0:
86
+ return add(value);
87
+ case 1:
88
+ return paths.forEach(add);
89
+ default:
90
+ return mapdir(resolveFrom, (dir) => add(path.join(dir, value)));
91
+ }
92
+ });
93
+
94
+ // 返回结果
95
+ return result;
96
+ };
97
+ }
98
+
99
+
100
+ /**
101
+ *****************************************
102
+ * 模块解析器
103
+ *****************************************
104
+ */
105
+ interface Resolver {
106
+ paths: string[];
107
+ resolve(id: string, context?: string): void | string;
108
+ }
109
+
110
+
111
+ /**
112
+ *****************************************
113
+ * 配置项
114
+ *****************************************
115
+ */
116
+ export interface Options {
117
+ context?: string;
118
+ modules?: string[];
119
+ extensions?: string[];
120
+ mainFields?: string[];
121
+ useRelativePath?: boolean;
122
+ }
123
+
124
+
125
+ /**
126
+ *****************************************
127
+ * 解析函数
128
+ *****************************************
129
+ */
130
+ export type Handler = (id: string, context?: string) => void | string;
131
+
132
+
133
+ /**
134
+ *****************************************
135
+ * 文件解析器
136
+ *****************************************
137
+ */
138
+ export interface FileResolver {
139
+ resolvePath: Handler;
140
+ resolveFile: Handler;
141
+ resolveModule: Handler;
142
+ wrapHandler(handler: Handler): Handler;
143
+ }
144
+
145
+
146
+ /**
147
+ *****************************************
148
+ * 创建路径解析器
149
+ *****************************************
150
+ */
151
+ export function createFileResolver(options: Options = {}): FileResolver {
152
+ const exts = options.extensions || ['.js', '.ts', '.mjs', '.jsx', '.tsx', '.json'];
153
+ const cached = new Map<string, Resolver>();
154
+ const mainFields = options.mainFields || ['main'];
155
+ const basePath = options.context || process.cwd();
156
+ const useRelativePath = !!options.useRelativePath;
157
+ const resolvePaths = resolveModules(basePath, options.modules || ['node_modules']);
158
+
159
+ // 执行函数函数
160
+ function invokeHandler<T>(resolve: () => T): void | T {
161
+ const extensions = Module._extensions;
162
+ const keys = Object.keys(extensions);
163
+ const cached: Extensions = {};
164
+
165
+ // 添加扩展函数
166
+ keys.forEach(key => (cached[key] = extensions[key], delete extensions[key]));
167
+ exts.forEach(key => (extensions[key] = cached[key] || cached['.js']));
168
+
169
+ // 解析文件
170
+ const result = safeCall(resolve);
171
+
172
+ // 清空扩展函数
173
+ exts.forEach(key => (delete extensions[key]));
174
+ keys.forEach(key => (extensions[key] = cached[key]));
175
+
176
+ // 返回结果
177
+ return result;
178
+ }
179
+
180
+ // 创建解析器
181
+ function createResolver(context: string): Resolver {
182
+ const filename = path.join(context, 'noop.js');
183
+ const module = new Module(filename);
184
+ const paths = resolvePaths(context, Module._nodeModulePaths(context));
185
+
186
+ // 更新模块信息
187
+ module.filename = filename;
188
+ module.paths = paths;
189
+ module.loaded = true;
190
+
191
+ // 创建解析器
192
+ const resolver: Resolver = {
193
+ paths,
194
+ resolve(id) {
195
+ return invokeHandler(() => Module._resolveFilename(id, module, false));
196
+ },
197
+ };
198
+
199
+ // 生成缓存
200
+ cached.set(context, resolver);
201
+
202
+ // 返回解析函数
203
+ return resolver;
204
+ }
205
+
206
+ // 解析函数
207
+ function resolvePath(id: string, context?: string): void | string {
208
+ if (path.isAbsolute(id) || isRelativePath(id)) {
209
+ return resolveFile(id, context);
210
+ } else {
211
+ return resolveModule(id, context);
212
+ }
213
+ }
214
+
215
+ // 解析文件路径
216
+ function resolveFile(id: string, context?: string): void | string {
217
+ const resolveFrom = path.resolve(basePath, context || '');
218
+ const resolver = cached.get(resolveFrom) || createResolver(resolveFrom);
219
+ const data = normalizePath(id);
220
+ const resolvedPath = resolver.resolve(data.path);
221
+
222
+ // 解析失败
223
+ if (!resolvedPath) {
224
+ return;
225
+ }
226
+
227
+ // 返回解析结果
228
+ if (!useRelativePath) {
229
+ return resolvedPath + data.query + data.hash;
230
+ }
231
+
232
+ // 解析相对路径
233
+ return isRelativePath(data.path) ? id : relative(resolveFrom, id);
234
+ }
235
+
236
+ // 解析模块路径
237
+ function resolveModule(id: string, context?: string): void | string {
238
+ const resolveFrom = path.resolve(basePath, context || '');
239
+ const resolver = cached.get(resolveFrom) || createResolver(resolveFrom);
240
+ const data = normalizePath(id);
241
+ const resolvedPath = match(mainFields, (name) => {
242
+
243
+ // 使用默认解析
244
+ if (name === 'main') {
245
+ return resolver.resolve(id);
246
+ }
247
+
248
+ // 获取包配置信息
249
+ return match(resolver.paths, (dir) => {
250
+ const pkg = json<Record<string, string>>(path.join(dir, id, 'package.json'));
251
+
252
+ // 存在配置
253
+ if (pkg?.[name]) {
254
+ return resolver.resolve(path.resolve(dir, id, pkg[name]));
255
+ }
256
+ });
257
+ });
258
+
259
+ // 解析失败
260
+ if (!resolvedPath) {
261
+ return;
262
+ }
263
+
264
+ // 返回结果
265
+ return useRelativePath ? id : `${resolvedPath}${data.query}${data.hash}`;
266
+ }
267
+
268
+ // 包裹函数
269
+ function wrapHandler(resolve: Handler): Handler {
270
+ return (id, context) => {
271
+ if (path.isAbsolute(id) || isRelativePath(id)) {
272
+ return resolveFile(id, context);
273
+ } else {
274
+ return resolve(id, context);
275
+ }
276
+ };
277
+ }
278
+
279
+ // 返回结果
280
+ return { resolvePath, resolveFile, resolveModule, wrapHandler };
281
+ }
282
+
@@ -0,0 +1,49 @@
1
+ /**
2
+ *****************************************
3
+ * Created by edonet@163.com
4
+ * Created on 2021-12-16 22:20:04
5
+ *****************************************
6
+ */
7
+ 'use strict';
8
+
9
+
10
+ /**
11
+ *****************************************
12
+ * 解析规则
13
+ *****************************************
14
+ */
15
+ type Handler = (...args: any[]) => void | unknown;
16
+
17
+
18
+ /**
19
+ *****************************************
20
+ * 组合函数
21
+ *****************************************
22
+ */
23
+ export function compose<T extends Handler>(rules: T[]): T {
24
+
25
+ // 无处理函数
26
+ if (rules.length === 0) {
27
+ return (() => undefined) as T;
28
+ }
29
+
30
+ // 单一处理函数
31
+ if (rules.length === 1) {
32
+ return rules[0];
33
+ }
34
+
35
+ // 复合处理函数
36
+ function composed(...args: any[]): void | unknown {
37
+ for (let idx = 0, len = rules.length; idx < len; idx++) {
38
+ const result = rules[idx](...args);
39
+
40
+ // 解析成功
41
+ if (result) {
42
+ return result;
43
+ }
44
+ }
45
+ }
46
+
47
+ // 返回结果
48
+ return composed as T;
49
+ }
@@ -0,0 +1,19 @@
1
+ /**
2
+ *****************************************
3
+ * Created by edonet@163.com
4
+ * Created on 2021-12-18 16:52:32
5
+ *****************************************
6
+ */
7
+ 'use strict';
8
+
9
+
10
+ /**
11
+ *****************************************
12
+ * 遍历对象
13
+ *****************************************
14
+ */
15
+ export function each<T>(target: T, handler: (value: T[keyof T], key: string) => void): void {
16
+ if (target && typeof target === 'object') {
17
+ Object.keys(target).forEach(key => handler(target[key as keyof T], key));
18
+ }
19
+ }
@@ -0,0 +1,31 @@
1
+ /**
2
+ *****************************************
3
+ * Created by edonet@163.com
4
+ * Created on 2021-12-16 00:10:41
5
+ *****************************************
6
+ */
7
+ 'use strict';
8
+
9
+
10
+ /**
11
+ *****************************************
12
+ * 匹配列表
13
+ *****************************************
14
+ */
15
+ export function match<T, P>(list: T[], handler: (data: T, index: number) => void | P): void | P {
16
+
17
+ // 处理只有一个元素的情况
18
+ if (list.length === 1) {
19
+ return handler(list[0], 0);
20
+ }
21
+
22
+ // 处理列表
23
+ for (let idx = 0, len = list.length; idx < len; idx ++) {
24
+ const result = handler(list[idx], idx);
25
+
26
+ // 返回结果
27
+ if (typeof result !== 'undefined') {
28
+ return result;
29
+ }
30
+ }
31
+ }
@@ -0,0 +1,51 @@
1
+ /**
2
+ *****************************************
3
+ * Created by edonet@163.com
4
+ * Created on 2021-12-18 11:16:21
5
+ *****************************************
6
+ */
7
+ 'use strict';
8
+
9
+
10
+ /**
11
+ *****************************************
12
+ * 路径信息
13
+ *****************************************
14
+ */
15
+ export interface PathInformation {
16
+ path: string;
17
+ query: string;
18
+ hash: string;
19
+ }
20
+
21
+
22
+ /**
23
+ *****************************************
24
+ * 切分字符
25
+ *****************************************
26
+ */
27
+ export function split(str: string, sep: string): [string, string] {
28
+ const idx = str.indexOf(sep);
29
+
30
+ // 未找到分割字符串
31
+ if (idx === -1) {
32
+ return [str, ''];
33
+ }
34
+
35
+ // 分割字符串
36
+ return [str.substring(0, idx), str.substring(idx)];
37
+ }
38
+
39
+
40
+ /**
41
+ *****************************************
42
+ * 格式化路径
43
+ *****************************************
44
+ */
45
+ export function normalizePath(id: string): PathInformation {
46
+ const [str, hash] = split(id, '#');
47
+ const [path, query] = split(str, '?');
48
+
49
+ // 返回结果
50
+ return { path, query, hash };
51
+ }
@@ -0,0 +1,21 @@
1
+ /**
2
+ *****************************************
3
+ * Created by edonet@163.com
4
+ * Created on 2021-12-16 00:02:15
5
+ *****************************************
6
+ */
7
+ 'use strict';
8
+
9
+
10
+ /**
11
+ *****************************************
12
+ * 安全执行
13
+ *****************************************
14
+ */
15
+ export function safeCall<T>(handler: () => T): void | T {
16
+ try {
17
+ return handler();
18
+ } catch (err) {
19
+ return;
20
+ }
21
+ }
@@ -0,0 +1,25 @@
1
+ /**
2
+ *****************************************
3
+ * Created by edonet@163.com
4
+ * Created on 2021-12-21 21:17:28
5
+ *****************************************
6
+ */
7
+ 'use strict';
8
+
9
+
10
+ /**
11
+ *****************************************
12
+ * 分割字符串
13
+ *****************************************
14
+ */
15
+ export function split(str: string, sep: string): [string, string?] {
16
+ const idx = str.indexOf(sep);
17
+
18
+ // 不包含匹配符
19
+ if (idx < 0) {
20
+ return [str];
21
+ }
22
+
23
+ // 分割配置字符
24
+ return [str.substring(0, idx), str.substring(idx + 1)];
25
+ }
@@ -39,7 +39,7 @@ export function includePaths(paths: string[]): (source: string) => boolean {
39
39
  }
40
40
 
41
41
  // 切分路径分段
42
- const chunks = value.split(sep);
42
+ const chunks = value.split('/');
43
43
 
44
44
  // 生成匹配函数
45
45
  return (source: string) => {
package/src/index.ts CHANGED
@@ -14,6 +14,7 @@
14
14
  */
15
15
  export { homedir } from './homedir';
16
16
  export { cwd } from './cwd';
17
+ export { realpath } from './realpath';
17
18
  export { dirname } from './dirname';
18
19
  export { filename } from './filename';
19
20
  export { basename } from './basename';
@@ -44,7 +45,7 @@ export { mapdir } from './mapdir';
44
45
  export { symlink } from './symlink';
45
46
  export { usedir } from './usedir';
46
47
  export { supdir } from './supdir';
47
- export { resolveAlias } from './resolveAlias';
48
+ export { resolvePath } from './resolvePath';
48
49
  export { includePaths } from './includePaths';
49
50
  export { isAbsolutePath } from './isAbsolutePath';
50
51
  export { isRelativePath } from './isRelativePath';