@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.
- package/README.md +1 -0
- package/bin/cli.js +0 -0
- package/dist/copy.d.ts +1 -1
- package/dist/copy.d.ts.map +1 -1
- package/dist/copy.js +6 -3
- package/dist/copy.js.map +1 -1
- package/dist/find.d.ts +2 -2
- package/dist/find.d.ts.map +1 -1
- package/dist/find.js +12 -8
- package/dist/find.js.map +1 -1
- package/dist/findFile.d.ts +1 -1
- package/dist/findFile.d.ts.map +1 -1
- package/dist/findFile.js.map +1 -1
- package/dist/finddir.d.ts +1 -1
- package/dist/finddir.d.ts.map +1 -1
- 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/includePaths.js +1 -1
- package/dist/includePaths.js.map +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +75 -73
- package/dist/index.js.map +1 -1
- package/dist/isRelativePath.d.ts.map +1 -1
- package/dist/isRelativePath.js +27 -1
- package/dist/isRelativePath.js.map +1 -1
- package/dist/json.d.ts +2 -2
- package/dist/json.d.ts.map +1 -1
- package/dist/json.js.map +1 -1
- package/dist/jsonc.d.ts +2 -2
- package/dist/jsonc.d.ts.map +1 -1
- package/dist/jsonc.js +2 -3
- package/dist/jsonc.js.map +1 -1
- package/dist/{stripComments.d.ts → normalizeJSON.d.ts} +2 -2
- package/dist/normalizeJSON.d.ts.map +1 -0
- package/dist/normalizeJSON.js +145 -0
- package/dist/normalizeJSON.js.map +1 -0
- 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.map +1 -1
- package/dist/relative.js +5 -1
- package/dist/relative.js.map +1 -1
- package/dist/resolve.d.ts +1 -0
- package/dist/resolve.d.ts.map +1 -1
- package/dist/resolve.js +1 -1
- 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/stat.d.ts +1 -2
- package/dist/stat.d.ts.map +1 -1
- package/dist/supdir.d.ts +6 -0
- package/dist/supdir.d.ts.map +1 -1
- package/dist/supdir.js +28 -1
- package/dist/supdir.js.map +1 -1
- package/dist/writeFile.d.ts +8 -2
- package/dist/writeFile.d.ts.map +1 -1
- package/dist/writeFile.js +6 -3
- package/dist/writeFile.js.map +1 -1
- package/package.json +5 -5
- 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/includePaths.ts +1 -1
- package/src/index.ts +2 -1
- package/src/isRelativePath.ts +36 -1
- package/src/json.ts +3 -3
- package/src/jsonc.ts +5 -6
- package/src/normalizeJSON.ts +167 -0
- package/src/realpath.ts +35 -0
- package/src/relative.ts +6 -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.map +0 -1
- package/dist/stripComments.js +0 -100
- package/dist/stripComments.js.map +0 -1
- package/src/resolveAlias.ts +0 -153
- package/src/stripBOM.ts +0 -24
- package/src/stripComments.ts +0 -112
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
|
@@ -32,9 +32,9 @@ interface Data {
|
|
|
32
32
|
* 读取/写入【json】文件
|
|
33
33
|
*****************************************
|
|
34
34
|
*/
|
|
35
|
-
function json<T
|
|
36
|
-
function json<T
|
|
37
|
-
function json<T
|
|
35
|
+
function json<T = Data>(path: string): T | null;
|
|
36
|
+
function json<T = Data>(path: string, data: T): T;
|
|
37
|
+
function json<T = Data>(...args: [string] | [string, T]): T | null {
|
|
38
38
|
const [name, data] = args;
|
|
39
39
|
|
|
40
40
|
// 读写数据
|
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 { normalizeJSON } from './normalizeJSON';
|
|
20
19
|
|
|
21
20
|
|
|
22
21
|
/**
|
|
@@ -34,9 +33,9 @@ interface Data {
|
|
|
34
33
|
* 读取/写入【jsonc】文件
|
|
35
34
|
*****************************************
|
|
36
35
|
*/
|
|
37
|
-
function jsonc<T
|
|
38
|
-
function jsonc<T
|
|
39
|
-
function jsonc<T
|
|
36
|
+
function jsonc<T = Data>(path: string): T | null;
|
|
37
|
+
function jsonc<T = Data>(path: string, data: T): T;
|
|
38
|
+
function jsonc<T = Data>(...args: [string] | [string, T]): T | null {
|
|
40
39
|
const [name, data] = args;
|
|
41
40
|
|
|
42
41
|
// 读写数据
|
|
@@ -46,7 +45,7 @@ function jsonc<T extends Data>(...args: [string] | [string, T]): T | null {
|
|
|
46
45
|
const stats = stat(name);
|
|
47
46
|
|
|
48
47
|
if (stats && stats.isFile()) {
|
|
49
|
-
const content =
|
|
48
|
+
const content = normalizeJSON(readFile(stats.path));
|
|
50
49
|
|
|
51
50
|
// 空白文件
|
|
52
51
|
if (!content.trim()) {
|
|
@@ -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 normalizeJSON(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/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
|
@@ -21,7 +21,12 @@ import * as path from 'path';
|
|
|
21
21
|
*****************************************
|
|
22
22
|
*/
|
|
23
23
|
export function relative(from: string, to: string): string {
|
|
24
|
-
|
|
24
|
+
let result = path.relative(from, to);
|
|
25
|
+
|
|
26
|
+
// 替换分隔符
|
|
27
|
+
if (path.sep === '\\') {
|
|
28
|
+
result = result.split('\\').join('/');
|
|
29
|
+
}
|
|
25
30
|
|
|
26
31
|
// 补全相对路径
|
|
27
32
|
if (result.charAt(0) !== '.') {
|
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
|
|
|
@@ -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/stat.ts
CHANGED
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/writeFile.ts
CHANGED
|
@@ -25,7 +25,7 @@ import { dirname } from './dirname';
|
|
|
25
25
|
interface Options {
|
|
26
26
|
encoding?: string;
|
|
27
27
|
append?: boolean;
|
|
28
|
-
mode?:
|
|
28
|
+
mode?: fs.Mode;
|
|
29
29
|
flag?: string;
|
|
30
30
|
}
|
|
31
31
|
|
|
@@ -35,24 +35,27 @@ interface Options {
|
|
|
35
35
|
* 写入文件
|
|
36
36
|
*****************************************
|
|
37
37
|
*/
|
|
38
|
-
function writeFile(path: string, content
|
|
38
|
+
function writeFile(path: string, content?: string | Buffer): void;
|
|
39
39
|
function writeFile(path: string, content: string | Buffer, encoding: string): void;
|
|
40
40
|
function writeFile(path: string, content: string | Buffer, append: boolean): void;
|
|
41
41
|
function writeFile(path: string, content: string | Buffer, { append, encoding, mode, flag }: Options): void;
|
|
42
|
-
function writeFile(path: string, content
|
|
42
|
+
function writeFile(path: string, content?: string | Buffer, options?: string | boolean | Options): void {
|
|
43
43
|
|
|
44
44
|
// 默认配置
|
|
45
45
|
if (!options) {
|
|
46
46
|
options = { encoding: 'utf8' };
|
|
47
47
|
}
|
|
48
48
|
|
|
49
|
-
//
|
|
50
|
-
if (typeof
|
|
49
|
+
// 默认值
|
|
50
|
+
if (typeof content === 'undefined') {
|
|
51
|
+
content = '';
|
|
52
|
+
}
|
|
51
53
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
+
// 创建目录
|
|
55
|
+
mkdir(dirname(path));
|
|
54
56
|
|
|
55
|
-
|
|
57
|
+
// 校验配置
|
|
58
|
+
if (typeof options === 'object') {
|
|
56
59
|
if (options.append) {
|
|
57
60
|
return fs.appendFileSync(path, content, options as fs.WriteFileOptions);
|
|
58
61
|
} else {
|
package/dist/resolveAlias.d.ts
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
*****************************************
|
|
3
|
-
* 别名配置
|
|
4
|
-
*****************************************
|
|
5
|
-
*/
|
|
6
|
-
export interface Options {
|
|
7
|
-
baseUrl?: string;
|
|
8
|
-
paths?: {
|
|
9
|
-
[key: string]: string[];
|
|
10
|
-
};
|
|
11
|
-
alias?: {
|
|
12
|
-
[key: string]: string;
|
|
13
|
-
};
|
|
14
|
-
}
|
|
15
|
-
/**
|
|
16
|
-
*****************************************
|
|
17
|
-
* 解析路径别名
|
|
18
|
-
*****************************************
|
|
19
|
-
*/
|
|
20
|
-
export declare function resolveAlias(options: Options): (source: string) => string;
|
|
21
|
-
//# sourceMappingURL=resolveAlias.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"resolveAlias.d.ts","sourceRoot":"","sources":["../src/resolveAlias.ts"],"names":[],"mappings":"AAsGA;;;;GAIG;AACH,MAAM,WAAW,OAAO;IACpB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,KAAK,CAAC,EAAE;QAAE,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,EAAE,CAAA;KAAE,CAAC;IACpC,KAAK,CAAC,EAAE;QAAE,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAAA;KAAE,CAAC;CACrC;AAGD;;;;GAIG;AACH,wBAAgB,YAAY,CAAC,OAAO,EAAE,OAAO,GAAG,CAAC,MAAM,EAAE,MAAM,KAAK,MAAM,CAiCzE"}
|