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