@goodbones/core 0.1.0-beta.2 → 0.1.0-beta.4
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/build/dts/core/graph.d.ts +1 -0
- package/build/dts/core/graph.d.ts.map +1 -1
- package/build/dts/domain/manifest-location.d.ts +2 -0
- package/build/dts/domain/manifest-location.d.ts.map +1 -1
- package/build/dts/index.d.ts +6 -4
- package/build/dts/index.d.ts.map +1 -1
- package/build/dts/infrastructure/manifest-file.d.ts +1 -0
- package/build/dts/infrastructure/manifest-file.d.ts.map +1 -1
- package/build/dts/infrastructure/manifest-include.d.ts +16 -0
- package/build/dts/infrastructure/manifest-include.d.ts.map +1 -0
- package/build/dts/infrastructure/walk.d.ts +6 -0
- package/build/dts/infrastructure/walk.d.ts.map +1 -1
- package/build/dts/manifest/infer.d.ts +55 -0
- package/build/dts/manifest/infer.d.ts.map +1 -0
- package/build/dts/manifest/json-schema.d.ts +2 -0
- package/build/dts/manifest/json-schema.d.ts.map +1 -1
- package/build/dts/manifest/manifest.d.ts +1 -1
- package/build/dts/manifest/manifest.d.ts.map +1 -1
- package/build/dts/ports/language.d.ts +2 -0
- package/build/dts/ports/language.d.ts.map +1 -1
- package/build/esm/core/graph.js +8 -0
- package/build/esm/core/graph.js.map +1 -1
- package/build/esm/domain/manifest-location.js +16 -1
- package/build/esm/domain/manifest-location.js.map +1 -1
- package/build/esm/index.js +6 -3
- package/build/esm/index.js.map +1 -1
- package/build/esm/infrastructure/manifest-file.js +31 -17
- package/build/esm/infrastructure/manifest-file.js.map +1 -1
- package/build/esm/infrastructure/manifest-include.js +187 -0
- package/build/esm/infrastructure/manifest-include.js.map +1 -0
- package/build/esm/infrastructure/walk.js +57 -1
- package/build/esm/infrastructure/walk.js.map +1 -1
- package/build/esm/manifest/infer.js +455 -0
- package/build/esm/manifest/infer.js.map +1 -0
- package/build/esm/manifest/json-schema.js +72 -17
- package/build/esm/manifest/json-schema.js.map +1 -1
- package/build/esm/manifest/manifest.js +7 -18
- package/build/esm/manifest/manifest.js.map +1 -1
- package/package.json +1 -1
- package/schema/architecture-node.schema.json +1518 -0
- package/schema/architecture.schema.json +1102 -691
- package/src/core/graph.ts +12 -0
- package/src/domain/manifest-location.ts +22 -0
- package/src/index.ts +33 -2
- package/src/infrastructure/manifest-file.ts +37 -17
- package/src/infrastructure/manifest-include.ts +318 -0
- package/src/infrastructure/walk.ts +70 -1
- package/src/manifest/infer.ts +643 -0
- package/src/manifest/json-schema.ts +83 -18
- package/src/manifest/manifest.ts +11 -20
- package/src/ports/language.ts +11 -0
package/src/core/graph.ts
CHANGED
|
@@ -215,6 +215,18 @@ const stronglyConnected = (
|
|
|
215
215
|
return components;
|
|
216
216
|
};
|
|
217
217
|
|
|
218
|
+
// Every cycle in the graph, each as its sorted member set. What `infer` asks
|
|
219
|
+
// before it writes a no-cycles rule: a rule the tree fails on day one is a
|
|
220
|
+
// baseline entry, not a description of the tree.
|
|
221
|
+
export const cyclesIn = (graph: Graph): ReadonlyArray<ReadonlyArray<string>> =>
|
|
222
|
+
stronglyConnected(graph.files, graph).filter((component) => {
|
|
223
|
+
const [first] = component;
|
|
224
|
+
return (
|
|
225
|
+
first !== undefined &&
|
|
226
|
+
(component.length > 1 || (graph.edges.get(first) ?? []).includes(first))
|
|
227
|
+
);
|
|
228
|
+
});
|
|
229
|
+
|
|
218
230
|
const evaluateCycles = (rule: CompiledGraphCycleRule, graph: Graph): ReadonlyArray<Violation> => {
|
|
219
231
|
const nodes = graph.files.filter((file) => inScope(rule, file));
|
|
220
232
|
const violations: Array<Violation> = [];
|
|
@@ -11,9 +11,31 @@ export type ManifestPosition = {
|
|
|
11
11
|
// 1-based, as editors count.
|
|
12
12
|
readonly line: number;
|
|
13
13
|
readonly column: number;
|
|
14
|
+
// The file the position is in, when it is not the manifest itself: a
|
|
15
|
+
// manifest assembled from several files through `include` answers with the
|
|
16
|
+
// included file's path, relative to the manifest's own directory. Absent
|
|
17
|
+
// for a position in the manifest file.
|
|
18
|
+
readonly file?: string | undefined;
|
|
14
19
|
};
|
|
15
20
|
|
|
16
21
|
// Answers with the position of the value at `path`, or the nearest ancestor
|
|
17
22
|
// that exists when the path names something the file does not contain (a
|
|
18
23
|
// missing key), or `null` when the source has no positions to give.
|
|
19
24
|
export type ManifestLocator = (path: ManifestPath) => ManifestPosition | null;
|
|
25
|
+
|
|
26
|
+
const isIdentifier = (key: string): boolean => /^[A-Za-z_$][\w$]*$/.test(key);
|
|
27
|
+
|
|
28
|
+
// `tree["~/core/"].members[0].subject` — dotted where a key reads as a name,
|
|
29
|
+
// bracketed where it does not, so a node key that is a path pattern stays
|
|
30
|
+
// legible.
|
|
31
|
+
export const renderManifestPath = (path: ManifestPath): string =>
|
|
32
|
+
path.length === 0
|
|
33
|
+
? "(root)"
|
|
34
|
+
: path
|
|
35
|
+
.map((segment, index) => {
|
|
36
|
+
if (typeof segment === "number") return `[${String(segment)}]`;
|
|
37
|
+
const key = String(segment);
|
|
38
|
+
if (isIdentifier(key)) return index === 0 ? key : `.${key}`;
|
|
39
|
+
return `[${JSON.stringify(key)}]`;
|
|
40
|
+
})
|
|
41
|
+
.join("");
|
package/src/index.ts
CHANGED
|
@@ -37,6 +37,7 @@ export {
|
|
|
37
37
|
export {
|
|
38
38
|
type CompiledGraph,
|
|
39
39
|
compileGraphRules,
|
|
40
|
+
cyclesIn,
|
|
40
41
|
EMPTY_GRAPH_RULES,
|
|
41
42
|
evaluateGraph,
|
|
42
43
|
type Graph,
|
|
@@ -112,6 +113,7 @@ export {
|
|
|
112
113
|
type ManifestLocator,
|
|
113
114
|
type ManifestPath,
|
|
114
115
|
type ManifestPosition,
|
|
116
|
+
renderManifestPath,
|
|
115
117
|
} from "./domain/manifest-location.js";
|
|
116
118
|
export {
|
|
117
119
|
fingerprintOf,
|
|
@@ -127,7 +129,19 @@ export {
|
|
|
127
129
|
type ManifestFile,
|
|
128
130
|
readManifestFile,
|
|
129
131
|
} from "./infrastructure/manifest-file.js";
|
|
130
|
-
export {
|
|
132
|
+
export {
|
|
133
|
+
expandIncludes,
|
|
134
|
+
type IncludedManifest,
|
|
135
|
+
type IncludeReader,
|
|
136
|
+
type SourceDocument,
|
|
137
|
+
} from "./infrastructure/manifest-include.js";
|
|
138
|
+
export {
|
|
139
|
+
listPackageRoots,
|
|
140
|
+
listSourceFiles,
|
|
141
|
+
type PackagedLanguage,
|
|
142
|
+
type PackageRoot,
|
|
143
|
+
type WalkedLanguage,
|
|
144
|
+
} from "./infrastructure/walk.js";
|
|
131
145
|
export { type LoadedPolicy, loadPolicy, type LoadPolicyInput } from "./load/policy.js";
|
|
132
146
|
export { type LoweredRules, lowerManifest, type ProbeLanguage } from "./manifest/compile.js";
|
|
133
147
|
export {
|
|
@@ -138,7 +152,24 @@ export {
|
|
|
138
152
|
originOf,
|
|
139
153
|
type Substitution,
|
|
140
154
|
} from "./manifest/expand.js";
|
|
141
|
-
export {
|
|
155
|
+
export {
|
|
156
|
+
type Candidate,
|
|
157
|
+
candidatesOf,
|
|
158
|
+
type Generalization,
|
|
159
|
+
type InferInput,
|
|
160
|
+
inferManifest,
|
|
161
|
+
type InferOptions,
|
|
162
|
+
type InferPackage,
|
|
163
|
+
type Inferred,
|
|
164
|
+
type InferredTarget,
|
|
165
|
+
singularOf,
|
|
166
|
+
} from "./manifest/infer.js";
|
|
167
|
+
export {
|
|
168
|
+
MANIFEST_NODE_SCHEMA_ID,
|
|
169
|
+
MANIFEST_SCHEMA_ID,
|
|
170
|
+
manifestJsonSchema,
|
|
171
|
+
manifestNodeJsonSchema,
|
|
172
|
+
} from "./manifest/json-schema.js";
|
|
142
173
|
export {
|
|
143
174
|
type DecodedManifest,
|
|
144
175
|
decodeManifest,
|
|
@@ -21,14 +21,17 @@ import type {
|
|
|
21
21
|
ManifestPath,
|
|
22
22
|
ManifestPosition,
|
|
23
23
|
} from "../domain/manifest-location.js";
|
|
24
|
+
import { expandIncludes, type SourceDocument } from "./manifest-include.js";
|
|
24
25
|
|
|
25
|
-
// The manifest, as the
|
|
26
|
+
// The manifest, as the files on disk state it, before any decoding. A data
|
|
26
27
|
// file — YAML, or JSON, which YAML 1.2 contains — is what any host in any
|
|
27
28
|
// language can read, and is the form the docs are written in. A JavaScript
|
|
28
29
|
// module is the escape hatch a Node host alone honours: for a manifest
|
|
29
30
|
// generated from other data, and for what the ecosystem expects of a config
|
|
30
|
-
// file.
|
|
31
|
-
//
|
|
31
|
+
// file. Either may `include` further data files, so a monorepo's policy can
|
|
32
|
+
// live beside the packages it governs. Whichever it is, what comes out is one
|
|
33
|
+
// `unknown` value for the decoder, and, from the data formats, a way to turn
|
|
34
|
+
// a path in it back into a file and a line.
|
|
32
35
|
|
|
33
36
|
// In discovery order. A repository with none of these is told all four; one
|
|
34
37
|
// with more than one is refused, so nobody edits the wrong file for a week.
|
|
@@ -42,8 +45,12 @@ export const MANIFEST_FILENAMES = [
|
|
|
42
45
|
export type ManifestFile = {
|
|
43
46
|
readonly configPath: string;
|
|
44
47
|
readonly manifest: unknown;
|
|
45
|
-
// Absent for a JavaScript module, which has no
|
|
48
|
+
// Absent for a JavaScript module that includes nothing, which has no
|
|
49
|
+
// positions to give.
|
|
46
50
|
readonly locate: ManifestLocator | undefined;
|
|
51
|
+
// Every file the manifest was read from, the root first: what a host that
|
|
52
|
+
// caches or watches the policy has to look at.
|
|
53
|
+
readonly files: ReadonlyArray<string>;
|
|
47
54
|
};
|
|
48
55
|
|
|
49
56
|
export const findManifestFile = (repoRoot: string): string => {
|
|
@@ -76,24 +83,38 @@ const isDataManifest = (configPath: string): boolean =>
|
|
|
76
83
|
const isModuleManifest = (configPath: string): boolean =>
|
|
77
84
|
[".mjs", ".js", ".cjs"].includes(extensionOf(configPath));
|
|
78
85
|
|
|
86
|
+
// Whichever form the root takes, an `include` in it names a data file, read
|
|
87
|
+
// by the same parser; the locator that comes back answers across every file.
|
|
79
88
|
export const readManifestFile = async (configPath: string): Promise<ManifestFile> => {
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
89
|
+
const root = isDataManifest(configPath)
|
|
90
|
+
? parseDataFile(configPath)
|
|
91
|
+
: isModuleManifest(configPath)
|
|
92
|
+
? await readModule(configPath)
|
|
93
|
+
: undefined;
|
|
94
|
+
if (root === undefined) {
|
|
95
|
+
throw new ConfigInvalid({
|
|
96
|
+
configPath,
|
|
97
|
+
detail:
|
|
98
|
+
"a manifest is a .yaml, .yml or .json file, or a .mjs/.js module — " +
|
|
99
|
+
`not ${JSON.stringify(path.basename(configPath))}.`,
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
const assembled = expandIncludes(configPath, root, { exists: existsSync, read: parseDataFile });
|
|
103
|
+
return {
|
|
83
104
|
configPath,
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
}
|
|
105
|
+
manifest: assembled.value,
|
|
106
|
+
locate: assembled.locate,
|
|
107
|
+
files: assembled.files,
|
|
108
|
+
};
|
|
88
109
|
};
|
|
89
110
|
|
|
90
|
-
const
|
|
111
|
+
const readModule = async (configPath: string): Promise<SourceDocument> => {
|
|
91
112
|
const module: unknown = await import(pathToFileURL(configPath).href).catch((cause: unknown) => {
|
|
92
113
|
throw new ConfigInvalid({ configPath, detail: String(cause) });
|
|
93
114
|
});
|
|
94
|
-
const
|
|
115
|
+
const value =
|
|
95
116
|
typeof module === "object" && module !== null && "default" in module ? module.default : module;
|
|
96
|
-
return {
|
|
117
|
+
return { value, locate: undefined };
|
|
97
118
|
};
|
|
98
119
|
|
|
99
120
|
// YAML 1.2 core schema, which is what a reader without a YAML background
|
|
@@ -102,7 +123,7 @@ const readModuleManifest = async (configPath: string): Promise<ManifestFile> =>
|
|
|
102
123
|
// duplicate keys are refused rather than last-one-wins. A tag the parser does
|
|
103
124
|
// not know is refused too — a manifest is data, and a `!!js/function` in it
|
|
104
125
|
// would be a manifest only one runtime could read.
|
|
105
|
-
const
|
|
126
|
+
const parseDataFile = (configPath: string): SourceDocument => {
|
|
106
127
|
let text: string;
|
|
107
128
|
try {
|
|
108
129
|
text = readFileSync(configPath, "utf8");
|
|
@@ -140,8 +161,7 @@ const readDataManifest = (configPath: string): ManifestFile => {
|
|
|
140
161
|
}
|
|
141
162
|
|
|
142
163
|
return {
|
|
143
|
-
|
|
144
|
-
manifest: document.toJS({ mapAsMap: false }) as unknown,
|
|
164
|
+
value: document.toJS({ mapAsMap: false }) as unknown,
|
|
145
165
|
locate: makeLocator(document, lines),
|
|
146
166
|
};
|
|
147
167
|
};
|
|
@@ -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
|
+
};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { readdirSync, statSync } from "node:fs";
|
|
1
|
+
import { existsSync, readdirSync, statSync } from "node:fs";
|
|
2
2
|
import * as path from "node:path";
|
|
3
3
|
|
|
4
4
|
import type { Language } from "../ports/language.js";
|
|
@@ -52,3 +52,72 @@ export const listSourceFiles = (
|
|
|
52
52
|
}
|
|
53
53
|
return found.sort();
|
|
54
54
|
};
|
|
55
|
+
|
|
56
|
+
// What `infer` needs to know about a language beyond the walker's two: what
|
|
57
|
+
// marks a package of it, and where a package keeps its source.
|
|
58
|
+
export type PackagedLanguage = Pick<Language, "packageMarkers" | "sourceRoots">;
|
|
59
|
+
|
|
60
|
+
export type PackageRoot = {
|
|
61
|
+
// Repo-relative folder with forward slashes; `""` is the repository itself.
|
|
62
|
+
readonly root: string;
|
|
63
|
+
// The package's source folder, repo-relative, when one of the language's
|
|
64
|
+
// `sourceRoots` exists under it; otherwise `null`, and source sits at the root.
|
|
65
|
+
readonly source: string | null;
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
// The packages under (and above) `roots`: every folder holding one of the
|
|
69
|
+
// languages' marker files, the walk roots' own ancestors included, so a walk
|
|
70
|
+
// rooted at `src/` still finds the `package.json` beside it. Sorted by path.
|
|
71
|
+
export const listPackageRoots = (
|
|
72
|
+
repoRoot: string,
|
|
73
|
+
roots: ReadonlyArray<string>,
|
|
74
|
+
languages: ReadonlyArray<PackagedLanguage>,
|
|
75
|
+
): ReadonlyArray<PackageRoot> => {
|
|
76
|
+
const markers = languages.flatMap((language) => language.packageMarkers);
|
|
77
|
+
const sourceRoots = languages.flatMap((language) => language.sourceRoots);
|
|
78
|
+
if (markers.length === 0) return [];
|
|
79
|
+
|
|
80
|
+
const isDirectory = (absolute: string): boolean => {
|
|
81
|
+
try {
|
|
82
|
+
return statSync(absolute).isDirectory();
|
|
83
|
+
} catch {
|
|
84
|
+
return false;
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
const relative = (absolute: string): string =>
|
|
88
|
+
path.relative(repoRoot, absolute).replaceAll(path.sep, "/");
|
|
89
|
+
|
|
90
|
+
const found = new Map<string, PackageRoot>();
|
|
91
|
+
const consider = (absolute: string): void => {
|
|
92
|
+
const key = relative(absolute);
|
|
93
|
+
if (found.has(key)) return;
|
|
94
|
+
if (!markers.some((marker) => existsSync(path.join(absolute, marker)))) return;
|
|
95
|
+
const source = sourceRoots.find((name) => isDirectory(path.join(absolute, name)));
|
|
96
|
+
found.set(key, {
|
|
97
|
+
root: key,
|
|
98
|
+
source: source === undefined ? null : relative(path.join(absolute, source)),
|
|
99
|
+
});
|
|
100
|
+
};
|
|
101
|
+
const walk = (absolute: string): void => {
|
|
102
|
+
consider(absolute);
|
|
103
|
+
for (const entry of readdirSync(absolute)) {
|
|
104
|
+
if (SKIPPED.has(entry)) continue;
|
|
105
|
+
const child = path.join(absolute, entry);
|
|
106
|
+
if (isDirectory(child)) walk(child);
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
const top = path.resolve(repoRoot);
|
|
111
|
+
for (const root of roots) {
|
|
112
|
+
let absolute = path.resolve(repoRoot, root);
|
|
113
|
+
if (!isDirectory(absolute)) absolute = path.dirname(absolute);
|
|
114
|
+
walk(absolute);
|
|
115
|
+
// The ancestors, up to the repository: a package is as often above the
|
|
116
|
+
// walk root as inside it.
|
|
117
|
+
for (let above = absolute; above.startsWith(top); above = path.dirname(above)) {
|
|
118
|
+
consider(above);
|
|
119
|
+
if (above === top) break;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
return [...found.values()].sort((a, b) => a.root.localeCompare(b.root));
|
|
123
|
+
};
|