@awsless/ts-file-cache 0.0.20 → 0.0.21
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 +50 -14
- package/dist/index.d.cts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +50 -14
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -84,10 +84,9 @@ const generateRecursiveFileHashes = async (workspace, file, sourceFile, allowedE
|
|
|
84
84
|
return;
|
|
85
85
|
}
|
|
86
86
|
const module = getPackageName(file);
|
|
87
|
-
if (hashes.has(module)) return;
|
|
88
87
|
const dependency = findDependency(workspace, module, sourceFile);
|
|
89
88
|
if (dependency) {
|
|
90
|
-
if (dependency.type === "package") hashes.set(
|
|
89
|
+
if (dependency.type === "package") hashes.set(`${module}@${dependency.version}`, Buffer.from(dependency.treeHash, "hex"));
|
|
91
90
|
else {
|
|
92
91
|
const localPackage = workspace.packages[dependency.link];
|
|
93
92
|
if (!localPackage) throw new Error(`Can't find the local workspace package for: ${file}`);
|
|
@@ -122,7 +121,7 @@ const findDependency = (workspace, module, source) => {
|
|
|
122
121
|
};
|
|
123
122
|
//#endregion
|
|
124
123
|
//#region src/package-manager/bun.ts
|
|
125
|
-
const bun = async (cwd, lockFile) => {
|
|
124
|
+
const bun = async (cwd, lockFile, lockfileHash) => {
|
|
126
125
|
const data = parseJsonc(lockFile);
|
|
127
126
|
const resolvedVersions = {};
|
|
128
127
|
for (const [key, entry] of Object.entries(data.packages ?? {})) {
|
|
@@ -158,11 +157,13 @@ const bun = async (cwd, lockFile) => {
|
|
|
158
157
|
const version = resolvedVersions[name] ?? resolvedVersions[`${workspace.name}/${name}`];
|
|
159
158
|
if (version) dependencies[name] = {
|
|
160
159
|
type: "package",
|
|
161
|
-
version
|
|
160
|
+
version,
|
|
161
|
+
treeHash: lockfileHash
|
|
162
162
|
};
|
|
163
163
|
else dependencies[name] = {
|
|
164
164
|
type: "package",
|
|
165
|
-
version: specifier
|
|
165
|
+
version: specifier,
|
|
166
|
+
treeHash: lockfileHash
|
|
166
167
|
};
|
|
167
168
|
}
|
|
168
169
|
importers[path$4] = dependencies;
|
|
@@ -225,8 +226,9 @@ const stripJsoncSyntax = (text) => {
|
|
|
225
226
|
};
|
|
226
227
|
//#endregion
|
|
227
228
|
//#region src/package-manager/pnpm.ts
|
|
228
|
-
const pnpm = async (cwd, lockFile) => {
|
|
229
|
+
const pnpm = async (cwd, lockFile, lockfileHash) => {
|
|
229
230
|
const data = (0, yaml.parse)(lockFile);
|
|
231
|
+
const treeHash = createTreeHasher(data, lockfileHash);
|
|
230
232
|
const importers = {};
|
|
231
233
|
for (const [path$2, importee] of Object.entries(data.importers)) {
|
|
232
234
|
const deps = {
|
|
@@ -241,7 +243,8 @@ const pnpm = async (cwd, lockFile) => {
|
|
|
241
243
|
};
|
|
242
244
|
else dependencies[name] = {
|
|
243
245
|
type: "package",
|
|
244
|
-
version: entry.version
|
|
246
|
+
version: entry.version,
|
|
247
|
+
treeHash: treeHash(`${name}@${entry.version}`)
|
|
245
248
|
};
|
|
246
249
|
importers[path$2] = dependencies;
|
|
247
250
|
}
|
|
@@ -250,6 +253,41 @@ const pnpm = async (cwd, lockFile) => {
|
|
|
250
253
|
packages: await buildPackages(cwd, importers)
|
|
251
254
|
};
|
|
252
255
|
};
|
|
256
|
+
const createTreeHasher = (data, lockfileHash) => {
|
|
257
|
+
if (!data.snapshots) return () => lockfileHash;
|
|
258
|
+
const graph = {};
|
|
259
|
+
for (const [key, entry] of Object.entries(data.snapshots)) {
|
|
260
|
+
const deps = {
|
|
261
|
+
...entry.dependencies,
|
|
262
|
+
...entry.optionalDependencies
|
|
263
|
+
};
|
|
264
|
+
const children = [];
|
|
265
|
+
for (const [name, version] of Object.entries(deps)) if (!version.startsWith("link:")) children.push(`${name}@${version}`);
|
|
266
|
+
graph[key] = children;
|
|
267
|
+
}
|
|
268
|
+
const hashed = /* @__PURE__ */ new Map();
|
|
269
|
+
return (key) => {
|
|
270
|
+
const cached = hashed.get(key);
|
|
271
|
+
if (cached) return cached;
|
|
272
|
+
const reachable = /* @__PURE__ */ new Set([key]);
|
|
273
|
+
const queue = [key];
|
|
274
|
+
while (queue.length > 0) {
|
|
275
|
+
const current = queue.pop();
|
|
276
|
+
for (const child of graph[current] ?? []) if (!reachable.has(child)) {
|
|
277
|
+
reachable.add(child);
|
|
278
|
+
queue.push(child);
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
const hash = (0, crypto.createHash)("sha1");
|
|
282
|
+
for (const entry of Array.from(reachable).toSorted()) {
|
|
283
|
+
hash.update(entry);
|
|
284
|
+
hash.update("\n");
|
|
285
|
+
}
|
|
286
|
+
const digest = hash.digest("hex");
|
|
287
|
+
hashed.set(key, digest);
|
|
288
|
+
return digest;
|
|
289
|
+
};
|
|
290
|
+
};
|
|
253
291
|
//#endregion
|
|
254
292
|
//#region src/package-manager/util.ts
|
|
255
293
|
const parsers = {
|
|
@@ -262,9 +300,10 @@ const loadPackageManager = async (search, level = 5) => {
|
|
|
262
300
|
const file = (0, path.join)(search, lockFileName);
|
|
263
301
|
if (await fileExist(file)) {
|
|
264
302
|
const content = await (0, fs_promises.readFile)(file, "utf8");
|
|
303
|
+
const lockfileHash = (0, crypto.createHash)("sha1").update(content).digest("hex");
|
|
265
304
|
return {
|
|
266
|
-
...await parser(search, content),
|
|
267
|
-
lockfileHash
|
|
305
|
+
...await parser(search, content, lockfileHash),
|
|
306
|
+
lockfileHash
|
|
268
307
|
};
|
|
269
308
|
}
|
|
270
309
|
}
|
|
@@ -304,15 +343,12 @@ const defaultOptions = { extensions: [
|
|
|
304
343
|
"mts",
|
|
305
344
|
"tsx"
|
|
306
345
|
] };
|
|
307
|
-
const seedHashes = (workspace) => {
|
|
308
|
-
return /* @__PURE__ */ new Map([["#lockfile", Buffer.from(workspace.lockfileHash, "hex")]]);
|
|
309
|
-
};
|
|
310
346
|
const generateFileHash = async (workspace, file, opts = {}) => {
|
|
311
347
|
const options = {
|
|
312
348
|
...defaultOptions,
|
|
313
349
|
...opts
|
|
314
350
|
};
|
|
315
|
-
const hashes =
|
|
351
|
+
const hashes = /* @__PURE__ */ new Map();
|
|
316
352
|
const absoluteFile = toAbsolute(file);
|
|
317
353
|
await generateRecursiveFileHashes(workspace, absoluteFile, absoluteFile, options.extensions, hashes);
|
|
318
354
|
return mergeHashes(hashes);
|
|
@@ -322,7 +358,7 @@ const generateFolderHash = async (workspace, folder, opts = {}) => {
|
|
|
322
358
|
...defaultOptions,
|
|
323
359
|
...opts
|
|
324
360
|
};
|
|
325
|
-
const hashes =
|
|
361
|
+
const hashes = /* @__PURE__ */ new Map();
|
|
326
362
|
const absoluteFolder = toAbsolute(folder);
|
|
327
363
|
const files = await (0, fs_promises.readdir)(absoluteFolder, {
|
|
328
364
|
recursive: true,
|
package/dist/index.d.cts
CHANGED
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -83,10 +83,9 @@ const generateRecursiveFileHashes = async (workspace, file, sourceFile, allowedE
|
|
|
83
83
|
return;
|
|
84
84
|
}
|
|
85
85
|
const module = getPackageName(file);
|
|
86
|
-
if (hashes.has(module)) return;
|
|
87
86
|
const dependency = findDependency(workspace, module, sourceFile);
|
|
88
87
|
if (dependency) {
|
|
89
|
-
if (dependency.type === "package") hashes.set(
|
|
88
|
+
if (dependency.type === "package") hashes.set(`${module}@${dependency.version}`, Buffer.from(dependency.treeHash, "hex"));
|
|
90
89
|
else {
|
|
91
90
|
const localPackage = workspace.packages[dependency.link];
|
|
92
91
|
if (!localPackage) throw new Error(`Can't find the local workspace package for: ${file}`);
|
|
@@ -121,7 +120,7 @@ const findDependency = (workspace, module, source) => {
|
|
|
121
120
|
};
|
|
122
121
|
//#endregion
|
|
123
122
|
//#region src/package-manager/bun.ts
|
|
124
|
-
const bun = async (cwd, lockFile) => {
|
|
123
|
+
const bun = async (cwd, lockFile, lockfileHash) => {
|
|
125
124
|
const data = parseJsonc(lockFile);
|
|
126
125
|
const resolvedVersions = {};
|
|
127
126
|
for (const [key, entry] of Object.entries(data.packages ?? {})) {
|
|
@@ -157,11 +156,13 @@ const bun = async (cwd, lockFile) => {
|
|
|
157
156
|
const version = resolvedVersions[name] ?? resolvedVersions[`${workspace.name}/${name}`];
|
|
158
157
|
if (version) dependencies[name] = {
|
|
159
158
|
type: "package",
|
|
160
|
-
version
|
|
159
|
+
version,
|
|
160
|
+
treeHash: lockfileHash
|
|
161
161
|
};
|
|
162
162
|
else dependencies[name] = {
|
|
163
163
|
type: "package",
|
|
164
|
-
version: specifier
|
|
164
|
+
version: specifier,
|
|
165
|
+
treeHash: lockfileHash
|
|
165
166
|
};
|
|
166
167
|
}
|
|
167
168
|
importers[path] = dependencies;
|
|
@@ -224,8 +225,9 @@ const stripJsoncSyntax = (text) => {
|
|
|
224
225
|
};
|
|
225
226
|
//#endregion
|
|
226
227
|
//#region src/package-manager/pnpm.ts
|
|
227
|
-
const pnpm = async (cwd, lockFile) => {
|
|
228
|
+
const pnpm = async (cwd, lockFile, lockfileHash) => {
|
|
228
229
|
const data = parse(lockFile);
|
|
230
|
+
const treeHash = createTreeHasher(data, lockfileHash);
|
|
229
231
|
const importers = {};
|
|
230
232
|
for (const [path, importee] of Object.entries(data.importers)) {
|
|
231
233
|
const deps = {
|
|
@@ -240,7 +242,8 @@ const pnpm = async (cwd, lockFile) => {
|
|
|
240
242
|
};
|
|
241
243
|
else dependencies[name] = {
|
|
242
244
|
type: "package",
|
|
243
|
-
version: entry.version
|
|
245
|
+
version: entry.version,
|
|
246
|
+
treeHash: treeHash(`${name}@${entry.version}`)
|
|
244
247
|
};
|
|
245
248
|
importers[path] = dependencies;
|
|
246
249
|
}
|
|
@@ -249,6 +252,41 @@ const pnpm = async (cwd, lockFile) => {
|
|
|
249
252
|
packages: await buildPackages(cwd, importers)
|
|
250
253
|
};
|
|
251
254
|
};
|
|
255
|
+
const createTreeHasher = (data, lockfileHash) => {
|
|
256
|
+
if (!data.snapshots) return () => lockfileHash;
|
|
257
|
+
const graph = {};
|
|
258
|
+
for (const [key, entry] of Object.entries(data.snapshots)) {
|
|
259
|
+
const deps = {
|
|
260
|
+
...entry.dependencies,
|
|
261
|
+
...entry.optionalDependencies
|
|
262
|
+
};
|
|
263
|
+
const children = [];
|
|
264
|
+
for (const [name, version] of Object.entries(deps)) if (!version.startsWith("link:")) children.push(`${name}@${version}`);
|
|
265
|
+
graph[key] = children;
|
|
266
|
+
}
|
|
267
|
+
const hashed = /* @__PURE__ */ new Map();
|
|
268
|
+
return (key) => {
|
|
269
|
+
const cached = hashed.get(key);
|
|
270
|
+
if (cached) return cached;
|
|
271
|
+
const reachable = /* @__PURE__ */ new Set([key]);
|
|
272
|
+
const queue = [key];
|
|
273
|
+
while (queue.length > 0) {
|
|
274
|
+
const current = queue.pop();
|
|
275
|
+
for (const child of graph[current] ?? []) if (!reachable.has(child)) {
|
|
276
|
+
reachable.add(child);
|
|
277
|
+
queue.push(child);
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
const hash = createHash("sha1");
|
|
281
|
+
for (const entry of Array.from(reachable).toSorted()) {
|
|
282
|
+
hash.update(entry);
|
|
283
|
+
hash.update("\n");
|
|
284
|
+
}
|
|
285
|
+
const digest = hash.digest("hex");
|
|
286
|
+
hashed.set(key, digest);
|
|
287
|
+
return digest;
|
|
288
|
+
};
|
|
289
|
+
};
|
|
252
290
|
//#endregion
|
|
253
291
|
//#region src/package-manager/util.ts
|
|
254
292
|
const parsers = {
|
|
@@ -261,9 +299,10 @@ const loadPackageManager = async (search, level = 5) => {
|
|
|
261
299
|
const file = join(search, lockFileName);
|
|
262
300
|
if (await fileExist(file)) {
|
|
263
301
|
const content = await readFile(file, "utf8");
|
|
302
|
+
const lockfileHash = createHash("sha1").update(content).digest("hex");
|
|
264
303
|
return {
|
|
265
|
-
...await parser(search, content),
|
|
266
|
-
lockfileHash
|
|
304
|
+
...await parser(search, content, lockfileHash),
|
|
305
|
+
lockfileHash
|
|
267
306
|
};
|
|
268
307
|
}
|
|
269
308
|
}
|
|
@@ -303,15 +342,12 @@ const defaultOptions = { extensions: [
|
|
|
303
342
|
"mts",
|
|
304
343
|
"tsx"
|
|
305
344
|
] };
|
|
306
|
-
const seedHashes = (workspace) => {
|
|
307
|
-
return /* @__PURE__ */ new Map([["#lockfile", Buffer.from(workspace.lockfileHash, "hex")]]);
|
|
308
|
-
};
|
|
309
345
|
const generateFileHash = async (workspace, file, opts = {}) => {
|
|
310
346
|
const options = {
|
|
311
347
|
...defaultOptions,
|
|
312
348
|
...opts
|
|
313
349
|
};
|
|
314
|
-
const hashes =
|
|
350
|
+
const hashes = /* @__PURE__ */ new Map();
|
|
315
351
|
const absoluteFile = toAbsolute(file);
|
|
316
352
|
await generateRecursiveFileHashes(workspace, absoluteFile, absoluteFile, options.extensions, hashes);
|
|
317
353
|
return mergeHashes(hashes);
|
|
@@ -321,7 +357,7 @@ const generateFolderHash = async (workspace, folder, opts = {}) => {
|
|
|
321
357
|
...defaultOptions,
|
|
322
358
|
...opts
|
|
323
359
|
};
|
|
324
|
-
const hashes =
|
|
360
|
+
const hashes = /* @__PURE__ */ new Map();
|
|
325
361
|
const absoluteFolder = toAbsolute(folder);
|
|
326
362
|
const files = await readdir(absoluteFolder, {
|
|
327
363
|
recursive: true,
|