@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 CHANGED
@@ -1,485 +1,384 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
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
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
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
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
-
20
- // src/index.ts
21
- var index_exports = {};
22
- __export(index_exports, {
23
- generateFileHash: () => generateFileHash,
24
- generateFolderHash: () => generateFolderHash,
25
- loadWorkspace: () => loadWorkspace
26
- });
27
- module.exports = __toCommonJS(index_exports);
28
- var import_promises5 = require("fs/promises");
29
- var import_path8 = require("path");
30
-
31
- // src/hash.ts
32
- var import_crypto = require("crypto");
33
- var import_promises2 = require("fs/promises");
34
- var import_node_module = require("module");
35
- var import_path3 = require("path");
36
-
37
- // src/import.ts
38
- var import_core = require("@swc/core");
39
- var import_path = require("path");
40
- var import_swc_walk = require("swc-walk");
41
- var import_baseVisitor = require("swc-walk/baseVisitor");
42
- var PatchedBaseVisitor = class extends import_baseVisitor.BaseVisitor {
43
- FunctionBody(node, state, callback) {
44
- for (const statement of node.stmts) {
45
- callback(statement, state);
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
- var baseVisitor = new PatchedBaseVisitor();
50
- var parseOptions = (file) => {
51
- if (file.endsWith(".tsx")) {
52
- return { syntax: "typescript", tsx: true, decorators: true };
53
- }
54
- if (file.endsWith(".ts") || file.endsWith(".mts") || file.endsWith(".cts")) {
55
- return { syntax: "typescript", decorators: true };
56
- }
57
- return { syntax: "ecmascript", jsx: true, decorators: true };
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
- var findImports = async (file, code) => {
60
- let ast;
61
- try {
62
- ast = await (0, import_core.parse)(code, parseOptions(file));
63
- } catch (error) {
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
- // src/module.ts
110
- var import_promises = require("fs/promises");
111
- var import_path2 = require("path");
112
- var findFile = async (files) => {
113
- for (const file of files) {
114
- try {
115
- const s = await (0, import_promises.stat)(file);
116
- if (s.isFile()) {
117
- return file;
118
- }
119
- } catch (_) {
120
- continue;
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
- var alternateExtensions = {
126
- ".js": [".ts", ".tsx"],
127
- ".mjs": [".mts"],
128
- ".cjs": [".cts"],
129
- ".jsx": [".tsx"]
103
+ const isLocalCodeFile = (file) => {
104
+ return file.startsWith("/") || file.startsWith(".");
130
105
  };
131
- var resolveModuleImportFile = (file, allowedExtensions) => {
132
- const extension = (0, import_path2.extname)(file);
133
- const alternates = alternateExtensions[extension];
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
- var isLocalCodeFile = (file) => {
153
- return file.startsWith("/") || file.startsWith(".");
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}"`);
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
- var toAbsolute = (file) => {
156
- if ((0, import_path2.isAbsolute)(file)) {
157
- return file;
158
- }
159
- return (0, import_path2.join)(process.cwd(), file);
146
+ const mergeHashes = (hashes) => {
147
+ const names = Array.from(hashes.keys()).sort();
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
- // src/hash.ts
163
- var generateRecursiveFileHashes = async (workspace, file, sourceFile, allowedExtensions, hashes) => {
164
- if (isLocalCodeFile(file)) {
165
- try {
166
- file = await resolveModuleImportFile(file, allowedExtensions);
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
- var mergeHashes = (hashes) => {
219
- const names = Array.from(hashes.keys()).sort();
220
- const merged = (0, import_crypto.createHash)("sha1");
221
- for (const name of names) {
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)).sort((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
- var getPackageName = (importee) => {
228
- const parts = importee.split("/");
229
- if (importee.startsWith("@")) {
230
- if (parts.length >= 2) {
231
- return `${parts[0]}/${parts[1]}`;
232
- }
233
- } else if (parts.length >= 1) {
234
- return parts[0];
235
- }
236
- throw new Error(`Malformed importee: ${importee}`);
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
- var findDependency = (workspace, module2, source) => {
239
- const pkg = Object.values(workspace.packages).filter((p) => source === p.path || source.startsWith(p.path + import_path3.sep)).sort((a, b) => b.path.split(import_path3.sep).length - a.path.split(import_path3.sep).length).find((p) => p.dependencies[module2]);
240
- if (!pkg) {
241
- return;
242
- }
243
- return pkg.dependencies[module2];
219
+ const parseJsonc = (text) => {
220
+ try {
221
+ return JSON.parse(text);
222
+ } catch (_) {
223
+ return JSON.parse(stripJsoncSyntax(text));
224
+ }
244
225
  };
245
-
246
- // src/package-manager/index.ts
247
- var import_promises4 = require("fs/promises");
248
- var import_path7 = require("path");
249
-
250
- // src/package-manager/bun.ts
251
- var import_path5 = require("path");
252
-
253
- // src/package-manager/importer.ts
254
- var import_promises3 = require("fs/promises");
255
- var import_path4 = require("path");
256
- var buildPackages = async (cwd, importers) => {
257
- const packages = {};
258
- await Promise.all(
259
- Object.entries(importers).map(async ([path, dependencies]) => {
260
- const packageJson = await (0, import_promises3.readFile)((0, import_path4.join)(cwd, path, "package.json"), "utf8");
261
- const packageData = JSON.parse(packageJson);
262
- const entry = packageData.module ?? packageData.main;
263
- packages[(0, import_path4.join)(cwd, path)] = {
264
- name: packageData.name,
265
- path: (0, import_path4.join)(cwd, path),
266
- main: entry ? (0, import_path4.join)(cwd, path, entry) : void 0,
267
- dependencies
268
- };
269
- })
270
- );
271
- return packages;
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
- // src/package-manager/bun.ts
275
- var bun = async (cwd, lockFile) => {
276
- const data = parseJsonc(lockFile);
277
- const resolvedVersions = {};
278
- for (const [key, entry] of Object.entries(data.packages ?? {})) {
279
- const resolution = entry[0];
280
- const at = resolution.lastIndexOf("@");
281
- const version = resolution.substring(at + 1);
282
- if (!version.startsWith("workspace:")) {
283
- resolvedVersions[key] = version;
284
- }
285
- }
286
- const workspacePaths = {};
287
- for (const [path, workspace] of Object.entries(data.workspaces)) {
288
- if (workspace.name) {
289
- workspacePaths[workspace.name] = path;
290
- }
291
- }
292
- const importers = {};
293
- for (const [path, workspace] of Object.entries(data.workspaces)) {
294
- const deps = { ...workspace.devDependencies, ...workspace.optionalDependencies, ...workspace.dependencies };
295
- const dependencies = {};
296
- for (const [name, specifier] of Object.entries(deps)) {
297
- if (specifier.startsWith("workspace:")) {
298
- const target = specifier.substring(10);
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
- var parseJsonc = (text) => {
335
- try {
336
- return JSON.parse(text);
337
- } catch (_) {
338
- return JSON.parse(stripJsoncSyntax(text));
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
- var stripJsoncSyntax = (text) => {
342
- let result = "";
343
- let index = 0;
344
- while (index < text.length) {
345
- const char = text[index];
346
- if (char === '"') {
347
- result += char;
348
- index++;
349
- while (index < text.length) {
350
- const stringChar = text[index];
351
- result += stringChar;
352
- index++;
353
- if (stringChar === "\\") {
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
- // src/package-manager/pnpm.ts
395
- var import_path6 = require("path");
396
- var import_yaml = require("yaml");
397
- var pnpm = async (cwd, lockFile) => {
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 (error) {}
321
+ return false;
423
322
  };
424
-
425
- // src/package-manager/index.ts
426
- var parsers = {
427
- "pnpm-lock.yaml": pnpm,
428
- "bun.lock": bun
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
- var loadPackageManager = async (search, level = 5) => {
431
- if (!level) {
432
- throw new TypeError("No pnpm or bun lock file found");
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
- var fileExist = async (file) => {
443
- try {
444
- const stat2 = await (0, import_promises4.lstat)(file);
445
- if (stat2.isFile()) {
446
- return true;
447
- }
448
- } catch (error) {
449
- }
450
- return false;
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
- // src/index.ts
454
- var loadWorkspace = async (search) => {
455
- return loadPackageManager(toAbsolute(search));
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
- var defaultOptions = {
458
- extensions: ["js", "mjs", "jsx", "ts", "mts", "tsx"]
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
- var generateFileHash = async (workspace, file, opts = {}) => {
461
- const options = { ...defaultOptions, ...opts };
462
- const hashes = /* @__PURE__ */ new Map();
463
- const absoluteFile = toAbsolute(file);
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;