@osdlc/wayfinder 0.0.1-stage → 0.7.1

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 (53) hide show
  1. package/CHANGELOG.md +245 -0
  2. package/CONTRIBUTING.md +62 -0
  3. package/{LICENSE → LICENSE-APACHE} +1 -11
  4. package/LICENSE-MIT +21 -0
  5. package/README.md +505 -2
  6. package/SECURITY.md +44 -0
  7. package/bin/way.js +18 -0
  8. package/dist/app.d.ts +24 -0
  9. package/dist/app.js +24 -0
  10. package/dist/args.d.ts +44 -0
  11. package/dist/args.js +93 -0
  12. package/dist/cache.d.ts +7 -0
  13. package/dist/cache.js +15 -0
  14. package/dist/catalog.d.ts +38 -0
  15. package/dist/catalog.js +61 -0
  16. package/dist/clean-command.d.ts +43 -0
  17. package/dist/clean-command.js +274 -0
  18. package/dist/cli.d.ts +76 -0
  19. package/dist/cli.js +612 -0
  20. package/dist/config-command.d.ts +52 -0
  21. package/dist/config-command.js +342 -0
  22. package/dist/config.d.ts +367 -0
  23. package/dist/config.js +700 -0
  24. package/dist/devlink.d.ts +14 -0
  25. package/dist/devlink.js +30 -0
  26. package/dist/groups.d.ts +34 -0
  27. package/dist/groups.js +38 -0
  28. package/dist/guide.d.ts +32 -0
  29. package/dist/guide.js +411 -0
  30. package/dist/http.d.ts +22 -0
  31. package/dist/http.js +149 -0
  32. package/dist/origin-owner.d.ts +14 -0
  33. package/dist/origin-owner.js +69 -0
  34. package/dist/page.d.ts +15 -0
  35. package/dist/page.js +58 -0
  36. package/dist/providers-command.d.ts +40 -0
  37. package/dist/providers-command.js +132 -0
  38. package/dist/serve.d.ts +97 -0
  39. package/dist/serve.js +678 -0
  40. package/dist/tools.d.ts +46 -0
  41. package/dist/tools.js +102 -0
  42. package/dist/trust-command.d.ts +33 -0
  43. package/dist/trust-command.js +221 -0
  44. package/dist/trust-store.d.ts +18 -0
  45. package/dist/trust-store.js +46 -0
  46. package/dist/tui.d.ts +178 -0
  47. package/dist/tui.js +846 -0
  48. package/dist/web/main.js +2315 -0
  49. package/dist/web/page.html +110 -0
  50. package/dist/web/styles.css +1514 -0
  51. package/dist/xdg.d.ts +10 -0
  52. package/dist/xdg.js +17 -0
  53. package/package.json +54 -9
@@ -0,0 +1,342 @@
1
+ /**
2
+ * The `config` subcommand. Everything outside the project root is injected
3
+ * (`stdout`, `stderr`, `env`, `home`), so a test never reads or edits the
4
+ * developer's own config.
5
+ */
6
+ import { access } from 'node:fs/promises';
7
+ import { homedir } from 'node:os';
8
+ import { BIN } from "./app.js";
9
+ import { CONFIG_ENV_VAR, ConfigError, configLocations, DEFAULT_CONFIG, getConfigValue, loadConfig, mergeConfig, parseConfigValue, readRawConfigFile, setConfigValue, unsetConfigValue, validateConfig, writeConfigFile, } from "./config.js";
10
+ import { EXIT } from '@osdlc/cli-tool';
11
+ function usage() {
12
+ return `${BIN} config — read and write the layered task configuration
13
+
14
+ usage: ${BIN} config <command>
15
+
16
+ list [--json] every setting, and the layer it came from
17
+ get <key> one setting's effective value, as JSON
18
+ set <key> <value> [--user|--project] write a setting (default --project)
19
+ unset <key> [--user|--project] remove a setting (default --project)
20
+ path where each layer's file lives
21
+ -h, --help this text
22
+
23
+ Keys are dotted: runners.disabled, runners.order, allowExec, discovery,
24
+ excludePaths, trust.paths, trust.owners, tools.
25
+
26
+ A value is read as JSON, falling back to the bare string when that fails — so
27
+ \`set discovery git\` and \`set runners.order '["moon","just"]'\` both work, and a
28
+ plain word needs no quoting.
29
+
30
+ Layers, lowest precedence first: built-in defaults, the user file, the project
31
+ file, then this tool's own flags. ${CONFIG_ENV_VAR} replaces the user file's
32
+ location and nothing else.
33
+ `;
34
+ }
35
+ /**
36
+ * `loadConfig`, with the calling tool's defaults slid in under the file layers.
37
+ *
38
+ * `sources` is left alone on purpose: a value the tool defaulted still came
39
+ * from no file, so `(default)` remains the honest attribution. Only the VALUE
40
+ * that label is attached to changes.
41
+ */
42
+ async function load(toolName, root, env, home, defaults) {
43
+ const loaded = await loadConfig(root, env, home);
44
+ if (defaults === undefined || Object.keys(defaults).length === 0)
45
+ return loaded;
46
+ const base = mergeConfig(DEFAULT_CONFIG, defaults);
47
+ const effective = mergeConfig(mergeConfig(base, loaded.layers.user ?? {}), loaded.layers.project ?? {});
48
+ return { ...loaded, effective };
49
+ }
50
+ async function exists(file) {
51
+ try {
52
+ await access(file);
53
+ return true;
54
+ }
55
+ catch {
56
+ return false;
57
+ }
58
+ }
59
+ /** `<path> (exists)` / `<path> (missing)` — the question `config path` is asked to answer. */
60
+ async function describeFile(file) {
61
+ return `${file} ${(await exists(file)) ? '(exists)' : '(missing)'}`;
62
+ }
63
+ function pad(text, width) {
64
+ return text.length >= width ? text : text + ' '.repeat(width - text.length);
65
+ }
66
+ /**
67
+ * Split `--user` / `--project` off the argument list, leaving the positionals.
68
+ *
69
+ * The two contradict each other, and both callers WRITE, so a last-one-wins
70
+ * reading would silently edit a file the user named the other of. A pair is a
71
+ * usage error instead.
72
+ */
73
+ function takeTarget(argv) {
74
+ let target;
75
+ const rest = [];
76
+ for (const arg of argv) {
77
+ if (arg === '--user' || arg === '--project') {
78
+ const named = arg === '--user' ? 'user' : 'project';
79
+ if (target !== undefined && target !== named) {
80
+ return { error: '--user and --project name different files; give at most one' };
81
+ }
82
+ target = named;
83
+ }
84
+ else if (arg.startsWith('--')) {
85
+ return { error: `unknown option: ${arg}` };
86
+ }
87
+ else {
88
+ rest.push(arg);
89
+ }
90
+ }
91
+ return { target: target ?? 'project', rest };
92
+ }
93
+ /**
94
+ * Pull `-C PATH` / `--dir PATH` out of a `config` argument list.
95
+ *
96
+ * `-C` is a global flag, but subcommands run before the main parser, so
97
+ * without this it would be accepted and ignored. All four spellings are
98
+ * accepted: `-C X`, `-CX`, `--dir X`, `--dir=X`.
99
+ */
100
+ export function takeConfigDir(argv) {
101
+ let dir;
102
+ const rest = [];
103
+ for (let i = 0; i < argv.length; i++) {
104
+ const arg = argv[i];
105
+ const attached = arg.startsWith('--dir=')
106
+ ? arg.slice('--dir='.length)
107
+ : arg.startsWith('-C=')
108
+ ? arg.slice('-C='.length)
109
+ : arg.startsWith('-C') && arg.length > 2
110
+ ? arg.slice(2)
111
+ : undefined;
112
+ if (attached !== undefined) {
113
+ if (attached === '')
114
+ return { error: `${arg.split('=')[0]} needs a path` };
115
+ dir = attached;
116
+ }
117
+ else if (arg === '-C' || arg === '--dir') {
118
+ const value = argv[++i];
119
+ if (value === undefined)
120
+ return { error: `${arg} needs a path` };
121
+ dir = value;
122
+ }
123
+ else {
124
+ rest.push(arg);
125
+ }
126
+ }
127
+ return { ...(dir !== undefined ? { dir } : {}), rest };
128
+ }
129
+ /**
130
+ * `<tool> config <sub> …`. `root` is the resolved project root. Returns the
131
+ * exit code.
132
+ *
133
+ * Exit codes: 0 succeeded. 2 means the
134
+ * command cannot be run as typed — an unknown subcommand or option, a wrong
135
+ * argument count, contradictory flags, an unknown setting name, a value of the
136
+ * wrong type for its setting. Every one of those is decided from the argument
137
+ * list and the static schema alone, with no file involved, so re-running it can
138
+ * only produce the same answer. 1 means a well-formed command that FAILED: a
139
+ * file that will not parse, a key that is not set where you asked to unset it,
140
+ * a `get` that found nothing.
141
+ *
142
+ * The split matters because a script can act on it — 2 means fix the command,
143
+ * 1 means look at the state.
144
+ */
145
+ export async function runConfigCommand(toolName, root, argv, io) {
146
+ const env = io.env ?? process.env;
147
+ const home = io.home ?? homedir();
148
+ const fail = (message) => {
149
+ io.stderr(`${toolName}: ${message}\n`);
150
+ return EXIT.error;
151
+ };
152
+ const [sub, ...rest] = argv;
153
+ try {
154
+ switch (sub) {
155
+ // Usage on stdout with exit 0: `--help` is what was asked for, not a
156
+ // misuse of the command.
157
+ case '-h':
158
+ case '--help':
159
+ io.stdout(usage());
160
+ return EXIT.ok;
161
+ case undefined:
162
+ case 'list':
163
+ return await listCommand(toolName, root, rest, io, env, home);
164
+ case 'get':
165
+ return await getCommand(toolName, root, rest, io, env, home);
166
+ case 'set':
167
+ return await setCommand(toolName, root, rest, io, env, home);
168
+ case 'unset':
169
+ return await unsetCommand(toolName, root, rest, io, env, home);
170
+ case 'path':
171
+ return await pathCommand(toolName, rest, io, env, home, root);
172
+ default:
173
+ io.stderr(`${toolName}: unknown config command: ${sub}\n\n${usage()}`);
174
+ return EXIT.usage;
175
+ }
176
+ }
177
+ catch (error) {
178
+ // A ConfigError names its file and its problem, and that is the whole point
179
+ // of it: a setting that quietly did not apply is worse than one that
180
+ // refused to load, so this never degrades to a silent fallback.
181
+ if (error instanceof ConfigError)
182
+ return fail(error.message);
183
+ throw error;
184
+ }
185
+ }
186
+ async function listCommand(toolName, root, argv, io, env, home) {
187
+ let json = false;
188
+ for (const arg of argv) {
189
+ if (arg === '--json')
190
+ json = true;
191
+ else {
192
+ io.stderr(`${toolName}: unknown option: ${arg}\n\n${usage()}`);
193
+ return EXIT.usage;
194
+ }
195
+ }
196
+ const loaded = await load(toolName, root, env, home, io.defaults);
197
+ if (json) {
198
+ const sources = {};
199
+ for (const [key, layer] of loaded.sources)
200
+ sources[key] = layer;
201
+ io.stdout(`${JSON.stringify({ effective: loaded.effective, sources, locations: loaded.locations }, null, 2)}\n`);
202
+ }
203
+ else {
204
+ const rows = [...loaded.sources].map(([key, layer]) => ({
205
+ key,
206
+ value: JSON.stringify(getConfigValue(loaded.effective, key)),
207
+ layer,
208
+ }));
209
+ const keyWidth = rows.reduce((n, r) => Math.max(n, r.key.length), 0);
210
+ const valueWidth = rows.reduce((n, r) => Math.max(n, r.value.length), 0);
211
+ for (const row of rows) {
212
+ io.stdout(`${pad(row.key, keyWidth)} ${pad(row.value, valueWidth)} (${row.layer})\n`);
213
+ }
214
+ }
215
+ // Context and warnings go to stderr so `--json` stays machine-readable and
216
+ // the plain listing stays greppable.
217
+ io.stderr(`user: ${await describeFile(loaded.locations.user)}\n`);
218
+ if (loaded.locations.userEnvVar !== null)
219
+ io.stderr(` (from ${loaded.locations.userEnvVar})\n`);
220
+ io.stderr(`project: ${await describeFile(loaded.locations.project)}\n`);
221
+ for (const warning of loaded.warnings)
222
+ io.stderr(`warning: ${warning}\n`);
223
+ return EXIT.ok;
224
+ }
225
+ async function getCommand(toolName, root, argv, io, env, home) {
226
+ const key = argv[0];
227
+ if (key === undefined || argv.length > 1) {
228
+ io.stderr(`${toolName}: config get takes exactly one key\n\n${usage()}`);
229
+ return EXIT.usage;
230
+ }
231
+ const loaded = await load(toolName, root, env, home, io.defaults);
232
+ const value = getConfigValue(loaded.effective, key);
233
+ // Warnings go to stderr rather than being suppressed: `get` stays scriptable
234
+ // because its STDOUT is only the value, and a file with an ignored key is
235
+ // something the user wants told wherever they read from it.
236
+ for (const warning of loaded.warnings)
237
+ io.stderr(`warning: ${warning}\n`);
238
+ if (value === undefined) {
239
+ io.stderr(`${toolName}: no value for ${key}\n`);
240
+ return EXIT.error;
241
+ }
242
+ io.stdout(`${JSON.stringify(value)}\n`);
243
+ return EXIT.ok;
244
+ }
245
+ async function setCommand(toolName, root, argv, io, env, home) {
246
+ const taken = takeTarget(argv);
247
+ if ('error' in taken) {
248
+ io.stderr(`${toolName}: ${taken.error}\n\n${usage()}`);
249
+ return EXIT.usage;
250
+ }
251
+ const [key, raw] = taken.rest;
252
+ if (key === undefined || raw === undefined || taken.rest.length > 2) {
253
+ io.stderr(`${toolName}: config set takes a key and a value\n\n${usage()}`);
254
+ return EXIT.usage;
255
+ }
256
+ // configLocations rather than loadConfig: resolving where to write must not
257
+ // depend on the OTHER layer parsing, and `set --user` has to work when
258
+ // WAYFINDER_CONFIG names a file that does not exist yet.
259
+ const locations = configLocations(root, env, home);
260
+ const file = taken.target === 'user' ? locations.user : locations.project;
261
+ // The RAW record, not the validated one: validation drops keys this version
262
+ // does not know, so writing a validated document back would silently delete a
263
+ // setting a newer version had written into the same file.
264
+ const document = (await readRawConfigFile(file)) ?? {};
265
+ let next;
266
+ try {
267
+ next = setConfigValue(document, key, parseConfigValue(raw));
268
+ }
269
+ catch (error) {
270
+ // A ConfigError carrying no file is about what was TYPED — an unknown
271
+ // setting name, or a value of the wrong type for a known one. `file` is
272
+ // what separates those from the errors below, which are about a file.
273
+ if (error instanceof ConfigError && error.file === '') {
274
+ io.stderr(`${toolName}: ${error.message}\n\n${usage()}`);
275
+ return EXIT.usage;
276
+ }
277
+ throw error;
278
+ }
279
+ // Validate the whole document before it reaches disk. A refused value is a
280
+ // message; a written one is a file that fails to load on every later run.
281
+ // A failure HERE names the file — a bad value already in it — so it exits 1.
282
+ validateConfig(next, file);
283
+ await writeConfigFile(file, next);
284
+ // Written, then contradicted. Refusing the write would be a stranger failure
285
+ // — the key is real and the value is valid — but a silent write would leave
286
+ // the author believing the setting took effect, and only `loadConfig`'s
287
+ // warning, which they may never read, would say otherwise.
288
+ if (taken.target === 'project' && key === 'allowExec' && getConfigValue(next, key) === true) {
289
+ io.stderr(`${toolName}: warning: allowExec: true in a project file is ignored — a repository cannot grant itself permission to run subprocesses; run "${BIN} trust" to trust this repository\n`);
290
+ }
291
+ if (taken.target === 'project' && (key === 'trust.paths' || key === 'trust.owners')) {
292
+ const written = getConfigValue(next, key);
293
+ if (Array.isArray(written) && written.length > 0) {
294
+ io.stderr(`${toolName}: warning: ${key} in a project file is ignored — a repository cannot grant itself trust; set it in your user config instead\n`);
295
+ }
296
+ }
297
+ io.stdout(`${file}: ${key} = ${JSON.stringify(getConfigValue(next, key))}\n`);
298
+ return EXIT.ok;
299
+ }
300
+ async function unsetCommand(toolName, root, argv, io, env, home) {
301
+ const taken = takeTarget(argv);
302
+ if ('error' in taken) {
303
+ io.stderr(`${toolName}: ${taken.error}\n\n${usage()}`);
304
+ return EXIT.usage;
305
+ }
306
+ const key = taken.rest[0];
307
+ if (key === undefined || taken.rest.length > 1) {
308
+ io.stderr(`${toolName}: config unset takes exactly one key\n\n${usage()}`);
309
+ return EXIT.usage;
310
+ }
311
+ const locations = configLocations(root, env, home);
312
+ const file = taken.target === 'user' ? locations.user : locations.project;
313
+ const document = await readRawConfigFile(file);
314
+ if (document === null || getConfigValue(document, key) === undefined) {
315
+ // Reported rather than shrugged off: "unset" succeeding on a key that was
316
+ // never there reads as "the setting is gone from where I meant", which is
317
+ // wrong whenever the value actually lives in the other layer.
318
+ io.stderr(`${toolName}: ${key} is not set in ${file}\n`);
319
+ return EXIT.error;
320
+ }
321
+ const next = unsetConfigValue(document, key);
322
+ await writeConfigFile(file, next);
323
+ io.stdout(`${file}: removed ${key}\n`);
324
+ return EXIT.ok;
325
+ }
326
+ async function pathCommand(toolName, argv, io, env, home, root) {
327
+ // Every sibling rejects arguments it does not understand. `path` swallowing
328
+ // them silently would make it the one place a typo looks like it worked.
329
+ if (argv.length > 0) {
330
+ io.stderr(`${toolName}: config path takes no arguments\n\n${usage()}`);
331
+ return EXIT.usage;
332
+ }
333
+ // Locations are resolved without reading or parsing either file's CONTENTS —
334
+ // existence is all this checks. `config path` is the command you reach for
335
+ // when a file will not load, so it must not fail on the file it names.
336
+ const locations = configLocations(root, env, home);
337
+ io.stdout(`user: ${await describeFile(locations.user)}\n`);
338
+ if (locations.userEnvVar !== null)
339
+ io.stdout(` (from ${locations.userEnvVar})\n`);
340
+ io.stdout(`project: ${await describeFile(locations.project)}\n`);
341
+ return EXIT.ok;
342
+ }