@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.js CHANGED
@@ -1,458 +1,381 @@
1
- // src/index.ts
2
- import { readdir } from "fs/promises";
3
- import { extname as extname2, resolve as resolve2 } from "path";
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
- FunctionBody(node, state, callback) {
18
- for (const statement of node.stmts) {
19
- callback(statement, state);
20
- }
21
- }
11
+ FunctionBody(node, state, callback) {
12
+ for (const statement of node.stmts) callback(statement, state);
13
+ }
22
14
  };
23
- var baseVisitor = new PatchedBaseVisitor();
24
- var parseOptions = (file) => {
25
- if (file.endsWith(".tsx")) {
26
- return { syntax: "typescript", tsx: true, decorators: true };
27
- }
28
- if (file.endsWith(".ts") || file.endsWith(".mts") || file.endsWith(".cts")) {
29
- return { syntax: "typescript", decorators: true };
30
- }
31
- return { syntax: "ecmascript", jsx: true, decorators: true };
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
- var findImports = async (file, code) => {
34
- let ast;
35
- try {
36
- ast = await 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
- 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) {
51
- importing.add(node.source.value);
52
- }
53
- },
54
- CallExpression(node) {
55
- if (node.callee.type === "Import") {
56
- const first = node.arguments.at(0);
57
- if (first && first.expression.type === "StringLiteral") {
58
- importing.add(first.expression.value);
59
- }
60
- }
61
- },
62
- TsImportEqualsDeclaration(node) {
63
- if (node.moduleRef.type === "TsExternalModuleReference") {
64
- importing.add(node.moduleRef.expression.value);
65
- }
66
- }
67
- }, baseVisitor);
68
- } catch (error) {
69
- throw new Error(`Failed to walk the AST of: ${file}`, { cause: error });
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
- // src/module.ts
84
- import { stat } from "fs/promises";
85
- import { basename, extname, isAbsolute, join } from "path";
86
- var findFile = async (files) => {
87
- for (const file of files) {
88
- try {
89
- const s = await stat(file);
90
- if (s.isFile()) {
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
- var alternateExtensions = {
100
- ".js": [".ts", ".tsx"],
101
- ".mjs": [".mts"],
102
- ".cjs": [".cts"],
103
- ".jsx": [".tsx"]
81
+ const alternateExtensions = {
82
+ ".js": [".ts", ".tsx"],
83
+ ".mjs": [".mts"],
84
+ ".cjs": [".cts"],
85
+ ".jsx": [".tsx"]
104
86
  };
105
- var resolveModuleImportFile = (file, allowedExtensions) => {
106
- const extension = extname(file);
107
- const alternates = alternateExtensions[extension];
108
- if (alternates) {
109
- const candidates = [file];
110
- for (const alternate of alternates) {
111
- if (allowedExtensions.includes(alternate.substring(1))) {
112
- candidates.push(file.substring(0, file.length - extension.length) + alternate);
113
- }
114
- }
115
- return findFile(candidates);
116
- }
117
- if (!basename(file).includes(".")) {
118
- return findFile([
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
- var isLocalCodeFile = (file) => {
127
- return file.startsWith("/") || file.startsWith(".");
102
+ const isLocalCodeFile = (file) => {
103
+ return file.startsWith("/") || file.startsWith(".");
128
104
  };
129
- var toAbsolute = (file) => {
130
- if (isAbsolute(file)) {
131
- return file;
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
- // src/hash.ts
137
- var generateRecursiveFileHashes = async (workspace, file, sourceFile, allowedExtensions, hashes) => {
138
- if (isLocalCodeFile(file)) {
139
- try {
140
- file = await resolveModuleImportFile(file, allowedExtensions);
141
- } catch (error) {
142
- throw new Error(`Can't find imported file: "${file}" inside the source: "${sourceFile}"`);
143
- }
144
- const relFile = relative(workspace.cwd, file).split(sep).join("/");
145
- if (hashes.has(relFile)) {
146
- return;
147
- }
148
- const code = await readFile(file, "utf8");
149
- const ext = file.split(".").pop();
150
- const hash = createHash("sha1").update(code).digest();
151
- hashes.set(relFile, hash);
152
- if (!ext || !allowedExtensions.includes(ext)) {
153
- return;
154
- }
155
- const deps = await findImports(file, code);
156
- for (const dep of deps) {
157
- await generateRecursiveFileHashes(workspace, dep, file, allowedExtensions, hashes);
158
- }
159
- return;
160
- }
161
- const module = getPackageName(file);
162
- if (hashes.has(module)) {
163
- return;
164
- }
165
- const dependency = findDependency(workspace, module, sourceFile);
166
- if (dependency) {
167
- if (dependency.type === "package") {
168
- hashes.set(module, Buffer.from(`${module}:${dependency.version}`, "utf8"));
169
- } else {
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
- var mergeHashes = (hashes) => {
193
- const names = Array.from(hashes.keys()).sort();
194
- const merged = createHash("sha1");
195
- for (const name of names) {
196
- merged.update(name);
197
- merged.update(hashes.get(name));
198
- }
199
- return merged.digest("hex");
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
- var getPackageName = (importee) => {
202
- const parts = importee.split("/");
203
- if (importee.startsWith("@")) {
204
- if (parts.length >= 2) {
205
- return `${parts[0]}/${parts[1]}`;
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
- var findDependency = (workspace, module, source) => {
213
- 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]);
214
- if (!pkg) {
215
- return;
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
- // src/package-manager/index.ts
221
- import { lstat, readFile as readFile3 } from "fs/promises";
222
- import { join as join5, normalize } from "path";
223
-
224
- // src/package-manager/bun.ts
225
- import { join as join3 } from "path";
226
-
227
- // src/package-manager/importer.ts
228
- import { readFile as readFile2 } from "fs/promises";
229
- import { join as join2 } from "path";
230
- var buildPackages = async (cwd, importers) => {
231
- const packages = {};
232
- await Promise.all(
233
- Object.entries(importers).map(async ([path, dependencies]) => {
234
- const packageJson = await readFile2(join2(cwd, path, "package.json"), "utf8");
235
- const packageData = JSON.parse(packageJson);
236
- const entry = packageData.module ?? packageData.main;
237
- packages[join2(cwd, path)] = {
238
- name: packageData.name,
239
- path: join2(cwd, path),
240
- main: entry ? join2(cwd, path, entry) : void 0,
241
- dependencies
242
- };
243
- })
244
- );
245
- return packages;
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
- // src/package-manager/bun.ts
249
- var bun = async (cwd, lockFile) => {
250
- const data = parseJsonc(lockFile);
251
- const resolvedVersions = {};
252
- for (const [key, entry] of Object.entries(data.packages ?? {})) {
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
- var parseJsonc = (text) => {
309
- try {
310
- return JSON.parse(text);
311
- } catch (_) {
312
- return JSON.parse(stripJsoncSyntax(text));
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
- var stripJsoncSyntax = (text) => {
316
- let result = "";
317
- let index = 0;
318
- while (index < text.length) {
319
- const char = text[index];
320
- if (char === '"') {
321
- result += char;
322
- index++;
323
- while (index < text.length) {
324
- const stringChar = text[index];
325
- result += stringChar;
326
- index++;
327
- if (stringChar === "\\") {
328
- result += text[index] ?? "";
329
- index++;
330
- continue;
331
- }
332
- if (stringChar === '"') {
333
- break;
334
- }
335
- }
336
- continue;
337
- }
338
- if (char === "/" && text[index + 1] === "/") {
339
- while (index < text.length && text[index] !== "\n") {
340
- index++;
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
- // src/package-manager/pnpm.ts
369
- import { join as join4 } from "path";
370
- import { parse as parse2 } from "yaml";
371
- var pnpm = async (cwd, lockFile) => {
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
- // src/package-manager/index.ts
400
- var parsers = {
401
- "pnpm-lock.yaml": pnpm,
402
- "bun.lock": bun
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
- var loadPackageManager = async (search, level = 5) => {
405
- if (!level) {
406
- throw new TypeError("No pnpm or bun lock file found");
407
- }
408
- for (const [lockFileName, parser] of Object.entries(parsers)) {
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
- var fileExist = async (file) => {
417
- try {
418
- const stat2 = await lstat(file);
419
- if (stat2.isFile()) {
420
- return true;
421
- }
422
- } catch (error) {
423
- }
424
- return false;
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
- // src/index.ts
428
- var loadWorkspace = async (search) => {
429
- return loadPackageManager(toAbsolute(search));
337
+ //#endregion
338
+ //#region src/index.ts
339
+ const loadWorkspace = async (search) => {
340
+ return loadPackageManager(toAbsolute(search));
430
341
  };
431
- var defaultOptions = {
432
- extensions: ["js", "mjs", "jsx", "ts", "mts", "tsx"]
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
- var generateFileHash = async (workspace, file, opts = {}) => {
435
- const options = { ...defaultOptions, ...opts };
436
- const hashes = /* @__PURE__ */ new Map();
437
- const absoluteFile = toAbsolute(file);
438
- await generateRecursiveFileHashes(workspace, absoluteFile, absoluteFile, options.extensions, hashes);
439
- return mergeHashes(hashes);
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
- var generateFolderHash = async (workspace, folder, opts = {}) => {
442
- const options = { ...defaultOptions, ...opts };
443
- const hashes = /* @__PURE__ */ new Map();
444
- const absoluteFolder = toAbsolute(folder);
445
- const files = await readdir(absoluteFolder, { recursive: true, withFileTypes: true });
446
- for (const file of files) {
447
- if (file.isFile() && options.extensions.includes(extname2(file.name).substring(1))) {
448
- const f = resolve2(file.parentPath, file.name);
449
- await generateRecursiveFileHashes(workspace, f, f, options.extensions, hashes);
450
- }
451
- }
452
- return mergeHashes(hashes);
453
- };
454
- export {
455
- generateFileHash,
456
- generateFolderHash,
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 };