@krizaka/architecture 0.0.0-stage → 0.2.0
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/LICENSE +201 -0
- package/README.md +72 -2
- package/package.json +59 -3
- package/src/c4.mjs +257 -0
- package/src/cli.mjs +44 -0
- package/src/files.mjs +141 -0
- package/src/imports.mjs +47 -0
- package/src/index.d.ts +92 -0
- package/src/index.mjs +3 -0
- package/src/order.mjs +5 -0
- package/src/scan.mjs +115 -0
- package/src/text.mjs +34 -0
- package/src/units.mjs +369 -0
package/src/units.mjs
ADDED
|
@@ -0,0 +1,369 @@
|
|
|
1
|
+
// The units of one repository, read from its manifests with their own parsers (ADR-026: no regex over a structured format).
|
|
2
|
+
// A unit is a package.json of the workspace, a module of the Maven reactor, a crate or a Python project; each carries the org
|
|
3
|
+
// dependencies it declares (`@krizaka/*`, `com.krizaka*`, `krizaka-*` crates, `krizaka*` Python packages) with the version
|
|
4
|
+
// that builds it: the lockfile's for npm, the pin otherwise.
|
|
5
|
+
import { XMLParser } from "fast-xml-parser";
|
|
6
|
+
import { parse as parseToml } from "smol-toml";
|
|
7
|
+
import { parse as parseYaml } from "yaml";
|
|
8
|
+
|
|
9
|
+
import { asc } from "./order.mjs";
|
|
10
|
+
|
|
11
|
+
const NPM_FIELDS = ["dependencies", "devDependencies", "peerDependencies", "optionalDependencies"];
|
|
12
|
+
const CARGO_FIELDS = ["dependencies", "dev-dependencies", "build-dependencies"];
|
|
13
|
+
const SKIPPED_DIRS = new Set([
|
|
14
|
+
"node_modules",
|
|
15
|
+
"target",
|
|
16
|
+
"dist",
|
|
17
|
+
"build",
|
|
18
|
+
".next",
|
|
19
|
+
"fixtures",
|
|
20
|
+
"__fixtures__",
|
|
21
|
+
"vendor",
|
|
22
|
+
]);
|
|
23
|
+
const PLAIN = /^(?:\^|~|>=)?(\d+\.\d+\.\d+)$/;
|
|
24
|
+
const PEP508_NAME = /^[A-Za-z0-9][\w.-]*/;
|
|
25
|
+
const PROPERTY = /^\$\{([\w.-]+)\}$/;
|
|
26
|
+
const NODE_MODULES = "node_modules/";
|
|
27
|
+
const xml = new XMLParser({
|
|
28
|
+
ignoreAttributes: true,
|
|
29
|
+
parseTagValue: false,
|
|
30
|
+
isArray: (name) => name === "module" || name === "dependency",
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
/** Is a package an org package, per ecosystem? Maven ids are `groupId:artifactId`. */
|
|
34
|
+
export const isOrgPackage = {
|
|
35
|
+
npm: (name) => name.startsWith("@krizaka/"),
|
|
36
|
+
maven: (id) => id.startsWith("com.krizaka:") || id.startsWith("com.krizaka."),
|
|
37
|
+
cargo: (name) => name.startsWith("krizaka-"),
|
|
38
|
+
python: (name) => name.toLowerCase().startsWith("krizaka"),
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
/** "0.19.0" from "^0.19.0", "~0.19.0", ">=0.19.0" or "0.19.0"; null for `workspace:`, tags, git or complex ranges. */
|
|
42
|
+
export const baseVersion = (range) => PLAIN.exec(String(range ?? "").trim())?.[1] ?? null;
|
|
43
|
+
|
|
44
|
+
const dirOf = (path) => (path.includes("/") ? path.slice(0, path.lastIndexOf("/")) : ".");
|
|
45
|
+
const joinPath = (dir, rel) => (dir === "." ? rel : `${dir}/${rel}`).replaceAll("/./", "/").replace(/\/$/, "");
|
|
46
|
+
const skipped = (path) => path.split("/").some((part) => SKIPPED_DIRS.has(part));
|
|
47
|
+
const byKey = (key) => (a, b) => asc(a[key] ?? "", b[key] ?? "");
|
|
48
|
+
const manifestsNamed = (files, name) =>
|
|
49
|
+
[...files.keys()].filter((p) => (p === name || p.endsWith(`/${name}`)) && !skipped(p)).sort(asc);
|
|
50
|
+
|
|
51
|
+
function safely(parse, text) {
|
|
52
|
+
try {
|
|
53
|
+
return parse(text);
|
|
54
|
+
} catch {
|
|
55
|
+
return null;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const parseJson = (text) => safely(JSON.parse, text ?? "");
|
|
60
|
+
|
|
61
|
+
/** Does a directory match a workspace glob (`apps/*`, `packages/**`, `deploy/backup`)? */
|
|
62
|
+
export function globMatches(glob, dir) {
|
|
63
|
+
const want = glob.replace(/^\.\//, "").replace(/\/$/, "").split("/");
|
|
64
|
+
const have = dir.split("/");
|
|
65
|
+
const walk = (i, j) => {
|
|
66
|
+
if (i === want.length) return j === have.length;
|
|
67
|
+
if (want[i] === "**") return [...new Array(have.length - j + 1).keys()].some((k) => walk(i + 1, j + k));
|
|
68
|
+
return j < have.length && (want[i] === "*" || want[i] === have[j]) && walk(i + 1, j + 1);
|
|
69
|
+
};
|
|
70
|
+
return walk(0, 0);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function workspaceGlobs(files) {
|
|
74
|
+
const pnpm = safely(parseYaml, files.get("pnpm-workspace.yaml") ?? "");
|
|
75
|
+
const root = parseJson(files.get("package.json"));
|
|
76
|
+
const fromRoot = Array.isArray(root?.workspaces) ? root.workspaces : root?.workspaces?.packages;
|
|
77
|
+
return (pnpm?.packages ?? fromRoot ?? []).filter((g) => typeof g === "string");
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** Versions by unit path and package, filled once per pair. */
|
|
81
|
+
class Locked {
|
|
82
|
+
#map = new Map();
|
|
83
|
+
put(unit, name, version) {
|
|
84
|
+
if (!isOrgPackage.npm(name) || !baseVersion(version)) return;
|
|
85
|
+
const byName = this.#map.get(unit) ?? new Map();
|
|
86
|
+
if (!byName.has(name)) byName.set(name, version);
|
|
87
|
+
this.#map.set(unit, byName);
|
|
88
|
+
}
|
|
89
|
+
/** A unit's lookup: its own version, else the root's (a package hoisted at the root builds every workspace). */
|
|
90
|
+
of(unit) {
|
|
91
|
+
const own = this.#map.get(unit);
|
|
92
|
+
const root = this.#map.get(".");
|
|
93
|
+
return { get: (name) => own?.get(name) ?? root?.get(name) };
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function lockedByPnpm(text, locked) {
|
|
98
|
+
const importers = safely(parseYaml, text ?? "")?.importers ?? {};
|
|
99
|
+
for (const [unit, importer] of Object.entries(importers)) {
|
|
100
|
+
const entries = NPM_FIELDS.flatMap((field) => Object.entries(importer?.[field] ?? {}));
|
|
101
|
+
for (const [name, entry] of entries) {
|
|
102
|
+
const version = typeof entry === "string" ? entry : entry?.version;
|
|
103
|
+
locked.put(unit, name, String(version ?? "").split("(")[0]);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function lockedByNpm(text, locked) {
|
|
109
|
+
for (const [where, entry] of Object.entries(parseJson(text)?.packages ?? {})) {
|
|
110
|
+
const at = where.lastIndexOf(NODE_MODULES);
|
|
111
|
+
if (at < 0) continue;
|
|
112
|
+
const unit = at === 0 ? "." : where.slice(0, at - 1);
|
|
113
|
+
if (!unit.includes(NODE_MODULES.slice(0, -1)))
|
|
114
|
+
locked.put(unit, where.slice(at + NODE_MODULES.length), entry?.version);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** The version the npm lockfile resolved, per unit: `pnpm-lock.yaml` importers, then `package-lock.json` packages. */
|
|
119
|
+
function lockedVersions(files) {
|
|
120
|
+
const locked = new Locked();
|
|
121
|
+
lockedByPnpm(files.get("pnpm-lock.yaml"), locked);
|
|
122
|
+
lockedByNpm(files.get("package-lock.json"), locked);
|
|
123
|
+
return locked;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function npmDeps(pkg, locked) {
|
|
127
|
+
const deps = new Map();
|
|
128
|
+
for (const field of NPM_FIELDS) {
|
|
129
|
+
for (const [name, range] of Object.entries(pkg?.[field] ?? {})) {
|
|
130
|
+
if (!isOrgPackage.npm(name) || deps.has(name)) continue;
|
|
131
|
+
const resolved = locked.get(name) ?? null;
|
|
132
|
+
deps.set(name, {
|
|
133
|
+
ecosystem: "npm",
|
|
134
|
+
name,
|
|
135
|
+
range: String(range),
|
|
136
|
+
have: resolved ?? baseVersion(range),
|
|
137
|
+
from: resolved ? "lockfile" : "range",
|
|
138
|
+
dev: field === "devDependencies",
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
return [...deps.values()].sort(byKey("name"));
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** The npm units: the root package.json and every package.json the workspace globs name. */
|
|
146
|
+
function npmUnits(files) {
|
|
147
|
+
const globs = workspaceGlobs(files);
|
|
148
|
+
const include = globs.filter((g) => !g.startsWith("!"));
|
|
149
|
+
const exclude = globs.filter((g) => g.startsWith("!")).map((g) => g.slice(1));
|
|
150
|
+
const member = (dir) =>
|
|
151
|
+
dir === "." || (include.some((g) => globMatches(g, dir)) && !exclude.some((g) => globMatches(g, dir)));
|
|
152
|
+
const locked = lockedVersions(files);
|
|
153
|
+
const units = [];
|
|
154
|
+
for (const path of manifestsNamed(files, "package.json")) {
|
|
155
|
+
const dir = dirOf(path);
|
|
156
|
+
const pkg = member(dir) ? parseJson(files.get(path)) : null;
|
|
157
|
+
if (!pkg) continue;
|
|
158
|
+
const typed = files.has(joinPath(dir, "tsconfig.json")) || files.has("tsconfig.json");
|
|
159
|
+
units.push({
|
|
160
|
+
path: dir,
|
|
161
|
+
manifest: path,
|
|
162
|
+
ecosystem: "npm",
|
|
163
|
+
language: typed ? "typescript" : "javascript",
|
|
164
|
+
name: pkg.name ?? null,
|
|
165
|
+
version: pkg.version ?? null,
|
|
166
|
+
published: Boolean(pkg.name) && pkg.private !== true && isOrgPackage.npm(pkg.name),
|
|
167
|
+
dependsOn: npmDeps(pkg, locked.of(dir)),
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
return units;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
const text = (value) => (value === undefined || value === null ? null : String(value).trim());
|
|
174
|
+
const listOf = (value) => (Array.isArray(value) ? value : [value].filter(Boolean));
|
|
175
|
+
|
|
176
|
+
function resolveProperty(value, props) {
|
|
177
|
+
const ref = PROPERTY.exec(value ?? "");
|
|
178
|
+
return ref ? text(props[ref[1]]) : text(value);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/** The artifactId of every `<dependency>` anywhere in a parsed POM (dependencies, management, plugins, profiles). */
|
|
182
|
+
function everyDependency(node, out = []) {
|
|
183
|
+
if (Array.isArray(node)) node.forEach((child) => everyDependency(child, out));
|
|
184
|
+
else if (node && typeof node === "object") {
|
|
185
|
+
for (const [key, child] of Object.entries(node)) {
|
|
186
|
+
if (key === "dependency") listOf(child).forEach((d) => d?.artifactId && out.push(text(d.artifactId)));
|
|
187
|
+
everyDependency(child, out);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
return out;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
function mavenDep(id, version, extra = {}) {
|
|
194
|
+
return {
|
|
195
|
+
ecosystem: "maven",
|
|
196
|
+
name: id,
|
|
197
|
+
range: version ?? "managed",
|
|
198
|
+
have: baseVersion(version),
|
|
199
|
+
from: "range",
|
|
200
|
+
dev: false,
|
|
201
|
+
...extra,
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
function pomDeps(project, parent) {
|
|
206
|
+
const props = project.properties ?? {};
|
|
207
|
+
// A BOM's managed list is not a dependency; an imported BOM is.
|
|
208
|
+
const imported = listOf(project.dependencyManagement?.dependencies?.dependency).filter(
|
|
209
|
+
(d) => text(d.scope) === "import",
|
|
210
|
+
);
|
|
211
|
+
const deps = new Map();
|
|
212
|
+
for (const dep of [...listOf(project.dependencies?.dependency), ...imported]) {
|
|
213
|
+
const id = `${resolveProperty(text(dep.groupId), props)}:${text(dep.artifactId)}`;
|
|
214
|
+
if (isOrgPackage.maven(id) && !deps.has(id))
|
|
215
|
+
deps.set(id, mavenDep(id, resolveProperty(text(dep.version), props), { dev: text(dep.scope) === "test" }));
|
|
216
|
+
}
|
|
217
|
+
if (parent && isOrgPackage.maven(parent.id))
|
|
218
|
+
deps.set(parent.id, mavenDep(parent.id, parent.version, { parent: true }));
|
|
219
|
+
return [...deps.values()].sort(byKey("name"));
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* One pom.xml: its coordinates (`id` is `groupId:artifactId`), description, packaging, modules, the org dependencies (an
|
|
224
|
+
* imported BOM and the org parent included) and the artifactId of every dependency it names. null when it does not parse.
|
|
225
|
+
*/
|
|
226
|
+
export function readPom(body) {
|
|
227
|
+
const project = safely((t) => xml.parse(t), body ?? "")?.project;
|
|
228
|
+
if (!project || typeof project !== "object") return null;
|
|
229
|
+
const parent = project.parent
|
|
230
|
+
? {
|
|
231
|
+
id: `${text(project.parent.groupId)}:${text(project.parent.artifactId)}`,
|
|
232
|
+
version: text(project.parent.version),
|
|
233
|
+
}
|
|
234
|
+
: null;
|
|
235
|
+
const groupId = text(project.groupId) ?? text(project.parent?.groupId);
|
|
236
|
+
return {
|
|
237
|
+
id: `${groupId}:${text(project.artifactId)}`,
|
|
238
|
+
artifactId: text(project.artifactId),
|
|
239
|
+
description: text(project.description) ?? "",
|
|
240
|
+
version: text(project.version) ?? parent?.version ?? null,
|
|
241
|
+
packaging: text(project.packaging) ?? "jar",
|
|
242
|
+
modules: listOf(project.modules?.module).map(text).filter(Boolean),
|
|
243
|
+
dependsOn: pomDeps(project, parent),
|
|
244
|
+
dependencyIds: everyDependency(project),
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/** The Maven units: the root pom.xml and its modules, recursively (a module whose pom is not in the files is left out). */
|
|
249
|
+
function mavenUnits(files) {
|
|
250
|
+
const units = [];
|
|
251
|
+
const seen = new Set();
|
|
252
|
+
const visit = (dir) => {
|
|
253
|
+
const manifest = joinPath(dir, "pom.xml");
|
|
254
|
+
if (seen.has(manifest) || !files.has(manifest)) return;
|
|
255
|
+
seen.add(manifest);
|
|
256
|
+
const pom = readPom(files.get(manifest));
|
|
257
|
+
if (!pom) return;
|
|
258
|
+
units.push({
|
|
259
|
+
path: dir,
|
|
260
|
+
manifest,
|
|
261
|
+
ecosystem: "maven",
|
|
262
|
+
language: "java",
|
|
263
|
+
name: pom.id,
|
|
264
|
+
version: pom.version,
|
|
265
|
+
packaging: pom.packaging,
|
|
266
|
+
published: isOrgPackage.maven(pom.id),
|
|
267
|
+
dependsOn: pom.dependsOn,
|
|
268
|
+
modules: pom.modules.map((m) => joinPath(dir, m)),
|
|
269
|
+
});
|
|
270
|
+
pom.modules.forEach((m) => visit(joinPath(dir, m)));
|
|
271
|
+
};
|
|
272
|
+
visit(".");
|
|
273
|
+
return units;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
function cargoRange(spec) {
|
|
277
|
+
if (typeof spec === "string") return spec;
|
|
278
|
+
return String(spec?.version ?? spec?.tag ?? spec?.rev ?? "git");
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
function cargoDeps(manifest) {
|
|
282
|
+
const deps = new Map();
|
|
283
|
+
for (const field of CARGO_FIELDS) {
|
|
284
|
+
for (const [name, spec] of Object.entries(manifest?.[field] ?? {})) {
|
|
285
|
+
if (!isOrgPackage.cargo(name) || deps.has(name)) continue;
|
|
286
|
+
const range = cargoRange(spec);
|
|
287
|
+
const dev = field === "dev-dependencies";
|
|
288
|
+
deps.set(name, {
|
|
289
|
+
ecosystem: "cargo",
|
|
290
|
+
name,
|
|
291
|
+
range,
|
|
292
|
+
have: baseVersion(range.replace(/^v/, "")),
|
|
293
|
+
from: "range",
|
|
294
|
+
dev,
|
|
295
|
+
});
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
return [...deps.values()].sort(byKey("name"));
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/** The crates: every Cargo.toml with a [package] (a bare [workspace] root is not a unit). */
|
|
302
|
+
function cargoUnits(files) {
|
|
303
|
+
const units = [];
|
|
304
|
+
for (const path of manifestsNamed(files, "Cargo.toml")) {
|
|
305
|
+
const pkg = safely(parseToml, files.get(path) ?? "")?.package;
|
|
306
|
+
if (!pkg?.name) continue;
|
|
307
|
+
units.push({
|
|
308
|
+
path: dirOf(path),
|
|
309
|
+
manifest: path,
|
|
310
|
+
ecosystem: "cargo",
|
|
311
|
+
language: "rust",
|
|
312
|
+
name: pkg.name,
|
|
313
|
+
version: typeof pkg.version === "string" ? pkg.version : null,
|
|
314
|
+
published: isOrgPackage.cargo(pkg.name) && pkg.publish !== false,
|
|
315
|
+
dependsOn: cargoDeps(safely(parseToml, files.get(path))),
|
|
316
|
+
});
|
|
317
|
+
}
|
|
318
|
+
return units;
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
function pythonDeps(lines) {
|
|
322
|
+
const deps = new Map();
|
|
323
|
+
for (const line of lines.map((l) => String(l).trim())) {
|
|
324
|
+
const name = PEP508_NAME.exec(line)?.[0];
|
|
325
|
+
if (!name || !isOrgPackage.python(name) || deps.has(name)) continue;
|
|
326
|
+
deps.set(name, {
|
|
327
|
+
ecosystem: "python",
|
|
328
|
+
name,
|
|
329
|
+
range: line.slice(name.length).trim(),
|
|
330
|
+
have: null,
|
|
331
|
+
from: "range",
|
|
332
|
+
dev: false,
|
|
333
|
+
});
|
|
334
|
+
}
|
|
335
|
+
return [...deps.values()].sort(byKey("name"));
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
const pythonUnit = (path, name, version, dependsOn) => ({
|
|
339
|
+
path: dirOf(path),
|
|
340
|
+
manifest: path,
|
|
341
|
+
ecosystem: "python",
|
|
342
|
+
language: "python",
|
|
343
|
+
name,
|
|
344
|
+
version,
|
|
345
|
+
published: Boolean(name) && isOrgPackage.python(name),
|
|
346
|
+
dependsOn,
|
|
347
|
+
});
|
|
348
|
+
|
|
349
|
+
/** The Python projects: every pyproject.toml with a [project], else a requirements.txt in a folder of its own. */
|
|
350
|
+
function pythonUnits(files) {
|
|
351
|
+
const units = [];
|
|
352
|
+
for (const path of manifestsNamed(files, "pyproject.toml")) {
|
|
353
|
+
const project = safely(parseToml, files.get(path) ?? "")?.project;
|
|
354
|
+
if (project?.name)
|
|
355
|
+
units.push(pythonUnit(path, project.name, project.version ?? null, pythonDeps(project.dependencies ?? [])));
|
|
356
|
+
}
|
|
357
|
+
const dirs = new Set(units.map((u) => u.path));
|
|
358
|
+
for (const path of manifestsNamed(files, "requirements.txt").filter((p) => !dirs.has(dirOf(p)))) {
|
|
359
|
+
units.push(pythonUnit(path, null, null, pythonDeps((files.get(path) ?? "").split("\n"))));
|
|
360
|
+
}
|
|
361
|
+
return units;
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
/** Every unit of a repository, sorted by path then ecosystem: the same files give the same list. */
|
|
365
|
+
export function unitsOf(files) {
|
|
366
|
+
return [...npmUnits(files), ...mavenUnits(files), ...cargoUnits(files), ...pythonUnits(files)].sort(
|
|
367
|
+
(a, b) => asc(a.path, b.path) || asc(a.ecosystem, b.ecosystem),
|
|
368
|
+
);
|
|
369
|
+
}
|