@ainc/fs 0.1.16 → 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 -20
  8. package/dist/jsonc.js +14 -31
  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 -147
  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 -173
  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 -81
  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
@@ -1,282 +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 * 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
-
@@ -1,49 +0,0 @@
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
- }
@@ -1,19 +0,0 @@
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
- }
@@ -1,31 +0,0 @@
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
- }
@@ -1,51 +0,0 @@
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
- }
@@ -1,21 +0,0 @@
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
- }
@@ -1,25 +0,0 @@
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
- }
@@ -1,17 +0,0 @@
1
- /**
2
- *****************************************
3
- * Created by edonet@163.com
4
- * Created on 2022-01-01 20:53:11
5
- *****************************************
6
- */
7
- 'use strict';
8
-
9
-
10
- /**
11
- *****************************************
12
- * 移除【BOM】标识
13
- *****************************************
14
- */
15
- export function stripBOM(content: string): string {
16
- return content.charCodeAt(0) === 0xFEFF ? content.slice(1) : content;
17
- }
@@ -1,173 +0,0 @@
1
- /**
2
- *****************************************
3
- * Created by edonet@163.com
4
- * Created on 2021-05-31 22:41:48
5
- *****************************************
6
- */
7
- 'use strict';
8
-
9
-
10
- /**
11
- *****************************************
12
- * 判断是否为转义字符
13
- *****************************************
14
- */
15
- function isEscaped(index: number, source: string): number {
16
- let count = 1;
17
-
18
- // 统计转义字符
19
- while (source[index - count] === '\\') {
20
- count ++;
21
- }
22
-
23
- // 判断是否转义
24
- return count % 2;
25
- }
26
-
27
-
28
- /**
29
- *****************************************
30
- * 查找字符串结束
31
- *****************************************
32
- */
33
- function indexStringEnd(start: number, ch: string, source: string): number {
34
- const idx = source.indexOf(ch, start);
35
-
36
- // 判断是否查找结束
37
- if (idx === -1) {
38
- return source.length;
39
- } else {
40
- return isEscaped(idx, source) ? idx + 1 : indexStringEnd(idx + 1, ch, source);
41
- }
42
- }
43
-
44
-
45
- /**
46
- *****************************************
47
- * 查找行注释结束
48
- *****************************************
49
- */
50
- function indexLineCommentEnd(start: number, source: string): number {
51
- const idx = source.indexOf('\n', start);
52
-
53
- // 判断是否查找结束
54
- if (idx === -1) {
55
- return source.length;
56
- } else {
57
- return source[idx - 1] === '\r' ? idx - 1 : idx;
58
- }
59
- }
60
-
61
-
62
- /**
63
- *****************************************
64
- * 查找块注释结束
65
- *****************************************
66
- */
67
- function indexBlockCommentEnd(start: number, source: string): number {
68
- const idx = source.indexOf('/', start);
69
-
70
- if (idx === -1) {
71
- return source.length;
72
- } else if (source[idx - 1] === '*') {
73
- return idx + 1;
74
- } else {
75
- return indexBlockCommentEnd(idx + 1, source);
76
- }
77
- }
78
-
79
-
80
- /**
81
- *****************************************
82
- * 过滤末尾逗号
83
- *****************************************
84
- */
85
- function stripTrailingComma(buffer: Buffer, offset: number): number {
86
- let idx = offset - 1;
87
- let code = buffer[idx];
88
-
89
- // 路过空白字符
90
- while ([0x20, 0x0a, 0x0d].includes(buffer[idx])) {
91
- idx --;
92
- code = buffer[idx];
93
- }
94
-
95
- // 判断为非逗号
96
- if (code !== 0x2c) {
97
- return offset;
98
- }
99
-
100
- // 移动字符
101
- while (idx < offset) {
102
- buffer[idx] = buffer[++ idx];
103
- }
104
-
105
- // 返回新索引
106
- return offset - 1;
107
- }
108
-
109
-
110
- /**
111
- *****************************************
112
- * 过滤注释
113
- *****************************************
114
- */
115
- export function stripComments(content: string): string {
116
- const size = content.length;
117
- const buffer = Buffer.alloc(3 * size);
118
-
119
- // 定义索引位置
120
- let idx = content.charCodeAt(0) === 0xFEFF ? 1 : 0;
121
- let offset = 0;
122
-
123
- // 写入内容
124
- function write(value: string): void {
125
-
126
- // 去除末尾的逗号
127
- if (value === ']' || value === '}') {
128
- offset = stripTrailingComma(buffer, offset);
129
- }
130
-
131
- // 写入内容
132
- offset += buffer.write(value, offset, 'utf8');
133
- }
134
-
135
- // 遍历字符
136
- while (idx < size) {
137
- let ch = content.charAt(idx);
138
-
139
- // 跳过字符串
140
- if (ch === '"' || ch === '\'') {
141
- write(content.slice(idx, idx = indexStringEnd(idx + 1, ch, content)));
142
- continue;
143
- }
144
-
145
- // 自增偏移
146
- idx ++;
147
-
148
- // 非注释
149
- if (ch !== '/') {
150
- write(ch);
151
- continue;
152
- }
153
-
154
- // 获取下一字符
155
- const next = content.charAt(idx);
156
-
157
- // 处理行注释
158
- if (next === '/') {
159
- idx = indexLineCommentEnd(idx + 1, content);
160
- continue;
161
- }
162
-
163
- // 处理多行注释
164
- if (next === '*') {
165
- idx = indexBlockCommentEnd(idx + 2, content);
166
- continue;
167
- }
168
- }
169
-
170
- // 输出结果
171
- return buffer.slice(0, offset).toString('utf8');
172
- }
173
-
package/src/homedir.ts DELETED
@@ -1,34 +0,0 @@
1
- /**
2
- *****************************************
3
- * Created by edonet@163.com
4
- * Created on 2019-11-14 10:19:06
5
- *****************************************
6
- */
7
- 'use strict';
8
-
9
-
10
- /**
11
- *****************************************
12
- * 加载依赖
13
- *****************************************
14
- */
15
- import * as os from 'os';
16
- import * as path from 'path';
17
-
18
-
19
- /**
20
- *****************************************
21
- * 用户目录
22
- *****************************************
23
- */
24
- const basedir = os.homedir();
25
-
26
-
27
- /**
28
- *****************************************
29
- * 使用目录路径
30
- *****************************************
31
- */
32
- export function homedir(...args: string[]): string {
33
- return path.resolve(basedir, ...args);
34
- }