@goodbones/core 0.1.0-beta.1 → 0.1.0-beta.11

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.
Files changed (150) hide show
  1. package/build/dts/core/coverage.d.ts +20 -0
  2. package/build/dts/core/coverage.d.ts.map +1 -1
  3. package/build/dts/core/graph.d.ts +2 -0
  4. package/build/dts/core/graph.d.ts.map +1 -1
  5. package/build/dts/core/imports.d.ts +3 -1
  6. package/build/dts/core/imports.d.ts.map +1 -1
  7. package/build/dts/core/slack.d.ts +22 -0
  8. package/build/dts/core/slack.d.ts.map +1 -0
  9. package/build/dts/core/structure.d.ts +1 -0
  10. package/build/dts/core/structure.d.ts.map +1 -1
  11. package/build/dts/domain/architecture-config.d.ts +19 -3
  12. package/build/dts/domain/architecture-config.d.ts.map +1 -1
  13. package/build/dts/domain/architecture-error.d.ts +8 -0
  14. package/build/dts/domain/architecture-error.d.ts.map +1 -1
  15. package/build/dts/domain/facts.d.ts +1 -0
  16. package/build/dts/domain/facts.d.ts.map +1 -1
  17. package/build/dts/domain/manifest-location.d.ts +9 -0
  18. package/build/dts/domain/manifest-location.d.ts.map +1 -0
  19. package/build/dts/domain/snapshot.d.ts +356 -0
  20. package/build/dts/domain/snapshot.d.ts.map +1 -0
  21. package/build/dts/domain/violation.d.ts +1 -1
  22. package/build/dts/domain/violation.d.ts.map +1 -1
  23. package/build/dts/index.d.ts +22 -10
  24. package/build/dts/index.d.ts.map +1 -1
  25. package/build/dts/infrastructure/file-system-fake.d.ts.map +1 -1
  26. package/build/dts/infrastructure/file-system-live.d.ts.map +1 -1
  27. package/build/dts/infrastructure/manifest-file.d.ts +11 -2
  28. package/build/dts/infrastructure/manifest-file.d.ts.map +1 -1
  29. package/build/dts/infrastructure/manifest-include.d.ts +16 -0
  30. package/build/dts/infrastructure/manifest-include.d.ts.map +1 -0
  31. package/build/dts/infrastructure/syntax-matcher-fake.d.ts +10 -0
  32. package/build/dts/infrastructure/syntax-matcher-fake.d.ts.map +1 -0
  33. package/build/dts/infrastructure/walk.d.ts +12 -1
  34. package/build/dts/infrastructure/walk.d.ts.map +1 -1
  35. package/build/dts/load/extension.d.ts +33 -0
  36. package/build/dts/load/extension.d.ts.map +1 -0
  37. package/build/dts/load/policy.d.ts +10 -0
  38. package/build/dts/load/policy.d.ts.map +1 -1
  39. package/build/dts/manifest/compile.d.ts +11 -1
  40. package/build/dts/manifest/compile.d.ts.map +1 -1
  41. package/build/dts/manifest/expand.d.ts +27 -0
  42. package/build/dts/manifest/expand.d.ts.map +1 -0
  43. package/build/dts/manifest/extension.d.ts +8 -0
  44. package/build/dts/manifest/extension.d.ts.map +1 -0
  45. package/build/dts/manifest/glob.d.ts +1 -0
  46. package/build/dts/manifest/glob.d.ts.map +1 -1
  47. package/build/dts/manifest/infer.d.ts +55 -0
  48. package/build/dts/manifest/infer.d.ts.map +1 -0
  49. package/build/dts/manifest/json-schema.d.ts +14 -0
  50. package/build/dts/manifest/json-schema.d.ts.map +1 -0
  51. package/build/dts/manifest/manifest.d.ts +26 -1
  52. package/build/dts/manifest/manifest.d.ts.map +1 -1
  53. package/build/dts/ports/file-system.d.ts +1 -0
  54. package/build/dts/ports/file-system.d.ts.map +1 -1
  55. package/build/dts/ports/language.d.ts +4 -0
  56. package/build/dts/ports/language.d.ts.map +1 -1
  57. package/build/dts/ports/syntax-matcher.d.ts +21 -0
  58. package/build/dts/ports/syntax-matcher.d.ts.map +1 -0
  59. package/build/dts/testing.d.ts +1 -0
  60. package/build/dts/testing.d.ts.map +1 -1
  61. package/build/esm/core/coverage.js +107 -32
  62. package/build/esm/core/coverage.js.map +1 -1
  63. package/build/esm/core/graph.js +45 -0
  64. package/build/esm/core/graph.js.map +1 -1
  65. package/build/esm/core/imports.js +14 -9
  66. package/build/esm/core/imports.js.map +1 -1
  67. package/build/esm/core/slack.js +76 -0
  68. package/build/esm/core/slack.js.map +1 -0
  69. package/build/esm/core/structure.js +6 -3
  70. package/build/esm/core/structure.js.map +1 -1
  71. package/build/esm/domain/architecture-config.js +28 -4
  72. package/build/esm/domain/architecture-config.js.map +1 -1
  73. package/build/esm/domain/architecture-error.js +27 -0
  74. package/build/esm/domain/architecture-error.js.map +1 -1
  75. package/build/esm/domain/manifest-location.js +21 -0
  76. package/build/esm/domain/manifest-location.js.map +1 -0
  77. package/build/esm/domain/snapshot.js +181 -0
  78. package/build/esm/domain/snapshot.js.map +1 -0
  79. package/build/esm/domain/violation.js.map +1 -1
  80. package/build/esm/index.js +28 -9
  81. package/build/esm/index.js.map +1 -1
  82. package/build/esm/infrastructure/file-system-fake.js +10 -0
  83. package/build/esm/infrastructure/file-system-fake.js.map +1 -1
  84. package/build/esm/infrastructure/file-system-live.js +9 -1
  85. package/build/esm/infrastructure/file-system-live.js.map +1 -1
  86. package/build/esm/infrastructure/manifest-file.js +158 -7
  87. package/build/esm/infrastructure/manifest-file.js.map +1 -1
  88. package/build/esm/infrastructure/manifest-include.js +187 -0
  89. package/build/esm/infrastructure/manifest-include.js.map +1 -0
  90. package/build/esm/infrastructure/syntax-matcher-fake.js +33 -0
  91. package/build/esm/infrastructure/syntax-matcher-fake.js.map +1 -0
  92. package/build/esm/infrastructure/walk.js +106 -4
  93. package/build/esm/infrastructure/walk.js.map +1 -1
  94. package/build/esm/load/extension.js +2 -0
  95. package/build/esm/load/extension.js.map +1 -0
  96. package/build/esm/load/policy.js +49 -2
  97. package/build/esm/load/policy.js.map +1 -1
  98. package/build/esm/manifest/compile.js +50 -27
  99. package/build/esm/manifest/compile.js.map +1 -1
  100. package/build/esm/manifest/expand.js +116 -0
  101. package/build/esm/manifest/expand.js.map +1 -0
  102. package/build/esm/manifest/extension.js +2 -0
  103. package/build/esm/manifest/extension.js.map +1 -0
  104. package/build/esm/manifest/glob.js +3 -0
  105. package/build/esm/manifest/glob.js.map +1 -1
  106. package/build/esm/manifest/infer.js +455 -0
  107. package/build/esm/manifest/infer.js.map +1 -0
  108. package/build/esm/manifest/json-schema.js +169 -0
  109. package/build/esm/manifest/json-schema.js.map +1 -0
  110. package/build/esm/manifest/manifest.js +127 -5
  111. package/build/esm/manifest/manifest.js.map +1 -1
  112. package/build/esm/ports/syntax-matcher.js +12 -0
  113. package/build/esm/ports/syntax-matcher.js.map +1 -0
  114. package/build/esm/testing.js +1 -0
  115. package/build/esm/testing.js.map +1 -1
  116. package/package.json +9 -3
  117. package/schema/architecture-node.schema.json +1518 -0
  118. package/schema/architecture.schema.json +4498 -0
  119. package/schema/conformance.schema.json +1019 -0
  120. package/src/core/coverage.ts +164 -34
  121. package/src/core/graph.ts +48 -0
  122. package/src/core/imports.ts +29 -13
  123. package/src/core/slack.ts +135 -0
  124. package/src/core/structure.ts +15 -8
  125. package/src/domain/architecture-config.ts +31 -4
  126. package/src/domain/architecture-error.ts +30 -0
  127. package/src/domain/facts.ts +9 -4
  128. package/src/domain/manifest-location.ts +41 -0
  129. package/src/domain/snapshot.ts +402 -0
  130. package/src/domain/violation.ts +4 -1
  131. package/src/index.ts +149 -7
  132. package/src/infrastructure/file-system-fake.ts +12 -0
  133. package/src/infrastructure/file-system-live.ts +8 -1
  134. package/src/infrastructure/manifest-file.ts +204 -8
  135. package/src/infrastructure/manifest-include.ts +318 -0
  136. package/src/infrastructure/syntax-matcher-fake.ts +51 -0
  137. package/src/infrastructure/walk.ts +127 -3
  138. package/src/load/extension.ts +66 -0
  139. package/src/load/policy.ts +77 -2
  140. package/src/manifest/compile.ts +81 -30
  141. package/src/manifest/expand.ts +183 -0
  142. package/src/manifest/extension.ts +41 -0
  143. package/src/manifest/glob.ts +5 -0
  144. package/src/manifest/infer.ts +643 -0
  145. package/src/manifest/json-schema.ts +210 -0
  146. package/src/manifest/manifest.ts +206 -10
  147. package/src/ports/file-system.ts +3 -0
  148. package/src/ports/language.ts +17 -0
  149. package/src/ports/syntax-matcher.ts +42 -0
  150. package/src/testing.ts +1 -0
@@ -0,0 +1,318 @@
1
+ import * as path from "node:path";
2
+
3
+ import { ConfigInvalid } from "../domain/architecture-error.js";
4
+ import {
5
+ type ManifestLocator,
6
+ type ManifestPath,
7
+ renderManifestPath,
8
+ } from "../domain/manifest-location.js";
9
+
10
+ // A manifest split across files. `{ include: "<path>" }` standing anywhere in
11
+ // a manifest — a node under `tree`, a whole section, one entry of a list — is
12
+ // replaced by the value of the file it names, read by the same parser and
13
+ // resolved relative to the file that wrote the reference. This runs on the raw
14
+ // value before anything else looks at it: the `defs`/`use` expansion sees one
15
+ // document, the decoder sees one document, and the schema never learns that a
16
+ // reference existed.
17
+ //
18
+ // What is deliberately not here: no merging (a value is replaced, full stop),
19
+ // no parameters, no glob of files. An included file is YAML or JSON only — a
20
+ // module would make a data manifest readable by one runtime — and a file may
21
+ // carry a top-level `defs` of its own, which joins the manifest's under one
22
+ // namespace, and a `$schema` for its editor, which is dropped.
23
+
24
+ // One file, as the reader parsed it.
25
+ export type SourceDocument = {
26
+ readonly value: unknown;
27
+ readonly locate: ManifestLocator | undefined;
28
+ };
29
+
30
+ export type IncludeReader = {
31
+ readonly exists: (file: string) => boolean;
32
+ // Throws `ConfigInvalid` naming the file when it does not parse.
33
+ readonly read: (file: string) => SourceDocument;
34
+ };
35
+
36
+ export type IncludedManifest = {
37
+ readonly value: unknown;
38
+ // Answers for every file: a position inside an included file carries that
39
+ // file's path, relative to the root manifest's directory.
40
+ readonly locate: ManifestLocator | undefined;
41
+ // Every file that took part, root first, as absolute paths.
42
+ readonly files: ReadonlyArray<string>;
43
+ };
44
+
45
+ const INCLUDABLE = [".yaml", ".yml", ".json"];
46
+
47
+ const isRecord = (value: unknown): value is Record<string, unknown> =>
48
+ typeof value === "object" && value !== null && !Array.isArray(value);
49
+
50
+ type IncludeReference = Record<string, unknown> & { readonly include: unknown };
51
+
52
+ const isInclude = (value: unknown): value is IncludeReference =>
53
+ isRecord(value) && "include" in value;
54
+
55
+ const isPrefix = (prefix: ManifestPath, whole: ManifestPath): boolean =>
56
+ prefix.length <= whole.length && prefix.every((segment, index) => whole[index] === segment);
57
+
58
+ // Which file a region of the assembled document came from: everything under
59
+ // `at` was written in the file whose locator this is, starting at `origin`
60
+ // there. The root file mounts at the root; each include mounts where it
61
+ // landed; a fragment hoisted out of an included file mounts under `defs`; and
62
+ // a list item that moved — spliced in from another file, or shifted by a
63
+ // splice before it — mounts on its own, since its index is not the one it
64
+ // was written at.
65
+ type Mount = {
66
+ readonly at: ManifestPath;
67
+ readonly origin: ManifestPath;
68
+ readonly label: string | undefined;
69
+ readonly locate: ManifestLocator | undefined;
70
+ };
71
+
72
+ // The file being walked.
73
+ type Source = {
74
+ readonly file: string;
75
+ // How the file is named in a message: its path relative to the root
76
+ // manifest's directory. The root file itself has none, and is named by
77
+ // whoever reports the error.
78
+ readonly label: string | undefined;
79
+ readonly locate: ManifestLocator | undefined;
80
+ // Every file on the way here, for the cycle check.
81
+ readonly stack: ReadonlyArray<string>;
82
+ };
83
+
84
+ type Opened = {
85
+ readonly document: SourceDocument;
86
+ readonly source: Source;
87
+ };
88
+
89
+ const makeLocator = (mounts: ReadonlyArray<Mount>): ManifestLocator | undefined => {
90
+ if (!mounts.some((mount) => mount.locate !== undefined)) return undefined;
91
+ return (manifestPath) => {
92
+ const innermost = mounts
93
+ .filter((mount) => isPrefix(mount.at, manifestPath))
94
+ .sort((a, b) => a.at.length - b.at.length)
95
+ .at(-1);
96
+ if (innermost === undefined) return null;
97
+ const found =
98
+ innermost.locate?.([...innermost.origin, ...manifestPath.slice(innermost.at.length)]) ?? null;
99
+ if (found === null) return null;
100
+ return innermost.label === undefined ? found : { ...found, file: innermost.label };
101
+ };
102
+ };
103
+
104
+ export const expandIncludes = (
105
+ rootPath: string,
106
+ root: SourceDocument,
107
+ reader: IncludeReader,
108
+ ): IncludedManifest => {
109
+ const rootDirectory = path.dirname(rootPath);
110
+ const labelOf = (file: string): string =>
111
+ path.relative(rootDirectory, file).split(path.sep).join("/");
112
+ const nameOf = (source: Source): string => source.label ?? path.basename(rootPath);
113
+
114
+ const files: Array<string> = [rootPath];
115
+ const mounts: Array<Mount> = [{ at: [], origin: [], label: undefined, locate: root.locate }];
116
+ // Fragments hoisted out of included files, and the file each name came from.
117
+ const hoisted: Record<string, unknown> = {};
118
+ const definedIn = new Map<string, string>();
119
+
120
+ const refuse = (source: Source, origin: ManifestPath, detail: string): never => {
121
+ const position = source.locate?.(origin) ?? null;
122
+ const at =
123
+ position === null
124
+ ? ""
125
+ : `${nameOf(source)}:${String(position.line)}:${String(position.column)} `;
126
+ throw new ConfigInvalid({
127
+ configPath: rootPath,
128
+ detail: `the manifest does not include:\n ${at}${renderManifestPath(origin)}: ${detail}`,
129
+ });
130
+ };
131
+
132
+ // Checks a reference and reads the file it names. Nothing is placed yet:
133
+ // where the value lands depends on whether it is a list spliced into a list.
134
+ const open = (reference: IncludeReference, origin: ManifestPath, source: Source): Opened => {
135
+ const { include: specifier, ...rest } = reference;
136
+ if (typeof specifier !== "string") {
137
+ return refuse(source, origin, "`include` names a file, as a string.");
138
+ }
139
+ const shown = `\`include: ${JSON.stringify(specifier)}\``;
140
+ const beside = Object.keys(rest);
141
+ if (beside.length > 0) {
142
+ return refuse(
143
+ source,
144
+ origin,
145
+ `${shown} stands alone: an included file is replaced whole, so there is nothing for ` +
146
+ `${beside.map((key) => `\`${key}\``).join(", ")} to override. ` +
147
+ `To override a fragment, put it under \`defs\` and \`use\` it.`,
148
+ );
149
+ }
150
+ const target = path.resolve(path.dirname(source.file), specifier);
151
+ if (!INCLUDABLE.includes(path.extname(target).toLowerCase())) {
152
+ return refuse(
153
+ source,
154
+ origin,
155
+ `${shown} names a file that is not YAML or JSON. Only a data file can be included: ` +
156
+ `a module would make the manifest readable by one runtime only.`,
157
+ );
158
+ }
159
+ if (source.stack.includes(target)) {
160
+ return refuse(
161
+ source,
162
+ origin,
163
+ `${shown} includes a file that is already being included: ` +
164
+ `${[...source.stack, target].map(labelOf).join(" → ")}.`,
165
+ );
166
+ }
167
+ if (!reader.exists(target)) {
168
+ return refuse(
169
+ source,
170
+ origin,
171
+ `${shown} names a file that does not exist (looked for ${labelOf(target)}, ` +
172
+ `relative to ${nameOf(source)}).`,
173
+ );
174
+ }
175
+ const document = reader.read(target);
176
+ files.push(target);
177
+ return {
178
+ document,
179
+ source: {
180
+ file: target,
181
+ label: labelOf(target),
182
+ locate: document.locate,
183
+ stack: [...source.stack, target],
184
+ },
185
+ };
186
+ };
187
+
188
+ // Places an opened file at `at`, whole, and walks it so no reference is
189
+ // left inside. A top-level `defs` in the file joins the root's, and its
190
+ // `$schema` is for the editor — unless the file was included somewhere
191
+ // under `defs`, where it is a fragment or the map itself, and a key by
192
+ // either name is the author's.
193
+ const place = (opened: Opened, at: ManifestPath): unknown => {
194
+ const { document, source } = opened;
195
+ mounts.push({ at, origin: [], label: source.label, locate: source.locate });
196
+ if (!isRecord(document.value) || at[0] === "defs") {
197
+ return walk(document.value, at, [], source);
198
+ }
199
+ const { $schema: _schema, defs, ...body } = document.value;
200
+ if (defs !== undefined) {
201
+ if (!isRecord(defs)) {
202
+ return refuse(source, ["defs"], "`defs` must be a map of named fragments.");
203
+ }
204
+ for (const [name, fragment] of Object.entries(defs)) {
205
+ const already = definedIn.get(name);
206
+ if (already !== undefined) {
207
+ return refuse(
208
+ source,
209
+ ["defs", name],
210
+ `\`defs.${name}\` is already defined in ${already}. Every file's \`defs\` share ` +
211
+ `one namespace; rename one of them.`,
212
+ );
213
+ }
214
+ definedIn.set(name, nameOf(source));
215
+ mounts.push({
216
+ at: ["defs", name],
217
+ origin: ["defs", name],
218
+ label: source.label,
219
+ locate: source.locate,
220
+ });
221
+ hoisted[name] = walk(fragment, ["defs", name], ["defs", name], source);
222
+ }
223
+ }
224
+ return walk(body, at, [], source);
225
+ };
226
+
227
+ // Appends each item of `list` to `items`. An include standing as an item
228
+ // whose file holds a list is spliced in, so a list can be split across
229
+ // files; an item that does not sit at the index it was written at is
230
+ // mounted on its own.
231
+ const append = (
232
+ items: Array<unknown>,
233
+ list: ReadonlyArray<unknown>,
234
+ at: ManifestPath,
235
+ origin: ManifestPath,
236
+ source: Source,
237
+ spliced: boolean,
238
+ ): void => {
239
+ for (const [index, item] of list.entries()) {
240
+ const itemOrigin = [...origin, index];
241
+ const slot = [...at, items.length];
242
+ if (isInclude(item)) {
243
+ const opened = open(item, itemOrigin, source);
244
+ if (Array.isArray(opened.document.value)) {
245
+ append(items, opened.document.value, at, [], opened.source, true);
246
+ } else {
247
+ items.push(place(opened, slot));
248
+ }
249
+ continue;
250
+ }
251
+ if (spliced || items.length !== index) {
252
+ mounts.push({ at: slot, origin: itemOrigin, label: source.label, locate: source.locate });
253
+ }
254
+ items.push(walk(item, slot, itemOrigin, source));
255
+ }
256
+ };
257
+
258
+ const walk = (
259
+ value: unknown,
260
+ at: ManifestPath,
261
+ origin: ManifestPath,
262
+ source: Source,
263
+ ): unknown => {
264
+ if (isInclude(value)) return place(open(value, origin, source), at);
265
+
266
+ if (Array.isArray(value)) {
267
+ const items: Array<unknown> = [];
268
+ append(items, value, at, origin, source, false);
269
+ return items;
270
+ }
271
+
272
+ if (isRecord(value)) {
273
+ const entries: Record<string, unknown> = {};
274
+ for (const [key, item] of Object.entries(value)) {
275
+ entries[key] = walk(item, [...at, key], [...origin, key], source);
276
+ }
277
+ return entries;
278
+ }
279
+
280
+ return value;
281
+ };
282
+
283
+ const rootSource: Source = {
284
+ file: rootPath,
285
+ label: undefined,
286
+ locate: root.locate,
287
+ stack: [rootPath],
288
+ };
289
+ const value = walk(root.value, [], [], rootSource);
290
+
291
+ // Nothing was included: the value and the locator are the reader's own.
292
+ if (files.length === 1) return { value, locate: root.locate, files };
293
+
294
+ const names = Object.keys(hoisted);
295
+ if (names.length === 0 || !isRecord(value)) {
296
+ return { value, locate: makeLocator(mounts), files };
297
+ }
298
+
299
+ const own = value.defs;
300
+ if (own !== undefined && !isRecord(own)) {
301
+ return refuse(rootSource, ["defs"], "`defs` must be a map of named fragments.");
302
+ }
303
+ for (const name of names) {
304
+ if (own !== undefined && name in own) {
305
+ return refuse(
306
+ rootSource,
307
+ ["defs", name],
308
+ `\`defs.${name}\` is also defined in ${definedIn.get(name) ?? "an included file"}. ` +
309
+ `Every file's \`defs\` share one namespace; rename one of them.`,
310
+ );
311
+ }
312
+ }
313
+ return {
314
+ value: { ...value, defs: { ...(own ?? {}), ...hoisted } },
315
+ locate: makeLocator(mounts),
316
+ files,
317
+ };
318
+ };
@@ -0,0 +1,51 @@
1
+ import type { SyntaxMatch, SyntaxMatcher } from "../ports/syntax-matcher.js";
2
+
3
+ // One match as a test states it: the text, its captures as a plain object,
4
+ // the anchor, and optionally a line — the rest of the range is filled in.
5
+ // `rule` narrows the match to one rule (compared by its JSON), so a test
6
+ // about two `syntax` terms can answer each differently; omit it to answer
7
+ // every rule the same.
8
+ export type StagedMatch = {
9
+ readonly text: string;
10
+ readonly captures?: Readonly<Record<string, string>>;
11
+ readonly anchor?: string | null;
12
+ readonly line?: number;
13
+ readonly rule?: unknown;
14
+ };
15
+
16
+ const ZERO = { line: 0, column: 0 };
17
+
18
+ // Keyed by the source text, as the extractor fake is: a core test states
19
+ // what a snippet's syntax yields and never the parse, which has its own
20
+ // tests in the matcher package. A text not staged parses to no matches; a
21
+ // file whose extension is not among `extensions` does not parse at all,
22
+ // which is the shape of a language with no matcher.
23
+ export const makeSyntaxMatcherFake = (
24
+ staged: Readonly<Record<string, ReadonlyArray<StagedMatch>>>,
25
+ extensions: ReadonlyArray<string> | null = null,
26
+ ): SyntaxMatcher => ({
27
+ parse: (file, text) => {
28
+ if (extensions !== null && !extensions.some((extension) => file.endsWith(extension))) {
29
+ return null;
30
+ }
31
+ const matches = staged[text] ?? [];
32
+ return {
33
+ // A staged match's line is what a position is anchored by.
34
+ anchorAt: (position) => matches.find((one) => one.line === position.line)?.anchor ?? null,
35
+ findAll: (rule) => {
36
+ const asked = JSON.stringify(rule);
37
+ return matches
38
+ .filter((one) => one.rule === undefined || JSON.stringify(one.rule) === asked)
39
+ .map((one): SyntaxMatch => ({
40
+ text: one.text,
41
+ captures: new Map(Object.entries(one.captures ?? {})),
42
+ range: {
43
+ start: one.line === undefined ? ZERO : { line: one.line, column: 0 },
44
+ end: one.line === undefined ? ZERO : { line: one.line, column: one.text.length },
45
+ },
46
+ anchor: one.anchor ?? null,
47
+ }));
48
+ },
49
+ };
50
+ },
51
+ });
@@ -1,4 +1,4 @@
1
- import { readdirSync, statSync } from "node:fs";
1
+ import { existsSync, readdirSync, readFileSync, statSync } from "node:fs";
2
2
  import * as path from "node:path";
3
3
 
4
4
  import type { Language } from "../ports/language.js";
@@ -24,13 +24,16 @@ export type WalkedLanguage = Pick<Language, "extensions" | "ignoredFiles">;
24
24
  // Which files count as source is the languages' to say: the extension set is
25
25
  // the union of theirs, and so is the set of files to step over. A root that
26
26
  // names a file is that file, whatever its extension — `architecture check
27
- // <file>` is a reasonable thing to type.
27
+ // <file>` is a reasonable thing to type. `widened` adds the extensions a
28
+ // campaign's scope names beyond the packs' — a file the pack cannot parse
29
+ // contributes no facts, and only a campaign that asked for it sees it.
28
30
  export const listSourceFiles = (
29
31
  repoRoot: string,
30
32
  roots: ReadonlyArray<string>,
31
33
  languages: ReadonlyArray<WalkedLanguage>,
34
+ widened: ReadonlyArray<string> = [],
32
35
  ): ReadonlyArray<string> => {
33
- const extensions = new Set(languages.flatMap((language) => language.extensions));
36
+ const extensions = new Set([...languages.flatMap((language) => language.extensions), ...widened]);
34
37
  const ignored = languages.flatMap((language) => language.ignoredFiles);
35
38
  const isSource = (entry: string): boolean =>
36
39
  extensions.has(path.extname(entry)) && !ignored.some((pattern) => pattern.test(entry));
@@ -52,3 +55,124 @@ export const listSourceFiles = (
52
55
  }
53
56
  return found.sort();
54
57
  };
58
+
59
+ // What `infer` needs to know about a language beyond the walker's two: what
60
+ // marks a package of it, and where a package keeps its source.
61
+ export type PackagedLanguage = Pick<Language, "packageMarkers" | "sourceRoots">;
62
+
63
+ export type PackageRoot = {
64
+ // Repo-relative folder with forward slashes; `""` is the repository itself.
65
+ readonly root: string;
66
+ // The package's source folder, repo-relative, when one of the language's
67
+ // `sourceRoots` exists under it; otherwise `null`, and source sits at the root.
68
+ readonly source: string | null;
69
+ };
70
+
71
+ // The packages under (and above) `roots`: every folder holding one of the
72
+ // languages' marker files, the walk roots' own ancestors included, so a walk
73
+ // rooted at `src/` still finds the `package.json` beside it. Sorted by path.
74
+ export const listPackageRoots = (
75
+ repoRoot: string,
76
+ roots: ReadonlyArray<string>,
77
+ languages: ReadonlyArray<PackagedLanguage>,
78
+ ): ReadonlyArray<PackageRoot> => {
79
+ const markers = languages.flatMap((language) => language.packageMarkers);
80
+ const sourceRoots = languages.flatMap((language) => language.sourceRoots);
81
+ if (markers.length === 0) return [];
82
+
83
+ const isDirectory = (absolute: string): boolean => {
84
+ try {
85
+ return statSync(absolute).isDirectory();
86
+ } catch {
87
+ return false;
88
+ }
89
+ };
90
+ const relative = (absolute: string): string =>
91
+ path.relative(repoRoot, absolute).replaceAll(path.sep, "/");
92
+
93
+ const found = new Map<string, PackageRoot>();
94
+ const consider = (absolute: string): void => {
95
+ const key = relative(absolute);
96
+ if (found.has(key)) return;
97
+ if (!markers.some((marker) => existsSync(path.join(absolute, marker)))) return;
98
+ const source = sourceRoots.find((name) => isDirectory(path.join(absolute, name)));
99
+ found.set(key, {
100
+ root: key,
101
+ source: source === undefined ? null : relative(path.join(absolute, source)),
102
+ });
103
+ };
104
+ const walk = (absolute: string): void => {
105
+ consider(absolute);
106
+ for (const entry of readdirSync(absolute)) {
107
+ if (SKIPPED.has(entry)) continue;
108
+ const child = path.join(absolute, entry);
109
+ if (isDirectory(child)) walk(child);
110
+ }
111
+ };
112
+
113
+ const top = path.resolve(repoRoot);
114
+ for (const root of roots) {
115
+ let absolute = path.resolve(repoRoot, root);
116
+ if (!isDirectory(absolute)) absolute = path.dirname(absolute);
117
+ walk(absolute);
118
+ // The ancestors, up to the repository: a package is as often above the
119
+ // walk root as inside it.
120
+ for (let above = absolute; above.startsWith(top); above = path.dirname(above)) {
121
+ consider(above);
122
+ if (above === top) break;
123
+ }
124
+ }
125
+ return [...found.values()].sort((a, b) => a.root.localeCompare(b.root));
126
+ };
127
+
128
+ export type WorkspaceProject = {
129
+ readonly name: string;
130
+ // Repo-relative folder with forward slashes.
131
+ readonly root: string;
132
+ };
133
+
134
+ // The workspace's projects, for an `nx` perimeter: every folder under the
135
+ // roots holding a `project.json`, named by it — or, when it names nothing,
136
+ // by the `package.json` beside it, or the folder. Read from the files
137
+ // rather than from `nx graph`, so no process is spawned and a checkout
138
+ // without the workspace's tooling installed still has its sectors.
139
+ export const listWorkspaceProjects = (
140
+ repoRoot: string,
141
+ roots: ReadonlyArray<string>,
142
+ ): ReadonlyArray<WorkspaceProject> => {
143
+ const found: Array<WorkspaceProject> = [];
144
+ const nameIn = (absolute: string, file: string): string | null => {
145
+ try {
146
+ const parsed = JSON.parse(readFileSync(path.join(absolute, file), "utf8")) as unknown;
147
+ const name =
148
+ typeof parsed === "object" && parsed !== null
149
+ ? (parsed as { readonly name?: unknown }).name
150
+ : undefined;
151
+ return typeof name === "string" && name !== "" ? name : null;
152
+ } catch {
153
+ return null;
154
+ }
155
+ };
156
+ const walk = (absolute: string): void => {
157
+ if (existsSync(path.join(absolute, "project.json"))) {
158
+ const root = path.relative(repoRoot, absolute).replaceAll(path.sep, "/");
159
+ found.push({
160
+ name:
161
+ nameIn(absolute, "project.json") ??
162
+ nameIn(absolute, "package.json") ??
163
+ path.basename(absolute),
164
+ root,
165
+ });
166
+ }
167
+ for (const entry of readdirSync(absolute)) {
168
+ if (SKIPPED.has(entry)) continue;
169
+ const child = path.join(absolute, entry);
170
+ if (statSync(child).isDirectory()) walk(child);
171
+ }
172
+ };
173
+ for (const root of roots) {
174
+ const absolute = path.resolve(repoRoot, root);
175
+ if (statSync(absolute).isDirectory()) walk(absolute);
176
+ }
177
+ return found.sort((a, b) => a.root.localeCompare(b.root));
178
+ };
@@ -0,0 +1,66 @@
1
+ import type * as Result from "effect/Result";
2
+
3
+ import type { ResolveScope } from "../domain/architecture-config.js";
4
+ import type { ConfigInvalid, PatternInvalid } from "../domain/architecture-error.js";
5
+ import type { ManifestExtension } from "../manifest/extension.js";
6
+ import type { Manifest } from "../manifest/manifest.js";
7
+ import type { FactExtractor } from "../ports/fact-extractor.js";
8
+ import type { FileSystem } from "../ports/file-system.js";
9
+ import type { Language } from "../ports/language.js";
10
+ import type { SyntaxMatcher } from "../ports/syntax-matcher.js";
11
+
12
+ // The load half of a family the core does not own: what `loadPolicy` hands it,
13
+ // and what it hands back.
14
+ //
15
+ // A host composes the extensions it wants — `loadPolicy({ …, extensions: [
16
+ // campaignsExtension({ functions, reports }) ] })` — and reads its family's
17
+ // state back off `LoadedPolicy.extensions` through whatever typed accessor
18
+ // that package exports. The core carries the value and never looks inside it.
19
+ //
20
+ // An extension is given the same things the core's own families are given at
21
+ // load: the routing extractor and syntax matcher, the file system port, the
22
+ // language packs, and which scope covers a given file. It is not given the
23
+ // resolver, which is built after every rule has been probed — a family that
24
+ // needs one resolves at evaluation, off `LoadedPolicy`.
25
+
26
+ export type ExtensionRoute = {
27
+ readonly language: Language;
28
+ readonly scope: ResolveScope;
29
+ };
30
+
31
+ export type ExtensionContext<Spec = unknown> = {
32
+ // What this family's `decode` returned.
33
+ readonly spec: Spec;
34
+ readonly configPath: string;
35
+ readonly repoRoot: string;
36
+ // The manifest the core decoded — `resolve`, `aliases`, `tree`, the
37
+ // families the core owns. The family's own keys are in `spec`.
38
+ readonly config: Manifest;
39
+ readonly languages: ReadonlyArray<Language>;
40
+ readonly fileSystem: FileSystem;
41
+ // Routes each file to the extractor of the language whose scope covers it:
42
+ // the same one the core parsed its own probes with.
43
+ readonly extractor: FactExtractor;
44
+ // Routes each file to the syntax matcher of its scope's language; `parse`
45
+ // answers `null` for a language carrying none.
46
+ readonly syntax: SyntaxMatcher;
47
+ readonly routeFor: (file: string) => ExtensionRoute | undefined;
48
+ readonly now: number;
49
+ };
50
+
51
+ export type LoadedExtension<Value = unknown> = {
52
+ // Carried on `LoadedPolicy.extensions`, keyed by the extension's id.
53
+ readonly value: Value;
54
+ // Rules of this family that do not report their own probe. Merged into the
55
+ // one refusal the loader raises for every family at once, so a vacuous
56
+ // campaign and a vacuous import rule are reported in the same sentence.
57
+ readonly vacuous?: ReadonlyArray<string> | undefined;
58
+ };
59
+
60
+ export type PolicyExtension<Spec = unknown, Value = unknown> = ManifestExtension<Spec> & {
61
+ // A method, like `decode`, so the loader can hold every extension at one
62
+ // erased type after their specs have been decoded.
63
+ load(
64
+ context: ExtensionContext<Spec>,
65
+ ): Result.Result<LoadedExtension<Value>, ConfigInvalid | PatternInvalid>;
66
+ };