@ainc/fs 0.1.22 → 0.1.24
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/dist/cache.d.ts +16 -0
- package/dist/cache.js +24 -0
- package/dist/dict.d.ts +21 -0
- package/dist/dict.js +66 -0
- package/dist/download.js +4 -16
- package/dist/downloadFile.js +19 -13
- package/dist/gzip.d.ts +13 -0
- package/dist/gzip.js +50 -0
- package/dist/index.d.ts +37 -1
- package/dist/index.exports.json +34 -0
- package/dist/index.js +60 -3
- package/dist/isEsmModule.d.ts +18 -0
- package/dist/isEsmModule.js +63 -0
- package/dist/loadPackageDescription.d.ts +61 -0
- package/dist/loadPackageDescription.js +71 -0
- package/dist/loadTsConfig.d.ts +87 -0
- package/dist/loadTsConfig.js +219 -0
- package/dist/match.d.ts +30 -0
- package/dist/match.js +59 -0
- package/dist/promises.d.ts +69 -0
- package/dist/promises.js +103 -0
- package/dist/resolveAlias.d.ts +25 -0
- package/dist/resolveAlias.js +82 -0
- package/dist/resolveDirect.d.ts +15 -0
- package/dist/resolveDirect.js +144 -0
- package/dist/resolveExports.d.ts +19 -0
- package/dist/resolveExports.js +54 -0
- package/dist/resolveImports.d.ts +21 -0
- package/dist/resolveImports.js +114 -0
- package/dist/resolveModuleDir.d.ts +12 -0
- package/dist/resolveModuleDir.js +67 -0
- package/dist/resolveModuleId.d.ts +6 -0
- package/dist/resolveModuleId.js +32 -0
- package/dist/resolvePath.d.ts +23 -0
- package/dist/resolvePath.js +86 -0
- package/dist/resolvePaths.d.ts +13 -0
- package/dist/resolvePaths.js +42 -0
- package/dist/split.d.ts +6 -0
- package/dist/split.js +24 -0
- package/dist/sys.d.ts +60 -0
- package/dist/sys.js +260 -0
- package/esm/cache.mjs +22 -0
- package/esm/dict.mjs +63 -0
- package/esm/download.mjs +4 -16
- package/esm/downloadFile.mjs +19 -13
- package/esm/gzip.mjs +47 -0
- package/esm/index.exports.json +34 -0
- package/esm/index.mjs +29 -1
- package/esm/isEsmModule.mjs +59 -0
- package/esm/loadPackageDescription.mjs +67 -0
- package/esm/loadTsConfig.mjs +215 -0
- package/esm/match.mjs +53 -0
- package/esm/promises.mjs +96 -0
- package/esm/resolveAlias.mjs +79 -0
- package/esm/resolveDirect.mjs +142 -0
- package/esm/resolveExports.mjs +51 -0
- package/esm/resolveImports.mjs +112 -0
- package/esm/resolveModuleDir.mjs +64 -0
- package/esm/resolveModuleId.mjs +30 -0
- package/esm/resolvePath.mjs +84 -0
- package/esm/resolvePaths.mjs +40 -0
- package/esm/split.mjs +22 -0
- package/esm/sys.mjs +258 -0
- package/package.json +5 -6
package/esm/sys.mjs
ADDED
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
/**
|
|
2
|
+
*****************************************
|
|
3
|
+
* Created by edonet@163.com
|
|
4
|
+
* Created on 2025-06-29 16:06:27
|
|
5
|
+
*****************************************
|
|
6
|
+
*/
|
|
7
|
+
'use strict';
|
|
8
|
+
/**
|
|
9
|
+
*****************************************
|
|
10
|
+
* 加载依赖
|
|
11
|
+
*****************************************
|
|
12
|
+
*/
|
|
13
|
+
import { fileURLToPath } from 'node:url';
|
|
14
|
+
import { resolve, isAbsolute, sep } from 'node:path';
|
|
15
|
+
import { builtinModules } from 'node:module';
|
|
16
|
+
import { isRelative } from './relative.mjs';
|
|
17
|
+
import { stat } from './stat.mjs';
|
|
18
|
+
import { resolvePackageDescription } from './loadPackageDescription.mjs';
|
|
19
|
+
import { resolveModuleId } from './resolveModuleId.mjs';
|
|
20
|
+
import { resolveModuleDir } from './resolveModuleDir.mjs';
|
|
21
|
+
import { resolveAlias } from './resolveAlias.mjs';
|
|
22
|
+
import { resolvePaths } from './resolvePaths.mjs';
|
|
23
|
+
import { resolveImports } from './resolveImports.mjs';
|
|
24
|
+
import { resolveExports } from './resolveExports.mjs';
|
|
25
|
+
/**
|
|
26
|
+
*****************************************
|
|
27
|
+
* 全局缓存
|
|
28
|
+
*****************************************
|
|
29
|
+
*/
|
|
30
|
+
const cache = new Map();
|
|
31
|
+
/**
|
|
32
|
+
*****************************************
|
|
33
|
+
* 文件系统
|
|
34
|
+
*****************************************
|
|
35
|
+
*/
|
|
36
|
+
export const sys = {
|
|
37
|
+
/** 解析基准路径 */
|
|
38
|
+
baseUrl: undefined,
|
|
39
|
+
/** 别名配置 */
|
|
40
|
+
alias: undefined,
|
|
41
|
+
/** 路径配置 */
|
|
42
|
+
paths: undefined,
|
|
43
|
+
/** 缓存 ID */
|
|
44
|
+
cacheSalt: '',
|
|
45
|
+
/** 缓存 */
|
|
46
|
+
cache,
|
|
47
|
+
/** 当前目录 */
|
|
48
|
+
cwd: process.cwd(),
|
|
49
|
+
/** 是否解析模块 */
|
|
50
|
+
module: true,
|
|
51
|
+
/** 是否解析模块导出 */
|
|
52
|
+
moduleExports: true,
|
|
53
|
+
/** 解析 main 字段 */
|
|
54
|
+
mainFields: ['main'],
|
|
55
|
+
/** 解析条件 */
|
|
56
|
+
conditions: ['require', 'node'],
|
|
57
|
+
/** 解析扩展名 */
|
|
58
|
+
extensions: ['.ts', '.tsx', '.js', '.json', '.jsx', '.mjs', '.cjs', '.mts', '.cts', '.node', '.wasm'],
|
|
59
|
+
/** 解析路径 */
|
|
60
|
+
resolve(id, from) {
|
|
61
|
+
const cacheId = `${from}\x00${id}`;
|
|
62
|
+
const cacheSaltId = `${cacheId}\x00${this.cacheSalt}`;
|
|
63
|
+
// 存在缓存时,直接返回
|
|
64
|
+
if (this.cache.has(cacheId)) {
|
|
65
|
+
return this.cache.get(cacheId);
|
|
66
|
+
}
|
|
67
|
+
// 存在缓存时,直接返回
|
|
68
|
+
if (this.cache.has(cacheSaltId)) {
|
|
69
|
+
return this.cache.get(cacheSaltId);
|
|
70
|
+
}
|
|
71
|
+
// 添加缓存,避免循环解析
|
|
72
|
+
this.cache.set(cacheSaltId, null);
|
|
73
|
+
// 匹配规则
|
|
74
|
+
const rules = [
|
|
75
|
+
[cacheId, this.resolveBuiltin],
|
|
76
|
+
[cacheSaltId, this.resolveModuleImports],
|
|
77
|
+
[cacheId, this.resolveFile],
|
|
78
|
+
[cacheSaltId, this.resolveModule],
|
|
79
|
+
];
|
|
80
|
+
// 解析规则
|
|
81
|
+
for (let i = 0, l = rules.length; i < l; i++) {
|
|
82
|
+
const [cacheId, handler] = rules[i];
|
|
83
|
+
const resolved = handler.call(this, id, from);
|
|
84
|
+
if (resolved !== undefined) {
|
|
85
|
+
this.cache.set(cacheId, resolved);
|
|
86
|
+
return resolved;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
},
|
|
90
|
+
/** 解析内部模块 */
|
|
91
|
+
resolveBuiltin(id) {
|
|
92
|
+
if (builtinModules.includes(id)) {
|
|
93
|
+
return id;
|
|
94
|
+
}
|
|
95
|
+
// 处理内置模块
|
|
96
|
+
const prefix = id.slice(0, 5);
|
|
97
|
+
if (prefix === 'node:') {
|
|
98
|
+
return id;
|
|
99
|
+
}
|
|
100
|
+
// 处理 URL 路径
|
|
101
|
+
if (prefix === 'file:') {
|
|
102
|
+
return fileURLToPath(id);
|
|
103
|
+
}
|
|
104
|
+
// 处理资源路径
|
|
105
|
+
if (prefix === 'data:' || prefix === 'http:' || id.startsWith('https:')) {
|
|
106
|
+
return id;
|
|
107
|
+
}
|
|
108
|
+
},
|
|
109
|
+
/** 解析文件 */
|
|
110
|
+
resolveFile(id, from) {
|
|
111
|
+
// 解析相对路径
|
|
112
|
+
if (isRelative(id)) {
|
|
113
|
+
return this.resolveTarget(resolve(from, id));
|
|
114
|
+
}
|
|
115
|
+
// 解析绝对路径
|
|
116
|
+
if (isAbsolute(id)) {
|
|
117
|
+
return this.resolveTarget(id);
|
|
118
|
+
}
|
|
119
|
+
// 解析别名
|
|
120
|
+
if (this.alias) {
|
|
121
|
+
const alias = resolveAlias(this.alias);
|
|
122
|
+
const resolved = alias.match(resolve => resolve(id, alias => this.resolve(alias, this.cwd)));
|
|
123
|
+
if (resolved !== undefined) {
|
|
124
|
+
return resolved;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
// 解析基准路径
|
|
128
|
+
if (this.baseUrl) {
|
|
129
|
+
const resolved = this.resolveTarget(resolve(this.baseUrl, id));
|
|
130
|
+
if (resolved !== undefined) {
|
|
131
|
+
return resolved;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
// 解析路径映射
|
|
135
|
+
if (this.paths) {
|
|
136
|
+
const baseUrl = this.baseUrl || this.cwd;
|
|
137
|
+
const paths = resolvePaths(this.paths);
|
|
138
|
+
const resolved = paths.match(resolve => resolve(id, alias => this.resolve(alias, baseUrl)));
|
|
139
|
+
if (resolved !== undefined) {
|
|
140
|
+
return resolved;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
},
|
|
144
|
+
/** 解析文件目标 */
|
|
145
|
+
resolveTarget(path) {
|
|
146
|
+
const stats = stat(path);
|
|
147
|
+
// 目标存在时,需要解析目标是目录的情况
|
|
148
|
+
if (stats) {
|
|
149
|
+
return stats.isDirectory() ? this.resolveDir(path) : path;
|
|
150
|
+
}
|
|
151
|
+
else {
|
|
152
|
+
return this.resolveExtensions(path);
|
|
153
|
+
}
|
|
154
|
+
},
|
|
155
|
+
/** 解析目录 */
|
|
156
|
+
resolveDir(path) {
|
|
157
|
+
const pkg = resolvePackageDescription(path);
|
|
158
|
+
// 解析主入口配置
|
|
159
|
+
if (pkg && pkg.dir === path && pkg.config) {
|
|
160
|
+
return this.resolveMainFile(path, pkg.config);
|
|
161
|
+
}
|
|
162
|
+
else {
|
|
163
|
+
return this.resolveIndex(path);
|
|
164
|
+
}
|
|
165
|
+
},
|
|
166
|
+
/** 解析 main 文件 */
|
|
167
|
+
resolveMainFile(path, pkg) {
|
|
168
|
+
for (let i = 0, l = this.mainFields.length; i < l; i++) {
|
|
169
|
+
const main = pkg[this.mainFields[i]];
|
|
170
|
+
// 存在主入口文件配置
|
|
171
|
+
if (main) {
|
|
172
|
+
return this.resolveTarget(resolve(path, main));
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
// 解析 index 文件
|
|
176
|
+
return this.resolveIndex(path);
|
|
177
|
+
},
|
|
178
|
+
/** 解析 index 文件 */
|
|
179
|
+
resolveIndex(path) {
|
|
180
|
+
return this.resolveExtensions(path + sep + 'index');
|
|
181
|
+
},
|
|
182
|
+
/** 解析扩展名 */
|
|
183
|
+
resolveExtensions(path) {
|
|
184
|
+
for (let i = 0, l = this.extensions.length; i < l; i++) {
|
|
185
|
+
const file = path + this.extensions[i];
|
|
186
|
+
const stats = stat(file);
|
|
187
|
+
if (stats && stats.isFile()) {
|
|
188
|
+
return file;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
},
|
|
192
|
+
/** 解析包自身 */
|
|
193
|
+
resolveSelf(name, path, from) {
|
|
194
|
+
const pkg = resolvePackageDescription(from);
|
|
195
|
+
if (!pkg || !pkg.config) {
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
// 获取配置
|
|
199
|
+
const { dir, config } = pkg;
|
|
200
|
+
// 解析模块导出
|
|
201
|
+
if (config.name === name) {
|
|
202
|
+
return this.resolveModuleExports(path, dir, config) || null;
|
|
203
|
+
}
|
|
204
|
+
},
|
|
205
|
+
/** 解析模块 */
|
|
206
|
+
resolveModule(id, from) {
|
|
207
|
+
const [name, subpath] = resolveModuleId(id);
|
|
208
|
+
const path = subpath === './' ? '.' : subpath;
|
|
209
|
+
// 解析当前包本身
|
|
210
|
+
const resolved = this.resolveSelf(name, path, from);
|
|
211
|
+
if (resolved !== undefined || this.module === false) {
|
|
212
|
+
return resolved;
|
|
213
|
+
}
|
|
214
|
+
// 解析模块目录
|
|
215
|
+
const dir = resolveModuleDir(name, from);
|
|
216
|
+
if (!dir) {
|
|
217
|
+
return;
|
|
218
|
+
}
|
|
219
|
+
// 读取包描述文件
|
|
220
|
+
const pkg = resolvePackageDescription(dir);
|
|
221
|
+
if (pkg && pkg.dir === dir && pkg.config) {
|
|
222
|
+
return this.resolveModuleExports(path, dir, pkg.config);
|
|
223
|
+
}
|
|
224
|
+
// 解析目标路径
|
|
225
|
+
if (path === '.') {
|
|
226
|
+
return this.resolveIndex(path);
|
|
227
|
+
}
|
|
228
|
+
else {
|
|
229
|
+
return this.resolveTarget(resolve(dir, path));
|
|
230
|
+
}
|
|
231
|
+
},
|
|
232
|
+
/** 解析模块导入 */
|
|
233
|
+
resolveModuleImports(id, from) {
|
|
234
|
+
const pkg = id.charAt(0) === '#' && resolvePackageDescription(from);
|
|
235
|
+
if (!pkg || !pkg.config || !pkg.config.imports) {
|
|
236
|
+
return;
|
|
237
|
+
}
|
|
238
|
+
// 执行配置
|
|
239
|
+
const imports = pkg.config.imports;
|
|
240
|
+
const dir = pkg.dir;
|
|
241
|
+
// 执行解析
|
|
242
|
+
return resolveImports(id, this.conditions, imports, path => this.resolve(path, dir));
|
|
243
|
+
},
|
|
244
|
+
/** 解析模块导出 */
|
|
245
|
+
resolveModuleExports(id, from, pkg) {
|
|
246
|
+
// 处理导出配置
|
|
247
|
+
if (pkg.exports && this.moduleExports) {
|
|
248
|
+
return resolveExports(id, this.conditions, pkg.exports, path => this.resolveTarget(resolve(from, path))) || null;
|
|
249
|
+
}
|
|
250
|
+
// 解析目标路径
|
|
251
|
+
if (id === '.') {
|
|
252
|
+
return this.resolveMainFile(from, pkg);
|
|
253
|
+
}
|
|
254
|
+
else {
|
|
255
|
+
return this.resolveTarget(resolve(from, id));
|
|
256
|
+
}
|
|
257
|
+
},
|
|
258
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ainc/fs",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.24",
|
|
4
4
|
"description": "Let's do something nice with @ainc/esb!",
|
|
5
5
|
"types": "dist/index.d.ts",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -32,22 +32,21 @@
|
|
|
32
32
|
"publishConfig": {
|
|
33
33
|
"access": "public"
|
|
34
34
|
},
|
|
35
|
-
"dependencies": {
|
|
36
|
-
"typescript": "^5.9.3"
|
|
37
|
-
},
|
|
38
35
|
"devDependencies": {
|
|
39
36
|
"@jest/globals": "30.2.0",
|
|
40
37
|
"@swc/core": "^1.15.8",
|
|
41
38
|
"@swc/jest": "^0.2.39",
|
|
42
39
|
"@types/node": "^25.0.3",
|
|
43
|
-
"jest": "^30.2.0"
|
|
40
|
+
"jest": "^30.2.0",
|
|
41
|
+
"typescript": "^5.9.3"
|
|
44
42
|
},
|
|
45
43
|
"scripts": {
|
|
46
|
-
"build": "pnpm clean && pnpm compile && pnpm esm",
|
|
44
|
+
"build": "pnpm clean && pnpm compile && pnpm esm && pnpm exports",
|
|
47
45
|
"watch": "pnpm compile --watch",
|
|
48
46
|
"esm": "pnpm compile -m es2022 --outDir ./esm --declaration false && node ../../scripts/esm.js",
|
|
49
47
|
"compile": "tsc -p ./tsconfig.build.json",
|
|
50
48
|
"download": "node ./bin/download.mjs ./build/node-service.exe https://unpkg.com/@ainc/node-service-win32/core/node-service.exe",
|
|
49
|
+
"exports": "node ../../scripts/exports.js",
|
|
51
50
|
"test": "jest",
|
|
52
51
|
"clean": "rimraf ./dist ./esm"
|
|
53
52
|
}
|