bivium 0.7.0 → 0.9.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/cli.js +87 -32
- package/compute.js +20 -0
- package/config.js +15 -0
- package/docs/cli.md +24 -5
- package/docs/config.md +6 -2
- package/errors.js +8 -0
- package/index.d.ts +8 -1
- package/index.js +8 -7
- package/package.json +1 -1
- package/{shared.js → shared-07rt4zr7.js} +1509 -1994
- package/shared-211m8fjt.js +69 -0
- package/shared-550cvwt2.js +503 -0
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
// src/errors.ts
|
|
2
|
+
var KINDS = {
|
|
3
|
+
BV_MISSING_ARGUMENT: "usage",
|
|
4
|
+
BV_UNKNOWN_COMMAND: "usage",
|
|
5
|
+
BV_UNKNOWN_OPTION: "usage",
|
|
6
|
+
BV_BAD_OPTION_VALUE: "usage",
|
|
7
|
+
BV_UNEXPECTED_ARGUMENT: "usage",
|
|
8
|
+
BV_UNKNOWN_SUITE: "usage",
|
|
9
|
+
BV_UNKNOWN_TEST: "usage",
|
|
10
|
+
BV_UNKNOWN_FILE: "usage",
|
|
11
|
+
BV_NO_RUN_COMMAND: "usage",
|
|
12
|
+
BV_NO_GITHUB_OUTPUT: "usage",
|
|
13
|
+
BV_CONFIG_NOT_FOUND: "config",
|
|
14
|
+
BV_CONFIG_UNREADABLE: "config",
|
|
15
|
+
BV_CONFIG_INVALID: "config",
|
|
16
|
+
BV_GIT_NO_BASE: "git",
|
|
17
|
+
BV_GIT_NO_MERGE_BASE: "git",
|
|
18
|
+
BV_GIT_FAILED: "git",
|
|
19
|
+
BV_INTERNAL: "other"
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
class BiviumError2 extends Error {
|
|
23
|
+
code;
|
|
24
|
+
kind;
|
|
25
|
+
constructor(code, message, options) {
|
|
26
|
+
super(message, options);
|
|
27
|
+
this.code = code;
|
|
28
|
+
this.kind = KINDS[code];
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
function distance(a, b) {
|
|
32
|
+
const width = b.length + 1;
|
|
33
|
+
const cells = new Uint32Array((a.length + 1) * width);
|
|
34
|
+
const d = (i, j) => cells[i * width + j] ?? 0;
|
|
35
|
+
for (let i = 0;i <= a.length; i++)
|
|
36
|
+
cells[i * width] = i;
|
|
37
|
+
for (let j = 0;j <= b.length; j++)
|
|
38
|
+
cells[j] = j;
|
|
39
|
+
for (let i = 1;i <= a.length; i++) {
|
|
40
|
+
for (let j = 1;j <= b.length; j++) {
|
|
41
|
+
const cost = a[i - 1] === b[j - 1] ? 0 : 1;
|
|
42
|
+
let best = Math.min(d(i - 1, j) + 1, d(i, j - 1) + 1, d(i - 1, j - 1) + cost);
|
|
43
|
+
if (i > 1 && j > 1 && a[i - 1] === b[j - 2] && a[i - 2] === b[j - 1]) {
|
|
44
|
+
best = Math.min(best, d(i - 2, j - 2) + 1);
|
|
45
|
+
}
|
|
46
|
+
cells[i * width + j] = best;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return d(a.length, b.length);
|
|
50
|
+
}
|
|
51
|
+
function closest(input, candidates) {
|
|
52
|
+
const most = Math.max(1, Math.floor(input.length / 3));
|
|
53
|
+
let best;
|
|
54
|
+
let bestDistance = most + 1;
|
|
55
|
+
for (const candidate of candidates) {
|
|
56
|
+
const d = distance(input, candidate);
|
|
57
|
+
if (d < bestDistance) {
|
|
58
|
+
best = candidate;
|
|
59
|
+
bestDistance = d;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return best;
|
|
63
|
+
}
|
|
64
|
+
function didYouMean2(input, candidates) {
|
|
65
|
+
const match = closest(input, candidates);
|
|
66
|
+
return match === undefined ? "" : ` (did you mean ${match}?)`;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export { BiviumError2, didYouMean2 };
|
|
@@ -0,0 +1,503 @@
|
|
|
1
|
+
import {
|
|
2
|
+
BiviumError2,
|
|
3
|
+
didYouMean2
|
|
4
|
+
} from "./shared-211m8fjt.js";
|
|
5
|
+
|
|
6
|
+
// src/config.ts
|
|
7
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
8
|
+
import { isAbsolute, join, relative, resolve } from "node:path";
|
|
9
|
+
import { pathToFileURL } from "node:url";
|
|
10
|
+
|
|
11
|
+
// src/glob.ts
|
|
12
|
+
var GLOB_LITERAL = /[.+^$()|[\]\\]/g;
|
|
13
|
+
var REGEX_SPECIAL = /[.*+?^${}()|[\]\\]/g;
|
|
14
|
+
var DIGIT = /[0-9]/;
|
|
15
|
+
var CAPTURE_REFERENCE = /\$(\d+)/g;
|
|
16
|
+
function compile(glob, {
|
|
17
|
+
capture = false,
|
|
18
|
+
captures
|
|
19
|
+
} = {}) {
|
|
20
|
+
let source = "";
|
|
21
|
+
let wildcards = 0;
|
|
22
|
+
const group = (body) => {
|
|
23
|
+
wildcards++;
|
|
24
|
+
return capture ? `(${body})` : body;
|
|
25
|
+
};
|
|
26
|
+
for (let i = 0;i < glob.length; i++) {
|
|
27
|
+
const char = glob.charAt(i);
|
|
28
|
+
if (captures && char === "$" && DIGIT.test(glob[i + 1] ?? "")) {
|
|
29
|
+
let end = i + 1;
|
|
30
|
+
while (DIGIT.test(glob[end] ?? ""))
|
|
31
|
+
end++;
|
|
32
|
+
const text = captures[Number(glob.slice(i + 1, end)) - 1];
|
|
33
|
+
source += text === undefined ? ".*" : text.replace(REGEX_SPECIAL, "\\$&");
|
|
34
|
+
i = end - 1;
|
|
35
|
+
} else if (char === "*" && glob[i + 1] === "*") {
|
|
36
|
+
const slash = glob[i + 2] === "/";
|
|
37
|
+
source += group(slash ? "(?:.*/)?" : ".*");
|
|
38
|
+
i += slash ? 2 : 1;
|
|
39
|
+
} else if (char === "*")
|
|
40
|
+
source += group("[^/]*");
|
|
41
|
+
else if (char === "?")
|
|
42
|
+
source += group("[^/]");
|
|
43
|
+
else if (char === "{") {
|
|
44
|
+
wildcards++;
|
|
45
|
+
source += capture ? "(" : "(?:";
|
|
46
|
+
} else if (char === "}")
|
|
47
|
+
source += ")";
|
|
48
|
+
else if (char === ",")
|
|
49
|
+
source += "|";
|
|
50
|
+
else
|
|
51
|
+
source += char.replace(GLOB_LITERAL, "\\$&");
|
|
52
|
+
}
|
|
53
|
+
return { source: `^${source}$`, wildcards };
|
|
54
|
+
}
|
|
55
|
+
function globToRegExp(glob) {
|
|
56
|
+
return new RegExp(compile(glob).source);
|
|
57
|
+
}
|
|
58
|
+
function captureGlob(glob) {
|
|
59
|
+
const { source, wildcards } = compile(glob, { capture: true });
|
|
60
|
+
return { pattern: new RegExp(source), wildcards };
|
|
61
|
+
}
|
|
62
|
+
function captureReferences(glob) {
|
|
63
|
+
return [...glob.matchAll(CAPTURE_REFERENCE)].map((match) => Number(match[1]));
|
|
64
|
+
}
|
|
65
|
+
function expandGlob(glob, captures) {
|
|
66
|
+
return new RegExp(compile(glob, { captures }).source);
|
|
67
|
+
}
|
|
68
|
+
function matcher(globs = []) {
|
|
69
|
+
const list = typeof globs === "string" ? [globs] : globs;
|
|
70
|
+
if (list.length === 0)
|
|
71
|
+
return () => false;
|
|
72
|
+
const res = list.map(globToRegExp);
|
|
73
|
+
return (path) => res.some((re) => re.test(path));
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// src/config.ts
|
|
77
|
+
function defineConfig2(config) {
|
|
78
|
+
return config;
|
|
79
|
+
}
|
|
80
|
+
var DEFAULT_EXTENSIONS = [
|
|
81
|
+
".ts",
|
|
82
|
+
".tsx",
|
|
83
|
+
".mts",
|
|
84
|
+
".cts",
|
|
85
|
+
".js",
|
|
86
|
+
".jsx",
|
|
87
|
+
".mjs",
|
|
88
|
+
".cjs",
|
|
89
|
+
".d.ts",
|
|
90
|
+
".svelte",
|
|
91
|
+
".vue",
|
|
92
|
+
".json"
|
|
93
|
+
];
|
|
94
|
+
function list(value, fallback) {
|
|
95
|
+
if (value === undefined)
|
|
96
|
+
return [...fallback];
|
|
97
|
+
return typeof value === "string" ? [value] : [...value];
|
|
98
|
+
}
|
|
99
|
+
var LEADING_DOT_SLASH = /^\.\//;
|
|
100
|
+
var TRAILING_SLASHES = /\/+$/;
|
|
101
|
+
function normalizeRoot(root) {
|
|
102
|
+
return root.replace(LEADING_DOT_SLASH, "").replace(TRAILING_SLASHES, "") || ".";
|
|
103
|
+
}
|
|
104
|
+
function pathMaps(record = {}) {
|
|
105
|
+
return Object.entries(record).map(([key, globs]) => {
|
|
106
|
+
const { pattern, wildcards } = captureGlob(key);
|
|
107
|
+
const values = list(globs, []);
|
|
108
|
+
const fixed = values.map((value) => captureReferences(value).length === 0 ? globToRegExp(value) : undefined);
|
|
109
|
+
return {
|
|
110
|
+
key,
|
|
111
|
+
exact: wildcards === 0,
|
|
112
|
+
pattern,
|
|
113
|
+
values,
|
|
114
|
+
targets: (path) => {
|
|
115
|
+
const match = pattern.exec(path);
|
|
116
|
+
if (!match)
|
|
117
|
+
return;
|
|
118
|
+
const captures = match.slice(1);
|
|
119
|
+
return values.map((value, i) => fixed[i] ?? expandGlob(value, captures));
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
function isInertOf(inert) {
|
|
125
|
+
const listed = matcher(inert);
|
|
126
|
+
return (name) => name === "bivium" || listed(name);
|
|
127
|
+
}
|
|
128
|
+
function optional(key, value) {
|
|
129
|
+
const out = {};
|
|
130
|
+
if (value !== undefined)
|
|
131
|
+
out[key] = value;
|
|
132
|
+
return out;
|
|
133
|
+
}
|
|
134
|
+
function resolveConfig2(config) {
|
|
135
|
+
const names = Object.keys(config.suites);
|
|
136
|
+
if (names.length === 0)
|
|
137
|
+
throw new Error("bivium config: no suites");
|
|
138
|
+
const suites = Object.entries(config.suites).map(([name, suite]) => ({
|
|
139
|
+
name,
|
|
140
|
+
isTest: matcher(suite.tests),
|
|
141
|
+
...optional("run", suite.run),
|
|
142
|
+
...optional("runAll", suite.runAll),
|
|
143
|
+
roots: list(suite.root, ["."]).map(normalizeRoot),
|
|
144
|
+
urls: Object.entries(suite.urls ?? {}).map(([key, targets]) => ({
|
|
145
|
+
key,
|
|
146
|
+
targets: list(targets, [])
|
|
147
|
+
})),
|
|
148
|
+
isPage: matcher(suite.pages),
|
|
149
|
+
isWiring: matcher(suite.wiring),
|
|
150
|
+
isSetup: matcher(suite.setup),
|
|
151
|
+
setup: list(suite.setup, []),
|
|
152
|
+
isNonInput: matcher(suite.nonInputs),
|
|
153
|
+
output: suite.output ?? `${name}-tests`,
|
|
154
|
+
types: suite.types ?? true
|
|
155
|
+
}));
|
|
156
|
+
const narrowers = (config.narrowers ?? []).map((narrower) => {
|
|
157
|
+
const unknown = narrower.suites?.find((name) => !names.includes(name));
|
|
158
|
+
if (unknown !== undefined) {
|
|
159
|
+
throw new Error(`bivium config: narrower ${narrower.name} names no suite ${unknown}`);
|
|
160
|
+
}
|
|
161
|
+
return {
|
|
162
|
+
name: narrower.name,
|
|
163
|
+
match: matcher(narrower.match),
|
|
164
|
+
opaque: matcher(narrower.opaque),
|
|
165
|
+
suites: [...narrower.suites ?? names],
|
|
166
|
+
narrow: async (context) => checkResult(await narrower.narrow(context))
|
|
167
|
+
};
|
|
168
|
+
});
|
|
169
|
+
return {
|
|
170
|
+
suites,
|
|
171
|
+
narrowers,
|
|
172
|
+
roots: list(config.roots, ["."]).map(normalizeRoot),
|
|
173
|
+
isIgnored: matcher(config.ignore),
|
|
174
|
+
alias: Object.entries(config.alias ?? {}).map(([key, path]) => [
|
|
175
|
+
key.replace(TRAILING_SLASHES, "") || key,
|
|
176
|
+
normalizeRoot(path)
|
|
177
|
+
]).sort(([a], [b]) => b.length - a.length),
|
|
178
|
+
generated: pathMaps(config.edges),
|
|
179
|
+
reads: pathMaps(config.reads),
|
|
180
|
+
isSelf: matcher(config.self),
|
|
181
|
+
isImportOnly: matcher(config.importOnly),
|
|
182
|
+
isHermetic: matcher(config.hermetic),
|
|
183
|
+
isBarrel: config.barrels ? matcher(config.barrels) : () => true,
|
|
184
|
+
external: new Set(config.external),
|
|
185
|
+
isInert: isInertOf(config.inert),
|
|
186
|
+
...config.loadedByName !== undefined && {
|
|
187
|
+
isLoadedByName: matcher(config.loadedByName)
|
|
188
|
+
},
|
|
189
|
+
extensions: config.extensions ?? DEFAULT_EXTENSIONS,
|
|
190
|
+
deadGlobs: config.deadGlobs ?? [],
|
|
191
|
+
...config.knownUnknowns && { knownUnknowns: config.knownUnknowns },
|
|
192
|
+
...optional("base", config.base),
|
|
193
|
+
...optional("cache", config.cache)
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
function inRoot2(file, root) {
|
|
197
|
+
return root === "." || file === root || file.startsWith(`${root}/`);
|
|
198
|
+
}
|
|
199
|
+
function isRecord(value) {
|
|
200
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
201
|
+
}
|
|
202
|
+
function isStrings(value) {
|
|
203
|
+
return Array.isArray(value) && value.every((item) => typeof item === "string");
|
|
204
|
+
}
|
|
205
|
+
function isGlobs(value) {
|
|
206
|
+
return typeof value === "string" || isStrings(value);
|
|
207
|
+
}
|
|
208
|
+
function isStringRecord(value) {
|
|
209
|
+
return isRecord(value) && Object.values(value).every((item) => typeof item === "string");
|
|
210
|
+
}
|
|
211
|
+
function isGlobsRecord(value) {
|
|
212
|
+
return isRecord(value) && Object.values(value).every(isGlobs);
|
|
213
|
+
}
|
|
214
|
+
function isBoolean(value) {
|
|
215
|
+
return typeof value === "boolean";
|
|
216
|
+
}
|
|
217
|
+
function isString(value) {
|
|
218
|
+
return typeof value === "string";
|
|
219
|
+
}
|
|
220
|
+
function isCache(value) {
|
|
221
|
+
return typeof value === "boolean" || typeof value === "string";
|
|
222
|
+
}
|
|
223
|
+
function field(record, key, is, path, expected) {
|
|
224
|
+
const value = record[key];
|
|
225
|
+
if (value === undefined || is(value))
|
|
226
|
+
return value;
|
|
227
|
+
throw new Error(`bivium config: ${path}${key} must be ${expected}`);
|
|
228
|
+
}
|
|
229
|
+
var SUITE_KEYS = new Set([
|
|
230
|
+
"tests",
|
|
231
|
+
"run",
|
|
232
|
+
"runAll",
|
|
233
|
+
"root",
|
|
234
|
+
"urls",
|
|
235
|
+
"pages",
|
|
236
|
+
"wiring",
|
|
237
|
+
"setup",
|
|
238
|
+
"nonInputs",
|
|
239
|
+
"output",
|
|
240
|
+
"types"
|
|
241
|
+
]);
|
|
242
|
+
var NARROWER_KEYS = new Set(["name", "match", "opaque", "suites", "narrow"]);
|
|
243
|
+
var DEAD_GLOB_KEYS = new Set(["file", "glob", "reason"]);
|
|
244
|
+
var KNOWN_UNKNOWN_KEYS = new Set(["file", "reason"]);
|
|
245
|
+
var CONFIG_KEYS = new Set([
|
|
246
|
+
"suites",
|
|
247
|
+
"narrowers",
|
|
248
|
+
"roots",
|
|
249
|
+
"ignore",
|
|
250
|
+
"alias",
|
|
251
|
+
"edges",
|
|
252
|
+
"reads",
|
|
253
|
+
"self",
|
|
254
|
+
"importOnly",
|
|
255
|
+
"hermetic",
|
|
256
|
+
"barrels",
|
|
257
|
+
"external",
|
|
258
|
+
"inert",
|
|
259
|
+
"loadedByName",
|
|
260
|
+
"extensions",
|
|
261
|
+
"deadGlobs",
|
|
262
|
+
"knownUnknowns",
|
|
263
|
+
"base",
|
|
264
|
+
"cache"
|
|
265
|
+
]);
|
|
266
|
+
function unknownKeys(value) {
|
|
267
|
+
const found = [];
|
|
268
|
+
const visit = (record, known, path) => {
|
|
269
|
+
if (!isRecord(record))
|
|
270
|
+
return;
|
|
271
|
+
for (const key of Object.keys(record)) {
|
|
272
|
+
if (!known.has(key))
|
|
273
|
+
found.push(`${path}${key}${didYouMean2(key, known)}`);
|
|
274
|
+
}
|
|
275
|
+
};
|
|
276
|
+
visit(value, CONFIG_KEYS, "");
|
|
277
|
+
if (isRecord(value.suites)) {
|
|
278
|
+
for (const [name, suite] of Object.entries(value.suites)) {
|
|
279
|
+
visit(suite, SUITE_KEYS, `suites.${name}.`);
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
const lists = [
|
|
283
|
+
["narrowers", NARROWER_KEYS],
|
|
284
|
+
["deadGlobs", DEAD_GLOB_KEYS],
|
|
285
|
+
["knownUnknowns", KNOWN_UNKNOWN_KEYS]
|
|
286
|
+
];
|
|
287
|
+
for (const [key, known] of lists) {
|
|
288
|
+
const entries = value[key];
|
|
289
|
+
if (!Array.isArray(entries))
|
|
290
|
+
continue;
|
|
291
|
+
for (const [i, entry] of entries.entries()) {
|
|
292
|
+
visit(entry, known, `${key}[${i}].`);
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
return found;
|
|
296
|
+
}
|
|
297
|
+
function checkSuite(name, value) {
|
|
298
|
+
if (!isRecord(value)) {
|
|
299
|
+
throw new Error(`bivium config: suites.${name} must be an object`);
|
|
300
|
+
}
|
|
301
|
+
const at = `suites.${name}.`;
|
|
302
|
+
const tests = field(value, "tests", isGlobs, at, "a glob or a list of globs");
|
|
303
|
+
if (tests === undefined || tests.length === 0) {
|
|
304
|
+
throw new Error(`bivium config: suite ${name} has no tests`);
|
|
305
|
+
}
|
|
306
|
+
const globs = "a glob or a list of globs";
|
|
307
|
+
return {
|
|
308
|
+
tests,
|
|
309
|
+
...optional("run", field(value, "run", isString, at, "a command")),
|
|
310
|
+
...optional("runAll", field(value, "runAll", isString, at, "a command")),
|
|
311
|
+
...optional("root", paths(value, "root", at)),
|
|
312
|
+
...optional("urls", field(value, "urls", isGlobsRecord, at, "a map of URL patterns")),
|
|
313
|
+
...optional("pages", field(value, "pages", isGlobs, at, globs)),
|
|
314
|
+
...optional("wiring", field(value, "wiring", isGlobs, at, globs)),
|
|
315
|
+
...optional("setup", field(value, "setup", isGlobs, at, globs)),
|
|
316
|
+
...optional("nonInputs", field(value, "nonInputs", isGlobs, at, globs)),
|
|
317
|
+
...optional("output", field(value, "output", isString, at, "an output name")),
|
|
318
|
+
...optional("types", field(value, "types", isBoolean, at, "a boolean"))
|
|
319
|
+
};
|
|
320
|
+
}
|
|
321
|
+
var WILDCARD = /[*?{}[\]]/;
|
|
322
|
+
function paths(record, key, at) {
|
|
323
|
+
const value = field(record, key, isGlobs, at, "a path or a list of paths");
|
|
324
|
+
for (const path of typeof value === "string" ? [value] : value ?? []) {
|
|
325
|
+
const wildcard = WILDCARD.exec(path);
|
|
326
|
+
if (!wildcard)
|
|
327
|
+
continue;
|
|
328
|
+
const prefix = normalizeRoot(path.slice(0, wildcard.index));
|
|
329
|
+
throw new Error(`bivium config: ${at}${key} takes paths, not globs: "${path}" (the directory is "${prefix}")`);
|
|
330
|
+
}
|
|
331
|
+
return value;
|
|
332
|
+
}
|
|
333
|
+
function isResult(value) {
|
|
334
|
+
if (!isRecord(value))
|
|
335
|
+
return false;
|
|
336
|
+
if ("all" in value)
|
|
337
|
+
return typeof value.all === "string";
|
|
338
|
+
return "seeds" in value && isStrings(value.seeds) && (!("note" in value) || typeof value.note === "string");
|
|
339
|
+
}
|
|
340
|
+
function checkResult(value) {
|
|
341
|
+
if (isResult(value))
|
|
342
|
+
return value;
|
|
343
|
+
throw new Error("returned neither { seeds: string[] } nor { all: string }");
|
|
344
|
+
}
|
|
345
|
+
function checkNarrower(value, at) {
|
|
346
|
+
if (!isRecord(value)) {
|
|
347
|
+
throw new Error(`bivium config: ${at} must be an object`);
|
|
348
|
+
}
|
|
349
|
+
const name = field(value, "name", isString, `${at}.`, "a string");
|
|
350
|
+
const match = field(value, "match", isGlobs, `${at}.`, "globs");
|
|
351
|
+
const opaque = field(value, "opaque", isGlobs, `${at}.`, "globs");
|
|
352
|
+
const suites = field(value, "suites", isStrings, `${at}.`, "suite names");
|
|
353
|
+
const { narrow } = value;
|
|
354
|
+
if (name === undefined || match === undefined || opaque === undefined) {
|
|
355
|
+
throw new Error(`bivium config: ${at} needs a name, match and opaque`);
|
|
356
|
+
}
|
|
357
|
+
if (typeof narrow !== "function") {
|
|
358
|
+
throw new Error(`bivium config: ${at}.narrow must be a function`);
|
|
359
|
+
}
|
|
360
|
+
return {
|
|
361
|
+
name,
|
|
362
|
+
match,
|
|
363
|
+
opaque,
|
|
364
|
+
...optional("suites", suites),
|
|
365
|
+
narrow: async (context) => {
|
|
366
|
+
const result = await Reflect.apply(narrow, value, [context]);
|
|
367
|
+
return checkResult(result);
|
|
368
|
+
}
|
|
369
|
+
};
|
|
370
|
+
}
|
|
371
|
+
function checkDeadGlob(value, at) {
|
|
372
|
+
if (!isRecord(value)) {
|
|
373
|
+
throw new Error(`bivium config: ${at} must be an object`);
|
|
374
|
+
}
|
|
375
|
+
const file = field(value, "file", isString, `${at}.`, "a path");
|
|
376
|
+
const glob = field(value, "glob", isString, `${at}.`, "a glob");
|
|
377
|
+
const reason = field(value, "reason", isString, `${at}.`, "a string");
|
|
378
|
+
if (!(file && glob && reason)) {
|
|
379
|
+
throw new Error(`bivium config: ${at} needs a file, glob and reason`);
|
|
380
|
+
}
|
|
381
|
+
return { file, glob, reason };
|
|
382
|
+
}
|
|
383
|
+
function checkKnownUnknown(value, at) {
|
|
384
|
+
if (!isRecord(value)) {
|
|
385
|
+
throw new Error(`bivium config: ${at} must be an object`);
|
|
386
|
+
}
|
|
387
|
+
const file = field(value, "file", isString, `${at}.`, "a path");
|
|
388
|
+
const reason = field(value, "reason", isString, `${at}.`, "a string");
|
|
389
|
+
if (!(file && reason)) {
|
|
390
|
+
throw new Error(`bivium config: ${at} needs a file and reason`);
|
|
391
|
+
}
|
|
392
|
+
return { file, reason };
|
|
393
|
+
}
|
|
394
|
+
function checkCaptures(name, record) {
|
|
395
|
+
for (const [key, globs] of Object.entries(record ?? {})) {
|
|
396
|
+
const { wildcards } = captureGlob(key);
|
|
397
|
+
for (const value of list(globs, [])) {
|
|
398
|
+
const bad = captureReferences(value).find((n) => n < 1 || n > wildcards);
|
|
399
|
+
if (bad === undefined)
|
|
400
|
+
continue;
|
|
401
|
+
const has = `${wildcards} ${wildcards === 1 ? "wildcard" : "wildcards"}`;
|
|
402
|
+
const why = bad < 1 ? "captures count from $1" : `its key has ${has}`;
|
|
403
|
+
throw new Error(`bivium config: ${name}["${key}"] refers to $${bad} in ${value}, but ${why}`);
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
function checkConfig2(value) {
|
|
408
|
+
if (!isRecord(value))
|
|
409
|
+
throw new Error("bivium config: not an object");
|
|
410
|
+
if (!isRecord(value.suites)) {
|
|
411
|
+
throw new Error("bivium config: suites must be an object");
|
|
412
|
+
}
|
|
413
|
+
const unknown = unknownKeys(value);
|
|
414
|
+
if (unknown.length === 1) {
|
|
415
|
+
throw new Error(`bivium config: unknown key ${unknown[0]}`);
|
|
416
|
+
}
|
|
417
|
+
if (unknown.length > 1) {
|
|
418
|
+
throw new Error(`bivium config: unknown keys:
|
|
419
|
+
${unknown.map((key) => ` ${key}`).join(`
|
|
420
|
+
`)}`);
|
|
421
|
+
}
|
|
422
|
+
const { narrowers, deadGlobs, knownUnknowns } = value;
|
|
423
|
+
if (narrowers !== undefined && !Array.isArray(narrowers)) {
|
|
424
|
+
throw new Error("bivium config: narrowers must be a list");
|
|
425
|
+
}
|
|
426
|
+
if (deadGlobs !== undefined && !Array.isArray(deadGlobs)) {
|
|
427
|
+
throw new Error("bivium config: deadGlobs must be a list");
|
|
428
|
+
}
|
|
429
|
+
if (knownUnknowns !== undefined && !Array.isArray(knownUnknowns)) {
|
|
430
|
+
throw new Error("bivium config: knownUnknowns must be a list");
|
|
431
|
+
}
|
|
432
|
+
const suites = {};
|
|
433
|
+
for (const [name, suite] of Object.entries(value.suites)) {
|
|
434
|
+
suites[name] = checkSuite(name, suite);
|
|
435
|
+
}
|
|
436
|
+
const globs = "a glob or a list of globs";
|
|
437
|
+
const edges = field(value, "edges", isGlobsRecord, "", "a map of globs");
|
|
438
|
+
const reads = field(value, "reads", isGlobsRecord, "", "a map of globs");
|
|
439
|
+
checkCaptures("edges", edges);
|
|
440
|
+
checkCaptures("reads", reads);
|
|
441
|
+
return {
|
|
442
|
+
suites,
|
|
443
|
+
...optional("narrowers", narrowers?.map((narrower, i) => checkNarrower(narrower, `narrowers[${i}]`))),
|
|
444
|
+
...optional("roots", paths(value, "roots", "")),
|
|
445
|
+
...optional("ignore", field(value, "ignore", isGlobs, "", globs)),
|
|
446
|
+
...optional("alias", field(value, "alias", isStringRecord, "", "a map of paths")),
|
|
447
|
+
...optional("edges", edges),
|
|
448
|
+
...optional("reads", reads),
|
|
449
|
+
...optional("self", field(value, "self", isGlobs, "", globs)),
|
|
450
|
+
...optional("importOnly", field(value, "importOnly", isGlobs, "", globs)),
|
|
451
|
+
...optional("hermetic", field(value, "hermetic", isGlobs, "", globs)),
|
|
452
|
+
...optional("barrels", field(value, "barrels", isGlobs, "", globs)),
|
|
453
|
+
...optional("external", field(value, "external", isStrings, "", "a list of packages")),
|
|
454
|
+
...optional("inert", field(value, "inert", isGlobs, "", globs)),
|
|
455
|
+
...optional("loadedByName", field(value, "loadedByName", isGlobs, "", globs)),
|
|
456
|
+
...optional("extensions", field(value, "extensions", isStrings, "", "a list")),
|
|
457
|
+
...optional("deadGlobs", deadGlobs?.map((entry, i) => checkDeadGlob(entry, `deadGlobs[${i}]`))),
|
|
458
|
+
...optional("knownUnknowns", knownUnknowns?.map((entry, i) => checkKnownUnknown(entry, `knownUnknowns[${i}]`))),
|
|
459
|
+
...optional("base", field(value, "base", isString, "", "a ref")),
|
|
460
|
+
...optional("cache", field(value, "cache", isCache, "", "a directory or a boolean"))
|
|
461
|
+
};
|
|
462
|
+
}
|
|
463
|
+
var CONFIG_FILES = [
|
|
464
|
+
"bivium.config.ts",
|
|
465
|
+
"bivium.config.mts",
|
|
466
|
+
"bivium.config.mjs",
|
|
467
|
+
"bivium.config.js",
|
|
468
|
+
"bivium.config.json"
|
|
469
|
+
];
|
|
470
|
+
async function readConfig(file) {
|
|
471
|
+
if (file.endsWith(".json"))
|
|
472
|
+
return JSON.parse(readFileSync(file, "utf-8"));
|
|
473
|
+
const module = await import(pathToFileURL(file).href);
|
|
474
|
+
return isRecord(module) ? module.default : undefined;
|
|
475
|
+
}
|
|
476
|
+
var CONFIG_PREFIX = /^bivium config: /;
|
|
477
|
+
async function loadConfig2(root, path) {
|
|
478
|
+
const file = path === undefined ? CONFIG_FILES.map((name) => join(root, name)).find((f) => existsSync(f)) : resolve(process.cwd(), path);
|
|
479
|
+
if (!file || !existsSync(file)) {
|
|
480
|
+
throw new BiviumError2("BV_CONFIG_NOT_FOUND", path ? `bivium: no config found at ${path}` : `bivium: no config found in ${root}: looked for ${CONFIG_FILES.join(", ")} (--config names another file, --cwd another repository)`);
|
|
481
|
+
}
|
|
482
|
+
const inside = relative(root, file);
|
|
483
|
+
const shown = inside.startsWith("..") || isAbsolute(inside) ? file : inside;
|
|
484
|
+
const named = (code, error) => {
|
|
485
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
486
|
+
return new BiviumError2(code, `bivium config: ${shown}: ${message.replace(CONFIG_PREFIX, "")}`, { cause: error });
|
|
487
|
+
};
|
|
488
|
+
let raw;
|
|
489
|
+
try {
|
|
490
|
+
raw = await readConfig(file);
|
|
491
|
+
} catch (error) {
|
|
492
|
+
throw named("BV_CONFIG_UNREADABLE", error);
|
|
493
|
+
}
|
|
494
|
+
let config;
|
|
495
|
+
try {
|
|
496
|
+
config = resolveConfig2(checkConfig2(raw));
|
|
497
|
+
} catch (error) {
|
|
498
|
+
throw named("BV_CONFIG_INVALID", error);
|
|
499
|
+
}
|
|
500
|
+
return { config, file: shown };
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
export { globToRegExp, matcher, defineConfig2, resolveConfig2, inRoot2, checkConfig2, loadConfig2 };
|