@awsless/ts-file-cache 0.0.20 → 0.0.22
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 +55 -14
- package/dist/index.d.cts +3 -1
- package/dist/index.d.ts +3 -1
- package/dist/index.js +55 -15
- 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
|
}
|
|
@@ -296,6 +335,10 @@ const buildPackages = async (cwd, importers) => {
|
|
|
296
335
|
const loadWorkspace = async (search) => {
|
|
297
336
|
return loadPackageManager(toAbsolute(search));
|
|
298
337
|
};
|
|
338
|
+
const generateDependencyHash = (workspace, name) => {
|
|
339
|
+
const dependency = workspace.packages[workspace.cwd]?.dependencies[name];
|
|
340
|
+
if (dependency?.type === "package") return dependency.treeHash;
|
|
341
|
+
};
|
|
299
342
|
const defaultOptions = { extensions: [
|
|
300
343
|
"js",
|
|
301
344
|
"mjs",
|
|
@@ -304,15 +347,12 @@ const defaultOptions = { extensions: [
|
|
|
304
347
|
"mts",
|
|
305
348
|
"tsx"
|
|
306
349
|
] };
|
|
307
|
-
const seedHashes = (workspace) => {
|
|
308
|
-
return /* @__PURE__ */ new Map([["#lockfile", Buffer.from(workspace.lockfileHash, "hex")]]);
|
|
309
|
-
};
|
|
310
350
|
const generateFileHash = async (workspace, file, opts = {}) => {
|
|
311
351
|
const options = {
|
|
312
352
|
...defaultOptions,
|
|
313
353
|
...opts
|
|
314
354
|
};
|
|
315
|
-
const hashes =
|
|
355
|
+
const hashes = /* @__PURE__ */ new Map();
|
|
316
356
|
const absoluteFile = toAbsolute(file);
|
|
317
357
|
await generateRecursiveFileHashes(workspace, absoluteFile, absoluteFile, options.extensions, hashes);
|
|
318
358
|
return mergeHashes(hashes);
|
|
@@ -322,7 +362,7 @@ const generateFolderHash = async (workspace, folder, opts = {}) => {
|
|
|
322
362
|
...defaultOptions,
|
|
323
363
|
...opts
|
|
324
364
|
};
|
|
325
|
-
const hashes =
|
|
365
|
+
const hashes = /* @__PURE__ */ new Map();
|
|
326
366
|
const absoluteFolder = toAbsolute(folder);
|
|
327
367
|
const files = await (0, fs_promises.readdir)(absoluteFolder, {
|
|
328
368
|
recursive: true,
|
|
@@ -335,6 +375,7 @@ const generateFolderHash = async (workspace, folder, opts = {}) => {
|
|
|
335
375
|
return mergeHashes(hashes);
|
|
336
376
|
};
|
|
337
377
|
//#endregion
|
|
378
|
+
exports.generateDependencyHash = generateDependencyHash;
|
|
338
379
|
exports.generateFileHash = generateFileHash;
|
|
339
380
|
exports.generateFolderHash = generateFolderHash;
|
|
340
381
|
exports.loadWorkspace = loadWorkspace;
|
package/dist/index.d.cts
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
type PackageDependency = {
|
|
3
3
|
type: 'package';
|
|
4
4
|
version: string;
|
|
5
|
+
treeHash: string;
|
|
5
6
|
};
|
|
6
7
|
type WorkspaceDependency = {
|
|
7
8
|
type: 'workspace';
|
|
@@ -22,10 +23,11 @@ type Workspace = {
|
|
|
22
23
|
//#endregion
|
|
23
24
|
//#region src/index.d.ts
|
|
24
25
|
declare const loadWorkspace: (search: string) => Promise<Workspace>;
|
|
26
|
+
declare const generateDependencyHash: (workspace: Workspace, name: string) => string | undefined;
|
|
25
27
|
type Options = {
|
|
26
28
|
extensions?: string[];
|
|
27
29
|
};
|
|
28
30
|
declare const generateFileHash: (workspace: Workspace, file: string, opts?: Options) => Promise<string>;
|
|
29
31
|
declare const generateFolderHash: (workspace: Workspace, folder: string, opts?: Options) => Promise<string>;
|
|
30
32
|
//#endregion
|
|
31
|
-
export { Dependency, Package, PackageDependency, Workspace, WorkspaceDependency, generateFileHash, generateFolderHash, loadWorkspace };
|
|
33
|
+
export { Dependency, Package, PackageDependency, Workspace, WorkspaceDependency, generateDependencyHash, generateFileHash, generateFolderHash, loadWorkspace };
|
package/dist/index.d.ts
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
type PackageDependency = {
|
|
3
3
|
type: 'package';
|
|
4
4
|
version: string;
|
|
5
|
+
treeHash: string;
|
|
5
6
|
};
|
|
6
7
|
type WorkspaceDependency = {
|
|
7
8
|
type: 'workspace';
|
|
@@ -22,10 +23,11 @@ type Workspace = {
|
|
|
22
23
|
//#endregion
|
|
23
24
|
//#region src/index.d.ts
|
|
24
25
|
declare const loadWorkspace: (search: string) => Promise<Workspace>;
|
|
26
|
+
declare const generateDependencyHash: (workspace: Workspace, name: string) => string | undefined;
|
|
25
27
|
type Options = {
|
|
26
28
|
extensions?: string[];
|
|
27
29
|
};
|
|
28
30
|
declare const generateFileHash: (workspace: Workspace, file: string, opts?: Options) => Promise<string>;
|
|
29
31
|
declare const generateFolderHash: (workspace: Workspace, folder: string, opts?: Options) => Promise<string>;
|
|
30
32
|
//#endregion
|
|
31
|
-
export { Dependency, Package, PackageDependency, Workspace, WorkspaceDependency, generateFileHash, generateFolderHash, loadWorkspace };
|
|
33
|
+
export { Dependency, Package, PackageDependency, Workspace, WorkspaceDependency, generateDependencyHash, generateFileHash, generateFolderHash, loadWorkspace };
|
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
|
}
|
|
@@ -295,6 +334,10 @@ const buildPackages = async (cwd, importers) => {
|
|
|
295
334
|
const loadWorkspace = async (search) => {
|
|
296
335
|
return loadPackageManager(toAbsolute(search));
|
|
297
336
|
};
|
|
337
|
+
const generateDependencyHash = (workspace, name) => {
|
|
338
|
+
const dependency = workspace.packages[workspace.cwd]?.dependencies[name];
|
|
339
|
+
if (dependency?.type === "package") return dependency.treeHash;
|
|
340
|
+
};
|
|
298
341
|
const defaultOptions = { extensions: [
|
|
299
342
|
"js",
|
|
300
343
|
"mjs",
|
|
@@ -303,15 +346,12 @@ const defaultOptions = { extensions: [
|
|
|
303
346
|
"mts",
|
|
304
347
|
"tsx"
|
|
305
348
|
] };
|
|
306
|
-
const seedHashes = (workspace) => {
|
|
307
|
-
return /* @__PURE__ */ new Map([["#lockfile", Buffer.from(workspace.lockfileHash, "hex")]]);
|
|
308
|
-
};
|
|
309
349
|
const generateFileHash = async (workspace, file, opts = {}) => {
|
|
310
350
|
const options = {
|
|
311
351
|
...defaultOptions,
|
|
312
352
|
...opts
|
|
313
353
|
};
|
|
314
|
-
const hashes =
|
|
354
|
+
const hashes = /* @__PURE__ */ new Map();
|
|
315
355
|
const absoluteFile = toAbsolute(file);
|
|
316
356
|
await generateRecursiveFileHashes(workspace, absoluteFile, absoluteFile, options.extensions, hashes);
|
|
317
357
|
return mergeHashes(hashes);
|
|
@@ -321,7 +361,7 @@ const generateFolderHash = async (workspace, folder, opts = {}) => {
|
|
|
321
361
|
...defaultOptions,
|
|
322
362
|
...opts
|
|
323
363
|
};
|
|
324
|
-
const hashes =
|
|
364
|
+
const hashes = /* @__PURE__ */ new Map();
|
|
325
365
|
const absoluteFolder = toAbsolute(folder);
|
|
326
366
|
const files = await readdir(absoluteFolder, {
|
|
327
367
|
recursive: true,
|
|
@@ -334,4 +374,4 @@ const generateFolderHash = async (workspace, folder, opts = {}) => {
|
|
|
334
374
|
return mergeHashes(hashes);
|
|
335
375
|
};
|
|
336
376
|
//#endregion
|
|
337
|
-
export { generateFileHash, generateFolderHash, loadWorkspace };
|
|
377
|
+
export { generateDependencyHash, generateFileHash, generateFolderHash, loadWorkspace };
|