@osdlc/wayfinder 0.7.3 → 0.8.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.
Files changed (121) hide show
  1. package/CHANGELOG.md +221 -0
  2. package/CONTRIBUTING.md +6 -0
  3. package/README.md +295 -150
  4. package/SECURITY.md +19 -5
  5. package/dist/actions.d.ts +23 -0
  6. package/dist/actions.js +97 -0
  7. package/dist/app.d.ts +1 -13
  8. package/dist/app.js +1 -13
  9. package/dist/args.d.ts +11 -6
  10. package/dist/args.js +25 -7
  11. package/dist/cache.d.ts +2 -4
  12. package/dist/cache.js +5 -10
  13. package/dist/catalog.d.ts +30 -3
  14. package/dist/catalog.js +87 -11
  15. package/dist/clean-command.d.ts +65 -36
  16. package/dist/clean-command.js +372 -226
  17. package/dist/cli.d.ts +24 -57
  18. package/dist/cli.js +139 -568
  19. package/dist/commands/browse.d.ts +128 -0
  20. package/dist/commands/browse.js +151 -0
  21. package/dist/commands/index.d.ts +25 -0
  22. package/dist/commands/index.js +71 -0
  23. package/dist/commands/list.d.ts +208 -0
  24. package/dist/commands/list.js +127 -0
  25. package/dist/commands/query.d.ts +166 -0
  26. package/dist/commands/query.js +73 -0
  27. package/dist/commands/run.d.ts +114 -0
  28. package/dist/commands/run.js +139 -0
  29. package/dist/commands/shared.d.ts +152 -0
  30. package/dist/commands/shared.js +209 -0
  31. package/dist/config-command.d.ts +27 -43
  32. package/dist/config-command.js +129 -330
  33. package/dist/config-keys.d.ts +25 -0
  34. package/dist/config-keys.js +29 -0
  35. package/dist/config.d.ts +271 -257
  36. package/dist/config.js +610 -460
  37. package/dist/core/copy-text.d.ts +30 -0
  38. package/dist/core/copy-text.js +50 -0
  39. package/dist/core/filter.d.ts +55 -0
  40. package/dist/core/filter.js +59 -0
  41. package/dist/core/run.d.ts +22 -0
  42. package/dist/core/run.js +59 -0
  43. package/dist/core/saved-view.d.ts +59 -0
  44. package/dist/core/saved-view.js +119 -0
  45. package/dist/core/session.d.ts +262 -0
  46. package/dist/core/session.js +589 -0
  47. package/dist/core/skills.d.ts +53 -0
  48. package/dist/core/skills.js +54 -0
  49. package/dist/core/unknowns.d.ts +72 -0
  50. package/dist/core/unknowns.js +68 -0
  51. package/dist/devlink.d.ts +3 -0
  52. package/dist/devlink.js +5 -2
  53. package/dist/document-schemas.generated.d.ts +22 -0
  54. package/dist/document-schemas.generated.js +916 -0
  55. package/dist/documents.d.ts +432 -0
  56. package/dist/documents.js +163 -0
  57. package/dist/groups.d.ts +22 -26
  58. package/dist/groups.js +13 -29
  59. package/dist/guide.d.ts +26 -13
  60. package/dist/guide.js +119 -103
  61. package/dist/index.d.ts +112 -0
  62. package/dist/index.js +58 -0
  63. package/dist/keys.d.ts +51 -0
  64. package/dist/keys.js +273 -0
  65. package/dist/page.d.ts +18 -8
  66. package/dist/page.js +28 -17
  67. package/dist/providers-command.d.ts +81 -20
  68. package/dist/providers-command.js +33 -75
  69. package/dist/schema.d.ts +48 -0
  70. package/dist/schema.js +61 -0
  71. package/dist/serve/auth.d.ts +46 -0
  72. package/dist/serve/auth.js +82 -0
  73. package/dist/serve/catalog-share.d.ts +51 -0
  74. package/dist/serve/catalog-share.js +116 -0
  75. package/dist/serve/index.d.ts +98 -0
  76. package/dist/serve/index.js +251 -0
  77. package/dist/serve/routes.d.ts +60 -0
  78. package/dist/serve/routes.js +186 -0
  79. package/dist/serve/terminal-input.d.ts +50 -0
  80. package/dist/serve/terminal-input.js +108 -0
  81. package/dist/skills.d.ts +19 -0
  82. package/dist/skills.js +54 -0
  83. package/dist/terminal-text.d.ts +8 -0
  84. package/dist/terminal-text.js +19 -6
  85. package/dist/tool-info.d.ts +24 -0
  86. package/dist/tool-info.js +17 -0
  87. package/dist/trust-command.d.ts +15 -27
  88. package/dist/trust-command.js +110 -208
  89. package/dist/trust-store.d.ts +5 -10
  90. package/dist/trust-store.js +9 -42
  91. package/dist/tui/app.d.ts +180 -0
  92. package/dist/tui/app.js +1278 -0
  93. package/dist/tui/copy.d.ts +29 -0
  94. package/dist/tui/copy.js +39 -0
  95. package/dist/tui/index.d.ts +33 -0
  96. package/dist/tui/index.js +97 -0
  97. package/dist/tui/keys-input.d.ts +27 -0
  98. package/dist/tui/keys-input.js +87 -0
  99. package/dist/tui/settings.d.ts +94 -0
  100. package/dist/tui/settings.js +198 -0
  101. package/dist/tui/style.d.ts +62 -0
  102. package/dist/tui/style.js +77 -0
  103. package/dist/version.d.ts +6 -0
  104. package/dist/version.js +7 -0
  105. package/dist/vocabulary.d.ts +89 -0
  106. package/dist/vocabulary.js +153 -0
  107. package/dist/web/main.js +1965 -568
  108. package/dist/web/page.html +39 -33
  109. package/dist/web/styles.css +457 -329
  110. package/dist/xdg.d.ts +76 -4
  111. package/dist/xdg.js +77 -5
  112. package/package.json +28 -9
  113. package/schema/config.json +133 -0
  114. package/dist/http.d.ts +0 -22
  115. package/dist/http.js +0 -149
  116. package/dist/origin-owner.d.ts +0 -19
  117. package/dist/origin-owner.js +0 -85
  118. package/dist/serve.d.ts +0 -98
  119. package/dist/serve.js +0 -671
  120. package/dist/tui.d.ts +0 -177
  121. package/dist/tui.js +0 -840
@@ -1,346 +1,145 @@
1
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.
2
+ * `way config`: cli-tool's shared `config` group over wayfinder's files.
3
+ * {@link configStore} is the {@link ConfigStore} it runs on; this module also
4
+ * holds what every wayfinder command needs from its context (the home
5
+ * directory and the project root).
6
+ *
7
+ * `trust`, `allowExec` and `tools` are written from the command line only
8
+ * ({@link CLI_ONLY_KEYS}): the web view's server refuses them.
5
9
  */
6
- import { access } from 'node:fs/promises';
7
- import { BIN } from "./app.js";
8
- import { CONFIG_ENV_VAR, ConfigError, configLocations, envIgnoredWarning, DEFAULT_CONFIG, getConfigValue, loadConfig, mergeConfig, parseConfigValue, readRawConfigFile, setConfigValue, unsetConfigValue, validateConfig, writeConfigFile, } from "./config.js";
9
- import { jsonText, terminalSafe } from "./terminal-text.js";
10
- import { EXIT } from '@osdlc/cli-tool';
10
+ import { stat } from 'node:fs/promises';
11
+ import { isAbsolute, resolve } from 'node:path';
12
+ import { CommandError, UsageError } from '@osdlc/cli-tool/command';
13
+ import { ConfigUsageError, configCommands, } from '@osdlc/cli-tool/config-command';
11
14
  import { homeDir } from '@osdlc/trust-repo';
12
- function usage() {
13
- return `${BIN} config — read and write the layered task configuration
14
-
15
- usage: ${BIN} config <command>
16
-
17
- list [--json] every setting, and the layer it came from
18
- get <key> one setting's effective value, as JSON
19
- set <key> <value> [--user|--project] write a setting (default --project)
20
- unset <key> [--user|--project] remove a setting (default --project)
21
- path where each layer's file lives
22
- -h, --help this text
23
-
24
- Keys are dotted: runners.disabled, runners.order, allowExec, discovery,
25
- excludePaths, trust.paths, trust.owners, tools.
26
-
27
- A value is read as JSON, falling back to the bare string when that fails — so
28
- \`set discovery git\` and \`set runners.order '["moon","just"]'\` both work, and a
29
- plain word needs no quoting.
30
-
31
- Layers, lowest precedence first: built-in defaults, the user file, the project
32
- file, then this tool's own flags. ${CONFIG_ENV_VAR} replaces the user file's
33
- location and nothing else.
34
- `;
15
+ import { projectRootFor } from "./catalog.js";
16
+ import { CLI_ONLY_KEYS, CONFIG_KEYS, ConfigError, DEFAULT_CONFIG, configLocations, envIgnoredWarning, getConfigValue, loadConfig, projectIgnoreReason, readSettings, removeSetting, resolveConfigKey, writeSetting, } from "./config.js";
17
+ /** The home directory for `ctx`: an absolute `HOME`, else the OS account's home. */
18
+ function homeOf(ctx) {
19
+ const home = ctx.env['HOME'];
20
+ return home !== undefined && isAbsolute(home) ? home : homeDir();
35
21
  }
36
22
  /**
37
- * `loadConfig`, with the calling tool's defaults placed under the file layers.
38
- *
39
- * `sources` is left as it is on purpose. A value from the tool's defaults
40
- * still came from no file, so `(default)` is still the right label. Only the
41
- * value next to that label changes.
23
+ * The project root for `ctx`: the directory `-C` names (`ctx.globals.dir`),
24
+ * else `ctx.cwd`, resolved upward to the project root. A directory that does
25
+ * not exist is an error, so a typo never edits the enclosing repository.
42
26
  */
43
- async function load(toolName, root, env, home, defaults) {
44
- const loaded = await loadConfig(root, env, home);
45
- if (defaults === undefined || Object.keys(defaults).length === 0)
46
- return loaded;
47
- const base = mergeConfig(DEFAULT_CONFIG, defaults);
48
- const effective = mergeConfig(mergeConfig(base, loaded.layers.user ?? {}), loaded.layers.project ?? {});
49
- return { ...loaded, effective };
50
- }
51
- async function exists(file) {
52
- try {
53
- await access(file);
54
- return true;
55
- }
56
- catch {
57
- return false;
58
- }
27
+ async function projectRootOf(ctx) {
28
+ const named = ctx.globals['dir'];
29
+ const dir = resolve(ctx.cwd, typeof named === 'string' && named !== '' ? named : '.');
30
+ const root = await projectRootFor(dir);
31
+ if (root === null)
32
+ throw new CommandError(`no such directory: ${dir}`);
33
+ return root;
59
34
  }
60
- /** `<path> (exists)` or `<path> (missing)`, which is what `config path` reports. */
61
- async function describeFile(file) {
62
- return `${terminalSafe(file)} ${(await exists(file)) ? '(exists)' : '(missing)'}`;
35
+ /** {@link homeOf} and {@link projectRootOf}, or the caller's own. */
36
+ export function commandEnv(opts = {}) {
37
+ return { home: opts.home ?? homeOf, root: opts.root ?? projectRootOf };
63
38
  }
64
- function pad(text, width) {
65
- return text.length >= width ? text : text + ' '.repeat(width - text.length);
39
+ /** A broken file, as the command's failure (exit 1). */
40
+ function asCommandError(err) {
41
+ if (err instanceof ConfigError)
42
+ throw new CommandError(err.message);
43
+ throw err;
66
44
  }
67
45
  /**
68
- * Split `--user` / `--project` off the argument list, leaving the positionals.
69
- *
70
- * The two name different files, and both callers write. If the last flag won,
71
- * a typo could silently edit the wrong file. Giving both is a usage error.
46
+ * wayfinder's {@link ConfigStore}: the user and project files, with the
47
+ * project file the default for `set` and `unset`.
72
48
  */
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' };
49
+ function configStore(opts = {}) {
50
+ const env = commandEnv(opts);
51
+ const load = async (ctx) => loadConfig(await env.root(ctx), ctx.env, env.home(ctx)).catch(asCommandError);
52
+ const settings = async (ctx) => readSettings(await env.root(ctx), ctx.env, env.home(ctx)).catch(asCommandError);
53
+ const target = async (ctx, layer) => ({
54
+ layer: layer,
55
+ root: await env.root(ctx),
56
+ env: ctx.env,
57
+ home: env.home(ctx),
58
+ });
59
+ return {
60
+ layers: ['user', 'project'],
61
+ defaultLayer: 'project',
62
+ keys: CONFIG_KEYS,
63
+ cliOnlyKeys: CLI_ONLY_KEYS,
64
+ resolveKey: resolveConfigKey,
65
+ async list(ctx) {
66
+ const read = await settings(ctx);
67
+ return {
68
+ settings: read.settings.map(({ key, value, source }) => ({ key, value, source })),
69
+ warnings: read.warnings,
70
+ };
71
+ },
72
+ // Existence only: `config path` is what you run when a file will not load.
73
+ async locations(ctx) {
74
+ const locations = configLocations(await env.root(ctx), ctx.env, env.home(ctx));
75
+ const ignored = envIgnoredWarning(locations);
76
+ if (ignored !== null)
77
+ ctx.warn(ignored);
78
+ const exists = async (file) => (await stat(file).catch(() => null)) !== null;
79
+ return [
80
+ {
81
+ layer: 'user',
82
+ path: locations.user,
83
+ exists: await exists(locations.user),
84
+ envVar: locations.userEnvVar,
85
+ },
86
+ { layer: 'project', path: locations.project, exists: await exists(locations.project) },
87
+ ];
88
+ },
89
+ async set(ctx, layer, key, value) {
90
+ if (resolveConfigKey(key) === undefined)
91
+ throw new ConfigUsageError(`unknown setting: ${key}`);
92
+ try {
93
+ return await writeSetting(await target(ctx, layer), key, value);
81
94
  }
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 };
95
+ catch (err) {
96
+ // An empty `file` is about what was typed: a wrong value, or a key
97
+ // the project file may not hold.
98
+ if (err instanceof ConfigError && err.file === '')
99
+ throw new UsageError(err.message);
100
+ return asCommandError(err);
101
+ }
102
+ },
103
+ async unset(ctx, layer, key) {
104
+ return removeSetting(await target(ctx, layer), key).catch(asCommandError);
105
+ },
106
+ async explain(ctx, key) {
107
+ if (resolveConfigKey(key) === undefined)
108
+ throw new ConfigUsageError(`unknown setting: ${key}`);
109
+ const loaded = await load(ctx);
110
+ const fallback = getConfigValue(DEFAULT_CONFIG, key);
111
+ const user = loaded.files.user === null ? undefined : getConfigValue(loaded.files.user, key);
112
+ const project = loaded.files.project === null ? undefined : getConfigValue(loaded.files.project, key);
113
+ const projectApplied = loaded.layers.project === null ? undefined : getConfigValue(loaded.layers.project, key);
114
+ const reason = project === undefined ? null : projectIgnoreReason(key, project);
115
+ const layers = [
116
+ { layer: 'default', set: fallback !== undefined, value: fallback },
117
+ {
118
+ layer: 'user',
119
+ set: user !== undefined,
120
+ value: user,
121
+ file: loaded.locations.user,
122
+ ...(loaded.locations.userEnvVar !== null ? { envVar: loaded.locations.userEnvVar } : {}),
123
+ },
124
+ {
125
+ layer: 'project',
126
+ set: project !== undefined,
127
+ value: project,
128
+ file: loaded.locations.project,
129
+ allowed: project === undefined || projectApplied !== undefined,
130
+ ...(reason !== null ? { note: reason } : {}),
131
+ },
132
+ ];
133
+ return { layers, warnings: loaded.warnings };
134
+ },
135
+ };
128
136
  }
129
137
  /**
130
- * `<tool> config <sub> …`. `root` is the resolved project root. Returns the
131
- * exit code.
132
- *
133
- * Exit codes: 0 means success. 2 means the command cannot run as typed: an
134
- * unknown subcommand or option, a wrong number of arguments, conflicting
135
- * flags, an unknown setting name, or a value of the wrong type. Each is
136
- * decided from the arguments and the schema alone, with no file involved, so
137
- * running it again gives the same answer. 1 means a valid command that
138
- * failed: a file that will not parse, a key that is not set where you asked
139
- * to unset it, or a `get` that found nothing.
140
- *
141
- * A script can act on the difference: 2 means fix the command, 1 means look
142
- * at the state.
138
+ * The `config` group: `list` (the default), `get`, `set`, `unset`, `path` and
139
+ * `explain`. Mount it as `config: configCommand({ globals })`, with the app's
140
+ * global flags; `-C` must be declared as `dir`.
143
141
  */
144
- export async function runConfigCommand(toolName, root, argv, io) {
145
- const env = io.env ?? process.env;
146
- const home = io.home ?? homeDir();
147
- const fail = (message) => {
148
- io.stderr(`${toolName}: ${message}\n`);
149
- return EXIT.error;
150
- };
151
- const [sub, ...rest] = argv;
152
- try {
153
- switch (sub) {
154
- // Usage on stdout with exit 0, because `--help` asked for it.
155
- case '-h':
156
- case '--help':
157
- io.stdout(usage());
158
- return EXIT.ok;
159
- case undefined:
160
- case 'list':
161
- return await listCommand(toolName, root, rest, io, env, home);
162
- case 'get':
163
- return await getCommand(toolName, root, rest, io, env, home);
164
- case 'set':
165
- return await setCommand(toolName, root, rest, io, env, home);
166
- case 'unset':
167
- return await unsetCommand(toolName, root, rest, io, env, home);
168
- case 'path':
169
- return await pathCommand(toolName, rest, io, env, home, root);
170
- default:
171
- io.stderr(`${toolName}: unknown config command: ${sub}\n\n${usage()}`);
172
- return EXIT.usage;
173
- }
174
- }
175
- catch (error) {
176
- // A ConfigError names its file and its problem. A setting that quietly did
177
- // not apply is worse than one that refused to load, so there is no silent
178
- // fallback.
179
- if (error instanceof ConfigError)
180
- return fail(error.message);
181
- throw error;
182
- }
183
- }
184
- async function listCommand(toolName, root, argv, io, env, home) {
185
- let json = false;
186
- for (const arg of argv) {
187
- if (arg === '--json')
188
- json = true;
189
- else {
190
- io.stderr(`${toolName}: unknown option: ${arg}\n\n${usage()}`);
191
- return EXIT.usage;
192
- }
193
- }
194
- const loaded = await load(toolName, root, env, home, io.defaults);
195
- if (json) {
196
- const sources = {};
197
- for (const [key, layer] of loaded.sources)
198
- sources[key] = layer;
199
- io.stdout(`${jsonText({ effective: loaded.effective, sources, locations: loaded.locations }, 2)}\n`);
200
- }
201
- else {
202
- // JSON.stringify leaves DEL and C1 characters raw, so the human listing
203
- // still goes through terminalSafe.
204
- const rows = [...loaded.sources].map(([key, layer]) => ({
205
- key: terminalSafe(key),
206
- value: terminalSafe(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 hidden. `get` stays scriptable
234
- // because its stdout is only the value, and the user should hear about an
235
- // ignored key wherever they read the config.
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(`${jsonText(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: where to write must not depend on
257
- // 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
263
- // a setting a newer version had written to 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 with no file is about what was typed: an unknown setting
271
- // name, or a value of the wrong type. An empty `file` tells these apart
272
- // 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 writing it. A refused value is only a
280
- // message, but a bad written value makes the file fail to load on every
281
- // later run. A failure here is a bad value already in the file, so the
282
- // error names the file and exits 1.
283
- validateConfig(next, file);
284
- await writeConfigFile(file, next);
285
- // Written, then warned about. Refusing the write would be odd, since the key
286
- // is real and the value is valid. But a silent write would let the author
287
- // believe the setting took effect, with only `loadConfig`'s warning, which
288
- // they may never read, saying otherwise.
289
- if (taken.target === 'project' && key === 'allowExec' && getConfigValue(next, key) === true) {
290
- 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`);
291
- }
292
- if (taken.target === 'project' && (key === 'trust.paths' || key === 'trust.owners')) {
293
- const written = getConfigValue(next, key);
294
- if (Array.isArray(written) && written.length > 0) {
295
- 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`);
296
- }
297
- }
298
- io.stdout(terminalSafe(`${file}: ${key} = ${JSON.stringify(getConfigValue(next, key))}`) + '\n');
299
- return EXIT.ok;
300
- }
301
- async function unsetCommand(toolName, root, argv, io, env, home) {
302
- const taken = takeTarget(argv);
303
- if ('error' in taken) {
304
- io.stderr(`${toolName}: ${taken.error}\n\n${usage()}`);
305
- return EXIT.usage;
306
- }
307
- const key = taken.rest[0];
308
- if (key === undefined || taken.rest.length > 1) {
309
- io.stderr(`${toolName}: config unset takes exactly one key\n\n${usage()}`);
310
- return EXIT.usage;
311
- }
312
- const locations = configLocations(root, env, home);
313
- const file = taken.target === 'user' ? locations.user : locations.project;
314
- const document = await readRawConfigFile(file);
315
- if (document === null || getConfigValue(document, key) === undefined) {
316
- // Reported as an error. Succeeding on a key that was not there would
317
- // suggest the setting is gone, which is wrong when the value is set in
318
- // the other layer.
319
- io.stderr(`${toolName}: ${key} is not set in ${file}\n`);
320
- return EXIT.error;
321
- }
322
- const next = unsetConfigValue(document, key);
323
- await writeConfigFile(file, next);
324
- io.stdout(terminalSafe(`${file}: removed ${key}`) + '\n');
325
- return EXIT.ok;
326
- }
327
- async function pathCommand(toolName, argv, io, env, home, root) {
328
- // Every other subcommand rejects arguments it does not understand. If `path`
329
- // ignored them, a typo here would look like it worked.
330
- if (argv.length > 0) {
331
- io.stderr(`${toolName}: config path takes no arguments\n\n${usage()}`);
332
- return EXIT.usage;
333
- }
334
- // Locations are resolved without reading either file's contents. Only
335
- // existence is checked. You use `config path` when a file will not load, so
336
- // it must not fail on the file it names.
337
- const locations = configLocations(root, env, home);
338
- const ignored = envIgnoredWarning(locations);
339
- if (ignored !== null)
340
- io.stderr(`warning: ${ignored}\n`);
341
- io.stdout(`user: ${await describeFile(locations.user)}\n`);
342
- if (locations.userEnvVar !== null)
343
- io.stdout(` (from ${locations.userEnvVar})\n`);
344
- io.stdout(`project: ${await describeFile(locations.project)}\n`);
345
- return EXIT.ok;
142
+ export function configCommand(options = {}) {
143
+ const { globals, ...env } = options;
144
+ return configCommands(configStore(env), globals !== undefined ? { globals } : {});
346
145
  }
@@ -0,0 +1,25 @@
1
+ /**
2
+ * Config key lists that the page needs as well as the CLI. Browser-safe: no
3
+ * Node built-in, so the web view imports it.
4
+ */
5
+ /** The `theme` values for the web view. `system` follows the browser. */
6
+ export declare const THEME_VALUES: readonly ["system", "light", "dark"];
7
+ /** The web view's theme. */
8
+ export type Theme = (typeof THEME_VALUES)[number];
9
+ /**
10
+ * The keys `config set` and `config unset` change from the command line only.
11
+ * cli-tool's adapters, such as the HTTP one the web view's server runs,
12
+ * refuse a write of any of them: an agent or a page must not grant trust,
13
+ * turn execution on, or name code to load.
14
+ */
15
+ export declare const CLI_ONLY_KEYS: readonly string[];
16
+ /** The top-level keys only the user layer may set. */
17
+ export declare const USER_ONLY_KEYS: readonly string[];
18
+ /**
19
+ * The file a settings screen writes `key` to, given where its value comes
20
+ * from now (`default`, `user` or `project`). A user-only key goes to the
21
+ * user file. `runners.order` goes to the file that sets it, else the user
22
+ * file, so a reorder always takes effect. Every other key goes to the
23
+ * project file.
24
+ */
25
+ export declare function settingLayer(key: string, source: string): 'user' | 'project';
@@ -0,0 +1,29 @@
1
+ /**
2
+ * Config key lists that the page needs as well as the CLI. Browser-safe: no
3
+ * Node built-in, so the web view imports it.
4
+ */
5
+ /** The `theme` values for the web view. `system` follows the browser. */
6
+ export const THEME_VALUES = ['system', 'light', 'dark'];
7
+ /**
8
+ * The keys `config set` and `config unset` change from the command line only.
9
+ * cli-tool's adapters, such as the HTTP one the web view's server runs,
10
+ * refuse a write of any of them: an agent or a page must not grant trust,
11
+ * turn execution on, or name code to load.
12
+ */
13
+ export const CLI_ONLY_KEYS = ['trust', 'allowExec', 'tools'];
14
+ /** The top-level keys only the user layer may set. */
15
+ export const USER_ONLY_KEYS = ['trust', 'open', 'theme', 'skills'];
16
+ /**
17
+ * The file a settings screen writes `key` to, given where its value comes
18
+ * from now (`default`, `user` or `project`). A user-only key goes to the
19
+ * user file. `runners.order` goes to the file that sets it, else the user
20
+ * file, so a reorder always takes effect. Every other key goes to the
21
+ * project file.
22
+ */
23
+ export function settingLayer(key, source) {
24
+ if (USER_ONLY_KEYS.includes(key.split('.')[0]))
25
+ return 'user';
26
+ if (key === 'runners.order')
27
+ return source === 'project' ? 'project' : 'user';
28
+ return 'project';
29
+ }