@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/files.mjs
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
// The files the generator reads, as one map `path -> text` (repo-relative, "/"), whether they come from a checkout on disk or
|
|
2
|
+
// from somewhere else (a tarball, a fixture): code, env declarations, manifests and lockfiles. Everything after this file works
|
|
3
|
+
// on the map, so the same files give the same view wherever they come from.
|
|
4
|
+
import { readdirSync, readFileSync, statSync } from "node:fs";
|
|
5
|
+
import { join, posix } from "node:path";
|
|
6
|
+
|
|
7
|
+
import { parse as parseYaml } from "yaml";
|
|
8
|
+
|
|
9
|
+
import { asc } from "./order.mjs";
|
|
10
|
+
import { readPom } from "./units.mjs";
|
|
11
|
+
|
|
12
|
+
const SKIP_DIRS = new Set([
|
|
13
|
+
"node_modules",
|
|
14
|
+
".git",
|
|
15
|
+
"dist",
|
|
16
|
+
"build",
|
|
17
|
+
".next",
|
|
18
|
+
".turbo",
|
|
19
|
+
"coverage",
|
|
20
|
+
"out",
|
|
21
|
+
"target",
|
|
22
|
+
".open-next",
|
|
23
|
+
".expo",
|
|
24
|
+
".cache",
|
|
25
|
+
".venv",
|
|
26
|
+
]);
|
|
27
|
+
const CODE = /\.(?:[cm]?[jt]sx?)$/;
|
|
28
|
+
const TEST = /(?:\.(?:test|spec|stories)\.[cm]?[jt]sx?$|(?:^|\/)(?:__tests__|test|tests|e2e|fixtures)\/)/;
|
|
29
|
+
const ENV_FILES = /^(?:env\.(?:[cm]?[jt]s)|\.env\.(?:example|sample|template))$/;
|
|
30
|
+
const MANIFESTS = new Set([
|
|
31
|
+
"package.json",
|
|
32
|
+
"pnpm-workspace.yaml",
|
|
33
|
+
"pnpm-lock.yaml",
|
|
34
|
+
"package-lock.json",
|
|
35
|
+
"tsconfig.json",
|
|
36
|
+
"pom.xml",
|
|
37
|
+
"Cargo.toml",
|
|
38
|
+
"pyproject.toml",
|
|
39
|
+
"requirements.txt",
|
|
40
|
+
]);
|
|
41
|
+
const MAX_BYTES = 1_000_000;
|
|
42
|
+
// A lockfile of a large workspace is bigger than a source file; it is read whole, up to this size.
|
|
43
|
+
const MAX_MANIFEST_BYTES = 16_000_000;
|
|
44
|
+
|
|
45
|
+
const readCapped = (path, cap) => {
|
|
46
|
+
try {
|
|
47
|
+
return statSync(path).size > cap ? "" : readFileSync(path, "utf8");
|
|
48
|
+
} catch {
|
|
49
|
+
return "";
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
const wanted = (name) => MANIFESTS.has(name) || ENV_FILES.test(name) || CODE.test(name);
|
|
54
|
+
|
|
55
|
+
/** The files of a checkout the generator reads (code, env declarations, manifests, lockfiles), build folders left out. */
|
|
56
|
+
export function filesOfCheckout(root, rel = "", out = new Map()) {
|
|
57
|
+
for (const entry of readdirSync(join(root, rel), { withFileTypes: true })) {
|
|
58
|
+
const path = rel ? `${rel}/${entry.name}` : entry.name;
|
|
59
|
+
if (entry.isDirectory()) {
|
|
60
|
+
if (!SKIP_DIRS.has(entry.name)) filesOfCheckout(root, path, out);
|
|
61
|
+
} else if (wanted(entry.name)) {
|
|
62
|
+
out.set(path, readCapped(join(root, path), MANIFESTS.has(entry.name) ? MAX_MANIFEST_BYTES : MAX_BYTES));
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return out;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const skippedPath = (path) => path.split("/").some((part) => SKIP_DIRS.has(part));
|
|
69
|
+
const baseName = (path) => path.slice(path.lastIndexOf("/") + 1);
|
|
70
|
+
|
|
71
|
+
/** The code files, the tests and the env declaration files of a map, sorted; build folders left out. */
|
|
72
|
+
export function classify(files) {
|
|
73
|
+
const out = { code: [], tests: [], env: [] };
|
|
74
|
+
for (const path of [...files.keys()].filter((p) => !skippedPath(p)).sort(asc)) {
|
|
75
|
+
if (ENV_FILES.test(baseName(path))) out.env.push(path);
|
|
76
|
+
else if (CODE.test(path)) (TEST.test(path) ? out.tests : out.code).push(path);
|
|
77
|
+
}
|
|
78
|
+
return out;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export const textOf = (files, path) => files.get(path) ?? "";
|
|
82
|
+
|
|
83
|
+
export const jsonOf = (files, path) => {
|
|
84
|
+
try {
|
|
85
|
+
return JSON.parse(textOf(files, path));
|
|
86
|
+
} catch {
|
|
87
|
+
return undefined;
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
const yamlPackages = (text) => {
|
|
92
|
+
try {
|
|
93
|
+
const packages = parseYaml(text)?.packages;
|
|
94
|
+
return Array.isArray(packages) ? packages.filter((p) => typeof p === "string") : [];
|
|
95
|
+
} catch {
|
|
96
|
+
return [];
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
const hasPackage = (files, dir) => files.has(posix.join(dir, "package.json"));
|
|
101
|
+
|
|
102
|
+
/** The direct sub-folders of `rel` that hold a package.json (build folders left out). */
|
|
103
|
+
const packageSubdirs = (files, rel) => {
|
|
104
|
+
const prefix = `${rel}/`;
|
|
105
|
+
const found = new Set();
|
|
106
|
+
for (const path of files.keys()) {
|
|
107
|
+
if (!path.startsWith(prefix) || !path.endsWith("/package.json")) continue;
|
|
108
|
+
const parts = path.slice(prefix.length).split("/");
|
|
109
|
+
if (parts.length === 2 && !SKIP_DIRS.has(parts[0])) found.add(`${rel}/${parts[0]}`);
|
|
110
|
+
}
|
|
111
|
+
return [...found];
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
const expand = (files, pattern) => {
|
|
115
|
+
const clean = pattern
|
|
116
|
+
.replace(/^\.\//, "")
|
|
117
|
+
.replace(/\/\*\*$/, "/*")
|
|
118
|
+
.replace(/\/$/, "");
|
|
119
|
+
if (clean.includes("*") && !clean.endsWith("/*")) return [];
|
|
120
|
+
const dirs = clean.endsWith("/*") ? packageSubdirs(files, clean.slice(0, -2)) : [clean];
|
|
121
|
+
return dirs.filter((d) => hasPackage(files, d));
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
/** The directories of the workspace packages (npm/yarn `workspaces` and pnpm-workspace.yaml); [] when the repo is not a workspace. */
|
|
125
|
+
export function workspaceDirs(files) {
|
|
126
|
+
const pkg = jsonOf(files, "package.json");
|
|
127
|
+
const declared = Array.isArray(pkg?.workspaces) ? pkg.workspaces : (pkg?.workspaces?.packages ?? []);
|
|
128
|
+
const patterns = [...declared, ...yamlPackages(textOf(files, "pnpm-workspace.yaml"))];
|
|
129
|
+
const excluded = new Set(patterns.filter((p) => p.startsWith("!")).flatMap((p) => expand(files, p.slice(1))));
|
|
130
|
+
return [...new Set(patterns.filter((p) => !p.startsWith("!")).flatMap((p) => expand(files, p)))]
|
|
131
|
+
.filter((d) => !excluded.has(d))
|
|
132
|
+
.sort(asc);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** The Maven modules listed by the root pom (one level), as repo-relative directories that hold a pom.xml. */
|
|
136
|
+
export function mavenModules(files) {
|
|
137
|
+
const pom = readPom(textOf(files, "pom.xml"));
|
|
138
|
+
return [...new Set((pom?.modules ?? []).map((m) => posix.normalize(m)))]
|
|
139
|
+
.filter((d) => files.has(posix.join(d, "pom.xml")))
|
|
140
|
+
.sort(asc);
|
|
141
|
+
}
|
package/src/imports.mjs
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
// What a source file says about its neighbours: the modules it imports and the environment it reads.
|
|
2
|
+
import { posix } from "node:path";
|
|
3
|
+
|
|
4
|
+
import { asc } from "./order.mjs";
|
|
5
|
+
|
|
6
|
+
const SPECIFIERS = [
|
|
7
|
+
/(?:import|export)\s[^'"`;]*?from\s*['"]([^'"]+)['"]/g,
|
|
8
|
+
/import\s*['"]([^'"]+)['"]/g,
|
|
9
|
+
/(?:import|require)\(\s*['"]([^'"]+)['"]\s*\)/g,
|
|
10
|
+
];
|
|
11
|
+
const CONTEXTS = "APP|DATABASE|AUTH|MEDIA|MAIL|PUSH|PAYMENTS|REALTIME|ABUSE|OBSERVABILITY|AI|OPERATOR";
|
|
12
|
+
const ENV_NAME = new RegExp(String.raw`\b((?:NEXT_PUBLIC_)?(?:${CONTEXTS})_[A-Z0-9_]*[A-Z0-9])\b`, "g");
|
|
13
|
+
const ENV_ONLY = new RegExp(`^(?:NEXT_PUBLIC_)?(?:${CONTEXTS})_[A-Z0-9_]*[A-Z0-9]$`);
|
|
14
|
+
const PROCESS_ENV = /(?:process\.env|import\.meta\.env)\.([A-Z][A-Z0-9_]+)/g;
|
|
15
|
+
|
|
16
|
+
/** The module specifiers a source names, in source order. */
|
|
17
|
+
export const specifiersOf = (source) =>
|
|
18
|
+
SPECIFIERS.flatMap((re) => [...source.matchAll(re)])
|
|
19
|
+
.sort((a, b) => a.index - b.index)
|
|
20
|
+
.map((m) => m[1]);
|
|
21
|
+
|
|
22
|
+
/** `@scope/name/sub` -> `@scope/name`; `name/sub` -> `name`; a relative, alias or built-in path -> undefined. */
|
|
23
|
+
export function packageOf(specifier) {
|
|
24
|
+
if (/^(?:[./]|@\/|~\/|node:)/.test(specifier)) return undefined;
|
|
25
|
+
const parts = specifier.split("/");
|
|
26
|
+
return specifier.startsWith("@") ? parts.slice(0, 2).join("/") : parts[0];
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** The repo path a relative (`./x`, `../x`) or root-alias (`@/x`, `~/x`) specifier points to, extension-less. */
|
|
30
|
+
export function localTarget(specifier, fromFile, containerDir) {
|
|
31
|
+
if (specifier.startsWith(".")) return posix.normalize(posix.join(posix.dirname(fromFile), specifier));
|
|
32
|
+
if (/^[@~]\//.test(specifier)) return posix.join(containerDir === "." ? "" : containerDir, specifier.slice(2));
|
|
33
|
+
return undefined;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const strip = (names) => names.map((n) => n.replace(/^NEXT_PUBLIC_/, ""));
|
|
37
|
+
|
|
38
|
+
/** The env names of the declared contexts: every one a declaration file lists, only the ones a source file reads. */
|
|
39
|
+
export function envNamesOf(source, { declaration }) {
|
|
40
|
+
const found = declaration
|
|
41
|
+
? [...source.matchAll(ENV_NAME)].map((m) => m[1])
|
|
42
|
+
: [...source.matchAll(PROCESS_ENV)].map((m) => m[1]).filter((n) => ENV_ONLY.test(n));
|
|
43
|
+
return [...new Set(strip(found))].sort(asc);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** The context of an env name (`MAIL_PROVIDER` -> `MAIL`). */
|
|
47
|
+
export const contextOf = (name) => name.split("_")[0];
|
package/src/index.d.ts
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
export type C4LevelName = "context" | "containers" | "components";
|
|
2
|
+
|
|
3
|
+
export type Ecosystem = "npm" | "maven" | "cargo" | "python";
|
|
4
|
+
export type Language = "typescript" | "javascript" | "java" | "rust" | "python";
|
|
5
|
+
|
|
6
|
+
/** An org dependency of a unit: `@krizaka/*`, `com.krizaka*` (`groupId:artifactId`), a `krizaka-*` crate, a `krizaka*` Python package. */
|
|
7
|
+
export interface OrgDependency {
|
|
8
|
+
ecosystem: Ecosystem;
|
|
9
|
+
name: string;
|
|
10
|
+
/** What the manifest says: a range, a version, `managed` (Maven, from a BOM), a git tag or rev. */
|
|
11
|
+
range: string;
|
|
12
|
+
/** The version that builds the unit: the lockfile's for npm, else the pin's plain version; null when neither says one. */
|
|
13
|
+
have: string | null;
|
|
14
|
+
from: "lockfile" | "range";
|
|
15
|
+
/** A development (or test-scope) dependency. */
|
|
16
|
+
dev: boolean;
|
|
17
|
+
/** Maven: the dependency is the unit's parent POM. */
|
|
18
|
+
parent?: boolean;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** A unit of a repository: a package.json of the workspace, a module of the Maven reactor, a crate or a Python project. */
|
|
22
|
+
export interface Unit {
|
|
23
|
+
/** Repo-relative directory, `.` for the root. */
|
|
24
|
+
path: string;
|
|
25
|
+
manifest: string;
|
|
26
|
+
ecosystem: Ecosystem;
|
|
27
|
+
language: Language;
|
|
28
|
+
/** The package name (`groupId:artifactId` for Maven); null for a requirements.txt alone. */
|
|
29
|
+
name: string | null;
|
|
30
|
+
version: string | null;
|
|
31
|
+
/** An org package others can depend on (not private, in the org's namespace). */
|
|
32
|
+
published: boolean;
|
|
33
|
+
/** Maven only. */
|
|
34
|
+
packaging?: string;
|
|
35
|
+
/** Maven only: the directories of its modules. */
|
|
36
|
+
modules?: string[];
|
|
37
|
+
dependsOn: OrgDependency[];
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export interface C4Node {
|
|
41
|
+
id: string;
|
|
42
|
+
level: "system" | "container" | "component" | "external";
|
|
43
|
+
kind: "system" | "app" | "library" | "module" | "component" | "external";
|
|
44
|
+
name: string;
|
|
45
|
+
description: string;
|
|
46
|
+
technology?: string;
|
|
47
|
+
/** Repo-relative files behind the node (capped at 200; `fileCount` is the true number). */
|
|
48
|
+
files: string[];
|
|
49
|
+
fileCount: number;
|
|
50
|
+
/** A container's unit, when its manifest says one: */
|
|
51
|
+
ecosystem?: Ecosystem;
|
|
52
|
+
language?: Language;
|
|
53
|
+
version?: string | null;
|
|
54
|
+
published?: boolean;
|
|
55
|
+
dependsOn?: OrgDependency[];
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export interface C4Edge {
|
|
59
|
+
from: string;
|
|
60
|
+
to: string;
|
|
61
|
+
/** `uses` (container depends on / imports another), `imports` (component to component), `reads` (env of an external service). */
|
|
62
|
+
label: "uses" | "imports" | "reads";
|
|
63
|
+
count: number;
|
|
64
|
+
files: string[];
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export interface C4Level {
|
|
68
|
+
level: C4LevelName;
|
|
69
|
+
nodes: C4Node[];
|
|
70
|
+
edges: C4Edge[];
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export interface C4 {
|
|
74
|
+
schema: "krizaka.c4/1";
|
|
75
|
+
repo: string;
|
|
76
|
+
levels: Record<C4LevelName, C4Level>;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export const SCHEMA: "krizaka.c4/1";
|
|
80
|
+
/** The C4 view of a checkout on disk. */
|
|
81
|
+
export function buildC4(repoPath: string): C4;
|
|
82
|
+
/** The C4 view of a repository's files (`path -> text`, repo-relative): the same output as `buildC4` for the same files. */
|
|
83
|
+
export function buildC4FromFiles(files: Map<string, string>, options?: { name?: string }): C4;
|
|
84
|
+
export function toText(c4: C4): string;
|
|
85
|
+
/** Every unit of a repository's files, sorted by path then ecosystem. */
|
|
86
|
+
export function unitsOf(files: Map<string, string>): Unit[];
|
|
87
|
+
/** Is a package an org package, per ecosystem? */
|
|
88
|
+
export const isOrgPackage: Record<Ecosystem, (name: string) => boolean>;
|
|
89
|
+
/** "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. */
|
|
90
|
+
export function baseVersion(range: string | null | undefined): string | null;
|
|
91
|
+
/** Does a directory match a workspace glob (`apps/*`, `packages/**`, `deploy/backup`)? */
|
|
92
|
+
export function globMatches(glob: string, dir: string): boolean;
|
package/src/index.mjs
ADDED
package/src/order.mjs
ADDED
package/src/scan.mjs
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
// The facts of a repository's files: its containers (workspace packages or Maven modules) with the unit each one is, its source
|
|
2
|
+
// files with what each imports and reads.
|
|
3
|
+
import { posix } from "node:path";
|
|
4
|
+
|
|
5
|
+
import { classify, jsonOf, mavenModules, textOf, workspaceDirs } from "./files.mjs";
|
|
6
|
+
import { envNamesOf, localTarget, packageOf, specifiersOf } from "./imports.mjs";
|
|
7
|
+
import { readPom, unitsOf } from "./units.mjs";
|
|
8
|
+
|
|
9
|
+
const FRAMEWORKS = [
|
|
10
|
+
["next", "Next.js", true],
|
|
11
|
+
["expo", "Expo", true],
|
|
12
|
+
["react-native", "React Native", true],
|
|
13
|
+
["vite", "Vite", true],
|
|
14
|
+
["express", "Express", true],
|
|
15
|
+
["fastify", "Fastify", true],
|
|
16
|
+
["hono", "Hono", true],
|
|
17
|
+
];
|
|
18
|
+
|
|
19
|
+
const npmContainer = (files, dir, repo) => {
|
|
20
|
+
const pkg = jsonOf(files, posix.join(dir, "package.json")) ?? {};
|
|
21
|
+
const deps = { ...pkg.dependencies, ...pkg.devDependencies, ...pkg.peerDependencies };
|
|
22
|
+
const framework = FRAMEWORKS.find(([name]) => name in (pkg.dependencies ?? {}));
|
|
23
|
+
const app = /^apps?\//.test(dir) || Boolean(framework?.[2]) || Boolean(pkg.private && pkg.scripts?.start);
|
|
24
|
+
return {
|
|
25
|
+
dir,
|
|
26
|
+
name: pkg.name ?? (dir === "." ? repo : posix.basename(dir)),
|
|
27
|
+
description: pkg.description ?? "",
|
|
28
|
+
kind: app ? "app" : "library",
|
|
29
|
+
technology: framework?.[1] ?? "TypeScript",
|
|
30
|
+
ecosystem: "npm",
|
|
31
|
+
deps: Object.keys(deps),
|
|
32
|
+
manifest: posix.join(dir, "package.json"),
|
|
33
|
+
};
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
const mavenContainer = (files, dir) => {
|
|
37
|
+
const pom = readPom(textOf(files, posix.join(dir, "pom.xml")));
|
|
38
|
+
return {
|
|
39
|
+
dir,
|
|
40
|
+
name: pom?.artifactId ?? posix.basename(dir),
|
|
41
|
+
description: pom?.description ?? "",
|
|
42
|
+
kind: "module",
|
|
43
|
+
technology: "Maven",
|
|
44
|
+
ecosystem: "maven",
|
|
45
|
+
deps: pom?.dependencyIds ?? [],
|
|
46
|
+
manifest: posix.join(dir, "pom.xml"),
|
|
47
|
+
};
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
/** The containers of a repository: workspace packages, else Maven modules, else the root package itself. */
|
|
51
|
+
export function containersOf(files, repo) {
|
|
52
|
+
const packages = workspaceDirs(files);
|
|
53
|
+
if (packages.length) return packages.map((d) => npmContainer(files, d, repo));
|
|
54
|
+
const modules = mavenModules(files);
|
|
55
|
+
if (modules.length) return modules.map((d) => mavenContainer(files, d));
|
|
56
|
+
return jsonOf(files, "package.json") ? [npmContainer(files, ".", repo)] : [];
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const insideOf = (dir, path) => (dir === "." ? path : path.slice(dir.length + 1));
|
|
60
|
+
|
|
61
|
+
/** The container a repo path belongs to: the deepest directory that contains it. */
|
|
62
|
+
export const containerOfPath = (containers, path) =>
|
|
63
|
+
[...containers]
|
|
64
|
+
.sort((a, b) => b.dir.length - a.dir.length)
|
|
65
|
+
.find((c) => c.dir === "." || path.startsWith(`${c.dir}/`));
|
|
66
|
+
|
|
67
|
+
/** The component of a path inside its container: the first folder under `src/` (or the container root), `root` for loose files. */
|
|
68
|
+
export function componentOf(container, path) {
|
|
69
|
+
const segments = insideOf(container.dir, path)
|
|
70
|
+
.replace(/^src\//, "")
|
|
71
|
+
.split("/");
|
|
72
|
+
return segments.length < 2 ? "root" : segments[0];
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const factsOf = (files, containers, path) => {
|
|
76
|
+
const container = containerOfPath(containers, path);
|
|
77
|
+
if (!container) return undefined;
|
|
78
|
+
const source = textOf(files, path);
|
|
79
|
+
const imports = specifiersOf(source).flatMap((spec) => {
|
|
80
|
+
const name = packageOf(spec);
|
|
81
|
+
if (name) return [{ kind: "package", name }];
|
|
82
|
+
const target = localTarget(spec, path, container.dir);
|
|
83
|
+
return target ? [{ kind: "local", target }] : [];
|
|
84
|
+
});
|
|
85
|
+
return {
|
|
86
|
+
path,
|
|
87
|
+
container: container.dir,
|
|
88
|
+
component: componentOf(container, path),
|
|
89
|
+
imports,
|
|
90
|
+
envs: envNamesOf(source, { declaration: false }),
|
|
91
|
+
};
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
const declarationsOf = (files, containers, paths) =>
|
|
95
|
+
paths.flatMap((path) => {
|
|
96
|
+
const container = containerOfPath(containers, path);
|
|
97
|
+
return container
|
|
98
|
+
? [{ path, container: container.dir, names: envNamesOf(textOf(files, path), { declaration: true }) }]
|
|
99
|
+
: [];
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
/** Everything the C4 levels are built from: `name` is the repository's name when its root package.json has none. */
|
|
103
|
+
export function scan(files, name) {
|
|
104
|
+
const repo = jsonOf(files, "package.json")?.name ?? name;
|
|
105
|
+
const containers = containersOf(files, repo);
|
|
106
|
+
const found = classify(files);
|
|
107
|
+
return {
|
|
108
|
+
repo,
|
|
109
|
+
containers,
|
|
110
|
+
units: unitsOf(files),
|
|
111
|
+
files: found.code.map((p) => factsOf(files, containers, p)).filter(Boolean),
|
|
112
|
+
declarations: declarationsOf(files, containers, found.env),
|
|
113
|
+
testCount: found.tests.length,
|
|
114
|
+
};
|
|
115
|
+
}
|
package/src/text.mjs
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
// The text version of the C4 view (every drawing carries one): a list of nodes and a list of relations per level.
|
|
2
|
+
const TITLES = { context: "System context", containers: "Containers", components: "Components" };
|
|
3
|
+
|
|
4
|
+
const brief = (text) => (text.length > 120 ? `${text.slice(0, 117).trimEnd()}...` : text);
|
|
5
|
+
|
|
6
|
+
const nodeLine = (n) => {
|
|
7
|
+
const what = [n.kind, n.technology].filter(Boolean).join(", ");
|
|
8
|
+
const about = n.description ? ": " + brief(n.description) : "";
|
|
9
|
+
const size = n.fileCount ? " [" + n.fileCount + " file(s)]" : "";
|
|
10
|
+
return `- ${n.name} (${what})${about}${size}`;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
/** The edge lines, naming nodes rather than ids. */
|
|
14
|
+
function edgeLines(level) {
|
|
15
|
+
const name = new Map(level.nodes.map((n) => [n.id, n.name]));
|
|
16
|
+
return level.edges.map((e) => {
|
|
17
|
+
const files = e.count > 1 ? " (" + e.count + " files)" : "";
|
|
18
|
+
return `- ${name.get(e.from) ?? e.from} ${e.label} ${name.get(e.to) ?? e.to}${files}`;
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function toText(c4) {
|
|
23
|
+
const blocks = Object.values(c4.levels).map((level) =>
|
|
24
|
+
[
|
|
25
|
+
`## ${TITLES[level.level]}`,
|
|
26
|
+
"",
|
|
27
|
+
...level.nodes.map(nodeLine),
|
|
28
|
+
"",
|
|
29
|
+
...(level.edges.length ? ["Relations", ...edgeLines(level)] : ["No relation found."]),
|
|
30
|
+
"",
|
|
31
|
+
].join("\n"),
|
|
32
|
+
);
|
|
33
|
+
return `# ${c4.repo}: C4 from the code\n\n${blocks.join("\n")}`;
|
|
34
|
+
}
|