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