@awsless/ts-file-cache 0.0.17 → 0.0.18
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/index.cjs +360 -461
- package/dist/index.d.cts +17 -14
- package/dist/index.d.ts +17 -14
- package/dist/index.js +352 -429
- package/package.json +9 -9
package/dist/index.js
CHANGED
|
@@ -1,458 +1,381 @@
|
|
|
1
|
-
|
|
2
|
-
import { readdir } from "fs/promises";
|
|
3
|
-
import { extname
|
|
4
|
-
|
|
5
|
-
// src/hash.ts
|
|
1
|
+
import { builtinModules } from "node:module";
|
|
2
|
+
import { lstat, readFile, readdir, stat } from "fs/promises";
|
|
3
|
+
import { basename, dirname, extname, isAbsolute, join, normalize, relative, resolve, sep } from "path";
|
|
6
4
|
import { createHash } from "crypto";
|
|
7
|
-
import { readFile } from "fs/promises";
|
|
8
|
-
import { builtinModules } from "module";
|
|
9
|
-
import { relative, sep } from "path";
|
|
10
|
-
|
|
11
|
-
// src/import.ts
|
|
12
5
|
import { parse } from "@swc/core";
|
|
13
|
-
import { dirname, resolve } from "path";
|
|
14
6
|
import { simple } from "swc-walk";
|
|
15
7
|
import { BaseVisitor } from "swc-walk/baseVisitor";
|
|
8
|
+
import { parse as parse$1 } from "yaml";
|
|
9
|
+
//#region src/import.ts
|
|
16
10
|
var PatchedBaseVisitor = class extends BaseVisitor {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
}
|
|
21
|
-
}
|
|
11
|
+
FunctionBody(node, state, callback) {
|
|
12
|
+
for (const statement of node.stmts) callback(statement, state);
|
|
13
|
+
}
|
|
22
14
|
};
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
15
|
+
const baseVisitor = new PatchedBaseVisitor();
|
|
16
|
+
const parseOptions = (file) => {
|
|
17
|
+
if (file.endsWith(".tsx")) return {
|
|
18
|
+
syntax: "typescript",
|
|
19
|
+
tsx: true,
|
|
20
|
+
decorators: true
|
|
21
|
+
};
|
|
22
|
+
if (file.endsWith(".ts") || file.endsWith(".mts") || file.endsWith(".cts")) return {
|
|
23
|
+
syntax: "typescript",
|
|
24
|
+
decorators: true
|
|
25
|
+
};
|
|
26
|
+
return {
|
|
27
|
+
syntax: "ecmascript",
|
|
28
|
+
jsx: true,
|
|
29
|
+
decorators: true
|
|
30
|
+
};
|
|
32
31
|
};
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
return [...importing].map((importee) => {
|
|
72
|
-
if (importee.startsWith(".")) {
|
|
73
|
-
return resolve(dirname(file), importee);
|
|
74
|
-
}
|
|
75
|
-
const parts = importee.split("/");
|
|
76
|
-
if (parts.length > 2) {
|
|
77
|
-
return parts.slice(0, 2).join("/");
|
|
78
|
-
}
|
|
79
|
-
return importee;
|
|
80
|
-
});
|
|
32
|
+
const findImports = async (file, code) => {
|
|
33
|
+
let ast;
|
|
34
|
+
try {
|
|
35
|
+
ast = await parse(code, parseOptions(file));
|
|
36
|
+
} catch (error) {
|
|
37
|
+
throw new Error(`Failed to parse: ${file}`, { cause: error });
|
|
38
|
+
}
|
|
39
|
+
const importing = /* @__PURE__ */ new Set();
|
|
40
|
+
try {
|
|
41
|
+
simple(ast, {
|
|
42
|
+
ImportDeclaration(node) {
|
|
43
|
+
importing.add(node.source.value);
|
|
44
|
+
},
|
|
45
|
+
ExportAllDeclaration(node) {
|
|
46
|
+
importing.add(node.source.value);
|
|
47
|
+
},
|
|
48
|
+
ExportNamedDeclaration(node) {
|
|
49
|
+
if (node.source) importing.add(node.source.value);
|
|
50
|
+
},
|
|
51
|
+
CallExpression(node) {
|
|
52
|
+
if (node.callee.type === "Import") {
|
|
53
|
+
const first = node.arguments.at(0);
|
|
54
|
+
if (first && first.expression.type === "StringLiteral") importing.add(first.expression.value);
|
|
55
|
+
}
|
|
56
|
+
},
|
|
57
|
+
TsImportEqualsDeclaration(node) {
|
|
58
|
+
if (node.moduleRef.type === "TsExternalModuleReference") importing.add(node.moduleRef.expression.value);
|
|
59
|
+
}
|
|
60
|
+
}, baseVisitor);
|
|
61
|
+
} catch (error) {
|
|
62
|
+
throw new Error(`Failed to walk the AST of: ${file}`, { cause: error });
|
|
63
|
+
}
|
|
64
|
+
return [...importing].map((importee) => {
|
|
65
|
+
if (importee.startsWith(".")) return resolve(dirname(file), importee);
|
|
66
|
+
const parts = importee.split("/");
|
|
67
|
+
if (parts.length > 2) return parts.slice(0, 2).join("/");
|
|
68
|
+
return importee;
|
|
69
|
+
});
|
|
81
70
|
};
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
return file;
|
|
92
|
-
}
|
|
93
|
-
} catch (_) {
|
|
94
|
-
continue;
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
throw new Error(`No such file: ${files.join(", ")}`);
|
|
71
|
+
//#endregion
|
|
72
|
+
//#region src/module.ts
|
|
73
|
+
const findFile = async (files) => {
|
|
74
|
+
for (const file of files) try {
|
|
75
|
+
if ((await stat(file)).isFile()) return file;
|
|
76
|
+
} catch (_) {
|
|
77
|
+
continue;
|
|
78
|
+
}
|
|
79
|
+
throw new Error(`No such file: ${files.join(", ")}`);
|
|
98
80
|
};
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
81
|
+
const alternateExtensions = {
|
|
82
|
+
".js": [".ts", ".tsx"],
|
|
83
|
+
".mjs": [".mts"],
|
|
84
|
+
".cjs": [".cts"],
|
|
85
|
+
".jsx": [".tsx"]
|
|
104
86
|
};
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
file,
|
|
120
|
-
...allowedExtensions.map((ext) => `${file}.${ext}`),
|
|
121
|
-
...allowedExtensions.map((ext) => join(file, `index.${ext}`))
|
|
122
|
-
]);
|
|
123
|
-
}
|
|
124
|
-
return file;
|
|
87
|
+
const resolveModuleImportFile = (file, allowedExtensions) => {
|
|
88
|
+
const extension = extname(file);
|
|
89
|
+
const alternates = alternateExtensions[extension];
|
|
90
|
+
if (alternates) {
|
|
91
|
+
const candidates = [file];
|
|
92
|
+
for (const alternate of alternates) if (allowedExtensions.includes(alternate.substring(1))) candidates.push(file.substring(0, file.length - extension.length) + alternate);
|
|
93
|
+
return findFile(candidates);
|
|
94
|
+
}
|
|
95
|
+
if (!basename(file).includes(".")) return findFile([
|
|
96
|
+
file,
|
|
97
|
+
...allowedExtensions.map((ext) => `${file}.${ext}`),
|
|
98
|
+
...allowedExtensions.map((ext) => join(file, `index.${ext}`))
|
|
99
|
+
]);
|
|
100
|
+
return file;
|
|
125
101
|
};
|
|
126
|
-
|
|
127
|
-
|
|
102
|
+
const isLocalCodeFile = (file) => {
|
|
103
|
+
return file.startsWith("/") || file.startsWith(".");
|
|
128
104
|
};
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
}
|
|
133
|
-
return join(process.cwd(), file);
|
|
105
|
+
const toAbsolute = (file) => {
|
|
106
|
+
if (isAbsolute(file)) return file;
|
|
107
|
+
return join(process.cwd(), file);
|
|
134
108
|
};
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
const localPackage = workspace.packages[dependency.link];
|
|
171
|
-
if (!localPackage) {
|
|
172
|
-
throw new Error(`Can't find the local workspace package for: ${file}`);
|
|
173
|
-
}
|
|
174
|
-
if (!localPackage.main) {
|
|
175
|
-
throw new Error(`Workspace package doesn't have a main entry: ${file}`);
|
|
176
|
-
}
|
|
177
|
-
await generateRecursiveFileHashes(
|
|
178
|
-
workspace,
|
|
179
|
-
localPackage.main,
|
|
180
|
-
localPackage.main,
|
|
181
|
-
allowedExtensions,
|
|
182
|
-
hashes
|
|
183
|
-
);
|
|
184
|
-
}
|
|
185
|
-
return;
|
|
186
|
-
}
|
|
187
|
-
if (builtinModules.includes(module.replace(/^node\:/, ""))) {
|
|
188
|
-
return;
|
|
189
|
-
}
|
|
190
|
-
throw new Error(`Can't find the dependency version for: ${file} inside the source: ${sourceFile}`);
|
|
109
|
+
//#endregion
|
|
110
|
+
//#region src/hash.ts
|
|
111
|
+
const generateRecursiveFileHashes = async (workspace, file, sourceFile, allowedExtensions, hashes) => {
|
|
112
|
+
if (isLocalCodeFile(file)) {
|
|
113
|
+
try {
|
|
114
|
+
file = await resolveModuleImportFile(file, allowedExtensions);
|
|
115
|
+
} catch (error) {
|
|
116
|
+
throw new Error(`Can't find imported file: "${file}" inside the source: "${sourceFile}"`);
|
|
117
|
+
}
|
|
118
|
+
const relFile = relative(workspace.cwd, file).split(sep).join("/");
|
|
119
|
+
if (hashes.has(relFile)) return;
|
|
120
|
+
const code = await readFile(file, "utf8");
|
|
121
|
+
const ext = file.split(".").pop();
|
|
122
|
+
const hash = createHash("sha1").update(code).digest();
|
|
123
|
+
hashes.set(relFile, hash);
|
|
124
|
+
if (!ext || !allowedExtensions.includes(ext)) return;
|
|
125
|
+
const deps = await findImports(file, code);
|
|
126
|
+
for (const dep of deps) await generateRecursiveFileHashes(workspace, dep, file, allowedExtensions, hashes);
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
const module = getPackageName(file);
|
|
130
|
+
if (hashes.has(module)) return;
|
|
131
|
+
const dependency = findDependency(workspace, module, sourceFile);
|
|
132
|
+
if (dependency) {
|
|
133
|
+
if (dependency.type === "package") hashes.set(module, Buffer.from(`${module}:${dependency.version}`, "utf8"));
|
|
134
|
+
else {
|
|
135
|
+
const localPackage = workspace.packages[dependency.link];
|
|
136
|
+
if (!localPackage) throw new Error(`Can't find the local workspace package for: ${file}`);
|
|
137
|
+
if (!localPackage.main) throw new Error(`Workspace package doesn't have a main entry: ${file}`);
|
|
138
|
+
await generateRecursiveFileHashes(workspace, localPackage.main, localPackage.main, allowedExtensions, hashes);
|
|
139
|
+
}
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
if (builtinModules.includes(module.replace(/^node\:/, ""))) return;
|
|
143
|
+
throw new Error(`Can't find the dependency version for: ${file} inside the source: ${sourceFile}`);
|
|
191
144
|
};
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
145
|
+
const mergeHashes = (hashes) => {
|
|
146
|
+
const names = Array.from(hashes.keys()).sort();
|
|
147
|
+
const merged = createHash("sha1");
|
|
148
|
+
for (const name of names) {
|
|
149
|
+
merged.update(name);
|
|
150
|
+
merged.update(hashes.get(name));
|
|
151
|
+
}
|
|
152
|
+
return merged.digest("hex");
|
|
200
153
|
};
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
} else if (parts.length >= 1) {
|
|
208
|
-
return parts[0];
|
|
209
|
-
}
|
|
210
|
-
throw new Error(`Malformed importee: ${importee}`);
|
|
154
|
+
const getPackageName = (importee) => {
|
|
155
|
+
const parts = importee.split("/");
|
|
156
|
+
if (importee.startsWith("@")) {
|
|
157
|
+
if (parts.length >= 2) return `${parts[0]}/${parts[1]}`;
|
|
158
|
+
} else if (parts.length >= 1) return parts[0];
|
|
159
|
+
throw new Error(`Malformed importee: ${importee}`);
|
|
211
160
|
};
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
}
|
|
217
|
-
return pkg.dependencies[module];
|
|
161
|
+
const findDependency = (workspace, module, source) => {
|
|
162
|
+
const pkg = Object.values(workspace.packages).filter((p) => source === p.path || source.startsWith(p.path + sep)).sort((a, b) => b.path.split(sep).length - a.path.split(sep).length).find((p) => p.dependencies[module]);
|
|
163
|
+
if (!pkg) return;
|
|
164
|
+
return pkg.dependencies[module];
|
|
218
165
|
};
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
166
|
+
//#endregion
|
|
167
|
+
//#region src/package-manager/bun.ts
|
|
168
|
+
const bun = async (cwd, lockFile) => {
|
|
169
|
+
const data = parseJsonc(lockFile);
|
|
170
|
+
const resolvedVersions = {};
|
|
171
|
+
for (const [key, entry] of Object.entries(data.packages ?? {})) {
|
|
172
|
+
const resolution = entry[0];
|
|
173
|
+
const at = resolution.lastIndexOf("@");
|
|
174
|
+
const version = resolution.substring(at + 1);
|
|
175
|
+
if (!version.startsWith("workspace:")) resolvedVersions[key] = version;
|
|
176
|
+
}
|
|
177
|
+
const workspacePaths = {};
|
|
178
|
+
for (const [path, workspace] of Object.entries(data.workspaces)) if (workspace.name) workspacePaths[workspace.name] = path;
|
|
179
|
+
const importers = {};
|
|
180
|
+
for (const [path, workspace] of Object.entries(data.workspaces)) {
|
|
181
|
+
const deps = {
|
|
182
|
+
...workspace.devDependencies,
|
|
183
|
+
...workspace.optionalDependencies,
|
|
184
|
+
...workspace.dependencies
|
|
185
|
+
};
|
|
186
|
+
const dependencies = {};
|
|
187
|
+
for (const [name, specifier] of Object.entries(deps)) {
|
|
188
|
+
if (specifier.startsWith("workspace:")) {
|
|
189
|
+
const target = specifier.substring(10);
|
|
190
|
+
const workspacePath = workspacePaths[name];
|
|
191
|
+
if (workspacePath !== void 0) dependencies[name] = {
|
|
192
|
+
type: "workspace",
|
|
193
|
+
link: join(cwd, workspacePath)
|
|
194
|
+
};
|
|
195
|
+
else dependencies[name] = {
|
|
196
|
+
type: "workspace",
|
|
197
|
+
link: join(cwd, path, target)
|
|
198
|
+
};
|
|
199
|
+
continue;
|
|
200
|
+
}
|
|
201
|
+
const version = resolvedVersions[name] ?? resolvedVersions[`${workspace.name}/${name}`];
|
|
202
|
+
if (version) dependencies[name] = {
|
|
203
|
+
type: "package",
|
|
204
|
+
version
|
|
205
|
+
};
|
|
206
|
+
else dependencies[name] = {
|
|
207
|
+
type: "package",
|
|
208
|
+
version: specifier
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
importers[path] = dependencies;
|
|
212
|
+
}
|
|
213
|
+
return {
|
|
214
|
+
cwd,
|
|
215
|
+
packages: await buildPackages(cwd, importers)
|
|
216
|
+
};
|
|
246
217
|
};
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
const resolution = entry[0];
|
|
254
|
-
const at = resolution.lastIndexOf("@");
|
|
255
|
-
const version = resolution.substring(at + 1);
|
|
256
|
-
if (!version.startsWith("workspace:")) {
|
|
257
|
-
resolvedVersions[key] = version;
|
|
258
|
-
}
|
|
259
|
-
}
|
|
260
|
-
const workspacePaths = {};
|
|
261
|
-
for (const [path, workspace] of Object.entries(data.workspaces)) {
|
|
262
|
-
if (workspace.name) {
|
|
263
|
-
workspacePaths[workspace.name] = path;
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
const importers = {};
|
|
267
|
-
for (const [path, workspace] of Object.entries(data.workspaces)) {
|
|
268
|
-
const deps = { ...workspace.devDependencies, ...workspace.optionalDependencies, ...workspace.dependencies };
|
|
269
|
-
const dependencies = {};
|
|
270
|
-
for (const [name, specifier] of Object.entries(deps)) {
|
|
271
|
-
if (specifier.startsWith("workspace:")) {
|
|
272
|
-
const target = specifier.substring(10);
|
|
273
|
-
const workspacePath = workspacePaths[name];
|
|
274
|
-
if (workspacePath !== void 0) {
|
|
275
|
-
dependencies[name] = {
|
|
276
|
-
type: "workspace",
|
|
277
|
-
link: join3(cwd, workspacePath)
|
|
278
|
-
};
|
|
279
|
-
} else {
|
|
280
|
-
dependencies[name] = {
|
|
281
|
-
type: "workspace",
|
|
282
|
-
link: join3(cwd, path, target)
|
|
283
|
-
};
|
|
284
|
-
}
|
|
285
|
-
continue;
|
|
286
|
-
}
|
|
287
|
-
const version = resolvedVersions[name] ?? resolvedVersions[`${workspace.name}/${name}`];
|
|
288
|
-
if (version) {
|
|
289
|
-
dependencies[name] = {
|
|
290
|
-
type: "package",
|
|
291
|
-
version
|
|
292
|
-
};
|
|
293
|
-
} else {
|
|
294
|
-
dependencies[name] = {
|
|
295
|
-
type: "package",
|
|
296
|
-
version: specifier
|
|
297
|
-
};
|
|
298
|
-
}
|
|
299
|
-
}
|
|
300
|
-
importers[path] = dependencies;
|
|
301
|
-
}
|
|
302
|
-
const packages = await buildPackages(cwd, importers);
|
|
303
|
-
return {
|
|
304
|
-
cwd,
|
|
305
|
-
packages
|
|
306
|
-
};
|
|
218
|
+
const parseJsonc = (text) => {
|
|
219
|
+
try {
|
|
220
|
+
return JSON.parse(text);
|
|
221
|
+
} catch (_) {
|
|
222
|
+
return JSON.parse(stripJsoncSyntax(text));
|
|
223
|
+
}
|
|
307
224
|
};
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
225
|
+
const stripJsoncSyntax = (text) => {
|
|
226
|
+
let result = "";
|
|
227
|
+
let index = 0;
|
|
228
|
+
while (index < text.length) {
|
|
229
|
+
const char = text[index];
|
|
230
|
+
if (char === "\"") {
|
|
231
|
+
result += char;
|
|
232
|
+
index++;
|
|
233
|
+
while (index < text.length) {
|
|
234
|
+
const stringChar = text[index];
|
|
235
|
+
result += stringChar;
|
|
236
|
+
index++;
|
|
237
|
+
if (stringChar === "\\") {
|
|
238
|
+
result += text[index] ?? "";
|
|
239
|
+
index++;
|
|
240
|
+
continue;
|
|
241
|
+
}
|
|
242
|
+
if (stringChar === "\"") break;
|
|
243
|
+
}
|
|
244
|
+
continue;
|
|
245
|
+
}
|
|
246
|
+
if (char === "/" && text[index + 1] === "/") {
|
|
247
|
+
while (index < text.length && text[index] !== "\n") index++;
|
|
248
|
+
continue;
|
|
249
|
+
}
|
|
250
|
+
if (char === "/" && text[index + 1] === "*") {
|
|
251
|
+
index += 2;
|
|
252
|
+
while (index < text.length && !(text[index] === "*" && text[index + 1] === "/")) index++;
|
|
253
|
+
index += 2;
|
|
254
|
+
continue;
|
|
255
|
+
}
|
|
256
|
+
if (char === ",") {
|
|
257
|
+
let ahead = index + 1;
|
|
258
|
+
while (ahead < text.length && /\s/.test(text[ahead])) ahead++;
|
|
259
|
+
if (text[ahead] === "}" || text[ahead] === "]") {
|
|
260
|
+
index++;
|
|
261
|
+
continue;
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
result += char;
|
|
265
|
+
index++;
|
|
266
|
+
}
|
|
267
|
+
return result;
|
|
314
268
|
};
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
}
|
|
342
|
-
continue;
|
|
343
|
-
}
|
|
344
|
-
if (char === "/" && text[index + 1] === "*") {
|
|
345
|
-
index += 2;
|
|
346
|
-
while (index < text.length && !(text[index] === "*" && text[index + 1] === "/")) {
|
|
347
|
-
index++;
|
|
348
|
-
}
|
|
349
|
-
index += 2;
|
|
350
|
-
continue;
|
|
351
|
-
}
|
|
352
|
-
if (char === ",") {
|
|
353
|
-
let ahead = index + 1;
|
|
354
|
-
while (ahead < text.length && /\s/.test(text[ahead])) {
|
|
355
|
-
ahead++;
|
|
356
|
-
}
|
|
357
|
-
if (text[ahead] === "}" || text[ahead] === "]") {
|
|
358
|
-
index++;
|
|
359
|
-
continue;
|
|
360
|
-
}
|
|
361
|
-
}
|
|
362
|
-
result += char;
|
|
363
|
-
index++;
|
|
364
|
-
}
|
|
365
|
-
return result;
|
|
269
|
+
//#endregion
|
|
270
|
+
//#region src/package-manager/pnpm.ts
|
|
271
|
+
const pnpm = async (cwd, lockFile) => {
|
|
272
|
+
const data = parse$1(lockFile);
|
|
273
|
+
const importers = {};
|
|
274
|
+
for (const [path, importee] of Object.entries(data.importers)) {
|
|
275
|
+
const deps = {
|
|
276
|
+
...importee.devDependencies,
|
|
277
|
+
...importee.optionalDependencies,
|
|
278
|
+
...importee.dependencies
|
|
279
|
+
};
|
|
280
|
+
const dependencies = {};
|
|
281
|
+
for (const [name, entry] of Object.entries(deps)) if (entry.version.startsWith("link:")) dependencies[name] = {
|
|
282
|
+
type: "workspace",
|
|
283
|
+
link: join(cwd, path, entry.version.substring(5))
|
|
284
|
+
};
|
|
285
|
+
else dependencies[name] = {
|
|
286
|
+
type: "package",
|
|
287
|
+
version: entry.version
|
|
288
|
+
};
|
|
289
|
+
importers[path] = dependencies;
|
|
290
|
+
}
|
|
291
|
+
return {
|
|
292
|
+
cwd,
|
|
293
|
+
packages: await buildPackages(cwd, importers)
|
|
294
|
+
};
|
|
366
295
|
};
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
const data = parse2(lockFile);
|
|
373
|
-
const importers = {};
|
|
374
|
-
for (const [path, importee] of Object.entries(data.importers)) {
|
|
375
|
-
const deps = { ...importee.devDependencies, ...importee.optionalDependencies, ...importee.dependencies };
|
|
376
|
-
const dependencies = {};
|
|
377
|
-
for (const [name, entry] of Object.entries(deps)) {
|
|
378
|
-
if (entry.version.startsWith("link:")) {
|
|
379
|
-
dependencies[name] = {
|
|
380
|
-
type: "workspace",
|
|
381
|
-
link: join4(cwd, path, entry.version.substring(5))
|
|
382
|
-
};
|
|
383
|
-
} else {
|
|
384
|
-
dependencies[name] = {
|
|
385
|
-
type: "package",
|
|
386
|
-
version: entry.version
|
|
387
|
-
};
|
|
388
|
-
}
|
|
389
|
-
}
|
|
390
|
-
importers[path] = dependencies;
|
|
391
|
-
}
|
|
392
|
-
const packages = await buildPackages(cwd, importers);
|
|
393
|
-
return {
|
|
394
|
-
cwd,
|
|
395
|
-
packages
|
|
396
|
-
};
|
|
296
|
+
//#endregion
|
|
297
|
+
//#region src/package-manager/util.ts
|
|
298
|
+
const parsers = {
|
|
299
|
+
"pnpm-lock.yaml": pnpm,
|
|
300
|
+
"bun.lock": bun
|
|
397
301
|
};
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
302
|
+
const loadPackageManager = async (search, level = 5) => {
|
|
303
|
+
if (!level) throw new TypeError("No pnpm or bun lock file found");
|
|
304
|
+
for (const [lockFileName, parser] of Object.entries(parsers)) {
|
|
305
|
+
const file = join(search, lockFileName);
|
|
306
|
+
if (await fileExist(file)) {
|
|
307
|
+
const content = await readFile(file, "utf8");
|
|
308
|
+
return {
|
|
309
|
+
...await parser(search, content),
|
|
310
|
+
lockfileHash: createHash("sha1").update(content).digest("hex")
|
|
311
|
+
};
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
return loadPackageManager(normalize(join(search, "..")), level - 1);
|
|
403
315
|
};
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
const file = join5(search, lockFileName);
|
|
410
|
-
if (await fileExist(file)) {
|
|
411
|
-
return parser(search, await readFile3(file, "utf8"));
|
|
412
|
-
}
|
|
413
|
-
}
|
|
414
|
-
return loadPackageManager(normalize(join5(search, "..")), level - 1);
|
|
316
|
+
const fileExist = async (file) => {
|
|
317
|
+
try {
|
|
318
|
+
if ((await lstat(file)).isFile()) return true;
|
|
319
|
+
} catch (error) {}
|
|
320
|
+
return false;
|
|
415
321
|
};
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
322
|
+
const buildPackages = async (cwd, importers) => {
|
|
323
|
+
const packages = {};
|
|
324
|
+
await Promise.all(Object.entries(importers).map(async ([path, dependencies]) => {
|
|
325
|
+
const packageJson = await readFile(join(cwd, path, "package.json"), "utf8");
|
|
326
|
+
const packageData = JSON.parse(packageJson);
|
|
327
|
+
const entry = packageData.module ?? packageData.main;
|
|
328
|
+
packages[join(cwd, path)] = {
|
|
329
|
+
name: packageData.name,
|
|
330
|
+
path: join(cwd, path),
|
|
331
|
+
main: entry ? join(cwd, path, entry) : void 0,
|
|
332
|
+
dependencies
|
|
333
|
+
};
|
|
334
|
+
}));
|
|
335
|
+
return packages;
|
|
425
336
|
};
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
337
|
+
//#endregion
|
|
338
|
+
//#region src/index.ts
|
|
339
|
+
const loadWorkspace = async (search) => {
|
|
340
|
+
return loadPackageManager(toAbsolute(search));
|
|
430
341
|
};
|
|
431
|
-
|
|
432
|
-
|
|
342
|
+
const defaultOptions = { extensions: [
|
|
343
|
+
"js",
|
|
344
|
+
"mjs",
|
|
345
|
+
"jsx",
|
|
346
|
+
"ts",
|
|
347
|
+
"mts",
|
|
348
|
+
"tsx"
|
|
349
|
+
] };
|
|
350
|
+
const seedHashes = (workspace) => {
|
|
351
|
+
return /* @__PURE__ */ new Map([["#lockfile", Buffer.from(workspace.lockfileHash, "hex")]]);
|
|
433
352
|
};
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
353
|
+
const generateFileHash = async (workspace, file, opts = {}) => {
|
|
354
|
+
const options = {
|
|
355
|
+
...defaultOptions,
|
|
356
|
+
...opts
|
|
357
|
+
};
|
|
358
|
+
const hashes = seedHashes(workspace);
|
|
359
|
+
const absoluteFile = toAbsolute(file);
|
|
360
|
+
await generateRecursiveFileHashes(workspace, absoluteFile, absoluteFile, options.extensions, hashes);
|
|
361
|
+
return mergeHashes(hashes);
|
|
440
362
|
};
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
loadWorkspace
|
|
363
|
+
const generateFolderHash = async (workspace, folder, opts = {}) => {
|
|
364
|
+
const options = {
|
|
365
|
+
...defaultOptions,
|
|
366
|
+
...opts
|
|
367
|
+
};
|
|
368
|
+
const hashes = seedHashes(workspace);
|
|
369
|
+
const absoluteFolder = toAbsolute(folder);
|
|
370
|
+
const files = await readdir(absoluteFolder, {
|
|
371
|
+
recursive: true,
|
|
372
|
+
withFileTypes: true
|
|
373
|
+
});
|
|
374
|
+
for (const file of files) if (file.isFile() && options.extensions.includes(extname(file.name).substring(1))) {
|
|
375
|
+
const f = resolve(file.parentPath, file.name);
|
|
376
|
+
await generateRecursiveFileHashes(workspace, f, f, options.extensions, hashes);
|
|
377
|
+
}
|
|
378
|
+
return mergeHashes(hashes);
|
|
458
379
|
};
|
|
380
|
+
//#endregion
|
|
381
|
+
export { generateFileHash, generateFolderHash, loadWorkspace };
|