@awsless/ts-file-cache 0.0.16 → 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,301 +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_promises4 = require("fs/promises");
29
-
30
- // src/hash.ts
31
- var import_crypto = require("crypto");
32
- var import_promises2 = require("fs/promises");
33
- var import_node_module = require("module");
34
- var import_path3 = require("path");
35
-
36
- // src/import.ts
37
- var import_core = require("@swc/core");
38
- var import_path = require("path");
39
- var import_swc_walk = require("swc-walk");
40
- var findImports = async (file, code) => {
41
- const ast = await (0, import_core.parse)(code, {
42
- syntax: "typescript"
43
- });
44
- const importing = /* @__PURE__ */ new Set();
45
- try {
46
- (0, import_swc_walk.simple)(ast, {
47
- ImportDeclaration(node) {
48
- importing.add(node.source.value);
49
- },
50
- ExportAllDeclaration(node) {
51
- importing.add(node.source.value);
52
- },
53
- ExportNamedDeclaration(node) {
54
- if (node.source) {
55
- importing.add(node.source.value);
56
- }
57
- },
58
- CallExpression(node) {
59
- if (node.callee.type === "Import") {
60
- const first = node.arguments.at(0);
61
- if (first && first.expression.type === "StringLiteral") {
62
- importing.add(first.expression.value);
63
- }
64
- }
65
- }
66
- });
67
- } catch (_) {
68
- return [];
69
- }
70
- return [...importing].map((importee) => {
71
- if (importee.startsWith(".")) {
72
- return (0, import_path.resolve)((0, import_path.dirname)(file), importee);
73
- }
74
- const parts = importee.split("/");
75
- if (parts.length > 2) {
76
- return parts.slice(0, 2).join("/");
77
- }
78
- return importee;
79
- });
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
+ });
80
71
  };
81
-
82
- // src/module.ts
83
- var import_promises = require("fs/promises");
84
- var import_path2 = require("path");
85
- var findFile = async (files) => {
86
- for (const file of files) {
87
- try {
88
- const s = await (0, import_promises.stat)(file);
89
- if (s.isFile()) {
90
- return file;
91
- }
92
- } catch (_) {
93
- continue;
94
- }
95
- }
96
- throw new Error(`No such file: ${files.join(", ")}`);
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(", ")}`);
97
81
  };
98
- var resolveModuleImportFile = (file, allowedExtensions) => {
99
- if (file.endsWith(".js") && allowedExtensions.includes("js") && allowedExtensions.includes("ts")) {
100
- return findFile([file, file.substring(0, file.length - 3) + ".ts"]);
101
- }
102
- if (!(0, import_path2.basename)(file).includes(".")) {
103
- return findFile([
104
- file,
105
- ...allowedExtensions.map((exp) => `${file}.${exp}`),
106
- ...allowedExtensions.map((exp) => (0, import_path2.join)(file, `/index.${exp}`))
107
- ]);
108
- }
109
- return file;
82
+ const alternateExtensions = {
83
+ ".js": [".ts", ".tsx"],
84
+ ".mjs": [".mts"],
85
+ ".cjs": [".cts"],
86
+ ".jsx": [".tsx"]
110
87
  };
111
- var isLocalCodeFile = (file) => {
112
- return file.startsWith("/") || file.startsWith(".");
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;
113
102
  };
114
- var toAbsolute = (file) => {
115
- if ((0, import_path2.isAbsolute)(file)) {
116
- return file;
117
- }
118
- return (0, import_path2.join)(process.cwd(), file);
103
+ const isLocalCodeFile = (file) => {
104
+ return file.startsWith("/") || file.startsWith(".");
119
105
  };
120
-
121
- // src/hash.ts
122
- var generateRecursiveFileHashes = async (workspace, file, sourceFile, allowedExtensions, hashes) => {
123
- if (isLocalCodeFile(file)) {
124
- try {
125
- file = await resolveModuleImportFile(file, allowedExtensions);
126
- } catch (error) {
127
- throw new Error(`Can't find imported file: "${file}" inside the source: "${sourceFile}"`);
128
- }
129
- const relFile = (0, import_path3.relative)(workspace.cwd, file);
130
- if (hashes.has(relFile)) {
131
- return;
132
- }
133
- const code = await (0, import_promises2.readFile)(file, "utf8");
134
- const ext = file.split(".").pop();
135
- const hash = (0, import_crypto.createHash)("sha1").update(code).digest();
136
- hashes.set(relFile, hash);
137
- if (!ext || !allowedExtensions.includes(ext)) {
138
- return;
139
- }
140
- const deps = await findImports(file, code);
141
- for (const dep of deps) {
142
- await generateRecursiveFileHashes(workspace, dep, file, allowedExtensions, hashes);
143
- }
144
- return;
145
- }
146
- const module2 = getPackageName(file);
147
- if (hashes.has(module2)) {
148
- return;
149
- }
150
- const dependency = findDependency(workspace, module2, sourceFile);
151
- if (dependency) {
152
- if (dependency.type === "package") {
153
- hashes.set(module2, Buffer.from(`${module2}:${dependency.version}`, "utf8"));
154
- } else {
155
- const localPackage = workspace.packages[module2];
156
- if (!localPackage) {
157
- throw new Error(`Can't find the local workspace package for: ${file}`);
158
- }
159
- if (!localPackage.main) {
160
- throw new Error(`Workspace package doesn't have a main entry: ${file}`);
161
- }
162
- await generateRecursiveFileHashes(
163
- workspace,
164
- localPackage.main,
165
- localPackage.main,
166
- allowedExtensions,
167
- hashes
168
- );
169
- }
170
- return;
171
- }
172
- if (import_node_module.builtinModules.includes(module2.replace(/^node\:/, ""))) {
173
- return;
174
- }
175
- throw new Error(`Can't find the dependency version for: ${file} inside the source: ${sourceFile}`);
106
+ const toAbsolute = (file) => {
107
+ if ((0, path.isAbsolute)(file)) return file;
108
+ return (0, path.join)(process.cwd(), file);
176
109
  };
177
- var mergeHashes = (hashes) => {
178
- const merge = Buffer.concat(Array.from(hashes.values()).sort());
179
- return (0, import_crypto.createHash)("sha1").update(merge).digest("hex");
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}`);
180
145
  };
181
- var getPackageName = (importee) => {
182
- const parts = importee.split("/");
183
- if (importee.startsWith("@")) {
184
- if (parts.length >= 2) {
185
- return `${parts[0]}/${parts[1]}`;
186
- }
187
- } else if (parts.length >= 1) {
188
- return parts[0];
189
- }
190
- throw new Error(`Malformed importee: ${importee}`);
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");
191
154
  };
192
- var findDependency = (workspace, module2, source) => {
193
- const pkg = Object.values(workspace.packages).filter((p) => source.startsWith(p.path)).sort((a, b) => b.path.split("/").length - a.path.split("/").length).find((p) => p.dependencies[module2]);
194
- if (!pkg) {
195
- return;
196
- }
197
- return pkg.dependencies[module2];
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}`);
198
161
  };
199
-
200
- // src/index.ts
201
- var import_path5 = require("path");
202
-
203
- // src/package-manager/pnpm.ts
204
- var import_promises3 = require("fs/promises");
205
- var import_path4 = require("path");
206
- var import_yaml = require("yaml");
207
- var pnpm = async (search) => {
208
- const [cwd, lockFile] = await findLockFile(search);
209
- const data = (0, import_yaml.parse)(lockFile);
210
- const packages = {};
211
- await Promise.all(
212
- Object.entries(data.importers).map(async ([path, importee]) => {
213
- const deps = { ...importee.devDependencies, ...importee.dependencies };
214
- const dependencies = {};
215
- const packageJson = await (0, import_promises3.readFile)((0, import_path4.join)(cwd, path, "package.json"), "utf8");
216
- const packageData = JSON.parse(packageJson);
217
- for (const [name, entry2] of Object.entries(deps)) {
218
- if (entry2.version.startsWith("link:")) {
219
- dependencies[name] = {
220
- type: "workspace",
221
- link: (0, import_path4.join)(cwd, path, entry2.version.substring(5))
222
- };
223
- } else {
224
- dependencies[name] = {
225
- type: "package",
226
- version: entry2.version
227
- };
228
- }
229
- }
230
- const entry = packageData.module ?? packageData.main;
231
- packages[packageData.name] = {
232
- name: packageData.name,
233
- path: (0, import_path4.join)(cwd, path),
234
- main: entry ? (0, import_path4.join)(cwd, path, entry) : void 0,
235
- dependencies
236
- };
237
- })
238
- );
239
- return {
240
- cwd,
241
- packages
242
- };
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];
243
166
  };
244
- var findLockFile = async (path, level = 5) => {
245
- if (!level) {
246
- throw new TypeError("No pnpm lock file found");
247
- }
248
- const file = (0, import_path4.join)(path, "pnpm-lock.yaml");
249
- const exists = await fileExist(file);
250
- if (exists) {
251
- return [path, await (0, import_promises3.readFile)(file, "utf8")];
252
- }
253
- return findLockFile((0, import_path4.normalize)((0, import_path4.join)(path, "..")), level - 1);
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
+ };
254
218
  };
255
- var fileExist = async (file) => {
256
- try {
257
- const stat2 = await (0, import_promises3.lstat)(file);
258
- if (stat2.isFile()) {
259
- return true;
260
- }
261
- } catch (error) {
262
- }
263
- return false;
219
+ const parseJsonc = (text) => {
220
+ try {
221
+ return JSON.parse(text);
222
+ } catch (_) {
223
+ return JSON.parse(stripJsoncSyntax(text));
224
+ }
264
225
  };
265
-
266
- // src/index.ts
267
- var loadWorkspace = async (search) => {
268
- const { cwd, packages } = await pnpm(toAbsolute(search));
269
- return {
270
- cwd,
271
- packages
272
- };
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;
273
269
  };
274
- var defaultOptions = {
275
- extensions: ["js", "mjs", "jsx", "ts", "mts", "tsx"]
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
+ };
276
296
  };
277
- var generateFileHash = async (workspace, file, opts = {}) => {
278
- const options = { ...defaultOptions, ...opts };
279
- const hashes = /* @__PURE__ */ new Map();
280
- const absoluteFile = toAbsolute(file);
281
- await generateRecursiveFileHashes(workspace, absoluteFile, absoluteFile, options.extensions, hashes);
282
- return mergeHashes(hashes);
297
+ //#endregion
298
+ //#region src/package-manager/util.ts
299
+ const parsers = {
300
+ "pnpm-lock.yaml": pnpm,
301
+ "bun.lock": bun
283
302
  };
284
- var generateFolderHash = async (workspace, folder, opts = {}) => {
285
- const options = { ...defaultOptions, ...opts };
286
- const hashes = /* @__PURE__ */ new Map();
287
- const files = await (0, import_promises4.readdir)(folder, { recursive: true, withFileTypes: true });
288
- for (const file of files) {
289
- if (file.isFile() && options.extensions.includes((0, import_path5.extname)(file.name).substring(1))) {
290
- const f = (0, import_path5.resolve)(file.parentPath, file.name);
291
- await generateRecursiveFileHashes(workspace, f, f, options.extensions, hashes);
292
- }
293
- }
294
- return mergeHashes(hashes);
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);
295
316
  };
296
- // Annotate the CommonJS export names for ESM import in node:
297
- 0 && (module.exports = {
298
- generateFileHash,
299
- generateFolderHash,
300
- loadWorkspace
301
- });
317
+ const fileExist = async (file) => {
318
+ try {
319
+ if ((await (0, fs_promises.lstat)(file)).isFile()) return true;
320
+ } catch (error) {}
321
+ return false;
322
+ };
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;
337
+ };
338
+ //#endregion
339
+ //#region src/index.ts
340
+ const loadWorkspace = async (search) => {
341
+ return loadPackageManager(toAbsolute(search));
342
+ };
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")]]);
353
+ };
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);
363
+ };
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);
380
+ };
381
+ //#endregion
382
+ exports.generateFileHash = generateFileHash;
383
+ exports.generateFolderHash = generateFolderHash;
384
+ exports.loadWorkspace = loadWorkspace;