@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/README.MD +3 -4
- package/dist/index.cjs +366 -283
- package/dist/index.d.cts +17 -14
- package/dist/index.d.ts +17 -14
- package/dist/index.js +361 -254
- package/package.json +10 -10
package/dist/index.js
CHANGED
|
@@ -1,274 +1,381 @@
|
|
|
1
|
-
|
|
2
|
-
import { readdir } from "fs/promises";
|
|
3
|
-
|
|
4
|
-
// 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";
|
|
5
4
|
import { createHash } from "crypto";
|
|
6
|
-
import { readFile } from "fs/promises";
|
|
7
|
-
import { builtinModules } from "module";
|
|
8
|
-
import { relative } from "path";
|
|
9
|
-
|
|
10
|
-
// src/import.ts
|
|
11
5
|
import { parse } from "@swc/core";
|
|
12
|
-
import { dirname, resolve } from "path";
|
|
13
6
|
import { simple } from "swc-walk";
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
ImportDeclaration(node) {
|
|
22
|
-
importing.add(node.source.value);
|
|
23
|
-
},
|
|
24
|
-
ExportAllDeclaration(node) {
|
|
25
|
-
importing.add(node.source.value);
|
|
26
|
-
},
|
|
27
|
-
ExportNamedDeclaration(node) {
|
|
28
|
-
if (node.source) {
|
|
29
|
-
importing.add(node.source.value);
|
|
30
|
-
}
|
|
31
|
-
},
|
|
32
|
-
CallExpression(node) {
|
|
33
|
-
if (node.callee.type === "Import") {
|
|
34
|
-
const first = node.arguments.at(0);
|
|
35
|
-
if (first && first.expression.type === "StringLiteral") {
|
|
36
|
-
importing.add(first.expression.value);
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
});
|
|
41
|
-
} catch (_) {
|
|
42
|
-
return [];
|
|
43
|
-
}
|
|
44
|
-
return [...importing].map((importee) => {
|
|
45
|
-
if (importee.startsWith(".")) {
|
|
46
|
-
return resolve(dirname(file), importee);
|
|
47
|
-
}
|
|
48
|
-
const parts = importee.split("/");
|
|
49
|
-
if (parts.length > 2) {
|
|
50
|
-
return parts.slice(0, 2).join("/");
|
|
51
|
-
}
|
|
52
|
-
return importee;
|
|
53
|
-
});
|
|
7
|
+
import { BaseVisitor } from "swc-walk/baseVisitor";
|
|
8
|
+
import { parse as parse$1 } from "yaml";
|
|
9
|
+
//#region src/import.ts
|
|
10
|
+
var PatchedBaseVisitor = class extends BaseVisitor {
|
|
11
|
+
FunctionBody(node, state, callback) {
|
|
12
|
+
for (const statement of node.stmts) callback(statement, state);
|
|
13
|
+
}
|
|
54
14
|
};
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
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
|
+
};
|
|
71
31
|
};
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
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
|
+
});
|
|
84
70
|
};
|
|
85
|
-
|
|
86
|
-
|
|
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(", ")}`);
|
|
87
80
|
};
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
81
|
+
const alternateExtensions = {
|
|
82
|
+
".js": [".ts", ".tsx"],
|
|
83
|
+
".mjs": [".mts"],
|
|
84
|
+
".cjs": [".cts"],
|
|
85
|
+
".jsx": [".tsx"]
|
|
93
86
|
};
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
const ext = file.split(".").pop();
|
|
109
|
-
const hash = createHash("sha1").update(code).digest();
|
|
110
|
-
hashes.set(relFile, hash);
|
|
111
|
-
if (!ext || !allowedExtensions.includes(ext)) {
|
|
112
|
-
return;
|
|
113
|
-
}
|
|
114
|
-
const deps = await findImports(file, code);
|
|
115
|
-
for (const dep of deps) {
|
|
116
|
-
await generateRecursiveFileHashes(workspace, dep, file, allowedExtensions, hashes);
|
|
117
|
-
}
|
|
118
|
-
return;
|
|
119
|
-
}
|
|
120
|
-
const module = getPackageName(file);
|
|
121
|
-
if (hashes.has(module)) {
|
|
122
|
-
return;
|
|
123
|
-
}
|
|
124
|
-
const dependency = findDependency(workspace, module, sourceFile);
|
|
125
|
-
if (dependency) {
|
|
126
|
-
if (dependency.type === "package") {
|
|
127
|
-
hashes.set(module, Buffer.from(`${module}:${dependency.version}`, "utf8"));
|
|
128
|
-
} else {
|
|
129
|
-
const localPackage = workspace.packages[module];
|
|
130
|
-
if (!localPackage) {
|
|
131
|
-
throw new Error(`Can't find the local workspace package for: ${file}`);
|
|
132
|
-
}
|
|
133
|
-
if (!localPackage.main) {
|
|
134
|
-
throw new Error(`Workspace package doesn't have a main entry: ${file}`);
|
|
135
|
-
}
|
|
136
|
-
await generateRecursiveFileHashes(
|
|
137
|
-
workspace,
|
|
138
|
-
localPackage.main,
|
|
139
|
-
localPackage.main,
|
|
140
|
-
allowedExtensions,
|
|
141
|
-
hashes
|
|
142
|
-
);
|
|
143
|
-
}
|
|
144
|
-
return;
|
|
145
|
-
}
|
|
146
|
-
if (builtinModules.includes(module.replace(/^node\:/, ""))) {
|
|
147
|
-
return;
|
|
148
|
-
}
|
|
149
|
-
throw new Error(`Can't find the dependency version for: ${file} inside the source: ${sourceFile}`);
|
|
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;
|
|
150
101
|
};
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
return createHash("sha1").update(merge).digest("hex");
|
|
102
|
+
const isLocalCodeFile = (file) => {
|
|
103
|
+
return file.startsWith("/") || file.startsWith(".");
|
|
154
104
|
};
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
if (parts.length >= 2) {
|
|
159
|
-
return `${parts[0]}/${parts[1]}`;
|
|
160
|
-
}
|
|
161
|
-
} else if (parts.length >= 1) {
|
|
162
|
-
return parts[0];
|
|
163
|
-
}
|
|
164
|
-
throw new Error(`Malformed importee: ${importee}`);
|
|
105
|
+
const toAbsolute = (file) => {
|
|
106
|
+
if (isAbsolute(file)) return file;
|
|
107
|
+
return join(process.cwd(), file);
|
|
165
108
|
};
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
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}`);
|
|
172
144
|
};
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
var pnpm = async (search) => {
|
|
182
|
-
const [cwd, lockFile] = await findLockFile(search);
|
|
183
|
-
const data = parse2(lockFile);
|
|
184
|
-
const packages = {};
|
|
185
|
-
await Promise.all(
|
|
186
|
-
Object.entries(data.importers).map(async ([path, importee]) => {
|
|
187
|
-
const deps = { ...importee.devDependencies, ...importee.dependencies };
|
|
188
|
-
const dependencies = {};
|
|
189
|
-
const packageJson = await readFile2(join2(cwd, path, "package.json"), "utf8");
|
|
190
|
-
const packageData = JSON.parse(packageJson);
|
|
191
|
-
for (const [name, entry2] of Object.entries(deps)) {
|
|
192
|
-
if (entry2.version.startsWith("link:")) {
|
|
193
|
-
dependencies[name] = {
|
|
194
|
-
type: "workspace",
|
|
195
|
-
link: join2(cwd, path, entry2.version.substring(5))
|
|
196
|
-
};
|
|
197
|
-
} else {
|
|
198
|
-
dependencies[name] = {
|
|
199
|
-
type: "package",
|
|
200
|
-
version: entry2.version
|
|
201
|
-
};
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
const entry = packageData.module ?? packageData.main;
|
|
205
|
-
packages[packageData.name] = {
|
|
206
|
-
name: packageData.name,
|
|
207
|
-
path: join2(cwd, path),
|
|
208
|
-
main: entry ? join2(cwd, path, entry) : void 0,
|
|
209
|
-
dependencies
|
|
210
|
-
};
|
|
211
|
-
})
|
|
212
|
-
);
|
|
213
|
-
return {
|
|
214
|
-
cwd,
|
|
215
|
-
packages
|
|
216
|
-
};
|
|
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");
|
|
217
153
|
};
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
if (exists) {
|
|
225
|
-
return [path, await readFile2(file, "utf8")];
|
|
226
|
-
}
|
|
227
|
-
return findLockFile(normalize(join2(path, "..")), level - 1);
|
|
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}`);
|
|
228
160
|
};
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
return true;
|
|
234
|
-
}
|
|
235
|
-
} catch (error) {
|
|
236
|
-
}
|
|
237
|
-
return false;
|
|
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];
|
|
238
165
|
};
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
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
|
+
};
|
|
247
217
|
};
|
|
248
|
-
|
|
249
|
-
|
|
218
|
+
const parseJsonc = (text) => {
|
|
219
|
+
try {
|
|
220
|
+
return JSON.parse(text);
|
|
221
|
+
} catch (_) {
|
|
222
|
+
return JSON.parse(stripJsoncSyntax(text));
|
|
223
|
+
}
|
|
250
224
|
};
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
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;
|
|
257
268
|
};
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
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
|
+
};
|
|
269
295
|
};
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
296
|
+
//#endregion
|
|
297
|
+
//#region src/package-manager/util.ts
|
|
298
|
+
const parsers = {
|
|
299
|
+
"pnpm-lock.yaml": pnpm,
|
|
300
|
+
"bun.lock": bun
|
|
274
301
|
};
|
|
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);
|
|
315
|
+
};
|
|
316
|
+
const fileExist = async (file) => {
|
|
317
|
+
try {
|
|
318
|
+
if ((await lstat(file)).isFile()) return true;
|
|
319
|
+
} catch (error) {}
|
|
320
|
+
return false;
|
|
321
|
+
};
|
|
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;
|
|
336
|
+
};
|
|
337
|
+
//#endregion
|
|
338
|
+
//#region src/index.ts
|
|
339
|
+
const loadWorkspace = async (search) => {
|
|
340
|
+
return loadPackageManager(toAbsolute(search));
|
|
341
|
+
};
|
|
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")]]);
|
|
352
|
+
};
|
|
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);
|
|
362
|
+
};
|
|
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);
|
|
379
|
+
};
|
|
380
|
+
//#endregion
|
|
381
|
+
export { generateFileHash, generateFolderHash, loadWorkspace };
|
package/package.json
CHANGED
|
@@ -1,18 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@awsless/ts-file-cache",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.18",
|
|
4
|
+
"bugs": {
|
|
5
|
+
"url": "https://github.com/awsless/awsless/issues"
|
|
6
|
+
},
|
|
4
7
|
"license": "MIT",
|
|
5
|
-
"type": "module",
|
|
6
8
|
"repository": {
|
|
7
9
|
"type": "git",
|
|
8
10
|
"url": "https://github.com/awsless/awsless.git"
|
|
9
11
|
},
|
|
10
|
-
"bugs": {
|
|
11
|
-
"url": "https://github.com/awsless/awsless/issues"
|
|
12
|
-
},
|
|
13
12
|
"files": [
|
|
14
13
|
"dist"
|
|
15
14
|
],
|
|
15
|
+
"type": "module",
|
|
16
16
|
"main": "./dist/index.cjs",
|
|
17
17
|
"module": "./dist/index.js",
|
|
18
18
|
"types": "./dist/index.d.ts",
|
|
@@ -24,13 +24,13 @@
|
|
|
24
24
|
}
|
|
25
25
|
},
|
|
26
26
|
"dependencies": {
|
|
27
|
-
"@swc/core": "
|
|
28
|
-
"swc-walk": "
|
|
29
|
-
"yaml": "
|
|
27
|
+
"@swc/core": "1.16.1",
|
|
28
|
+
"swc-walk": "3.0.0-rc.0",
|
|
29
|
+
"yaml": "2.9.0"
|
|
30
30
|
},
|
|
31
31
|
"scripts": {
|
|
32
|
-
"test": "pnpm
|
|
33
|
-
"build": "pnpm
|
|
32
|
+
"test": "pnpm vitest run -c ../../vitest.shared.ts",
|
|
33
|
+
"build": "pnpm tsdown --no-fixed-extension src/index.ts --format cjs,esm --dts --clean",
|
|
34
34
|
"prepublish": "if pnpm test; then pnpm build; else exit; fi"
|
|
35
35
|
}
|
|
36
36
|
}
|