archidocs 0.0.0-stage → 1.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 +21 -0
- package/README.md +181 -2
- package/dist/cli/commands.d.ts +322 -0
- package/dist/cli/commands.js +174 -0
- package/dist/cli/dev.d.ts +31 -0
- package/dist/cli/dev.js +156 -0
- package/dist/cli/diff-format.d.ts +9 -0
- package/dist/cli/diff-format.js +50 -0
- package/dist/cli/format.d.ts +15 -0
- package/dist/cli/format.js +64 -0
- package/dist/cli/init.d.ts +17 -0
- package/dist/cli/init.js +82 -0
- package/dist/cli/main.d.ts +2 -0
- package/dist/cli/main.js +330 -0
- package/dist/core/adr.d.ts +28 -0
- package/dist/core/adr.js +88 -0
- package/dist/core/adrs.d.ts +20 -0
- package/dist/core/adrs.js +86 -0
- package/dist/core/catalogue.d.ts +7 -0
- package/dist/core/catalogue.js +35 -0
- package/dist/core/compare.d.ts +21 -0
- package/dist/core/compare.js +62 -0
- package/dist/core/deployment-views.d.ts +28 -0
- package/dist/core/deployment-views.js +108 -0
- package/dist/core/deployments.d.ts +18 -0
- package/dist/core/deployments.js +21 -0
- package/dist/core/diff.d.ts +44 -0
- package/dist/core/diff.js +138 -0
- package/dist/core/dynamic-views.d.ts +73 -0
- package/dist/core/dynamic-views.js +162 -0
- package/dist/core/files.d.ts +2 -0
- package/dist/core/files.js +1 -0
- package/dist/core/fs.d.ts +19 -0
- package/dist/core/fs.js +21 -0
- package/dist/core/history-scope.d.ts +40 -0
- package/dist/core/history-scope.js +63 -0
- package/dist/core/impact.d.ts +34 -0
- package/dist/core/impact.js +62 -0
- package/dist/core/index.d.ts +20 -0
- package/dist/core/index.js +18 -0
- package/dist/core/load.d.ts +32 -0
- package/dist/core/load.js +465 -0
- package/dist/core/model.d.ts +150 -0
- package/dist/core/model.js +1 -0
- package/dist/core/ownership.d.ts +34 -0
- package/dist/core/ownership.js +65 -0
- package/dist/core/paths.d.ts +47 -0
- package/dist/core/paths.js +115 -0
- package/dist/core/query.d.ts +51 -0
- package/dist/core/query.js +77 -0
- package/dist/core/refs.d.ts +31 -0
- package/dist/core/refs.js +50 -0
- package/dist/core/rules.d.ts +41 -0
- package/dist/core/rules.js +1033 -0
- package/dist/core/save-as-view.d.ts +45 -0
- package/dist/core/save-as-view.js +114 -0
- package/dist/core/saved-views.d.ts +48 -0
- package/dist/core/saved-views.js +243 -0
- package/dist/core/schema-check.d.ts +13 -0
- package/dist/core/schema-check.js +79 -0
- package/dist/core/search.d.ts +27 -0
- package/dist/core/search.js +63 -0
- package/dist/core/suggest.d.ts +3 -0
- package/dist/core/suggest.js +30 -0
- package/dist/core/views.d.ts +126 -0
- package/dist/core/views.js +217 -0
- package/dist/core/yaml.d.ts +16 -0
- package/dist/core/yaml.js +52 -0
- package/dist/git.d.ts +7 -0
- package/dist/git.js +23 -0
- package/dist/guide/index.d.ts +53 -0
- package/dist/guide/index.js +57 -0
- package/dist/history/git.d.ts +39 -0
- package/dist/history/git.js +126 -0
- package/dist/history/index.d.ts +118 -0
- package/dist/history/index.js +223 -0
- package/dist/history/migrate.d.ts +16 -0
- package/dist/history/migrate.js +30 -0
- package/dist/mcp/history.d.ts +36 -0
- package/dist/mcp/history.js +175 -0
- package/dist/mcp/http.d.ts +23 -0
- package/dist/mcp/http.js +85 -0
- package/dist/mcp/index.d.ts +3 -0
- package/dist/mcp/index.js +3 -0
- package/dist/mcp/server.d.ts +54 -0
- package/dist/mcp/server.js +824 -0
- package/dist/mcp/source.d.ts +50 -0
- package/dist/mcp/source.js +132 -0
- package/dist/mcp/writes.d.ts +10 -0
- package/dist/mcp/writes.js +267 -0
- package/dist/render/font.d.ts +14 -0
- package/dist/render/font.js +11 -0
- package/dist/render/index.d.ts +8 -0
- package/dist/render/index.js +8 -0
- package/dist/render/layout.d.ts +53 -0
- package/dist/render/layout.js +199 -0
- package/dist/render/node.d.ts +3 -0
- package/dist/render/node.js +8 -0
- package/dist/render/png.d.ts +11 -0
- package/dist/render/png.js +29 -0
- package/dist/render/sequence-svg.d.ts +33 -0
- package/dist/render/sequence-svg.js +98 -0
- package/dist/render/sequence.d.ts +103 -0
- package/dist/render/sequence.js +142 -0
- package/dist/render/svg.d.ts +36 -0
- package/dist/render/svg.js +222 -0
- package/dist/render/teams.d.ts +12 -0
- package/dist/render/teams.js +31 -0
- package/dist/render/text.d.ts +15 -0
- package/dist/render/text.js +29 -0
- package/dist/render/tokens.d.ts +53 -0
- package/dist/render/tokens.js +71 -0
- package/dist/schema/adr.d.ts +36 -0
- package/dist/schema/adr.js +27 -0
- package/dist/schema/bundle.d.ts +726 -0
- package/dist/schema/bundle.js +171 -0
- package/dist/schema/index.d.ts +4 -0
- package/dist/schema/index.js +4 -0
- package/dist/schema/json-schema.d.ts +8 -0
- package/dist/schema/json-schema.js +19 -0
- package/dist/schema/workspace.d.ts +1066 -0
- package/dist/schema/workspace.js +271 -0
- package/dist/viewer/assets/index-DvKgCpBr.css +1 -0
- package/dist/viewer/assets/index-gucix3xm.js +113 -0
- package/dist/viewer/assets/index_bg-Blvrv-U2.wasm +0 -0
- package/dist/viewer/assets/index_bg-D3t31u3-.js +1 -0
- package/dist/viewer/assets/layout.worker-Bg-OaLVN.js +26 -0
- package/dist/viewer/assets/overpass-latin-QyaMbcmi.woff2 +0 -0
- package/dist/viewer/assets/overpass-latin-ext-DSRhPfzW.woff2 +0 -0
- package/dist/viewer/assets/overpass-mono-latin-BTquVRaO.woff2 +0 -0
- package/dist/viewer/assets/overpass-mono-latin-ext-D47EiU4_.woff2 +0 -0
- package/dist/viewer/assets/png-BDKIXtm5.js +1 -0
- package/dist/viewer/assets/svg-Bulv5YWn.js +2 -0
- package/dist/viewer/favicon.svg +1 -0
- package/dist/viewer/index.html +53 -0
- package/dist/writer/adrs.d.ts +21 -0
- package/dist/writer/adrs.js +72 -0
- package/dist/writer/deployments.d.ts +51 -0
- package/dist/writer/deployments.js +145 -0
- package/dist/writer/diff.d.ts +6 -0
- package/dist/writer/diff.js +77 -0
- package/dist/writer/document.d.ts +25 -0
- package/dist/writer/document.js +79 -0
- package/dist/writer/edit.d.ts +78 -0
- package/dist/writer/edit.js +63 -0
- package/dist/writer/index.d.ts +67 -0
- package/dist/writer/index.js +310 -0
- package/dist/writer/overlay.d.ts +7 -0
- package/dist/writer/overlay.js +26 -0
- package/dist/writer/views.d.ts +29 -0
- package/dist/writer/views.js +78 -0
- package/dist/writer/workspace.d.ts +25 -0
- package/dist/writer/workspace.js +56 -0
- package/guide/SKILL.md +36 -0
- package/guide/topics/add-service.md +30 -0
- package/guide/topics/bootstrap.md +28 -0
- package/guide/topics/change.md +32 -0
- package/guide/topics/deployments.md +36 -0
- package/guide/topics/dynamic-view.md +39 -0
- package/guide/topics/propose-adr.md +12 -0
- package/guide/topics/relationships.md +39 -0
- package/guide/topics/saved-view.md +36 -0
- package/package.json +75 -4
- package/schemas/adr.schema.json +82 -0
- package/schemas/model.schema.json +1381 -0
- package/schemas/project.schema.json +24 -0
- package/schemas/workspace.schema.json +72 -0
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import type { Bundle, BundleLens } from "../schema/bundle.ts";
|
|
2
|
+
import { type ViewDirection } from "./views.ts";
|
|
3
|
+
/** What the viewer shows, as its URL holds it: the View, its open boxes, the Lens, the picks and a turned direction. */
|
|
4
|
+
export interface Screen {
|
|
5
|
+
view: string;
|
|
6
|
+
open: readonly string[];
|
|
7
|
+
lens: BundleLens | null;
|
|
8
|
+
picks: readonly string[];
|
|
9
|
+
/** The direction the viewer turned the View to; null keeps the View's own. */
|
|
10
|
+
direction: ViewDirection | null;
|
|
11
|
+
}
|
|
12
|
+
/** A Saved View definition as written under `views: structural: <id>:`, every id qualified. */
|
|
13
|
+
export type SavedViewDefinition = Record<string, unknown> & {
|
|
14
|
+
title: string;
|
|
15
|
+
level: string;
|
|
16
|
+
};
|
|
17
|
+
export interface SavedScreen {
|
|
18
|
+
definition: SavedViewDefinition;
|
|
19
|
+
/** The definition as Project YAML, under `views: structural:`. */
|
|
20
|
+
yaml: string;
|
|
21
|
+
/** The `upsert_view` MCP call that writes the same definition. */
|
|
22
|
+
call: {
|
|
23
|
+
tool: "upsert_view";
|
|
24
|
+
arguments: {
|
|
25
|
+
project: string;
|
|
26
|
+
id: string;
|
|
27
|
+
definition: SavedViewDefinition;
|
|
28
|
+
};
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Save as View: turns the screen into a Saved View definition that the core
|
|
33
|
+
* engine resolves to the same nodes. The picks (with their open containers),
|
|
34
|
+
* or else every node, become ids; open boxes whose children are all kept
|
|
35
|
+
* become `expand`; the Lens becomes an include selector (an Impact Lens
|
|
36
|
+
* `upstream`/`downstream: all`, a Team Lens `{team}`, Connect to… a path
|
|
37
|
+
* selector) and is kept as `lens:`. Nodes the Lens selector would add beyond
|
|
38
|
+
* the screen are excluded. A Saved View with no picks keeps its own selectors.
|
|
39
|
+
* A Deployment View is saved whole, over its Environment, its Lens as `lens:`.
|
|
40
|
+
*/
|
|
41
|
+
export declare function saveAsView(bundle: Bundle, screen: Screen, target: {
|
|
42
|
+
project: string;
|
|
43
|
+
id: string;
|
|
44
|
+
title: string;
|
|
45
|
+
}): SavedScreen;
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { stringify } from "yaml";
|
|
2
|
+
import { resolveSaved } from "./saved-views.js";
|
|
3
|
+
import { ModelIndex, pickedView, resolveView } from "./views.js";
|
|
4
|
+
const kindLevel = { person: "c1", system: "c1", service: "c2", component: "c3" };
|
|
5
|
+
const levelDepth = { c1: 1, c2: 2, c3: 3 };
|
|
6
|
+
/**
|
|
7
|
+
* Save as View: turns the screen into a Saved View definition that the core
|
|
8
|
+
* engine resolves to the same nodes. The picks (with their open containers),
|
|
9
|
+
* or else every node, become ids; open boxes whose children are all kept
|
|
10
|
+
* become `expand`; the Lens becomes an include selector (an Impact Lens
|
|
11
|
+
* `upstream`/`downstream: all`, a Team Lens `{team}`, Connect to… a path
|
|
12
|
+
* selector) and is kept as `lens:`. Nodes the Lens selector would add beyond
|
|
13
|
+
* the screen are excluded. A Saved View with no picks keeps its own selectors.
|
|
14
|
+
* A Deployment View is saved whole, over its Environment, its Lens as `lens:`.
|
|
15
|
+
*/
|
|
16
|
+
export function saveAsView(bundle, screen, target) {
|
|
17
|
+
const view = resolveView(bundle, screen.view, { expand: screen.open });
|
|
18
|
+
if (!view)
|
|
19
|
+
throw new Error(`No View ${screen.view} to save.`);
|
|
20
|
+
const own = bundle.views.find((v) => v.id === screen.view && v.kind === "structural");
|
|
21
|
+
const direction = screen.direction ?? own?.direction ?? null;
|
|
22
|
+
const lens = screen.lens ? { lens: writtenLens(screen.lens) } : {};
|
|
23
|
+
let body;
|
|
24
|
+
if (view.kind === "deployment" && screen.picks.length > 0)
|
|
25
|
+
throw new Error("Save as View keeps a whole Deployment View; clear the picks first.");
|
|
26
|
+
if (view.kind === "deployment" && !own) {
|
|
27
|
+
body = { level: "deployment", environment: screen.view.slice("deployment/".length) };
|
|
28
|
+
}
|
|
29
|
+
else if (own && screen.picks.length === 0) {
|
|
30
|
+
body = {
|
|
31
|
+
level: own.level,
|
|
32
|
+
...(own.include === null ? {} : { include: own.include }),
|
|
33
|
+
...nonEmpty({ exclude: own.exclude, expand: own.expand, collapse: own.collapse, targets: own.targets }),
|
|
34
|
+
...(own.environment === null ? {} : { environment: own.environment }),
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
else {
|
|
38
|
+
const model = new ModelIndex(bundle);
|
|
39
|
+
const kept = new Set((screen.picks.length === 0 ? view : pickedView(view, screen.picks)).nodes.map((n) => n.id));
|
|
40
|
+
const depthOf = (id) => model.ancestry(id).length;
|
|
41
|
+
const expand = screen.open
|
|
42
|
+
.filter((id) => kept.has(id) && model.children(id).length > 0 && model.children(id).every((c) => kept.has(c.id)))
|
|
43
|
+
.sort((a, b) => depthOf(a) - depthOf(b));
|
|
44
|
+
const byExpand = new Set(expand.flatMap((id) => model.children(id).map((c) => c.id)));
|
|
45
|
+
const ids = [...kept].filter((id) => !byExpand.has(id)).sort();
|
|
46
|
+
const deepest = ids.reduce((d, id) => Math.max(d, levelDepth[kindLevel[model.byId.get(id).kind]]), levelDepth[view.kind]);
|
|
47
|
+
const level = Object.keys(levelDepth).find((l) => levelDepth[l] === deepest);
|
|
48
|
+
const plain = { level, include: ids, ...nonEmpty({ expand }) };
|
|
49
|
+
const selector = screen.lens ? lensSelector(screen.lens) : undefined;
|
|
50
|
+
body = plain;
|
|
51
|
+
if (selector) {
|
|
52
|
+
const reached = nodesOf(bundle, { level, include: [selector] });
|
|
53
|
+
const include = [selector, ...ids.filter((id) => !reached.has(id))];
|
|
54
|
+
const extra = [...nodesOf(bundle, { level, include, expand })].filter((id) => !kept.has(id)).sort();
|
|
55
|
+
const withLens = { level, include, ...nonEmpty({ exclude: extra, expand }) };
|
|
56
|
+
if (sameSet(nodesOf(bundle, withLens), kept))
|
|
57
|
+
body = withLens;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
const definition = localise({ title: target.title, ...body, ...(direction === null ? {} : { direction }), ...lens }, target.project);
|
|
61
|
+
return {
|
|
62
|
+
definition,
|
|
63
|
+
yaml: stringify({ views: { structural: { [target.id]: definition } } }, { flowCollectionPadding: true }),
|
|
64
|
+
call: { tool: "upsert_view", arguments: { project: target.project, id: target.id, definition } },
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
/** Writes the target Project's own ids bare, as Load wants them inside that Project; Team and Environment ids are never qualified. */
|
|
68
|
+
function localise(value, project, key) {
|
|
69
|
+
if (typeof value === "string")
|
|
70
|
+
return key === "team" || key === "environment" || key === "title" || key === "level" || key === "direction" ? value : value.startsWith(`${project}:`) ? value.slice(project.length + 1) : value;
|
|
71
|
+
if (Array.isArray(value))
|
|
72
|
+
return value.map((v) => localise(v, project));
|
|
73
|
+
if (value !== null && typeof value === "object")
|
|
74
|
+
return Object.fromEntries(Object.entries(value).map(([k, v]) => [k, localise(v, project, k)]));
|
|
75
|
+
return value;
|
|
76
|
+
}
|
|
77
|
+
const nonEmpty = (lists) => Object.fromEntries(Object.entries(lists).filter(([, list]) => list.length > 0));
|
|
78
|
+
const sameSet = (a, b) => a.size === b.size && [...a].every((x) => b.has(x));
|
|
79
|
+
/** The Lens as an include selector: the Elements it reaches. */
|
|
80
|
+
function lensSelector(lens) {
|
|
81
|
+
if ("team" in lens)
|
|
82
|
+
return { team: lens.team };
|
|
83
|
+
if ("path" in lens)
|
|
84
|
+
return { from: lens.path.from, to: lens.path.to, paths: lens.path.paths ?? "shortest" };
|
|
85
|
+
const ways = lens.direction === "both" ? ["upstream", "downstream"] : [lens.direction];
|
|
86
|
+
return { element: lens.impact, ...Object.fromEntries(ways.map((w) => [w, "all"])) };
|
|
87
|
+
}
|
|
88
|
+
function writtenLens(lens) {
|
|
89
|
+
if ("path" in lens)
|
|
90
|
+
return { path: { from: lens.path.from, to: lens.path.to, paths: lens.path.paths ?? "shortest" } };
|
|
91
|
+
return { ...lens };
|
|
92
|
+
}
|
|
93
|
+
/** The node ids the core engine resolves a definition to. */
|
|
94
|
+
function nodesOf(bundle, def) {
|
|
95
|
+
const resolved = resolveSaved(bundle, {
|
|
96
|
+
id: "screen:saved",
|
|
97
|
+
project: "screen",
|
|
98
|
+
kind: "structural",
|
|
99
|
+
title: "",
|
|
100
|
+
description: null,
|
|
101
|
+
tags: [],
|
|
102
|
+
level: def.level,
|
|
103
|
+
include: def.include,
|
|
104
|
+
exclude: def.exclude ?? [],
|
|
105
|
+
expand: def.expand ?? [],
|
|
106
|
+
collapse: [],
|
|
107
|
+
direction: null,
|
|
108
|
+
lens: null,
|
|
109
|
+
environment: null,
|
|
110
|
+
targets: [],
|
|
111
|
+
source: { file: "", line: 0 },
|
|
112
|
+
});
|
|
113
|
+
return new Set(resolved?.view.nodes.map((n) => n.id) ?? []);
|
|
114
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type { Bundle, BundleLens, BundleStructuralView } from "../schema/bundle.ts";
|
|
2
|
+
import { ModelIndex, type ResolvedView } from "./views.ts";
|
|
3
|
+
/** An `expand` or `collapse` entry that can't apply: its id is not a node of the View, or it has no children. */
|
|
4
|
+
export interface BadExpand {
|
|
5
|
+
field: "expand" | "collapse";
|
|
6
|
+
index: number;
|
|
7
|
+
id: string;
|
|
8
|
+
reason: "not-in-view" | "no-children";
|
|
9
|
+
}
|
|
10
|
+
/** A path selector, or a path Lens, that finds no path. */
|
|
11
|
+
export interface UnresolvedPath {
|
|
12
|
+
/** Where it is written: `["include", 2]`, `["exclude", 0]` or `["lens", "path"]`. */
|
|
13
|
+
at: [string, string | number];
|
|
14
|
+
from: string;
|
|
15
|
+
to: string;
|
|
16
|
+
}
|
|
17
|
+
/** A Saved View resolved, with what `check` reports about it. */
|
|
18
|
+
export interface SavedResolution {
|
|
19
|
+
view: ResolvedView;
|
|
20
|
+
badExpand: BadExpand[];
|
|
21
|
+
unresolvedPaths: UnresolvedPath[];
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Resolves a Saved structural View:
|
|
25
|
+
* 1. union the `include` selectors (every Element when there is no `include`);
|
|
26
|
+
* 2. subtract the `exclude` selectors, an excluded Element taking everything inside it;
|
|
27
|
+
* 3. collapse every Element to `level`;
|
|
28
|
+
* 4. open each `expand` Element one level deeper, then close each `collapse` Element;
|
|
29
|
+
* 5. derive edges, leaving out Relationships of excluded Elements.
|
|
30
|
+
* The Lens marks nodes and lights edges but never changes membership.
|
|
31
|
+
* A Deployment View is resolved by `savedDeployment` instead.
|
|
32
|
+
*/
|
|
33
|
+
export declare function resolveSaved(bundle: Bundle, def: BundleStructuralView): SavedResolution;
|
|
34
|
+
/** A Lens in words, for tool text and a picture's key. */
|
|
35
|
+
export declare const describeLens: (lens: BundleLens) => string;
|
|
36
|
+
/**
|
|
37
|
+
* Marks every node and lights every edge for the Lens. An Element the Lens
|
|
38
|
+
* reaches marks the node that holds it, or that it holds, at its nearest
|
|
39
|
+
* distance. An impact Lens lights an edge carrying the impact one hop
|
|
40
|
+
* closer to its origin; a path Lens, an edge carrying a hop of a path; a
|
|
41
|
+
* Team Lens, an edge between two hit nodes.
|
|
42
|
+
*/
|
|
43
|
+
export declare function applyLens(bundle: Bundle, model: ModelIndex, view: ResolvedView, lens: BundleLens): {
|
|
44
|
+
unresolved?: {
|
|
45
|
+
from: string;
|
|
46
|
+
to: string;
|
|
47
|
+
};
|
|
48
|
+
};
|
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
import { resolveDeployment } from "./deployment-views.js";
|
|
2
|
+
import { getImpact } from "./impact.js";
|
|
3
|
+
import { pathsBetween } from "./paths.js";
|
|
4
|
+
import { lifter } from "./query.js";
|
|
5
|
+
import { build, ModelIndex } from "./views.js";
|
|
6
|
+
const levelOf = { c1: "system", c2: "service", c3: "component" };
|
|
7
|
+
/**
|
|
8
|
+
* Resolves a Saved structural View:
|
|
9
|
+
* 1. union the `include` selectors (every Element when there is no `include`);
|
|
10
|
+
* 2. subtract the `exclude` selectors, an excluded Element taking everything inside it;
|
|
11
|
+
* 3. collapse every Element to `level`;
|
|
12
|
+
* 4. open each `expand` Element one level deeper, then close each `collapse` Element;
|
|
13
|
+
* 5. derive edges, leaving out Relationships of excluded Elements.
|
|
14
|
+
* The Lens marks nodes and lights edges but never changes membership.
|
|
15
|
+
* A Deployment View is resolved by `savedDeployment` instead.
|
|
16
|
+
*/
|
|
17
|
+
export function resolveSaved(bundle, def) {
|
|
18
|
+
const model = new ModelIndex(bundle);
|
|
19
|
+
if (def.level === "deployment")
|
|
20
|
+
return savedDeployment(bundle, model, def);
|
|
21
|
+
const kind = def.level;
|
|
22
|
+
const unresolvedPaths = [];
|
|
23
|
+
const select = (selectors, field) => {
|
|
24
|
+
const ids = new Set();
|
|
25
|
+
selectors.forEach((s, i) => {
|
|
26
|
+
const found = selected(bundle, model, s);
|
|
27
|
+
if (found.unresolved)
|
|
28
|
+
unresolvedPaths.push({ at: [field, i], ...found.unresolved });
|
|
29
|
+
for (const id of found.ids)
|
|
30
|
+
ids.add(id);
|
|
31
|
+
});
|
|
32
|
+
return ids;
|
|
33
|
+
};
|
|
34
|
+
const members = def.include === null ? new Set(model.elements.map((e) => e.id)) : select(def.include, "include");
|
|
35
|
+
const excluded = select(def.exclude, "exclude");
|
|
36
|
+
const isExcluded = (id) => model.ancestry(id).some((a) => excluded.has(a));
|
|
37
|
+
const lift = lifter(bundle, levelOf[kind]);
|
|
38
|
+
const shown = new Set([...members].filter((id) => !isExcluded(id)).map(lift));
|
|
39
|
+
const badExpand = [];
|
|
40
|
+
for (const field of ["expand", "collapse"]) {
|
|
41
|
+
def[field].forEach((id, index) => {
|
|
42
|
+
const children = model.children(id);
|
|
43
|
+
if (!shown.has(id))
|
|
44
|
+
badExpand.push({ field, index, id, reason: "not-in-view" });
|
|
45
|
+
else if (children.length === 0)
|
|
46
|
+
badExpand.push({ field, index, id, reason: "no-children" });
|
|
47
|
+
else if (field === "expand")
|
|
48
|
+
for (const c of children)
|
|
49
|
+
shown.add(c.id);
|
|
50
|
+
else
|
|
51
|
+
for (const n of [...shown])
|
|
52
|
+
if (n !== id && model.ancestry(n).includes(id))
|
|
53
|
+
shown.delete(n);
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
const view = build(model, { id: def.id, kind, title: def.title }, shown, (r) => !isExcluded(r.from) && !isExcluded(r.to));
|
|
57
|
+
if (def.direction !== null)
|
|
58
|
+
view.direction = def.direction;
|
|
59
|
+
if (def.lens !== null) {
|
|
60
|
+
const lensed = applyLens(bundle, model, view, def.lens);
|
|
61
|
+
if (lensed.unresolved)
|
|
62
|
+
unresolvedPaths.push({ at: ["lens", "path"], ...lensed.unresolved });
|
|
63
|
+
}
|
|
64
|
+
return { view, badExpand, unresolvedPaths };
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Resolves a Saved Deployment View over its `environment`: `include` and
|
|
68
|
+
* `exclude` select Services (a System or Component selects the Services in
|
|
69
|
+
* it or holding it), and `targets` keeps the Deployments in those Target
|
|
70
|
+
* subtrees. A Service that is selected but has no Deployment kept is drawn,
|
|
71
|
+
* when related, as a logical node; an excluded one is left out altogether.
|
|
72
|
+
* `expand` and `collapse` don't apply to a Deployment View.
|
|
73
|
+
*/
|
|
74
|
+
function savedDeployment(bundle, model, def) {
|
|
75
|
+
const unresolvedPaths = [];
|
|
76
|
+
const services = (selectors, field) => {
|
|
77
|
+
const ids = new Set();
|
|
78
|
+
selectors.forEach((s, i) => {
|
|
79
|
+
const found = selected(bundle, model, s);
|
|
80
|
+
if (found.unresolved)
|
|
81
|
+
unresolvedPaths.push({ at: [field, i], ...found.unresolved });
|
|
82
|
+
for (const id of found.ids) {
|
|
83
|
+
for (const e of model.elements)
|
|
84
|
+
if (e.kind === "service" && (model.ancestry(e.id).includes(id) || model.ancestry(id).includes(e.id)))
|
|
85
|
+
ids.add(e.id);
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
return ids;
|
|
89
|
+
};
|
|
90
|
+
const included = def.include === null ? undefined : services(def.include, "include");
|
|
91
|
+
const excluded = services(def.exclude, "exclude");
|
|
92
|
+
const parentOf = new Map(bundle.targets.map((t) => [t.id, t.parent]));
|
|
93
|
+
const under = (target) => {
|
|
94
|
+
for (let t = target; t; t = parentOf.get(t))
|
|
95
|
+
if (def.targets.includes(t))
|
|
96
|
+
return true;
|
|
97
|
+
return false;
|
|
98
|
+
};
|
|
99
|
+
const view = resolveDeployment(bundle, { id: def.id, title: def.title }, def.environment ?? "", {
|
|
100
|
+
keep: (s) => !excluded.has(s),
|
|
101
|
+
deployed: (d) => (included === undefined || included.has(d.service)) && (def.targets.length === 0 || under(d.target)),
|
|
102
|
+
});
|
|
103
|
+
if (def.direction !== null)
|
|
104
|
+
view.direction = def.direction;
|
|
105
|
+
if (def.lens !== null) {
|
|
106
|
+
const lensed = applyLens(bundle, model, view, def.lens);
|
|
107
|
+
if (lensed.unresolved)
|
|
108
|
+
unresolvedPaths.push({ at: ["lens", "path"], ...lensed.unresolved });
|
|
109
|
+
}
|
|
110
|
+
return { view, badExpand: [], unresolvedPaths };
|
|
111
|
+
}
|
|
112
|
+
/** A Lens in words, for tool text and a picture's key. */
|
|
113
|
+
export const describeLens = (lens) => "impact" in lens ? `impact of ${lens.impact}, ${lens.direction}` : "team" in lens ? `Team ${lens.team}` : `paths from ${lens.path.from} to ${lens.path.to} (${lens.path.paths ?? "shortest"})`;
|
|
114
|
+
/** The Elements one selector names; unknown ids name none (`unknown-element` reports them). */
|
|
115
|
+
function selected(bundle, model, s) {
|
|
116
|
+
if (typeof s === "string")
|
|
117
|
+
return { ids: new Set(model.byId.has(s) ? [s] : []) };
|
|
118
|
+
if ("team" in s)
|
|
119
|
+
return { ids: new Set(model.elements.filter((e) => e.owner === s.team).map((e) => e.id)) };
|
|
120
|
+
if ("tag" in s)
|
|
121
|
+
return { ids: new Set(model.elements.filter((e) => e.tags.includes(s.tag)).map((e) => e.id)) };
|
|
122
|
+
if ("from" in s) {
|
|
123
|
+
const paths = pathsBetween(bundle, s.from, s.to, s.paths ?? "shortest");
|
|
124
|
+
const known = model.byId.has(s.from) && model.byId.has(s.to);
|
|
125
|
+
return { ids: nodesOn(paths), ...(known && paths.length === 0 ? { unresolved: { from: s.from, to: s.to } } : {}) };
|
|
126
|
+
}
|
|
127
|
+
const ids = new Set();
|
|
128
|
+
if (!model.byId.has(s.element))
|
|
129
|
+
return { ids };
|
|
130
|
+
ids.add(s.element);
|
|
131
|
+
const below = (id, deep) => {
|
|
132
|
+
for (const c of model.children(id)) {
|
|
133
|
+
ids.add(c.id);
|
|
134
|
+
if (deep)
|
|
135
|
+
below(c.id, deep);
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
if (s.with)
|
|
139
|
+
below(s.element, s.with === "descendants");
|
|
140
|
+
for (const direction of ["upstream", "downstream"]) {
|
|
141
|
+
const hops = s[direction];
|
|
142
|
+
if (hops === undefined || hops === 0)
|
|
143
|
+
continue;
|
|
144
|
+
const impact = getImpact(bundle, s.element, direction, hops === "all" ? {} : { maxDepth: hops });
|
|
145
|
+
for (const ring of impact[direction] ?? [])
|
|
146
|
+
for (const e of ring.elements)
|
|
147
|
+
ids.add(e.id);
|
|
148
|
+
}
|
|
149
|
+
return { ids };
|
|
150
|
+
}
|
|
151
|
+
const nodesOn = (paths) => new Set(paths.flatMap((p) => p.hops.flatMap((h) => [h.from, h.to])));
|
|
152
|
+
/**
|
|
153
|
+
* Marks every node and lights every edge for the Lens. An Element the Lens
|
|
154
|
+
* reaches marks the node that holds it, or that it holds, at its nearest
|
|
155
|
+
* distance. An impact Lens lights an edge carrying the impact one hop
|
|
156
|
+
* closer to its origin; a path Lens, an edge carrying a hop of a path; a
|
|
157
|
+
* Team Lens, an edge between two hit nodes.
|
|
158
|
+
*/
|
|
159
|
+
export function applyLens(bundle, model, view, lens) {
|
|
160
|
+
view.lens = lens;
|
|
161
|
+
/** Element ids the Lens reaches, with their distance when it has one. */
|
|
162
|
+
const reached = new Map();
|
|
163
|
+
/** For an impact Lens, which side of the origin each reached Element is on, at its nearest distance. */
|
|
164
|
+
const sides = new Map();
|
|
165
|
+
const carried = new Set();
|
|
166
|
+
let unresolved;
|
|
167
|
+
if ("impact" in lens) {
|
|
168
|
+
if (model.byId.has(lens.impact)) {
|
|
169
|
+
reached.set(lens.impact, 0);
|
|
170
|
+
sides.set(lens.impact, "origin");
|
|
171
|
+
const impact = getImpact(bundle, lens.impact, lens.direction);
|
|
172
|
+
const rings = [...(impact.upstream ?? []).map((ring) => ["upstream", ring]), ...(impact.downstream ?? []).map((ring) => ["downstream", ring])];
|
|
173
|
+
for (const [side, ring] of rings) {
|
|
174
|
+
for (const e of ring.elements) {
|
|
175
|
+
if (ring.distance < (reached.get(e.id) ?? Infinity)) {
|
|
176
|
+
reached.set(e.id, ring.distance);
|
|
177
|
+
sides.set(e.id, side);
|
|
178
|
+
}
|
|
179
|
+
for (const r of e.relationships)
|
|
180
|
+
carried.add(r);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
else if ("team" in lens) {
|
|
186
|
+
for (const e of model.elements)
|
|
187
|
+
if (e.owner === lens.team)
|
|
188
|
+
reached.set(e.id, undefined);
|
|
189
|
+
for (const t of bundle.targets)
|
|
190
|
+
if (t.owner === lens.team)
|
|
191
|
+
reached.set(t.id, undefined);
|
|
192
|
+
}
|
|
193
|
+
else {
|
|
194
|
+
const paths = pathsBetween(bundle, lens.path.from, lens.path.to, lens.path.paths ?? "shortest");
|
|
195
|
+
for (const id of nodesOn(paths))
|
|
196
|
+
reached.set(id, undefined);
|
|
197
|
+
for (const p of paths)
|
|
198
|
+
for (const h of p.hops)
|
|
199
|
+
for (const r of h.relationships)
|
|
200
|
+
carried.add(r);
|
|
201
|
+
if (paths.length === 0 && model.byId.has(lens.path.from) && model.byId.has(lens.path.to))
|
|
202
|
+
unresolved = { from: lens.path.from, to: lens.path.to };
|
|
203
|
+
}
|
|
204
|
+
// Each reached Element marks the nearest node holding it; an impact or path Lens also marks the nodes inside it.
|
|
205
|
+
// A node stands for its own id, or for the Element it draws: a Deployment View draws one Service as several Deployments.
|
|
206
|
+
const standing = new Map();
|
|
207
|
+
for (const n of view.nodes)
|
|
208
|
+
standing.set(n.element ?? n.id, [...(standing.get(n.element ?? n.id) ?? []), n.id]);
|
|
209
|
+
const marks = new Map(view.nodes.map((n) => [n.id, { hit: false }]));
|
|
210
|
+
const mark = (node, d, side) => {
|
|
211
|
+
const m = marks.get(node);
|
|
212
|
+
if (!m)
|
|
213
|
+
return;
|
|
214
|
+
m.hit = true;
|
|
215
|
+
if (d !== undefined && d < (m.distance ?? Infinity)) {
|
|
216
|
+
m.distance = d;
|
|
217
|
+
if (side !== undefined)
|
|
218
|
+
m.side = side;
|
|
219
|
+
}
|
|
220
|
+
};
|
|
221
|
+
for (const [id, d] of reached) {
|
|
222
|
+
const holder = model.ancestry(id).find((a) => standing.has(a));
|
|
223
|
+
for (const n of holder === undefined ? [] : standing.get(holder))
|
|
224
|
+
mark(n, d, sides.get(id));
|
|
225
|
+
if (!("team" in lens))
|
|
226
|
+
for (const n of view.nodes)
|
|
227
|
+
if ((n.element ?? n.id) !== id && model.ancestry(n.element ?? n.id).includes(id))
|
|
228
|
+
mark(n.id, d, sides.get(id));
|
|
229
|
+
}
|
|
230
|
+
for (const n of view.nodes)
|
|
231
|
+
n.mark = marks.get(n.id);
|
|
232
|
+
const markOf = new Map(view.nodes.map((n) => [n.id, n.mark]));
|
|
233
|
+
for (const e of view.edges) {
|
|
234
|
+
const [from, to] = [markOf.get(e.from), markOf.get(e.to)];
|
|
235
|
+
if ("team" in lens)
|
|
236
|
+
e.lit = from.hit && to.hit;
|
|
237
|
+
else if ("impact" in lens)
|
|
238
|
+
e.lit = e.relationships.some((r) => carried.has(r)) && from.distance !== undefined && to.distance !== undefined && Math.abs(from.distance - to.distance) === 1;
|
|
239
|
+
else
|
|
240
|
+
e.lit = e.relationships.some((r) => carried.has(r)) && from.hit && to.hit;
|
|
241
|
+
}
|
|
242
|
+
return unresolved ? { unresolved } : {};
|
|
243
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { z } from "zod";
|
|
2
|
+
import type { ParsedYaml, Position } from "./yaml.ts";
|
|
3
|
+
export type SchemaRule = "unknown-field" | "missing-field" | "invalid-value";
|
|
4
|
+
export interface SchemaFinding extends Position {
|
|
5
|
+
rule: SchemaRule;
|
|
6
|
+
message: string;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Checks `data` (found at `basePath` in a parsed file) against a zod schema
|
|
10
|
+
* and turns each zod issue into a finding for one of the three schema rules,
|
|
11
|
+
* positioned at the offending key or value.
|
|
12
|
+
*/
|
|
13
|
+
export declare function checkSchema(schema: z.ZodType, data: unknown, parsed: ParsedYaml, basePath?: readonly PropertyKey[]): SchemaFinding[];
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { didYouMean } from "./suggest.js";
|
|
2
|
+
/**
|
|
3
|
+
* Checks `data` (found at `basePath` in a parsed file) against a zod schema
|
|
4
|
+
* and turns each zod issue into a finding for one of the three schema rules,
|
|
5
|
+
* positioned at the offending key or value.
|
|
6
|
+
*/
|
|
7
|
+
export function checkSchema(schema, data, parsed, basePath = []) {
|
|
8
|
+
const result = schema.safeParse(data);
|
|
9
|
+
if (result.success)
|
|
10
|
+
return [];
|
|
11
|
+
const findings = [];
|
|
12
|
+
for (const issue of result.error.issues) {
|
|
13
|
+
const path = [...basePath, ...issue.path];
|
|
14
|
+
if (issue.code === "unrecognized_keys") {
|
|
15
|
+
const allowed = keysOf(schemaAt(schema, issue.path));
|
|
16
|
+
for (const key of issue.keys) {
|
|
17
|
+
findings.push({
|
|
18
|
+
rule: "unknown-field",
|
|
19
|
+
...parsed.positionOf([...path, key], { key: true }),
|
|
20
|
+
message: `Unknown field "${key}"${didYouMean(key, allowed)}`,
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
else if (issue.code === "invalid_type" && valueAt(data, issue.path) === undefined) {
|
|
25
|
+
const field = String(issue.path.at(-1));
|
|
26
|
+
findings.push({
|
|
27
|
+
rule: "missing-field",
|
|
28
|
+
...parsed.positionOf(path.slice(0, -1), { key: true }),
|
|
29
|
+
message: `Missing required field "${field}"`,
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
const where = issue.path.length > 0 ? `"${issue.path.join(".")}"` : "file";
|
|
34
|
+
const keyIssue = issue.code === "invalid_key";
|
|
35
|
+
findings.push({
|
|
36
|
+
rule: "invalid-value",
|
|
37
|
+
...parsed.positionOf(path, { key: keyIssue }),
|
|
38
|
+
message: `Invalid value for ${where}: ${issue.message.replace(/^Invalid (input|option): /, "")}`,
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return findings;
|
|
43
|
+
}
|
|
44
|
+
function valueAt(data, path) {
|
|
45
|
+
let cur = data;
|
|
46
|
+
for (const segment of path) {
|
|
47
|
+
if (cur === null || typeof cur !== "object")
|
|
48
|
+
return undefined;
|
|
49
|
+
cur = cur[segment];
|
|
50
|
+
}
|
|
51
|
+
return cur;
|
|
52
|
+
}
|
|
53
|
+
const defOf = (schema) => schema._zod.def;
|
|
54
|
+
function unwrap(schema) {
|
|
55
|
+
let def = defOf(schema);
|
|
56
|
+
while ((def.type === "optional" || def.type === "nullable" || def.type === "default") && def.innerType) {
|
|
57
|
+
schema = def.innerType;
|
|
58
|
+
def = defOf(schema);
|
|
59
|
+
}
|
|
60
|
+
return schema;
|
|
61
|
+
}
|
|
62
|
+
/** The sub-schema that validates the value at `path`. */
|
|
63
|
+
function schemaAt(schema, path) {
|
|
64
|
+
let cur = unwrap(schema);
|
|
65
|
+
for (const segment of path) {
|
|
66
|
+
if (!cur)
|
|
67
|
+
return undefined;
|
|
68
|
+
const def = defOf(cur);
|
|
69
|
+
const next = def.type === "object" ? def.shape?.[String(segment)] : def.type === "record" ? def.valueType : def.type === "array" ? def.element : undefined;
|
|
70
|
+
cur = next && unwrap(next);
|
|
71
|
+
}
|
|
72
|
+
return cur;
|
|
73
|
+
}
|
|
74
|
+
function keysOf(schema) {
|
|
75
|
+
if (!schema)
|
|
76
|
+
return [];
|
|
77
|
+
const def = defOf(schema);
|
|
78
|
+
return def.type === "object" && def.shape ? Object.keys(def.shape) : [];
|
|
79
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { Bundle, BundleElement } from "../schema/bundle.ts";
|
|
2
|
+
/** 0 for an exact match, 1 for a prefix (or a URL below a Deployment's), 2 for a substring. */
|
|
3
|
+
export type MatchRank = 0 | 1 | 2;
|
|
4
|
+
/** One Element `search` found. `deployment` says which URL matched, when a URL matched better than the id or name. */
|
|
5
|
+
export interface SearchMatch {
|
|
6
|
+
element: BundleElement;
|
|
7
|
+
rank: MatchRank;
|
|
8
|
+
deployment?: {
|
|
9
|
+
environment: string;
|
|
10
|
+
target: string;
|
|
11
|
+
url: string;
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
export interface SearchOptions {
|
|
15
|
+
kind?: BundleElement["kind"];
|
|
16
|
+
/** Also match each Element's technology, or its Owner's id or name; the viewer's Find asks for both. */
|
|
17
|
+
fields?: readonly ("team" | "technology")[];
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Finds Elements whose qualified id, bare id or name (and, when asked, technology
|
|
21
|
+
* or Owner, ranked as a substring) contains `query`, and
|
|
22
|
+
* Services one of whose Deployment URLs (`url` or an endpoint) matches it,
|
|
23
|
+
* all case-insensitively. A URL matches as a whole, without its scheme, by
|
|
24
|
+
* its host, or as the start of a longer URL below it; a trailing slash is
|
|
25
|
+
* ignored. Exact matches come first, then prefixes, then substrings, each by id.
|
|
26
|
+
*/
|
|
27
|
+
export declare function search(bundle: Bundle, query: string, options?: SearchOptions): SearchMatch[];
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Finds Elements whose qualified id, bare id or name (and, when asked, technology
|
|
3
|
+
* or Owner, ranked as a substring) contains `query`, and
|
|
4
|
+
* Services one of whose Deployment URLs (`url` or an endpoint) matches it,
|
|
5
|
+
* all case-insensitively. A URL matches as a whole, without its scheme, by
|
|
6
|
+
* its host, or as the start of a longer URL below it; a trailing slash is
|
|
7
|
+
* ignored. Exact matches come first, then prefixes, then substrings, each by id.
|
|
8
|
+
*/
|
|
9
|
+
export function search(bundle, query, options = {}) {
|
|
10
|
+
const q = normalise(query);
|
|
11
|
+
const urls = new Map();
|
|
12
|
+
for (const d of bundle.deployments) {
|
|
13
|
+
const list = urls.get(d.service) ?? [];
|
|
14
|
+
for (const url of [d.url, ...d.endpoints.map((e) => e.url)])
|
|
15
|
+
if (url)
|
|
16
|
+
list.push({ environment: d.environment, target: d.target, url });
|
|
17
|
+
urls.set(d.service, list);
|
|
18
|
+
}
|
|
19
|
+
const teamNames = new Map(bundle.teams.map((t) => [t.id, t.name]));
|
|
20
|
+
const extra = (e) => [
|
|
21
|
+
...(options.fields?.includes("team") ? [e.owner, teamNames.get(e.owner) ?? ""] : []),
|
|
22
|
+
...(options.fields?.includes("technology") && "technology" in e && e.technology ? [e.technology] : []),
|
|
23
|
+
];
|
|
24
|
+
const matches = [];
|
|
25
|
+
for (const element of bundle.elements) {
|
|
26
|
+
if (options.kind && element.kind !== options.kind)
|
|
27
|
+
continue;
|
|
28
|
+
const byName = nameRank(q, element) ?? (rankAgainst(q, extra(element).map((f) => f.toLowerCase())) === undefined ? undefined : 2);
|
|
29
|
+
let best = byName === undefined ? undefined : { element, rank: byName };
|
|
30
|
+
for (const deployment of urls.get(element.id) ?? []) {
|
|
31
|
+
const rank = urlRank(q, deployment.url);
|
|
32
|
+
if (rank !== undefined && (best === undefined || rank < best.rank))
|
|
33
|
+
best = { element, rank, deployment };
|
|
34
|
+
}
|
|
35
|
+
if (best)
|
|
36
|
+
matches.push(best);
|
|
37
|
+
}
|
|
38
|
+
return matches.sort((a, b) => a.rank - b.rank || (a.element.id < b.element.id ? -1 : a.element.id > b.element.id ? 1 : 0));
|
|
39
|
+
}
|
|
40
|
+
const normalise = (s) => s.trim().toLowerCase().replace(/\/+$/, "");
|
|
41
|
+
function nameRank(q, e) {
|
|
42
|
+
const fields = [e.id, e.id.slice(e.id.indexOf(":") + 1), e.name].map((f) => f.toLowerCase());
|
|
43
|
+
return rankAgainst(q, fields);
|
|
44
|
+
}
|
|
45
|
+
function urlRank(q, url) {
|
|
46
|
+
const full = normalise(url);
|
|
47
|
+
const bare = full.replace(/^[a-z][a-z0-9+.-]*:\/\//, "");
|
|
48
|
+
const host = bare.replace(/[/?#].*$/, "");
|
|
49
|
+
if (q === full || q === bare || q === host)
|
|
50
|
+
return 0;
|
|
51
|
+
if ([full, bare].some((f) => /^[/?#]/.test(q.slice(f.length)) && q.startsWith(f)))
|
|
52
|
+
return 1;
|
|
53
|
+
return rankAgainst(q, [full, bare]);
|
|
54
|
+
}
|
|
55
|
+
function rankAgainst(q, fields) {
|
|
56
|
+
if (fields.some((f) => f === q))
|
|
57
|
+
return 0;
|
|
58
|
+
if (fields.some((f) => f.startsWith(q)))
|
|
59
|
+
return 1;
|
|
60
|
+
if (fields.some((f) => f.includes(q)))
|
|
61
|
+
return 2;
|
|
62
|
+
return undefined;
|
|
63
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/** The candidate closest to `input` by edit distance, if any is close enough. */
|
|
2
|
+
export function suggest(input, candidates) {
|
|
3
|
+
let best;
|
|
4
|
+
let bestDistance = Math.max(2, Math.floor(input.length / 3)) + 1;
|
|
5
|
+
for (const candidate of candidates) {
|
|
6
|
+
// A prefix like "storefront" for "storefront-team" counts as a near miss.
|
|
7
|
+
const prefix = input.length >= 3 && (candidate.startsWith(input) || input.startsWith(candidate));
|
|
8
|
+
const d = prefix ? Math.min(1, distance(input, candidate)) : distance(input, candidate);
|
|
9
|
+
if (d < bestDistance || (d === bestDistance && best !== undefined && candidate < best)) {
|
|
10
|
+
best = candidate;
|
|
11
|
+
bestDistance = d;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
return best;
|
|
15
|
+
}
|
|
16
|
+
export function didYouMean(input, candidates) {
|
|
17
|
+
const s = suggest(input, candidates);
|
|
18
|
+
return s === undefined ? "" : `; did you mean "${s}"?`;
|
|
19
|
+
}
|
|
20
|
+
function distance(a, b) {
|
|
21
|
+
let prev = Array.from({ length: b.length + 1 }, (_, j) => j);
|
|
22
|
+
for (let i = 1; i <= a.length; i++) {
|
|
23
|
+
const cur = [i];
|
|
24
|
+
for (let j = 1; j <= b.length; j++) {
|
|
25
|
+
cur[j] = Math.min(prev[j] + 1, cur[j - 1] + 1, prev[j - 1] + (a[i - 1] === b[j - 1] ? 0 : 1));
|
|
26
|
+
}
|
|
27
|
+
prev = cur;
|
|
28
|
+
}
|
|
29
|
+
return prev[b.length];
|
|
30
|
+
}
|