@ainc/fs 0.1.10 → 0.1.14
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.
- package/README.md +2 -0
- package/dist/basename.d.ts +6 -6
- package/dist/basename.js +16 -16
- package/dist/copy.d.ts +40 -40
- package/dist/copy.d.ts.map +1 -1
- package/dist/copy.js +89 -86
- package/dist/copy.js.map +1 -1
- package/dist/cpdir.d.ts +19 -19
- package/dist/cpdir.js +28 -28
- package/dist/cwd.d.ts +6 -6
- package/dist/cwd.js +16 -16
- package/dist/dirname.d.ts +6 -6
- package/dist/dirname.js +30 -30
- package/dist/extname.d.ts +6 -6
- package/dist/extname.js +16 -16
- package/dist/filename.d.ts +6 -6
- package/dist/filename.js +24 -24
- package/dist/find.d.ts +26 -26
- package/dist/find.d.ts.map +1 -1
- package/dist/find.js +72 -68
- package/dist/find.js.map +1 -1
- package/dist/findFile.d.ts +20 -20
- package/dist/findFile.d.ts.map +1 -1
- package/dist/findFile.js +39 -39
- package/dist/findFile.js.map +1 -1
- package/dist/finddir.d.ts +20 -20
- package/dist/finddir.d.ts.map +1 -1
- package/dist/finddir.js +39 -39
- package/dist/finddir.js.map +1 -1
- package/dist/helpers/FileResolver.d.ts +54 -0
- package/dist/helpers/FileResolver.d.ts.map +1 -0
- package/dist/helpers/FileResolver.js +184 -0
- package/dist/helpers/FileResolver.js.map +1 -0
- package/dist/helpers/compose.d.ts +14 -0
- package/dist/helpers/compose.d.ts.map +1 -0
- package/dist/helpers/compose.js +38 -0
- package/dist/helpers/compose.js.map +1 -0
- package/dist/helpers/each.d.ts +7 -0
- package/dist/helpers/each.d.ts.map +1 -0
- package/dist/helpers/each.js +21 -0
- package/dist/helpers/each.js.map +1 -0
- package/dist/helpers/match.d.ts +7 -0
- package/dist/helpers/match.d.ts.map +1 -0
- package/dist/helpers/match.js +30 -0
- package/dist/helpers/match.js.map +1 -0
- package/dist/helpers/normalizePath.d.ts +23 -0
- package/dist/helpers/normalizePath.d.ts.map +1 -0
- package/dist/helpers/normalizePath.js +37 -0
- package/dist/helpers/normalizePath.js.map +1 -0
- package/dist/helpers/safeCall.d.ts +7 -0
- package/dist/helpers/safeCall.d.ts.map +1 -0
- package/dist/helpers/safeCall.js +24 -0
- package/dist/helpers/safeCall.js.map +1 -0
- package/dist/helpers/split.d.ts +7 -0
- package/dist/helpers/split.d.ts.map +1 -0
- package/dist/helpers/split.js +25 -0
- package/dist/helpers/split.js.map +1 -0
- package/dist/homedir.d.ts +6 -6
- package/dist/homedir.js +31 -31
- package/dist/includePaths.d.ts +6 -6
- package/dist/includePaths.js +53 -53
- package/dist/index.d.ts +42 -41
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +88 -86
- package/dist/index.js.map +1 -1
- package/dist/indir.d.ts +13 -13
- package/dist/indir.js +26 -26
- package/dist/isAbsolutePath.d.ts +6 -6
- package/dist/isAbsolutePath.js +24 -24
- package/dist/isDirectory.d.ts +12 -12
- package/dist/isDirectory.js +26 -26
- package/dist/isRelativePath.d.ts +6 -6
- package/dist/isRelativePath.d.ts.map +1 -1
- package/dist/isRelativePath.js +44 -18
- package/dist/isRelativePath.js.map +1 -1
- package/dist/json.d.ts +21 -21
- package/dist/json.js +33 -33
- package/dist/jsonc.d.ts +21 -21
- package/dist/jsonc.d.ts.map +1 -1
- package/dist/jsonc.js +40 -41
- package/dist/jsonc.js.map +1 -1
- package/dist/lstat.d.ts +22 -22
- package/dist/lstat.js +30 -30
- package/dist/mapdir.d.ts +22 -22
- package/dist/mapdir.js +42 -42
- package/dist/mkdir.d.ts +6 -6
- package/dist/mkdir.js +37 -37
- package/dist/normalizeJSON.d.ts +7 -0
- package/dist/normalizeJSON.d.ts.map +1 -0
- package/dist/normalizeJSON.js +145 -0
- package/dist/normalizeJSON.js.map +1 -0
- package/dist/readFile.d.ts +29 -29
- package/dist/readFile.js +26 -26
- package/dist/readStream.d.ts +36 -36
- package/dist/readStream.js +19 -19
- package/dist/readdir.d.ts +16 -16
- package/dist/readdir.js +34 -34
- package/dist/readline.d.ts +16 -16
- package/dist/readline.js +24 -24
- package/dist/realpath.d.ts +13 -0
- package/dist/realpath.d.ts.map +1 -0
- package/dist/realpath.js +33 -0
- package/dist/realpath.js.map +1 -0
- package/dist/relative.d.ts +6 -6
- package/dist/relative.js +38 -38
- package/dist/relative.js.map +1 -1
- package/dist/resolve.d.ts +26 -25
- package/dist/resolve.d.ts.map +1 -1
- package/dist/resolve.js +53 -53
- package/dist/resolve.js.map +1 -1
- package/dist/resolvePath.d.ts +27 -0
- package/dist/resolvePath.d.ts.map +1 -0
- package/dist/resolvePath.js +178 -0
- package/dist/resolvePath.js.map +1 -0
- package/dist/rmdir.d.ts +6 -6
- package/dist/rmdir.js +40 -40
- package/dist/stat.d.ts +21 -22
- package/dist/stat.d.ts.map +1 -1
- package/dist/stat.js +30 -30
- package/dist/supdir.d.ts +21 -15
- package/dist/supdir.d.ts.map +1 -1
- package/dist/supdir.js +62 -35
- package/dist/supdir.js.map +1 -1
- package/dist/symlink.d.ts +22 -22
- package/dist/symlink.js +39 -39
- package/dist/unlink.d.ts +6 -6
- package/dist/unlink.js +28 -28
- package/dist/usedir.d.ts +6 -6
- package/dist/usedir.js +24 -24
- package/dist/writeFile.d.ts +33 -27
- package/dist/writeFile.d.ts.map +1 -1
- package/dist/writeFile.js +49 -46
- package/dist/writeFile.js.map +1 -1
- package/dist/writeStream.d.ts +35 -35
- package/dist/writeStream.js +19 -19
- package/package.json +7 -7
- package/src/copy.ts +10 -4
- package/src/find.ts +16 -12
- package/src/findFile.ts +1 -1
- package/src/finddir.ts +1 -1
- package/src/helpers/FileResolver.ts +282 -0
- package/src/helpers/compose.ts +49 -0
- package/src/helpers/each.ts +19 -0
- package/src/helpers/match.ts +31 -0
- package/src/helpers/normalizePath.ts +51 -0
- package/src/helpers/safeCall.ts +21 -0
- package/src/helpers/split.ts +25 -0
- package/src/{stripBOM.ts → helpers/stripBOM.ts} +3 -10
- package/src/helpers/stripComments.ts +167 -0
- package/src/index.ts +3 -1
- package/src/isRelativePath.ts +36 -1
- package/src/json.ts +2 -1
- package/src/jsonc.ts +2 -3
- package/src/parseJSON.ts +40 -0
- package/src/realpath.ts +35 -0
- package/src/relative.ts +1 -1
- package/src/resolve.ts +2 -1
- package/src/resolvePath.ts +234 -0
- package/src/stat.ts +1 -1
- package/src/supdir.ts +31 -0
- package/src/writeFile.ts +11 -8
- package/dist/resolveAlias.d.ts +0 -21
- package/dist/resolveAlias.d.ts.map +0 -1
- package/dist/resolveAlias.js +0 -108
- package/dist/resolveAlias.js.map +0 -1
- package/dist/stripBOM.d.ts +0 -7
- package/dist/stripBOM.d.ts.map +0 -1
- package/dist/stripBOM.js +0 -24
- package/dist/stripBOM.js.map +0 -1
- package/dist/stripComments.d.ts +0 -7
- package/dist/stripComments.d.ts.map +0 -1
- package/dist/stripComments.js +0 -100
- package/dist/stripComments.js.map +0 -1
- package/src/resolveAlias.ts +0 -153
- package/src/stripComments.ts +0 -112
|
@@ -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
|
+
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
*****************************************
|
|
3
3
|
* Created by edonet@163.com
|
|
4
|
-
* Created on
|
|
4
|
+
* Created on 2022-01-01 20:53:11
|
|
5
5
|
*****************************************
|
|
6
6
|
*/
|
|
7
7
|
'use strict';
|
|
@@ -9,16 +9,9 @@
|
|
|
9
9
|
|
|
10
10
|
/**
|
|
11
11
|
*****************************************
|
|
12
|
-
*
|
|
12
|
+
* 移除【BOM】标识
|
|
13
13
|
*****************************************
|
|
14
14
|
*/
|
|
15
15
|
export function stripBOM(content: string): string {
|
|
16
|
-
|
|
17
|
-
// 检测第一个字符是否为BOM
|
|
18
|
-
if (content.charCodeAt(0) === 0xFEFF) {
|
|
19
|
-
return content.slice(1);
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
// 返回源内容
|
|
23
|
-
return content;
|
|
16
|
+
return content.charCodeAt(0) === 0xFEFF ? content.slice(1) : content;
|
|
24
17
|
}
|
|
@@ -0,0 +1,167 @@
|
|
|
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 isTrailingComma(start: number, source: string): boolean {
|
|
34
|
+
let ch = source[start];
|
|
35
|
+
|
|
36
|
+
// 过滤空白字符
|
|
37
|
+
while (' \n\r'.indexOf(ch) > -1) {
|
|
38
|
+
ch = source[++ start];
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// 返回是否为结束符
|
|
42
|
+
return '}]'.indexOf(ch) > -1;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
*****************************************
|
|
48
|
+
* 查找字符串结束
|
|
49
|
+
*****************************************
|
|
50
|
+
*/
|
|
51
|
+
function indexStringEnd(start: number, ch: string, source: string): number {
|
|
52
|
+
const idx = source.indexOf(ch, start);
|
|
53
|
+
|
|
54
|
+
// 判断是否查找结束
|
|
55
|
+
if (idx === -1) {
|
|
56
|
+
return source.length;
|
|
57
|
+
} else {
|
|
58
|
+
return isEscaped(idx, source) ? idx + 1 : indexStringEnd(idx + 1, ch, source);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
*****************************************
|
|
65
|
+
* 查找行注释结束
|
|
66
|
+
*****************************************
|
|
67
|
+
*/
|
|
68
|
+
function indexLineCommentEnd(start: number, source: string): number {
|
|
69
|
+
const idx = source.indexOf('\n', start);
|
|
70
|
+
|
|
71
|
+
// 判断是否查找结束
|
|
72
|
+
if (idx === -1) {
|
|
73
|
+
return source.length;
|
|
74
|
+
} else {
|
|
75
|
+
return source[idx - 1] === '\r' ? idx - 1 : idx;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
*****************************************
|
|
82
|
+
* 查找块注释结束
|
|
83
|
+
*****************************************
|
|
84
|
+
*/
|
|
85
|
+
function indexBlockCommentEnd(start: number, source: string): number {
|
|
86
|
+
const idx = source.indexOf('/', start);
|
|
87
|
+
|
|
88
|
+
if (idx === -1) {
|
|
89
|
+
return source.length;
|
|
90
|
+
} else if (source[idx - 1] === '*') {
|
|
91
|
+
return idx + 1;
|
|
92
|
+
} else {
|
|
93
|
+
return indexBlockCommentEnd(idx + 1, source);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
*****************************************
|
|
100
|
+
* 过滤注释
|
|
101
|
+
*****************************************
|
|
102
|
+
*/
|
|
103
|
+
export function stripComments(content: string): string {
|
|
104
|
+
let result = '';
|
|
105
|
+
let idx = 0;
|
|
106
|
+
let len = content.length;
|
|
107
|
+
let start = 0;
|
|
108
|
+
|
|
109
|
+
// 检测第一个字符是否为BOM
|
|
110
|
+
if (content.charCodeAt(0) === 0xFEFF) {
|
|
111
|
+
idx = start = 1;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// 遍历字符
|
|
115
|
+
while (idx < len) {
|
|
116
|
+
const ch = content[idx];
|
|
117
|
+
|
|
118
|
+
// 跳过字符串
|
|
119
|
+
if (ch === '"' || ch === '\'') {
|
|
120
|
+
idx = indexStringEnd(idx + 1, ch, content);
|
|
121
|
+
continue;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// 自增偏移
|
|
125
|
+
idx ++;
|
|
126
|
+
|
|
127
|
+
// 判断是为结尾的逗号
|
|
128
|
+
if (ch === ',' && isTrailingComma(idx, content)) {
|
|
129
|
+
result += content.slice(start, idx - 1);
|
|
130
|
+
start = idx;
|
|
131
|
+
idx = start + 1;
|
|
132
|
+
continue;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// 非注释
|
|
136
|
+
if (ch !== '/') {
|
|
137
|
+
continue;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// 获取下一字符
|
|
141
|
+
const next = content[idx];
|
|
142
|
+
|
|
143
|
+
// 处理行注释
|
|
144
|
+
if (next === '/') {
|
|
145
|
+
result += content.slice(start, idx - 1);
|
|
146
|
+
start = indexLineCommentEnd(idx + 1, content);
|
|
147
|
+
idx = start + 1;
|
|
148
|
+
continue;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// 处理多行注释
|
|
152
|
+
if (next === '*') {
|
|
153
|
+
result += content.slice(start, idx - 1);
|
|
154
|
+
start = indexBlockCommentEnd(idx + 2, content);
|
|
155
|
+
idx = start;
|
|
156
|
+
continue;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// 添加结束片段
|
|
161
|
+
if (start < len) {
|
|
162
|
+
result += content.slice(start);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// 返回结果
|
|
166
|
+
return result;
|
|
167
|
+
}
|
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';
|
|
@@ -24,6 +25,7 @@ export { isDirectory } from './isDirectory';
|
|
|
24
25
|
export { indir } from './indir';
|
|
25
26
|
export { json } from './json';
|
|
26
27
|
export { jsonc } from './jsonc';
|
|
28
|
+
export { parseJSON } from './parseJSON';
|
|
27
29
|
export { copy } from './copy';
|
|
28
30
|
export { cpdir } from './cpdir';
|
|
29
31
|
export { find } from './find';
|
|
@@ -44,7 +46,7 @@ export { mapdir } from './mapdir';
|
|
|
44
46
|
export { symlink } from './symlink';
|
|
45
47
|
export { usedir } from './usedir';
|
|
46
48
|
export { supdir } from './supdir';
|
|
47
|
-
export {
|
|
49
|
+
export { resolvePath } from './resolvePath';
|
|
48
50
|
export { includePaths } from './includePaths';
|
|
49
51
|
export { isAbsolutePath } from './isAbsolutePath';
|
|
50
52
|
export { isRelativePath } from './isRelativePath';
|
package/src/isRelativePath.ts
CHANGED
|
@@ -7,11 +7,46 @@
|
|
|
7
7
|
'use strict';
|
|
8
8
|
|
|
9
9
|
|
|
10
|
+
/**
|
|
11
|
+
*****************************************
|
|
12
|
+
* 加载依赖
|
|
13
|
+
*****************************************
|
|
14
|
+
*/
|
|
15
|
+
import { sep } from 'path';
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
*****************************************
|
|
20
|
+
* 判断是否为路径分隔符
|
|
21
|
+
*****************************************
|
|
22
|
+
*/
|
|
23
|
+
function isSep(ch: string): boolean {
|
|
24
|
+
|
|
25
|
+
// 模块相对标识
|
|
26
|
+
if (ch === '/' || ch === '') {
|
|
27
|
+
return true;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// 路径相对标识
|
|
31
|
+
return ch === sep;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
|
|
10
35
|
/**
|
|
11
36
|
*****************************************
|
|
12
37
|
* 判断是否为相对路径
|
|
13
38
|
*****************************************
|
|
14
39
|
*/
|
|
15
40
|
export function isRelativePath(source: string): boolean {
|
|
16
|
-
|
|
41
|
+
|
|
42
|
+
// 非相对标识
|
|
43
|
+
if (source.charAt(0) !== '.') {
|
|
44
|
+
return false;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// 位1字符
|
|
48
|
+
const ch1 = source.charAt(1);
|
|
49
|
+
|
|
50
|
+
// 非相对路径
|
|
51
|
+
return ch1 === '.' ? isSep(source.charAt(2)) : isSep(ch1);
|
|
17
52
|
}
|
package/src/json.ts
CHANGED
|
@@ -15,6 +15,7 @@
|
|
|
15
15
|
import { stat } from './stat';
|
|
16
16
|
import { readFile } from './readFile';
|
|
17
17
|
import { writeFile } from './writeFile';
|
|
18
|
+
import { stripBOM } from './helpers/stripBOM';
|
|
18
19
|
|
|
19
20
|
|
|
20
21
|
/**
|
|
@@ -44,7 +45,7 @@ function json<T = Data>(...args: [string] | [string, T]): T | null {
|
|
|
44
45
|
const stats = stat(name);
|
|
45
46
|
|
|
46
47
|
if (stats && stats.isFile()) {
|
|
47
|
-
return JSON.parse(readFile(stats.path));
|
|
48
|
+
return JSON.parse(stripBOM(readFile(stats.path)));
|
|
48
49
|
}
|
|
49
50
|
}
|
|
50
51
|
|
package/src/jsonc.ts
CHANGED
|
@@ -15,8 +15,7 @@
|
|
|
15
15
|
import { stat } from './stat';
|
|
16
16
|
import { readFile } from './readFile';
|
|
17
17
|
import { writeFile } from './writeFile';
|
|
18
|
-
import {
|
|
19
|
-
import { stripComments } from './stripComments';
|
|
18
|
+
import { stripComments } from './helpers/stripComments';
|
|
20
19
|
|
|
21
20
|
|
|
22
21
|
/**
|
|
@@ -46,7 +45,7 @@ function jsonc<T = Data>(...args: [string] | [string, T]): T | null {
|
|
|
46
45
|
const stats = stat(name);
|
|
47
46
|
|
|
48
47
|
if (stats && stats.isFile()) {
|
|
49
|
-
const content = stripComments(
|
|
48
|
+
const content = stripComments(readFile(stats.path));
|
|
50
49
|
|
|
51
50
|
// 空白文件
|
|
52
51
|
if (!content.trim()) {
|
package/src/parseJSON.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
*****************************************
|
|
3
|
+
* Created by edonet@163.com
|
|
4
|
+
* Created on 2022-01-01 21:01:19
|
|
5
|
+
*****************************************
|
|
6
|
+
*/
|
|
7
|
+
'use strict';
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
*****************************************
|
|
12
|
+
* 加载依赖
|
|
13
|
+
*****************************************
|
|
14
|
+
*/
|
|
15
|
+
import { stripBOM } from './helpers/stripBOM';
|
|
16
|
+
import { stripComments } from './helpers/stripComments';
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
*****************************************
|
|
21
|
+
* 选项
|
|
22
|
+
*****************************************
|
|
23
|
+
*/
|
|
24
|
+
interface Options {
|
|
25
|
+
comments?: boolean;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
*****************************************
|
|
31
|
+
* 解析【JSON】文件
|
|
32
|
+
*****************************************
|
|
33
|
+
*/
|
|
34
|
+
export function parseJSON<T>(content: string, options: Options = {}): T {
|
|
35
|
+
const normalized = options.comments ? stripComments(content) : stripBOM(content);
|
|
36
|
+
const source = normalized.trim();
|
|
37
|
+
|
|
38
|
+
// 解析内容
|
|
39
|
+
return source ? JSON.parse(source) : undefined;
|
|
40
|
+
}
|
package/src/realpath.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
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
CHANGED
package/src/resolve.ts
CHANGED
|
@@ -24,6 +24,7 @@ import { findFile } from './findFile';
|
|
|
24
24
|
*/
|
|
25
25
|
export interface Options {
|
|
26
26
|
ext?: string;
|
|
27
|
+
test?: RegExp;
|
|
27
28
|
exclude?: string[];
|
|
28
29
|
}
|
|
29
30
|
|
|
@@ -47,7 +48,7 @@ export interface Asset {
|
|
|
47
48
|
*/
|
|
48
49
|
export function resolve(dir: string | string[], options: Options = {}): Asset[] {
|
|
49
50
|
const input = Array.isArray(dir) ? dir : [dir];
|
|
50
|
-
const name = options.ext ? options.ext + '$' : '*';
|
|
51
|
+
const name = options.ext ? options.ext + '$' : options.test || '*';
|
|
51
52
|
const ignore = options.exclude ? ['node_modules', ...options.exclude] : ['node_modules'];
|
|
52
53
|
const assets: Asset[] = [];
|
|
53
54
|
|