@ainc/fs 0.1.12 → 0.1.16
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 +1 -0
- package/dist/basename.d.ts +6 -6
- package/dist/basename.js +16 -16
- package/dist/copy.d.ts +40 -40
- package/dist/copy.js +89 -89
- package/dist/copy.js.map +1 -1
- package/dist/cpdir.d.ts +19 -19
- package/dist/cpdir.js +28 -28
- package/dist/cpdir.js.map +1 -1
- 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.js +72 -72
- package/dist/find.js.map +1 -1
- package/dist/findFile.d.ts +20 -20
- package/dist/findFile.js +39 -39
- package/dist/findFile.js.map +1 -1
- package/dist/finddir.d.ts +20 -20
- 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/helpers/stripBOM.d.ts +7 -0
- package/dist/helpers/stripBOM.d.ts.map +1 -0
- package/dist/helpers/stripBOM.js +19 -0
- package/dist/helpers/stripBOM.js.map +1 -0
- package/dist/helpers/stripComments.d.ts +7 -0
- package/dist/helpers/stripComments.d.ts.map +1 -0
- package/dist/{normalizeJSON.js → helpers/stripComments.js} +147 -145
- package/dist/helpers/stripComments.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/includePaths.js.map +1 -1
- package/dist/index.d.ts +43 -44
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +90 -92
- 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/isDirectory.js.map +1 -1
- package/dist/isRelativePath.d.ts +6 -6
- package/dist/isRelativePath.js +44 -44
- package/dist/json.d.ts +21 -21
- package/dist/json.d.ts.map +1 -1
- package/dist/json.js +34 -33
- package/dist/json.js.map +1 -1
- package/dist/jsonc.d.ts +27 -21
- package/dist/jsonc.d.ts.map +1 -1
- package/dist/jsonc.js +49 -40
- 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/mapdir.js.map +1 -1
- package/dist/mkdir.d.ts +6 -6
- package/dist/mkdir.js +37 -37
- package/dist/mkdir.js.map +1 -1
- package/dist/parseJSON.d.ts +16 -0
- package/dist/parseJSON.d.ts.map +1 -0
- package/dist/parseJSON.js +29 -0
- package/dist/parseJSON.js.map +1 -0
- package/dist/readFile.d.ts +29 -29
- package/dist/readFile.js +26 -26
- package/dist/readFile.js.map +1 -1
- package/dist/readStream.d.ts +36 -36
- package/dist/readStream.d.ts.map +1 -1
- package/dist/readStream.js +19 -19
- package/dist/readStream.js.map +1 -1
- package/dist/readdir.d.ts +16 -16
- package/dist/readdir.js +34 -34
- package/dist/readdir.js.map +1 -1
- package/dist/readline.d.ts +16 -16
- package/dist/readline.d.ts.map +1 -1
- package/dist/readline.js +24 -24
- package/dist/readline.js.map +1 -1
- package/dist/realpath.d.ts +12 -12
- package/dist/realpath.js +32 -32
- package/dist/realpath.js.map +1 -1
- package/dist/relative.d.ts +6 -6
- package/dist/relative.js +38 -38
- package/dist/resolve.d.ts +26 -26
- 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/rmdir.js.map +1 -1
- package/dist/stat.d.ts +21 -21
- 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/symlink.js.map +1 -1
- package/dist/unlink.d.ts +6 -6
- package/dist/unlink.js +28 -28
- package/dist/unlink.js.map +1 -1
- package/dist/usedir.d.ts +6 -6
- package/dist/usedir.js +24 -24
- package/dist/writeFile.d.ts +33 -33
- package/dist/writeFile.js +49 -49
- package/dist/writeFile.js.map +1 -1
- package/dist/writeStream.d.ts +35 -35
- package/dist/writeStream.d.ts.map +1 -1
- package/dist/writeStream.js +19 -19
- package/dist/writeStream.js.map +1 -1
- package/package.json +4 -4
- 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/helpers/stripBOM.ts +17 -0
- package/src/{normalizeJSON.ts → helpers/stripComments.ts} +58 -52
- package/src/index.ts +2 -3
- package/src/json.ts +2 -1
- package/src/jsonc.ts +22 -11
- package/src/parseJSON.ts +40 -0
- package/src/readStream.ts +2 -2
- package/src/readline.ts +2 -2
- package/src/resolvePath.ts +234 -0
- package/src/supdir.ts +31 -0
- package/src/writeStream.ts +2 -2
- package/dist/normalizeJSON.d.ts +0 -7
- package/dist/normalizeJSON.d.ts.map +0 -1
- package/dist/normalizeJSON.js.map +0 -1
- package/dist/resolveFile.d.ts +0 -22
- package/dist/resolveFile.d.ts.map +0 -1
- package/dist/resolveFile.js +0 -80
- package/dist/resolveFile.js.map +0 -1
- package/dist/resolveModule.d.ts +0 -41
- package/dist/resolveModule.d.ts.map +0 -1
- package/dist/resolveModule.js +0 -119
- package/dist/resolveModule.js.map +0 -1
- package/dist/resolvePaths.d.ts +0 -22
- package/dist/resolvePaths.d.ts.map +0 -1
- package/dist/resolvePaths.js +0 -233
- package/dist/resolvePaths.js.map +0 -1
- package/src/resolveFile.ts +0 -103
- package/src/resolveModule.ts +0 -157
- package/src/resolvePaths.ts +0 -320
|
@@ -25,24 +25,6 @@ function isEscaped(index: number, source: string): number {
|
|
|
25
25
|
}
|
|
26
26
|
|
|
27
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
28
|
/**
|
|
47
29
|
*****************************************
|
|
48
30
|
* 查找字符串结束
|
|
@@ -95,73 +77,97 @@ function indexBlockCommentEnd(start: number, source: string): number {
|
|
|
95
77
|
}
|
|
96
78
|
|
|
97
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
|
+
|
|
98
110
|
/**
|
|
99
111
|
*****************************************
|
|
100
112
|
* 过滤注释
|
|
101
113
|
*****************************************
|
|
102
114
|
*/
|
|
103
|
-
export function
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
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');
|
|
112
133
|
}
|
|
113
134
|
|
|
114
135
|
// 遍历字符
|
|
115
|
-
while (idx <
|
|
116
|
-
|
|
136
|
+
while (idx < size) {
|
|
137
|
+
let ch = content.charAt(idx);
|
|
117
138
|
|
|
118
139
|
// 跳过字符串
|
|
119
140
|
if (ch === '"' || ch === '\'') {
|
|
120
|
-
idx = indexStringEnd(idx + 1, ch, content);
|
|
141
|
+
write(content.slice(idx, idx = indexStringEnd(idx + 1, ch, content)));
|
|
121
142
|
continue;
|
|
122
143
|
}
|
|
123
144
|
|
|
124
145
|
// 自增偏移
|
|
125
146
|
idx ++;
|
|
126
147
|
|
|
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
148
|
// 非注释
|
|
136
149
|
if (ch !== '/') {
|
|
150
|
+
write(ch);
|
|
137
151
|
continue;
|
|
138
152
|
}
|
|
139
153
|
|
|
140
154
|
// 获取下一字符
|
|
141
|
-
const next = content
|
|
155
|
+
const next = content.charAt(idx);
|
|
142
156
|
|
|
143
157
|
// 处理行注释
|
|
144
158
|
if (next === '/') {
|
|
145
|
-
|
|
146
|
-
start = indexLineCommentEnd(idx + 1, content);
|
|
147
|
-
idx = start + 1;
|
|
159
|
+
idx = indexLineCommentEnd(idx + 1, content);
|
|
148
160
|
continue;
|
|
149
161
|
}
|
|
150
162
|
|
|
151
163
|
// 处理多行注释
|
|
152
164
|
if (next === '*') {
|
|
153
|
-
|
|
154
|
-
start = indexBlockCommentEnd(idx + 2, content);
|
|
155
|
-
idx = start;
|
|
165
|
+
idx = indexBlockCommentEnd(idx + 2, content);
|
|
156
166
|
continue;
|
|
157
167
|
}
|
|
158
168
|
}
|
|
159
169
|
|
|
160
|
-
//
|
|
161
|
-
|
|
162
|
-
result += content.slice(start);
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
// 返回结果
|
|
166
|
-
return result;
|
|
170
|
+
// 输出结果
|
|
171
|
+
return buffer.slice(0, offset).toString('utf8');
|
|
167
172
|
}
|
|
173
|
+
|
package/src/index.ts
CHANGED
|
@@ -25,6 +25,7 @@ export { isDirectory } from './isDirectory';
|
|
|
25
25
|
export { indir } from './indir';
|
|
26
26
|
export { json } from './json';
|
|
27
27
|
export { jsonc } from './jsonc';
|
|
28
|
+
export { parseJSON } from './parseJSON';
|
|
28
29
|
export { copy } from './copy';
|
|
29
30
|
export { cpdir } from './cpdir';
|
|
30
31
|
export { find } from './find';
|
|
@@ -45,9 +46,7 @@ export { mapdir } from './mapdir';
|
|
|
45
46
|
export { symlink } from './symlink';
|
|
46
47
|
export { usedir } from './usedir';
|
|
47
48
|
export { supdir } from './supdir';
|
|
48
|
-
export {
|
|
49
|
-
export { resolveModule } from './resolveModule';
|
|
50
|
-
export { resolvePaths } from './resolvePaths';
|
|
49
|
+
export { resolvePath } from './resolvePath';
|
|
51
50
|
export { includePaths } from './includePaths';
|
|
52
51
|
export { isAbsolutePath } from './isAbsolutePath';
|
|
53
52
|
export { isRelativePath } from './isRelativePath';
|
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,7 +15,7 @@
|
|
|
15
15
|
import { stat } from './stat';
|
|
16
16
|
import { readFile } from './readFile';
|
|
17
17
|
import { writeFile } from './writeFile';
|
|
18
|
-
import {
|
|
18
|
+
import { stripComments } from './helpers/stripComments';
|
|
19
19
|
|
|
20
20
|
|
|
21
21
|
/**
|
|
@@ -28,6 +28,24 @@ interface Data {
|
|
|
28
28
|
}
|
|
29
29
|
|
|
30
30
|
|
|
31
|
+
/**
|
|
32
|
+
*****************************************
|
|
33
|
+
* 解析内容
|
|
34
|
+
*****************************************
|
|
35
|
+
*/
|
|
36
|
+
function parse<T = Data>(content: string): T | null {
|
|
37
|
+
|
|
38
|
+
// 格式化内容
|
|
39
|
+
content = stripComments(content);
|
|
40
|
+
|
|
41
|
+
// 打印输出
|
|
42
|
+
// console.log(content);
|
|
43
|
+
|
|
44
|
+
// 解析文件
|
|
45
|
+
return content ? JSON.parse(content) : null;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
|
|
31
49
|
/**
|
|
32
50
|
*****************************************
|
|
33
51
|
* 读取/写入【jsonc】文件
|
|
@@ -44,16 +62,9 @@ function jsonc<T = Data>(...args: [string] | [string, T]): T | null {
|
|
|
44
62
|
} else {
|
|
45
63
|
const stats = stat(name);
|
|
46
64
|
|
|
65
|
+
// 解析文件
|
|
47
66
|
if (stats && stats.isFile()) {
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
// 空白文件
|
|
51
|
-
if (!content.trim()) {
|
|
52
|
-
return null;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
// 解析文件
|
|
56
|
-
return JSON.parse(content);
|
|
67
|
+
return parse(readFile(stats.path));
|
|
57
68
|
}
|
|
58
69
|
}
|
|
59
70
|
|
|
@@ -67,4 +78,4 @@ function jsonc<T = Data>(...args: [string] | [string, T]): T | null {
|
|
|
67
78
|
* 抛出接口
|
|
68
79
|
*****************************************
|
|
69
80
|
*/
|
|
70
|
-
export { jsonc };
|
|
81
|
+
export { jsonc, parse };
|
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/readStream.ts
CHANGED
|
@@ -38,9 +38,9 @@ export interface Options {
|
|
|
38
38
|
* 创建可读流
|
|
39
39
|
*****************************************
|
|
40
40
|
*/
|
|
41
|
-
function readStream(path: string, encoding?:
|
|
41
|
+
function readStream(path: string, encoding?: BufferEncoding): fs.ReadStream;
|
|
42
42
|
function readStream(path: string, options?: Options): fs.ReadStream;
|
|
43
|
-
function readStream(path: string, options?:
|
|
43
|
+
function readStream(path: string, options?: BufferEncoding | Options): fs.ReadStream {
|
|
44
44
|
return fs.createReadStream(path, options);
|
|
45
45
|
}
|
|
46
46
|
|
package/src/readline.ts
CHANGED
|
@@ -23,9 +23,9 @@ import { Options } from './readStream';
|
|
|
23
23
|
* 逐行读取
|
|
24
24
|
*****************************************
|
|
25
25
|
*/
|
|
26
|
-
function readline(path: string, encoding?:
|
|
26
|
+
function readline(path: string, encoding?: BufferEncoding): Readable;
|
|
27
27
|
function readline(path: string, options?: Options): Readable;
|
|
28
|
-
function readline(path: string, options?:
|
|
28
|
+
function readline(path: string, options?: BufferEncoding | Options): Readable {
|
|
29
29
|
const input = fs.createReadStream(path, options);
|
|
30
30
|
const readable = Readable.from(createInterface({ input, crlfDelay: Infinity }));
|
|
31
31
|
|
|
@@ -0,0 +1,234 @@
|
|
|
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/supdir.ts
CHANGED
|
@@ -16,6 +16,37 @@ import * as path from 'path';
|
|
|
16
16
|
import { dirname } from './dirname';
|
|
17
17
|
|
|
18
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
|
+
|
|
19
50
|
/**
|
|
20
51
|
*****************************************
|
|
21
52
|
* 查找上一级目录回调
|
package/src/writeStream.ts
CHANGED
|
@@ -37,9 +37,9 @@ interface Options {
|
|
|
37
37
|
* 创建可写流
|
|
38
38
|
*****************************************
|
|
39
39
|
*/
|
|
40
|
-
function writeStream(path: string, encoding?:
|
|
40
|
+
function writeStream(path: string, encoding?: BufferEncoding): fs.WriteStream;
|
|
41
41
|
function writeStream(path: string, options?: Options): fs.WriteStream;
|
|
42
|
-
function writeStream(path: string, options?:
|
|
42
|
+
function writeStream(path: string, options?: BufferEncoding | Options): fs.WriteStream {
|
|
43
43
|
return fs.createWriteStream(path, options);
|
|
44
44
|
}
|
|
45
45
|
|
package/dist/normalizeJSON.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"normalizeJSON.d.ts","sourceRoot":"","sources":["../src/normalizeJSON.ts"],"names":[],"mappings":"AAiGA;;;;GAIG;AACH,wBAAgB,aAAa,CAAC,OAAO,EAAE,MAAM,GA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AO,MAAM,CAAC;AAClB,CAAC;AAhED,sCAgEC"}
|
package/dist/resolveFile.d.ts
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
*****************************************
|
|
3
|
-
* 配置项
|
|
4
|
-
*****************************************
|
|
5
|
-
*/
|
|
6
|
-
export interface Options {
|
|
7
|
-
extensions?: string[];
|
|
8
|
-
mainFields?: string[];
|
|
9
|
-
}
|
|
10
|
-
/**
|
|
11
|
-
*****************************************
|
|
12
|
-
* 匹配列表
|
|
13
|
-
*****************************************
|
|
14
|
-
*/
|
|
15
|
-
export declare function match<T, P>(list: T[], handler: (data: T, index: number) => void | P): void | P;
|
|
16
|
-
/**
|
|
17
|
-
*****************************************
|
|
18
|
-
* 解析文件
|
|
19
|
-
*****************************************
|
|
20
|
-
*/
|
|
21
|
-
export declare function resolveFile(file: string, options?: Options): void | string;
|
|
22
|
-
//# sourceMappingURL=resolveFile.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"resolveFile.d.ts","sourceRoot":"","sources":["../src/resolveFile.ts"],"names":[],"mappings":"AAmBA;;;;GAIG;AACH,MAAM,WAAW,OAAO;IACpB,UAAU,CAAC,EAAE,MAAM,EAAE,CAAC;IACtB,UAAU,CAAC,EAAE,MAAM,EAAE,CAAC;CACzB;AAGD;;;;GAIG;AACH,wBAAgB,KAAK,CAAC,CAAC,EAAE,CAAC,EAAE,IAAI,EAAE,CAAC,EAAE,EAAE,OAAO,EAAE,CAAC,IAAI,EAAE,CAAC,EAAE,KAAK,EAAE,MAAM,KAAK,IAAI,GAAG,CAAC,GAAG,IAAI,GAAG,CAAC,CAgB9F;AAGD;;;;GAIG;AACH,wBAAgB,WAAW,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,GAAE,OAAY,GAAG,IAAI,GAAG,MAAM,CA2C9E"}
|