@codewizard-dt/bootstrap 5.1.0 → 6.0.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/README.md +11 -3
- package/bin/cli.js +16 -0
- package/lib/hooks/README.md +124 -3
- package/lib/hooks/claude-settings-guard.js +8 -2
- package/lib/prompts/README.md +1 -1
- package/lib/prompts/migrate-wiki.md +7 -7
- package/lib/scripts/README.md +111 -10
- package/lib/scripts/bootstrap-prefs-tui.js +1222 -0
- package/lib/scripts/install-global.sh +137 -6
- package/lib/scripts/migrate-project.sh +52 -10
- package/lib/scripts/rename-wiki-ids.js +232 -0
- package/lib/scripts/templates/CLAUDE-wiki.md +6 -6
- package/lib/scripts/templates/bootstrap-prefs-schema.json +11 -2
- package/lib/scripts/templates/settings-sandbox-unattended.json +36 -0
- package/lib/scripts/templates/settings-sandbox.json +7 -0
- package/lib/scripts/templates/wiki/conventions.md +12 -10
- package/lib/scripts/templates/wiki/dashboard.html +527 -23
- package/lib/scripts/templates/wiki/index.md +2 -2
- package/lib/scripts/templates/wiki/work/bugs/lifecycle.md +1 -1
- package/lib/scripts/templates/wiki/work/decisions/index.md +12 -4
- package/lib/scripts/templates/wiki/work/decisions/lifecycle.md +11 -9
- package/lib/scripts/templates/wiki/work/requirements/index.md +12 -4
- package/lib/scripts/templates/wiki/work/requirements/lifecycle.md +8 -8
- package/lib/scripts/templates/wiki/work/roadmaps/index.md +11 -3
- package/lib/scripts/templates/wiki/work/roadmaps/lifecycle.md +7 -5
- package/lib/scripts/templates/wiki/work/tasks/index.md +1 -1
- package/lib/scripts/templates/wiki/work/tasks/lifecycle.md +16 -8
- package/lib/scripts/templates/wiki/work/uat/lifecycle.md +3 -3
- package/lib/scripts/wiki-dashboard-server.js +98 -0
- package/lib/skills/README.md +27 -22
- package/lib/skills/_shared/ARCHIVE-WORK-ITEM.md +103 -0
- package/lib/skills/_shared/ID-RESOLUTION.md +142 -0
- package/lib/skills/_shared/SKILL-BOILERPLATE.md +50 -0
- package/lib/skills/adr-assess/SKILL.md +112 -0
- package/lib/skills/{decision-create → adr-create}/SKILL.md +27 -26
- package/lib/skills/adr-deprecate/SKILL.md +190 -0
- package/lib/skills/adr-extract/SKILL.md +255 -0
- package/lib/skills/{decision-finalize → adr-finalize}/SKILL.md +38 -38
- package/lib/skills/{decision-next → adr-next}/SKILL.md +20 -19
- package/lib/skills/{decision-walkthrough → adr-walkthrough}/SKILL.md +23 -29
- package/lib/skills/architecture-audit/SKILL.md +24 -3
- package/lib/skills/autopilot/SKILL.md +110 -0
- package/lib/skills/bootstrap-config/SKILL.md +1 -1
- package/lib/skills/bug-assess/SKILL.md +3 -3
- package/lib/skills/bug-close/SKILL.md +15 -26
- package/lib/skills/{bug-file → bug-create}/SKILL.md +25 -15
- package/lib/skills/bug-triage/SKILL.md +5 -19
- package/lib/skills/demo/SKILL.md +11 -11
- package/lib/skills/elevator-pitch/SKILL.md +1 -1
- package/lib/skills/extract-feature/SKILL.md +29 -87
- package/lib/skills/git-commit/SKILL.md +4 -4
- package/lib/skills/marp-slideshow/SKILL.md +2 -2
- package/lib/skills/port-feature/SKILL.md +28 -83
- package/lib/skills/power-mode/SKILL.md +42 -49
- package/lib/skills/prd-assess/SKILL.md +105 -0
- package/lib/skills/{req-compile → prd-compile}/SKILL.md +6 -13
- package/lib/skills/{req-create → prd-create}/SKILL.md +19 -19
- package/lib/skills/prd-deprecate/SKILL.md +173 -0
- package/lib/skills/{req-finalize → prd-finalize}/SKILL.md +20 -29
- package/lib/skills/primer/SKILL.md +2 -2
- package/lib/skills/research/SKILL.md +3 -3
- package/lib/skills/roadmap-assess/SKILL.md +20 -20
- package/lib/skills/roadmap-create/SKILL.md +23 -29
- package/lib/skills/roadmap-next/SKILL.md +16 -28
- package/lib/skills/security-audit/SKILL.md +3 -3
- package/lib/skills/tackle/SKILL.md +13 -15
- package/lib/skills/{task-audit → task-assess}/SKILL.md +34 -44
- package/lib/skills/task-close/SKILL.md +73 -0
- package/lib/skills/{task-add → task-create}/SKILL.md +44 -24
- package/lib/skills/uat-assess/SKILL.md +124 -0
- package/lib/skills/uat-auto/SKILL.md +9 -6
- package/lib/skills/uat-auto-plus/SKILL.md +2 -2
- package/lib/skills/{uat-generate → uat-create}/SKILL.md +10 -12
- package/lib/skills/uat-skip/SKILL.md +5 -9
- package/lib/skills/uat-walk/SKILL.md +2 -2
- package/lib/skills/uat-walk/UAT-CORE.md +29 -16
- package/lib/skills/update-docs/SKILL.md +1 -1
- package/lib/skills/usage-report/SKILL.md +52 -0
- package/lib/skills/usage-report/scripts/pricing.json +22 -0
- package/lib/skills/usage-report/scripts/usage-report.js +518 -0
- package/lib/skills/whats-next/SKILL.md +177 -0
- package/lib/skills/wiki-archive/SKILL.md +12 -23
- package/lib/skills/wiki-lint/SKILL.md +42 -5
- package/lib/skills/wiki-query/SKILL.md +1 -1
- package/lib/skills/wiki-rotate-log/SKILL.md +1 -1
- package/lib/skills/wiki-tidy/SKILL.md +6 -9
- package/package.json +1 -1
- package/lib/scripts/templates/wiki/work/decisions/archive/index.md +0 -10
- package/lib/scripts/templates/wiki/work/requirements/archive/index.md +0 -10
- package/lib/scripts/templates/wiki/work/roadmaps/archive/index.md +0 -10
- package/lib/skills/req-extract-decisions/SKILL.md +0 -241
- package/lib/skills/req-retire/SKILL.md +0 -185
- package/lib/skills/req-update/SKILL.md +0 -265
- package/lib/skills/task-trash/SKILL.md +0 -115
- package/lib/skills/task-update/SKILL.md +0 -136
|
@@ -0,0 +1,1222 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// Interactive terminal menu for the bootstrap preference store — view, edit,
|
|
3
|
+
// and reset any bootstrap-prefs.json key from a plain terminal, with no Claude
|
|
4
|
+
// Code session. It is the readline port of the /bootstrap-config skill's
|
|
5
|
+
// Step A-G algorithm (lib/skills/bootstrap-config/SKILL.md).
|
|
6
|
+
//
|
|
7
|
+
// THE ENGINE IS THE ONLY WRITER. Every read (--list / --get / --section-key)
|
|
8
|
+
// and every write (--set / --unset) is a child-process call to the sibling
|
|
9
|
+
// bootstrap-prefs.js. This file never require()s the engine and never writes
|
|
10
|
+
// bootstrap-prefs.json or bootstrap-prefs.README.md itself, so schema
|
|
11
|
+
// validation, the scope-refusal rule (BUG-0009), true/false coercion, and
|
|
12
|
+
// atomic writes are inherited unchanged. A non-zero engine exit is surfaced
|
|
13
|
+
// verbatim and never retried or worked around.
|
|
14
|
+
//
|
|
15
|
+
// Scope is bootstrap-prefs.json only — never Claude Code's settings.json.
|
|
16
|
+
//
|
|
17
|
+
// Usage:
|
|
18
|
+
// bootstrap-prefs-tui.js [--global] [--project <dir>] [--schema <path>]
|
|
19
|
+
// bootstrap-prefs-tui.js -h | --help
|
|
20
|
+
//
|
|
21
|
+
// --global pin every edit/reset to the global layer
|
|
22
|
+
// --project <dir> use <dir> as the project (and pin edits to the project layer)
|
|
23
|
+
// --schema <path> alternate schema; forwarded to the engine only when it is
|
|
24
|
+
// not the default templates/bootstrap-prefs-schema.json
|
|
25
|
+
//
|
|
26
|
+
// The pure helpers (parseArgs, parseValues, resolveEntry, permittedLayers,
|
|
27
|
+
// parseListOutput, groupRows, resolveProjectDir, formatRow, renderSummary,
|
|
28
|
+
// buildKeyOptions, renderKeyDetail, familyExamples) are exported for tests, along
|
|
29
|
+
// with the input helpers (createReader, ask, pickFromList); the menu only runs
|
|
30
|
+
// under `require.main === module`.
|
|
31
|
+
|
|
32
|
+
'use strict';
|
|
33
|
+
|
|
34
|
+
const fs = require('fs');
|
|
35
|
+
const os = require('os');
|
|
36
|
+
const path = require('path');
|
|
37
|
+
const readline = require('readline');
|
|
38
|
+
const { spawnSync } = require('child_process');
|
|
39
|
+
|
|
40
|
+
const ENGINE = path.join(__dirname, 'bootstrap-prefs.js');
|
|
41
|
+
const DEFAULT_SCHEMA = path.join(__dirname, 'templates', 'bootstrap-prefs-schema.json');
|
|
42
|
+
|
|
43
|
+
const USAGE = [
|
|
44
|
+
'Usage:',
|
|
45
|
+
' bootstrap-prefs-tui.js [--global] [--project <dir>] [--schema <path>]',
|
|
46
|
+
' bootstrap-prefs-tui.js -h | --help',
|
|
47
|
+
].join('\n');
|
|
48
|
+
|
|
49
|
+
const ROW_RE = /^ {2}(\S+) = (.*?) {2}\[(target|project|global|default|unset)\]\s*$/;
|
|
50
|
+
const SUMMARY_RE = /^ {6}(\S.*?)\s*$/;
|
|
51
|
+
const GROUP_HEADER_RE = /^(\S+) — (.*)$/;
|
|
52
|
+
const TRAILER_RE = /^(Showing .*|No --project given.*|Layers: .*)$/;
|
|
53
|
+
|
|
54
|
+
const CONSUMER_TITLES = {
|
|
55
|
+
installer: 'Installer preferences',
|
|
56
|
+
skill: 'Skill preferences',
|
|
57
|
+
unrecognized: 'Unrecognized keys',
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
// ---------------------------------------------------------------------------
|
|
61
|
+
// Arguments
|
|
62
|
+
// ---------------------------------------------------------------------------
|
|
63
|
+
|
|
64
|
+
// parseArgs(argv) -> { help, global, project, schema, pin, error }
|
|
65
|
+
// argv process.argv.slice(2)
|
|
66
|
+
// project the --project <dir> operand, or null
|
|
67
|
+
// schema the --schema <path> operand, or null
|
|
68
|
+
// pin 'global' | 'project' | null — the layer pinned by a flag
|
|
69
|
+
// error usage-error message, or null. The caller prints it plus USAGE to
|
|
70
|
+
// stderr and exits 2.
|
|
71
|
+
function parseArgs(argv) {
|
|
72
|
+
const out = { help: false, global: false, project: null, schema: null, pin: null, error: null };
|
|
73
|
+
for (let i = 0; i < argv.length; i++) {
|
|
74
|
+
const arg = argv[i];
|
|
75
|
+
if (arg === '-h' || arg === '--help') {
|
|
76
|
+
out.help = true;
|
|
77
|
+
} else if (arg === '--global') {
|
|
78
|
+
out.global = true;
|
|
79
|
+
} else if (arg === '--project' || arg === '--schema') {
|
|
80
|
+
const operand = argv[i + 1];
|
|
81
|
+
if (operand === undefined || operand.startsWith('--')) {
|
|
82
|
+
out.error = `${arg} requires an operand`;
|
|
83
|
+
return out;
|
|
84
|
+
}
|
|
85
|
+
out[arg.slice(2)] = operand;
|
|
86
|
+
i++;
|
|
87
|
+
} else {
|
|
88
|
+
out.error = `unknown argument: ${arg}`;
|
|
89
|
+
return out;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
if (out.global && out.project !== null) {
|
|
93
|
+
out.error = '--global and --project are mutually exclusive';
|
|
94
|
+
return out;
|
|
95
|
+
}
|
|
96
|
+
out.pin = out.global ? 'global' : out.project !== null ? 'project' : null;
|
|
97
|
+
return out;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// ---------------------------------------------------------------------------
|
|
101
|
+
// Engine wrapper
|
|
102
|
+
// ---------------------------------------------------------------------------
|
|
103
|
+
|
|
104
|
+
// runEngine(args, opts) -> { status, stdout, stderr }
|
|
105
|
+
// args engine argv, e.g. ['--list', '--project', dir]
|
|
106
|
+
// opts.schema schema path; forwarded as --schema only when it differs from
|
|
107
|
+
// DEFAULT_SCHEMA
|
|
108
|
+
// Never throws on a non-zero exit. A spawn failure yields status 1 with the
|
|
109
|
+
// error message in stderr.
|
|
110
|
+
function runEngine(args, opts) {
|
|
111
|
+
const schema = opts && opts.schema;
|
|
112
|
+
const schemaArgs =
|
|
113
|
+
schema && path.resolve(schema) !== path.resolve(DEFAULT_SCHEMA) ? ['--schema', schema] : [];
|
|
114
|
+
const result = spawnSync(process.execPath, [ENGINE, ...args, ...schemaArgs], {
|
|
115
|
+
encoding: 'utf8',
|
|
116
|
+
});
|
|
117
|
+
return {
|
|
118
|
+
status: typeof result.status === 'number' ? result.status : 1,
|
|
119
|
+
stdout: result.stdout || '',
|
|
120
|
+
stderr: result.stderr || (result.error ? result.error.message : ''),
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// ---------------------------------------------------------------------------
|
|
125
|
+
// Schema
|
|
126
|
+
// ---------------------------------------------------------------------------
|
|
127
|
+
|
|
128
|
+
function isPlainObject(value) {
|
|
129
|
+
return value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// loadSchema(file) -> schema object (flat map key -> entry). A missing or
|
|
133
|
+
// malformed schema warns on stderr and yields {} — the menu still runs, with
|
|
134
|
+
// no descriptions or value lists, exactly as the engine degrades.
|
|
135
|
+
function loadSchema(file) {
|
|
136
|
+
let parsed;
|
|
137
|
+
try {
|
|
138
|
+
parsed = JSON.parse(fs.readFileSync(file, 'utf8'));
|
|
139
|
+
} catch (err) {
|
|
140
|
+
process.stderr.write(`Warning: cannot read preference schema at ${file} (${err.message})\n`);
|
|
141
|
+
return {};
|
|
142
|
+
}
|
|
143
|
+
if (!isPlainObject(parsed)) {
|
|
144
|
+
process.stderr.write(`Warning: preference schema at ${file} is not a JSON object\n`);
|
|
145
|
+
return {};
|
|
146
|
+
}
|
|
147
|
+
return parsed;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// resolveEntry(schema, key) -> entry | null
|
|
151
|
+
// Exact match first (this also matches the `guides.*` / `gitignore.section.*`
|
|
152
|
+
// pattern rows themselves), then a dynamic-prefix match mirroring the engine's
|
|
153
|
+
// lookupSchema: a `dynamic: true` pattern `<prefix>.*` matches any key that is
|
|
154
|
+
// longer than `<prefix>.` and starts with it.
|
|
155
|
+
function resolveEntry(schema, key) {
|
|
156
|
+
if (Object.prototype.hasOwnProperty.call(schema, key)) return schema[key];
|
|
157
|
+
for (const [pattern, entry] of Object.entries(schema)) {
|
|
158
|
+
if (!isPlainObject(entry) || entry.dynamic !== true || !pattern.endsWith('.*')) continue;
|
|
159
|
+
const prefix = pattern.slice(0, -1);
|
|
160
|
+
if (key.length > prefix.length && key.startsWith(prefix)) return entry;
|
|
161
|
+
}
|
|
162
|
+
return null;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// parseValues(entry) -> string[]
|
|
166
|
+
// The legal values, from the entry's `values` string ("a | b | c"). Empty array
|
|
167
|
+
// when there is no entry or no values string. Never hardcode a value list.
|
|
168
|
+
function parseValues(entry) {
|
|
169
|
+
if (!entry || typeof entry.values !== 'string') return [];
|
|
170
|
+
return entry.values
|
|
171
|
+
.split('|')
|
|
172
|
+
.map((v) => v.trim())
|
|
173
|
+
.filter((v) => v.length > 0);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// permittedLayers(entry, projectDir, pin) -> { layers, error }
|
|
177
|
+
// entry schema entry or null (null is treated as scope 'either')
|
|
178
|
+
// projectDir resolved project directory or null
|
|
179
|
+
// pin 'global' | 'project' | null
|
|
180
|
+
// layers [{ layer: 'project'|'global', args: [...engine layer flags] }]
|
|
181
|
+
// project -> ['--project', projectDir]; global -> ['--global']
|
|
182
|
+
// error null, or a plain-language reason no layer is available (layers
|
|
183
|
+
// is then [])
|
|
184
|
+
//
|
|
185
|
+
// Scope rules: project -> project only; global -> global only; either -> both
|
|
186
|
+
// (project only when projectDir is set). A pin restricts the result to that
|
|
187
|
+
// layer and is returned even when it contradicts the scope, so the engine's own
|
|
188
|
+
// refusal is what the user sees. A project layer is never offered without a
|
|
189
|
+
// projectDir, and scope 'project' never falls back to global.
|
|
190
|
+
function permittedLayers(entry, projectDir, pin) {
|
|
191
|
+
const scope = entry && typeof entry.scope === 'string' ? entry.scope : 'either';
|
|
192
|
+
const projectLayer = () => ({ layer: 'project', args: ['--project', projectDir] });
|
|
193
|
+
const globalLayer = () => ({ layer: 'global', args: ['--global'] });
|
|
194
|
+
const noProject = (why) => ({
|
|
195
|
+
layers: [],
|
|
196
|
+
error: `${why} but no project directory was found (no .git or .claude/ above the current directory and no --project given). Run from a project, or pass --project <dir>.`,
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
if (pin === 'project') {
|
|
200
|
+
return projectDir ? { layers: [projectLayer()], error: null } : noProject('The project layer is pinned');
|
|
201
|
+
}
|
|
202
|
+
if (pin === 'global') return { layers: [globalLayer()], error: null };
|
|
203
|
+
|
|
204
|
+
if (scope === 'project') {
|
|
205
|
+
return projectDir ? { layers: [projectLayer()], error: null } : noProject('This key is project-scoped');
|
|
206
|
+
}
|
|
207
|
+
if (scope === 'global') return { layers: [globalLayer()], error: null };
|
|
208
|
+
return {
|
|
209
|
+
layers: projectDir ? [projectLayer(), globalLayer()] : [globalLayer()],
|
|
210
|
+
error: null,
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
// ---------------------------------------------------------------------------
|
|
215
|
+
// Engine --list parsing and grouping
|
|
216
|
+
// ---------------------------------------------------------------------------
|
|
217
|
+
|
|
218
|
+
// parseListOutput(text) -> { rows, groups, unrecognized, trailer }
|
|
219
|
+
// rows flat, in printed order:
|
|
220
|
+
// { key, value, layer, summary, group, placeholder }
|
|
221
|
+
// value text as printed ('unset' when layer is unset)
|
|
222
|
+
// layer 'target'|'project'|'global'|'default'|'unset'
|
|
223
|
+
// summary the 6-space-indented line under the row, or ''
|
|
224
|
+
// (unrecognized rows never have one)
|
|
225
|
+
// group the header's leading word ('installer',
|
|
226
|
+
// 'skill', 'unrecognized', ...); '' if a row
|
|
227
|
+
// appears before any header
|
|
228
|
+
// placeholder true for a dynamic-family pattern row such as
|
|
229
|
+
// `guides.* = unset [unset]` (key ends '.*')
|
|
230
|
+
// groups [{ name, header, rows }] in printed order
|
|
231
|
+
// unrecognized the rows whose group is 'unrecognized'
|
|
232
|
+
// trailer the engine's closing line ('Showing ...' / 'No --project
|
|
233
|
+
// given...' / 'Layers: ...'), or null
|
|
234
|
+
// Rows have exactly two leading spaces and summaries six, so indentation, not
|
|
235
|
+
// the row regex alone, separates them. Unmatched lines are ignored.
|
|
236
|
+
function parseListOutput(text) {
|
|
237
|
+
const rows = [];
|
|
238
|
+
const groups = [];
|
|
239
|
+
let group = null;
|
|
240
|
+
let last = null;
|
|
241
|
+
let trailer = null;
|
|
242
|
+
|
|
243
|
+
for (const line of String(text).split(/\r?\n/)) {
|
|
244
|
+
if (line.trim() === '') continue;
|
|
245
|
+
|
|
246
|
+
const summary = line.match(SUMMARY_RE);
|
|
247
|
+
if (summary && last && /^ {6}\S/.test(line)) {
|
|
248
|
+
last.summary = summary[1];
|
|
249
|
+
continue;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
const row = line.match(ROW_RE);
|
|
253
|
+
if (row) {
|
|
254
|
+
last = {
|
|
255
|
+
key: row[1],
|
|
256
|
+
value: row[2],
|
|
257
|
+
layer: row[3],
|
|
258
|
+
summary: '',
|
|
259
|
+
group: group ? group.name : '',
|
|
260
|
+
placeholder: row[1].endsWith('.*'),
|
|
261
|
+
};
|
|
262
|
+
rows.push(last);
|
|
263
|
+
if (group) group.rows.push(last);
|
|
264
|
+
continue;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
if (/^\S/.test(line)) {
|
|
268
|
+
if (TRAILER_RE.test(line)) {
|
|
269
|
+
trailer = line;
|
|
270
|
+
last = null;
|
|
271
|
+
group = null;
|
|
272
|
+
continue;
|
|
273
|
+
}
|
|
274
|
+
const header = line.match(GROUP_HEADER_RE);
|
|
275
|
+
if (header) {
|
|
276
|
+
group = { name: header[1], header: line, rows: [] };
|
|
277
|
+
groups.push(group);
|
|
278
|
+
last = null;
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
return {
|
|
284
|
+
rows,
|
|
285
|
+
groups,
|
|
286
|
+
unrecognized: rows.filter((r) => r.group === 'unrecognized'),
|
|
287
|
+
trailer,
|
|
288
|
+
};
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
// groupRows(rows, schema) -> [{ consumer, title, rows }]
|
|
292
|
+
// Groups parsed rows by their schema entry's `consumer`, in the skill's order:
|
|
293
|
+
// installer, skill, any other consumer (alphabetical), then unrecognized.
|
|
294
|
+
// A row is unrecognized when the engine grouped it that way or the schema has
|
|
295
|
+
// no entry for its key. Each returned row is a copy carrying `entry` (the
|
|
296
|
+
// schema entry or null) and a `summary` filled from the schema when the engine
|
|
297
|
+
// printed none. Empty groups are omitted.
|
|
298
|
+
function groupRows(rows, schema) {
|
|
299
|
+
const buckets = new Map();
|
|
300
|
+
const put = (consumer, row) => {
|
|
301
|
+
if (!buckets.has(consumer)) buckets.set(consumer, []);
|
|
302
|
+
buckets.get(consumer).push(row);
|
|
303
|
+
};
|
|
304
|
+
|
|
305
|
+
for (const row of rows) {
|
|
306
|
+
const entry = resolveEntry(schema, row.key);
|
|
307
|
+
const consumer =
|
|
308
|
+
row.group === 'unrecognized' || !entry
|
|
309
|
+
? 'unrecognized'
|
|
310
|
+
: typeof entry.consumer === 'string'
|
|
311
|
+
? entry.consumer
|
|
312
|
+
: 'installer';
|
|
313
|
+
put(consumer, { ...row, entry, summary: row.summary || (entry && entry.summary) || '' });
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
const others = [...buckets.keys()]
|
|
317
|
+
.filter((c) => c !== 'installer' && c !== 'skill' && c !== 'unrecognized')
|
|
318
|
+
.sort();
|
|
319
|
+
return ['installer', 'skill', ...others, 'unrecognized']
|
|
320
|
+
.filter((c) => buckets.has(c))
|
|
321
|
+
.map((c) => ({ consumer: c, title: CONSUMER_TITLES[c] || c, rows: buckets.get(c) }));
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
// ---------------------------------------------------------------------------
|
|
325
|
+
// State read and grouped summary (Step A, B, B2)
|
|
326
|
+
// ---------------------------------------------------------------------------
|
|
327
|
+
|
|
328
|
+
const CONSUMER_NOTES = {
|
|
329
|
+
installer: 'read by the setup/update scripts',
|
|
330
|
+
skill: 'read by slash commands at run time; changing these changes what a command does',
|
|
331
|
+
unrecognized: 'present in a values file, absent from the schema (kept untouched)',
|
|
332
|
+
};
|
|
333
|
+
|
|
334
|
+
const FAMILY_NOTES = {
|
|
335
|
+
'guides.*':
|
|
336
|
+
'family: one key per optional guide (guides.<name>) holding the remembered install answer for that guide. None stored yet.',
|
|
337
|
+
'gitignore.section.*':
|
|
338
|
+
'family: one key per .gitignore section (gitignore.section.<slug>); each holds only `false`, a permanently declined section. None stored yet.',
|
|
339
|
+
};
|
|
340
|
+
|
|
341
|
+
// resolveProjectDir(cwd, projectFlag) -> absolute project dir | null
|
|
342
|
+
// --project <dir> wins (resolved to an absolute path). Otherwise the nearest
|
|
343
|
+
// ancestor of cwd (cwd included) holding a `.git` (file or directory) or a
|
|
344
|
+
// `.claude/` directory. The home directory is not treated as a project on the
|
|
345
|
+
// strength of its `.claude/` alone: ~/.claude/bootstrap-prefs.json is the global
|
|
346
|
+
// layer, so calling ~ a project would make both layers the same file. No match
|
|
347
|
+
// gives null (global layer only).
|
|
348
|
+
function resolveProjectDir(cwd, projectFlag) {
|
|
349
|
+
if (projectFlag) return path.resolve(projectFlag);
|
|
350
|
+
const home = path.resolve(os.homedir());
|
|
351
|
+
let dir = path.resolve(cwd);
|
|
352
|
+
for (;;) {
|
|
353
|
+
if (fs.existsSync(path.join(dir, '.git'))) return dir;
|
|
354
|
+
if (dir !== home && fs.existsSync(path.join(dir, '.claude'))) return dir;
|
|
355
|
+
const parent = path.dirname(dir);
|
|
356
|
+
if (parent === dir) return null;
|
|
357
|
+
dir = parent;
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
// readState(ctx) -> { ok, status, stderr, rows, groups, unrecognized, trailer }
|
|
362
|
+
// ctx { schemaPath, projectDir, ... }
|
|
363
|
+
// Runs `--list` (always with `--project <dir>` when a project is resolved, since
|
|
364
|
+
// omitting it silently hides project answers) and parses the text. ok is false
|
|
365
|
+
// on a non-zero engine exit; stderr then carries the engine's message verbatim
|
|
366
|
+
// (on success it may still hold engine warnings). Rows are as parseListOutput
|
|
367
|
+
// returns them.
|
|
368
|
+
function readState(ctx) {
|
|
369
|
+
const projectArgs = ctx && ctx.projectDir ? ['--project', ctx.projectDir] : [];
|
|
370
|
+
const result = runEngine(['--list', ...projectArgs], { schema: ctx && ctx.schemaPath });
|
|
371
|
+
const parsed = parseListOutput(result.stdout);
|
|
372
|
+
return { ok: result.status === 0, status: result.status, stderr: result.stderr, ...parsed };
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
// describeRow(row) -> string — the value column, with its meaning spelled out.
|
|
376
|
+
// `false` is a remembered decline only for installer keys (and a declined
|
|
377
|
+
// section for gitignore.section.*); on a skill key it is an ordinary setting.
|
|
378
|
+
function describeRow(row) {
|
|
379
|
+
const entry = row.entry || null;
|
|
380
|
+
const consumer = entry && typeof entry.consumer === 'string' ? entry.consumer : 'installer';
|
|
381
|
+
if (row.layer === 'unset') {
|
|
382
|
+
if (row.placeholder) return 'unset';
|
|
383
|
+
if (consumer === 'skill') {
|
|
384
|
+
const fallback = entry && entry.default !== null && entry.default !== undefined
|
|
385
|
+
? `falls back to ${entry.default}`
|
|
386
|
+
: 'no stored answer';
|
|
387
|
+
return `unset (${fallback})`;
|
|
388
|
+
}
|
|
389
|
+
return 'unset (not answered yet, the installer will ask)';
|
|
390
|
+
}
|
|
391
|
+
if (row.value === 'false') {
|
|
392
|
+
if (row.key.startsWith('gitignore.section.')) return 'false (declined section)';
|
|
393
|
+
if (consumer === 'installer') return 'false (remembered decline)';
|
|
394
|
+
return 'false';
|
|
395
|
+
}
|
|
396
|
+
if (row.value === 'ask') return 'ask (prompts each time)';
|
|
397
|
+
return row.value;
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
// formatRow(row) -> string (no trailing newline)
|
|
401
|
+
// row a groupRows() row: { key, value, layer, summary, placeholder, entry }
|
|
402
|
+
// Line 1: ` <key> = <described value> [<layer>]`; `[default]` is spelled out as
|
|
403
|
+
// "no stored answer". Then the summary (6-space indent) and, for a dynamic
|
|
404
|
+
// family placeholder, a one-line family explanation.
|
|
405
|
+
function formatRow(row) {
|
|
406
|
+
const layerTag = row.layer === 'default' ? '[default: no stored answer]' : `[${row.layer}]`;
|
|
407
|
+
const lines = [` ${row.key} = ${describeRow(row)} ${layerTag}`];
|
|
408
|
+
if (row.summary) lines.push(` ${row.summary}`);
|
|
409
|
+
if (row.placeholder) {
|
|
410
|
+
const family = FAMILY_NOTES[row.key] || (row.entry && row.entry.detail) || '';
|
|
411
|
+
if (family) lines.push(` ${family}`);
|
|
412
|
+
}
|
|
413
|
+
return lines.join('\n');
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
// renderSummary(rows, schema, opts) -> string (ends with a newline)
|
|
417
|
+
// rows parseListOutput().rows
|
|
418
|
+
// opts { trailer } — the engine's closing line, passed through when present
|
|
419
|
+
// (notably "No --project given: ..."). Nothing is printed for a null
|
|
420
|
+
// trailer.
|
|
421
|
+
// Groups by schema consumer in the skill's order (Installer, Skill, other
|
|
422
|
+
// consumers, Unrecognized). Pure: returns the text, writes nothing.
|
|
423
|
+
function renderSummary(rows, schema, opts) {
|
|
424
|
+
const blocks = groupRows(rows, schema).map((g) => {
|
|
425
|
+
const header = `${g.title} — ${CONSUMER_NOTES[g.consumer] || `read by ${g.consumer}`}`;
|
|
426
|
+
return [header, ...g.rows.map(formatRow)].join('\n');
|
|
427
|
+
});
|
|
428
|
+
let text = blocks.length ? `${blocks.join('\n\n')}\n` : 'No preferences to show.\n';
|
|
429
|
+
const trailer = opts && opts.trailer;
|
|
430
|
+
if (trailer) text += `\n${trailer}\n`;
|
|
431
|
+
return text;
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
// printSummary(rows, schema, opts) — writes renderSummary(...) to stdout.
|
|
435
|
+
function printSummary(rows, schema, opts) {
|
|
436
|
+
process.stdout.write(`\n${renderSummary(rows, schema, opts)}\n`);
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
// ---------------------------------------------------------------------------
|
|
440
|
+
// Interactive menu
|
|
441
|
+
// ---------------------------------------------------------------------------
|
|
442
|
+
|
|
443
|
+
// --- Input: a line-queue reader ---------------------------------------------
|
|
444
|
+
//
|
|
445
|
+
// readline on a piped (non-TTY) stdin can emit 'close' before every queued line
|
|
446
|
+
// has been consumed by a question callback, so rl.question() loses scripted
|
|
447
|
+
// answers. Instead every 'line' event is pushed onto a queue (or handed straight
|
|
448
|
+
// to a pending ask) and 'close' only marks EOF; queued lines are always drained
|
|
449
|
+
// first. EOF surfaces as an EofError thrown from ask(), which runMenu catches
|
|
450
|
+
// once at the top — callers never need to test for it.
|
|
451
|
+
|
|
452
|
+
class EofError extends Error {
|
|
453
|
+
constructor() {
|
|
454
|
+
super('end of input');
|
|
455
|
+
this.name = 'EofError';
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
const EOF = Symbol('eof');
|
|
460
|
+
|
|
461
|
+
// createReader(input, output) -> reader { rl, input, output, tty, queue,
|
|
462
|
+
// waiters, closed, close() }. Defaults to process.stdin / process.stdout.
|
|
463
|
+
// Ctrl-C on a TTY closes the reader, which reads as EOF.
|
|
464
|
+
function createReader(input, output) {
|
|
465
|
+
const inp = input || process.stdin;
|
|
466
|
+
const out = output || process.stdout;
|
|
467
|
+
const tty = Boolean(inp.isTTY && out.isTTY);
|
|
468
|
+
const rl = readline.createInterface({ input: inp, output: out, terminal: tty });
|
|
469
|
+
const reader = {
|
|
470
|
+
rl,
|
|
471
|
+
input: inp,
|
|
472
|
+
output: out,
|
|
473
|
+
tty,
|
|
474
|
+
queue: [],
|
|
475
|
+
waiters: [],
|
|
476
|
+
closed: false,
|
|
477
|
+
close() {
|
|
478
|
+
if (!reader.closed) rl.close();
|
|
479
|
+
},
|
|
480
|
+
};
|
|
481
|
+
rl.on('line', (line) => {
|
|
482
|
+
const waiter = reader.waiters.shift();
|
|
483
|
+
if (waiter) waiter(line);
|
|
484
|
+
else reader.queue.push(line);
|
|
485
|
+
});
|
|
486
|
+
rl.on('close', () => {
|
|
487
|
+
reader.closed = true;
|
|
488
|
+
for (const waiter of reader.waiters.splice(0)) waiter(EOF);
|
|
489
|
+
});
|
|
490
|
+
rl.on('SIGINT', () => rl.close());
|
|
491
|
+
return reader;
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
// ask(rl, prompt) -> Promise<string> — the next input line, trimmed.
|
|
495
|
+
// rl a createReader() reader
|
|
496
|
+
// Throws EofError at end of input (after every queued line has been consumed).
|
|
497
|
+
// On a non-TTY input the prompt and the answer are echoed to the output so a
|
|
498
|
+
// piped session leaves a readable transcript.
|
|
499
|
+
async function ask(rl, prompt) {
|
|
500
|
+
const echo = !rl.tty;
|
|
501
|
+
if (rl.queue.length === 0 && rl.closed) throw new EofError();
|
|
502
|
+
if (echo) rl.output.write(prompt);
|
|
503
|
+
|
|
504
|
+
let line;
|
|
505
|
+
if (rl.queue.length > 0) {
|
|
506
|
+
line = rl.queue.shift();
|
|
507
|
+
} else {
|
|
508
|
+
if (!echo) {
|
|
509
|
+
rl.rl.setPrompt(prompt);
|
|
510
|
+
rl.rl.prompt();
|
|
511
|
+
}
|
|
512
|
+
line = await new Promise((resolve) => rl.waiters.push(resolve));
|
|
513
|
+
if (line === EOF) {
|
|
514
|
+
rl.output.write('\n');
|
|
515
|
+
throw new EofError();
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
if (echo) rl.output.write(`${line}\n`);
|
|
519
|
+
return line.trim();
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
function isBackToken(answer) {
|
|
523
|
+
const a = answer.toLowerCase();
|
|
524
|
+
return a === '0' || a === 'b' || a === 'back';
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
// askNonEmpty(rl, prompt) -> Promise<string | null>
|
|
528
|
+
// Re-prompts on empty input. Returns null when the user types b / back / 0.
|
|
529
|
+
async function askNonEmpty(rl, prompt) {
|
|
530
|
+
for (;;) {
|
|
531
|
+
const answer = await ask(rl, prompt);
|
|
532
|
+
if (answer === '') {
|
|
533
|
+
process.stdout.write('Please type a value, or b to go back.\n');
|
|
534
|
+
continue;
|
|
535
|
+
}
|
|
536
|
+
if (isBackToken(answer)) return null;
|
|
537
|
+
return answer;
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
// pickFromList(rl, title, options, opts) -> Promise<option | null>
|
|
542
|
+
// options [{ label, group?, ...anything }] — printed as a numbered list; a
|
|
543
|
+
// changed `group` string prints a heading line above that option
|
|
544
|
+
// opts { backLabel } — text of the Back entry (numbered 0; 'b' / 'back'
|
|
545
|
+
// also accepted). Default 'Back'. Pass backLabel: null for a menu
|
|
546
|
+
// with no Back entry (the main menu, whose Quit is an option).
|
|
547
|
+
// Returns the chosen option object, or null for Back. Re-prompts on empty,
|
|
548
|
+
// non-numeric, or out-of-range input.
|
|
549
|
+
async function pickFromList(rl, title, options, opts) {
|
|
550
|
+
const backLabel = opts && opts.backLabel !== undefined ? opts.backLabel : 'Back';
|
|
551
|
+
if (options.length === 0) return null;
|
|
552
|
+
|
|
553
|
+
const lines = [''];
|
|
554
|
+
if (title) lines.push(title);
|
|
555
|
+
let group;
|
|
556
|
+
options.forEach((option, i) => {
|
|
557
|
+
if (option.group && option.group !== group) {
|
|
558
|
+
group = option.group;
|
|
559
|
+
lines.push(` ${group}`);
|
|
560
|
+
}
|
|
561
|
+
lines.push(` ${String(i + 1).padStart(2)}) ${option.label}`);
|
|
562
|
+
});
|
|
563
|
+
if (backLabel) lines.push(` 0) ${backLabel}`);
|
|
564
|
+
process.stdout.write(`${lines.join('\n')}\n`);
|
|
565
|
+
|
|
566
|
+
const range = `1-${options.length}${backLabel ? ', 0 = back' : ''}`;
|
|
567
|
+
for (;;) {
|
|
568
|
+
const answer = await ask(rl, `Choose [${range}]: `);
|
|
569
|
+
if (backLabel && isBackToken(answer)) return null;
|
|
570
|
+
if (/^\d+$/.test(answer)) {
|
|
571
|
+
const n = Number(answer);
|
|
572
|
+
if (n >= 1 && n <= options.length) return options[n - 1];
|
|
573
|
+
}
|
|
574
|
+
process.stdout.write(
|
|
575
|
+
`Please enter a number from 1 to ${options.length}${backLabel ? ' (or 0 to go back)' : ''}.\n`
|
|
576
|
+
);
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
// --- Key picker (Step D) ----------------------------------------------------
|
|
581
|
+
|
|
582
|
+
const GROUP_HEADINGS = {
|
|
583
|
+
installer: 'Installer preferences (only change what setup/update ask)',
|
|
584
|
+
skill: 'Skill preferences (change what a slash command does)',
|
|
585
|
+
unrecognized: 'Unrecognized keys (not in the schema)',
|
|
586
|
+
};
|
|
587
|
+
|
|
588
|
+
const FAMILY_ACTIONS = {
|
|
589
|
+
'gitignore.section.*': 'derive a slug from a section title, or type one',
|
|
590
|
+
'guides.*': 'enter a guide name',
|
|
591
|
+
};
|
|
592
|
+
|
|
593
|
+
// describeCurrent(row) -> '<value>, <layer>' | 'unset' — the parenthesised part
|
|
594
|
+
// of a picker label.
|
|
595
|
+
function describeCurrent(row) {
|
|
596
|
+
if (row.layer === 'unset') return 'unset';
|
|
597
|
+
return `${row.value}, ${row.layer}`;
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
function familyOption(pattern, entry, summary) {
|
|
601
|
+
const action = FAMILY_ACTIONS[pattern] || 'enter a key under this family';
|
|
602
|
+
const text = summary || (entry && entry.summary) || '';
|
|
603
|
+
return {
|
|
604
|
+
action: 'family',
|
|
605
|
+
key: pattern,
|
|
606
|
+
pattern,
|
|
607
|
+
consumer: entry && typeof entry.consumer === 'string' ? entry.consumer : 'installer',
|
|
608
|
+
label: `${pattern}${text ? ` — ${text}` : ''} (family: ${action})`,
|
|
609
|
+
};
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
// buildKeyOptions(rows, schema, opts) -> { options, hidden }
|
|
613
|
+
// rows parseListOutput().rows
|
|
614
|
+
// opts { mode: 'edit' | 'reset', pin: 'global' | 'project' | null }
|
|
615
|
+
// options picker entries in group order (installer, skill, ...), each
|
|
616
|
+
// { action: 'key' | 'family' | 'free', key, label, group, consumer,
|
|
617
|
+
// row?, pattern? }. `group` is the heading; consumer:skill keys and
|
|
618
|
+
// consumer:installer keys therefore print under separate headings.
|
|
619
|
+
// Labels read `<key> — <summary> (<value>, <layer>)`.
|
|
620
|
+
// hidden reset mode only: [{ key, layer }] for keys left out because nothing
|
|
621
|
+
// is stored for them in a writable layer ([unset] / [default] /
|
|
622
|
+
// [target], or a layer other than the pinned one)
|
|
623
|
+
// Edit mode lists every schema-known key, one option per stored concrete
|
|
624
|
+
// dynamic-family key, and one family action per dynamic pattern (from the
|
|
625
|
+
// engine's placeholder row, or synthesized from the schema when concrete keys
|
|
626
|
+
// hide the placeholder). Reset mode lists only rows whose layer is writable,
|
|
627
|
+
// plus unrecognized keys stored there. A free-text entry closes both lists.
|
|
628
|
+
function buildKeyOptions(rows, schema, opts) {
|
|
629
|
+
const mode = opts && opts.mode === 'reset' ? 'reset' : 'edit';
|
|
630
|
+
const pin = (opts && opts.pin) || null;
|
|
631
|
+
const writable = pin ? [pin] : ['project', 'global'];
|
|
632
|
+
const groups = [];
|
|
633
|
+
const hidden = [];
|
|
634
|
+
|
|
635
|
+
for (const g of groupRows(rows, schema)) {
|
|
636
|
+
if (g.consumer === 'unrecognized' && mode === 'edit') continue;
|
|
637
|
+
const options = [];
|
|
638
|
+
for (const row of g.rows) {
|
|
639
|
+
if (row.placeholder) {
|
|
640
|
+
if (mode === 'edit') options.push(familyOption(row.key, row.entry, row.summary));
|
|
641
|
+
continue;
|
|
642
|
+
}
|
|
643
|
+
if (mode === 'reset' && !writable.includes(row.layer)) {
|
|
644
|
+
hidden.push({ key: row.key, layer: row.layer });
|
|
645
|
+
continue;
|
|
646
|
+
}
|
|
647
|
+
options.push({
|
|
648
|
+
action: 'key',
|
|
649
|
+
key: row.key,
|
|
650
|
+
consumer: g.consumer,
|
|
651
|
+
row,
|
|
652
|
+
label: `${row.key}${row.summary ? ` — ${row.summary}` : ''} (${describeCurrent(row)})`,
|
|
653
|
+
});
|
|
654
|
+
}
|
|
655
|
+
groups.push({ consumer: g.consumer, options });
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
if (mode === 'edit') {
|
|
659
|
+
for (const [pattern, entry] of Object.entries(schema)) {
|
|
660
|
+
if (!isPlainObject(entry) || entry.dynamic !== true || !pattern.endsWith('.*')) continue;
|
|
661
|
+
if (groups.some((g) => g.options.some((o) => o.pattern === pattern))) continue;
|
|
662
|
+
const consumer = typeof entry.consumer === 'string' ? entry.consumer : 'installer';
|
|
663
|
+
let group = groups.find((g) => g.consumer === consumer);
|
|
664
|
+
if (!group) {
|
|
665
|
+
group = { consumer, options: [] };
|
|
666
|
+
groups.push(group);
|
|
667
|
+
}
|
|
668
|
+
group.options.push(familyOption(pattern, entry, entry.summary));
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
|
|
672
|
+
const options = [];
|
|
673
|
+
for (const g of groups) {
|
|
674
|
+
const heading = GROUP_HEADINGS[g.consumer] || `${g.consumer} preferences`;
|
|
675
|
+
for (const o of g.options) options.push({ ...o, group: heading });
|
|
676
|
+
}
|
|
677
|
+
options.push({ action: 'free', key: null, label: 'Enter a key by name', group: 'Other' });
|
|
678
|
+
return { options, hidden };
|
|
679
|
+
}
|
|
680
|
+
|
|
681
|
+
// skillCommand(entry) -> '/command' | null — the slash command a skill
|
|
682
|
+
// preference affects, read from the entry's summary (`How /git-commit ...`).
|
|
683
|
+
function skillCommand(entry) {
|
|
684
|
+
const text = `${entry.summary || ''}`;
|
|
685
|
+
const m = text.match(/(?:^|[\s(])(\/[a-z][a-z0-9-]*)/);
|
|
686
|
+
return m ? m[1] : null;
|
|
687
|
+
}
|
|
688
|
+
|
|
689
|
+
// renderKeyDetail(schema, key) -> string (ends with a newline)
|
|
690
|
+
// The schema `detail` in full, then the framing for the key's consumer: the
|
|
691
|
+
// heavier "skill preference" banner (naming the affected command) or the lighter
|
|
692
|
+
// installer framing.
|
|
693
|
+
function renderKeyDetail(schema, key) {
|
|
694
|
+
const entry = resolveEntry(schema, key);
|
|
695
|
+
const lines = ['', key];
|
|
696
|
+
if (!entry) {
|
|
697
|
+
lines.push(
|
|
698
|
+
' This key is not in the preference schema, so there is no description or value list for it.'
|
|
699
|
+
);
|
|
700
|
+
return `${lines.join('\n')}\n`;
|
|
701
|
+
}
|
|
702
|
+
if (entry.summary) lines.push(` ${entry.summary}`);
|
|
703
|
+
if (entry.detail) lines.push('', entry.detail);
|
|
704
|
+
lines.push('');
|
|
705
|
+
if (entry.consumer === 'skill') {
|
|
706
|
+
const command = skillCommand(entry);
|
|
707
|
+
lines.push(
|
|
708
|
+
'This is a SKILL PREFERENCE. Changing it changes what a slash command actually does the next time it runs — not merely whether the installer asks you a question.'
|
|
709
|
+
);
|
|
710
|
+
lines.push(
|
|
711
|
+
command
|
|
712
|
+
? `Affected command: ${command}`
|
|
713
|
+
: `Affects: ${entry.summary || 'a slash command or hook'}`
|
|
714
|
+
);
|
|
715
|
+
} else {
|
|
716
|
+
lines.push(
|
|
717
|
+
`This is an installer preference. It only affects whether ${
|
|
718
|
+
entry.askedBy ? `${entry.askedBy} (a setup/update script)` : 'a setup/update script'
|
|
719
|
+
} prompts you; it does not change what any slash command does.`
|
|
720
|
+
);
|
|
721
|
+
}
|
|
722
|
+
return `${lines.join('\n')}\n`;
|
|
723
|
+
}
|
|
724
|
+
|
|
725
|
+
// familyExamples(entry, prefix) -> string[] — example key suffixes mentioned in
|
|
726
|
+
// the family entry's `detail` (e.g. 'evals-framework.md' for `guides.`).
|
|
727
|
+
function familyExamples(entry, prefix) {
|
|
728
|
+
if (!entry || typeof entry.detail !== 'string') return [];
|
|
729
|
+
const escaped = prefix.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
730
|
+
const found = entry.detail.match(new RegExp(`${escaped}[A-Za-z0-9][\\w.-]*`, 'g')) || [];
|
|
731
|
+
const suffixes = found.map((k) => k.slice(prefix.length).replace(/[.-]+$/, ''));
|
|
732
|
+
return [...new Set(suffixes.filter((s) => s.length > 0))];
|
|
733
|
+
}
|
|
734
|
+
|
|
735
|
+
// resolveFamilyKey(ctx, rl, pattern) -> Promise<string | null>
|
|
736
|
+
// Turns a dynamic-family pattern (`guides.*`, `gitignore.section.*`) into one
|
|
737
|
+
// concrete key. gitignore.section.* offers "derive a slug from a section title"
|
|
738
|
+
// (engine --section-key, never a hand-rolled slugify) and states the false-only
|
|
739
|
+
// grammar; guides.* asks for a free-text guide name with examples from the
|
|
740
|
+
// schema detail. Returns null on Back or on an engine failure (its stderr is
|
|
741
|
+
// printed verbatim).
|
|
742
|
+
async function resolveFamilyKey(ctx, rl, pattern) {
|
|
743
|
+
const entry = resolveEntry(ctx.schema, pattern);
|
|
744
|
+
const prefix = pattern.slice(0, -1);
|
|
745
|
+
|
|
746
|
+
if (pattern === 'gitignore.section.*') {
|
|
747
|
+
process.stdout.write(
|
|
748
|
+
'\ngitignore.section.<slug> — one key per titled .gitignore template section. The only storable value is `false` (a permanently declined section); there is no `true`, because an accepted section is deliberately never recorded.\n'
|
|
749
|
+
);
|
|
750
|
+
const how = await pickFromList(rl, 'How do you want to name the section?', [
|
|
751
|
+
{ label: 'Derive a slug from a section title', value: 'derive' },
|
|
752
|
+
{ label: 'Type a slug directly', value: 'type' },
|
|
753
|
+
]);
|
|
754
|
+
if (!how) return null;
|
|
755
|
+
if (how.value === 'derive') {
|
|
756
|
+
const title = await askNonEmpty(rl, 'Section banner title (b to go back): ');
|
|
757
|
+
if (title === null) return null;
|
|
758
|
+
const result = runEngine(['--section-key', title], { schema: ctx.schemaPath });
|
|
759
|
+
if (result.status !== 0) {
|
|
760
|
+
process.stdout.write(`${result.stderr || 'The engine could not derive a section key.\n'}`);
|
|
761
|
+
return null;
|
|
762
|
+
}
|
|
763
|
+
const derived = result.stdout.trim();
|
|
764
|
+
if (!derived.startsWith(prefix)) {
|
|
765
|
+
process.stdout.write(`Unexpected --section-key output: ${derived}\n`);
|
|
766
|
+
return null;
|
|
767
|
+
}
|
|
768
|
+
process.stdout.write(`Derived key: ${derived}\n`);
|
|
769
|
+
return derived;
|
|
770
|
+
}
|
|
771
|
+
}
|
|
772
|
+
|
|
773
|
+
const examples = familyExamples(entry, prefix);
|
|
774
|
+
const noun = pattern === 'guides.*' ? 'Guide name' : `Name after "${prefix}"`;
|
|
775
|
+
const hint = examples.length ? ` (e.g. ${examples.join(' or ')})` : '';
|
|
776
|
+
for (;;) {
|
|
777
|
+
const name = await askNonEmpty(rl, `${noun}${hint}, b to go back: `);
|
|
778
|
+
if (name === null) return null;
|
|
779
|
+
if (/\s/.test(name)) {
|
|
780
|
+
process.stdout.write('A key cannot contain whitespace.\n');
|
|
781
|
+
continue;
|
|
782
|
+
}
|
|
783
|
+
return name.startsWith(prefix) ? name : `${prefix}${name}`;
|
|
784
|
+
}
|
|
785
|
+
}
|
|
786
|
+
|
|
787
|
+
// chooseKey(ctx, rl, state, mode) -> Promise<string | null>
|
|
788
|
+
// Step D: numbered key picker (skill and installer keys under separate
|
|
789
|
+
// headings), free-text key entry, family actions, and Back (null). Reset mode
|
|
790
|
+
// lists only keys stored in a writable layer and says which keys it left out.
|
|
791
|
+
// Prints the schema detail and the consumer banner before returning the key.
|
|
792
|
+
async function chooseKey(ctx, rl, state, mode) {
|
|
793
|
+
const writable = ctx.pin ? [ctx.pin] : ['project', 'global'];
|
|
794
|
+
for (;;) {
|
|
795
|
+
const { options, hidden } = buildKeyOptions(state.rows, ctx.schema, { mode, pin: ctx.pin });
|
|
796
|
+
|
|
797
|
+
if (mode === 'reset') {
|
|
798
|
+
if (hidden.length) {
|
|
799
|
+
process.stdout.write(
|
|
800
|
+
`\nNot listed — nothing stored in a writable layer (${writable.join(' or ')}), so there is nothing to reset: ${hidden
|
|
801
|
+
.map((h) => `${h.key} [${h.layer}]`)
|
|
802
|
+
.join(', ')}\n`
|
|
803
|
+
);
|
|
804
|
+
}
|
|
805
|
+
if (!options.some((o) => o.action === 'key')) {
|
|
806
|
+
process.stdout.write('\nNothing is stored in a writable layer, so there is nothing to reset.\n');
|
|
807
|
+
return null;
|
|
808
|
+
}
|
|
809
|
+
}
|
|
810
|
+
|
|
811
|
+
const title =
|
|
812
|
+
mode === 'edit'
|
|
813
|
+
? 'Which preference do you want to edit?'
|
|
814
|
+
: 'Which preference do you want to reset? (keys stored in a writable layer)';
|
|
815
|
+
const picked = await pickFromList(rl, title, options, { backLabel: 'Back / No changes' });
|
|
816
|
+
if (!picked) return null;
|
|
817
|
+
|
|
818
|
+
let key;
|
|
819
|
+
if (picked.action === 'family') {
|
|
820
|
+
key = await resolveFamilyKey(ctx, rl, picked.pattern);
|
|
821
|
+
} else if (picked.action === 'free') {
|
|
822
|
+
key = await askNonEmpty(rl, 'Key name (b to go back): ');
|
|
823
|
+
if (key !== null && /\s/.test(key)) {
|
|
824
|
+
process.stdout.write('A key cannot contain whitespace.\n');
|
|
825
|
+
continue;
|
|
826
|
+
}
|
|
827
|
+
const pattern = key !== null && isPlainObject(ctx.schema[key]) && ctx.schema[key].dynamic === true;
|
|
828
|
+
if (pattern) key = await resolveFamilyKey(ctx, rl, key);
|
|
829
|
+
} else {
|
|
830
|
+
key = picked.key;
|
|
831
|
+
}
|
|
832
|
+
if (key === null) continue;
|
|
833
|
+
|
|
834
|
+
if (mode === 'reset') {
|
|
835
|
+
const row = state.rows.find((r) => r.key === key);
|
|
836
|
+
if (!row || !writable.includes(row.layer)) {
|
|
837
|
+
process.stdout.write(
|
|
838
|
+
`\n${key} is ${row ? `[${row.layer}]` : 'not stored anywhere'} — nothing is stored in a writable layer, so there is nothing to reset.\n`
|
|
839
|
+
);
|
|
840
|
+
continue;
|
|
841
|
+
}
|
|
842
|
+
}
|
|
843
|
+
|
|
844
|
+
process.stdout.write(renderKeyDetail(ctx.schema, key));
|
|
845
|
+
return key;
|
|
846
|
+
}
|
|
847
|
+
}
|
|
848
|
+
|
|
849
|
+
// --- Actions: edit, reset, re-verify (Steps E, F, G) ------------------------
|
|
850
|
+
//
|
|
851
|
+
// Every write below is a single runEngine() call; nothing here touches
|
|
852
|
+
// bootstrap-prefs.json or its README. All engine output (success line, warnings,
|
|
853
|
+
// refusals) is printed verbatim to stdout so a piped session leaves one
|
|
854
|
+
// readable, correctly ordered transcript.
|
|
855
|
+
|
|
856
|
+
const SECRET_WARNING =
|
|
857
|
+
'Reminder: a preference file never holds a secret. No API key, token, or password belongs in it — not in the values file, not in the generated README, not in a new schema key.';
|
|
858
|
+
|
|
859
|
+
// shellQuote(text) -> string — quoted only when the text has shell-special
|
|
860
|
+
// characters, so the printed command can be copy-pasted as shown.
|
|
861
|
+
function shellQuote(text) {
|
|
862
|
+
return /^[A-Za-z0-9_@%+=:,./~-]+$/.test(text) ? text : `'${text.replace(/'/g, `'\\''`)}'`;
|
|
863
|
+
}
|
|
864
|
+
|
|
865
|
+
// displayCommand(ctx, engineArgs) -> string — the exact command runEngine() will
|
|
866
|
+
// run, including --schema when the schema is not the default.
|
|
867
|
+
function displayCommand(ctx, engineArgs) {
|
|
868
|
+
const schema = ctx.schemaPath;
|
|
869
|
+
const schemaArgs =
|
|
870
|
+
schema && path.resolve(schema) !== path.resolve(DEFAULT_SCHEMA) ? ['--schema', schema] : [];
|
|
871
|
+
return ['node', ENGINE, ...engineArgs, ...schemaArgs].map(shellQuote).join(' ');
|
|
872
|
+
}
|
|
873
|
+
|
|
874
|
+
// confirmCommand(ctx, rl, engineArgs) -> Promise<boolean>
|
|
875
|
+
// Prints the exact command and asks y/N. Default is No; only y / yes proceeds.
|
|
876
|
+
async function confirmCommand(ctx, rl, engineArgs) {
|
|
877
|
+
process.stdout.write(`\nAbout to run:\n ${displayCommand(ctx, engineArgs)}\n`);
|
|
878
|
+
const answer = (await ask(rl, 'Apply this change? [y/N]: ')).toLowerCase();
|
|
879
|
+
if (answer === 'y' || answer === 'yes') return true;
|
|
880
|
+
process.stdout.write('Cancelled. Nothing was written.\n');
|
|
881
|
+
return false;
|
|
882
|
+
}
|
|
883
|
+
|
|
884
|
+
function layerLabel(layer, ctx) {
|
|
885
|
+
return layer.layer === 'project'
|
|
886
|
+
? `Project layer (${ctx.projectDir}/.claude/bootstrap-prefs.json) — applies to this project only and wins over global, per key`
|
|
887
|
+
: 'Global layer (~/.claude/bootstrap-prefs.json) — applies to every project that has no project answer for the key';
|
|
888
|
+
}
|
|
889
|
+
|
|
890
|
+
// pickLayer(ctx, rl, layers, explain) -> Promise<{ layer, args } | null>
|
|
891
|
+
// One permitted layer is stated, not asked. Two are asked, with the
|
|
892
|
+
// project-wins-over-global explanation. null means Back.
|
|
893
|
+
async function pickLayer(ctx, rl, layers, explain) {
|
|
894
|
+
if (layers.length === 1) {
|
|
895
|
+
process.stdout.write(`\nLayer: ${layerLabel(layers[0], ctx)}\n`);
|
|
896
|
+
return layers[0];
|
|
897
|
+
}
|
|
898
|
+
return pickFromList(
|
|
899
|
+
rl,
|
|
900
|
+
`${explain}\nWhich layer?`,
|
|
901
|
+
layers.map((l) => ({ ...l, label: layerLabel(l, ctx) })),
|
|
902
|
+
{ backLabel: 'Back / No changes' }
|
|
903
|
+
);
|
|
904
|
+
}
|
|
905
|
+
|
|
906
|
+
// changedRows(before, after, schema, key) -> groupRows()-shaped rows
|
|
907
|
+
// Rows whose value or layer differs from `before` (or that are new), plus the
|
|
908
|
+
// written key itself — synthesized as unset when a reset removed its row.
|
|
909
|
+
function changedRows(before, after, schema, key) {
|
|
910
|
+
const signature = (r) => `${r.value}\u0000${r.layer}`;
|
|
911
|
+
const prior = new Map(before.map((r) => [r.key, signature(r)]));
|
|
912
|
+
const rows = groupRows(after, schema).flatMap((g) => g.rows);
|
|
913
|
+
const out = rows.filter(
|
|
914
|
+
(r) => r.key === key || (!r.placeholder && prior.get(r.key) !== signature(r))
|
|
915
|
+
);
|
|
916
|
+
if (!out.some((r) => r.key === key)) {
|
|
917
|
+
const entry = resolveEntry(schema, key);
|
|
918
|
+
out.push({
|
|
919
|
+
key,
|
|
920
|
+
value: 'unset',
|
|
921
|
+
layer: 'unset',
|
|
922
|
+
summary: (entry && entry.summary) || '',
|
|
923
|
+
placeholder: false,
|
|
924
|
+
entry,
|
|
925
|
+
});
|
|
926
|
+
}
|
|
927
|
+
return out;
|
|
928
|
+
}
|
|
929
|
+
|
|
930
|
+
// commitWrite(ctx, state, key, engineArgs, layer) -> boolean — true on success.
|
|
931
|
+
// Runs the engine once. A non-zero exit prints stderr verbatim and returns false
|
|
932
|
+
// with no retry. On success: the engine's stdout line and any warnings, then
|
|
933
|
+
// Step G — re-read, print the changed rows with their layers, the README
|
|
934
|
+
// regeneration note, and the secret warning.
|
|
935
|
+
function commitWrite(ctx, state, key, engineArgs, layer) {
|
|
936
|
+
const result = runEngine(engineArgs, { schema: ctx.schemaPath });
|
|
937
|
+
if (result.status !== 0) {
|
|
938
|
+
const message =
|
|
939
|
+
result.stderr || result.stdout || `The engine exited ${result.status} with no message.`;
|
|
940
|
+
process.stdout.write(`\n${message}${message.endsWith('\n') ? '' : '\n'}Nothing was written.\n`);
|
|
941
|
+
return false;
|
|
942
|
+
}
|
|
943
|
+
|
|
944
|
+
process.stdout.write(`\n${result.stdout}`);
|
|
945
|
+
if (result.stdout && !result.stdout.endsWith('\n')) process.stdout.write('\n');
|
|
946
|
+
if (result.stderr) {
|
|
947
|
+
process.stdout.write(result.stderr);
|
|
948
|
+
if (!result.stderr.endsWith('\n')) process.stdout.write('\n');
|
|
949
|
+
}
|
|
950
|
+
|
|
951
|
+
const after = readState(ctx);
|
|
952
|
+
if (after.ok) {
|
|
953
|
+
process.stdout.write('\nChanged rows (as the engine now reports them):\n');
|
|
954
|
+
const rows = changedRows(state.rows, after.rows, ctx.schema, key);
|
|
955
|
+
process.stdout.write(`${rows.map(formatRow).join('\n')}\n`);
|
|
956
|
+
if (after.trailer && /^No --project given/.test(after.trailer)) {
|
|
957
|
+
process.stdout.write(`\n${after.trailer}\n`);
|
|
958
|
+
}
|
|
959
|
+
} else {
|
|
960
|
+
process.stdout.write(
|
|
961
|
+
`\nThe write succeeded, but re-reading the preferences failed:\n${after.stderr || `engine --list exited ${after.status}\n`}`
|
|
962
|
+
);
|
|
963
|
+
}
|
|
964
|
+
process.stdout.write(
|
|
965
|
+
`\nbootstrap-prefs.README.md next to the ${layer} values file was regenerated. It is generated output — read it, never hand-edit it.\n${SECRET_WARNING}\n`
|
|
966
|
+
);
|
|
967
|
+
return true;
|
|
968
|
+
}
|
|
969
|
+
|
|
970
|
+
// editKey(ctx, rl, state, key) -> Promise<boolean> — true when a write happened.
|
|
971
|
+
// Step E: value picker (from the schema `values` string), layer picker (per the
|
|
972
|
+
// entry's scope and any pin), exact-command confirmation, engine --set. A key
|
|
973
|
+
// with no value list (unrecognized) takes a free-text value; the engine remains
|
|
974
|
+
// the judge of legality and of `unset` / `null`, and its refusal is shown as-is.
|
|
975
|
+
async function editKey(ctx, rl, state, key) {
|
|
976
|
+
const entry = resolveEntry(ctx.schema, key);
|
|
977
|
+
const { layers, error } = permittedLayers(entry, ctx.projectDir, ctx.pin);
|
|
978
|
+
if (error) {
|
|
979
|
+
process.stdout.write(`\nCannot edit ${key}: ${error}\nNothing was written.\n`);
|
|
980
|
+
return false;
|
|
981
|
+
}
|
|
982
|
+
|
|
983
|
+
const values = parseValues(entry);
|
|
984
|
+
const current = state.rows.find((r) => r.key === key);
|
|
985
|
+
let value;
|
|
986
|
+
if (values.length > 0) {
|
|
987
|
+
if (values.length === 1) {
|
|
988
|
+
process.stdout.write(`\n${key} has exactly one storable value: ${values[0]}.\n`);
|
|
989
|
+
}
|
|
990
|
+
const picked = await pickFromList(
|
|
991
|
+
rl,
|
|
992
|
+
`New value for ${key}?`,
|
|
993
|
+
values.map((v) => ({
|
|
994
|
+
value: v,
|
|
995
|
+
label: current && current.layer !== 'unset' && current.value === v ? `${v} (current)` : v,
|
|
996
|
+
})),
|
|
997
|
+
{ backLabel: 'Back / No changes' }
|
|
998
|
+
);
|
|
999
|
+
if (!picked) return false;
|
|
1000
|
+
value = picked.value;
|
|
1001
|
+
} else {
|
|
1002
|
+
process.stdout.write(
|
|
1003
|
+
`\nThe schema lists no value grammar for ${key}, so type the value yourself. The engine will refuse anything it does not accept.\n`
|
|
1004
|
+
);
|
|
1005
|
+
value = await askNonEmpty(rl, `New value for ${key} (b to go back): `);
|
|
1006
|
+
if (value === null) return false;
|
|
1007
|
+
}
|
|
1008
|
+
|
|
1009
|
+
const layer = await pickLayer(
|
|
1010
|
+
ctx,
|
|
1011
|
+
rl,
|
|
1012
|
+
layers,
|
|
1013
|
+
'This key can live in either layer. Project wins over global, per key.'
|
|
1014
|
+
);
|
|
1015
|
+
if (!layer) return false;
|
|
1016
|
+
|
|
1017
|
+
const engineArgs = ['--set', key, '--value', value, ...layer.args];
|
|
1018
|
+
if (!(await confirmCommand(ctx, rl, engineArgs))) return false;
|
|
1019
|
+
return commitWrite(ctx, state, key, engineArgs, layer.layer);
|
|
1020
|
+
}
|
|
1021
|
+
|
|
1022
|
+
// storedLayers(ctx, state, key) -> [{ layer, args }] — the writable layers that
|
|
1023
|
+
// hold the key. --list shows only the winning layer, so:
|
|
1024
|
+
// * winner is [global] -> project holds nothing (project would have won) ->
|
|
1025
|
+
// global only.
|
|
1026
|
+
// * winner is [project] -> the global layer may also hold it; ask the engine
|
|
1027
|
+
// (`--get <key> --global` reads the global file alone). Both is reported so
|
|
1028
|
+
// the user can choose; a pin restricts to the pinned layer (no probe).
|
|
1029
|
+
// The probe inherits the engine's scope filtering: a global value the key's scope
|
|
1030
|
+
// never consults reads as `unset` there, so it is not offered — the same "inert
|
|
1031
|
+
// layer" rule the edit path follows.
|
|
1032
|
+
function storedLayers(ctx, state, key) {
|
|
1033
|
+
const projectLayer = { layer: 'project', args: ['--project', ctx.projectDir] };
|
|
1034
|
+
const globalLayer = { layer: 'global', args: ['--global'] };
|
|
1035
|
+
const row = state.rows.find((r) => r.key === key);
|
|
1036
|
+
if (!row) return [];
|
|
1037
|
+
if (ctx.pin) return row.layer === ctx.pin ? [ctx.pin === 'project' ? projectLayer : globalLayer] : [];
|
|
1038
|
+
if (row.layer === 'global') return [globalLayer];
|
|
1039
|
+
if (row.layer !== 'project' || !ctx.projectDir) return [];
|
|
1040
|
+
const probe = runEngine(['--get', key, '--global'], { schema: ctx.schemaPath });
|
|
1041
|
+
const alsoGlobal = probe.status === 0 && probe.stdout.trim() !== '' && probe.stdout.trim() !== 'unset';
|
|
1042
|
+
return alsoGlobal ? [projectLayer, globalLayer] : [projectLayer];
|
|
1043
|
+
}
|
|
1044
|
+
|
|
1045
|
+
// resetKey(ctx, rl, state, key) -> Promise<boolean> — true when a write happened.
|
|
1046
|
+
// Step F: layer from the [layer] annotation (asked when the key is stored in
|
|
1047
|
+
// both), exact-command confirmation, engine --unset. Reset deletes the key: the
|
|
1048
|
+
// next installer run or skill invocation asks the question again.
|
|
1049
|
+
async function resetKey(ctx, rl, state, key) {
|
|
1050
|
+
const layers = storedLayers(ctx, state, key);
|
|
1051
|
+
if (layers.length === 0) {
|
|
1052
|
+
process.stdout.write(
|
|
1053
|
+
`\n${key} is not stored in a writable layer, so there is nothing to reset. Nothing was written.\n`
|
|
1054
|
+
);
|
|
1055
|
+
return false;
|
|
1056
|
+
}
|
|
1057
|
+
|
|
1058
|
+
process.stdout.write(
|
|
1059
|
+
'\nReset deletes the key from the values file. The next installer run or skill invocation will ask that question again.\n'
|
|
1060
|
+
);
|
|
1061
|
+
const layer = await pickLayer(
|
|
1062
|
+
ctx,
|
|
1063
|
+
rl,
|
|
1064
|
+
layers,
|
|
1065
|
+
`${key} is stored in BOTH layers. Clearing the project layer alone re-exposes the global answer instead of re-opening the question; clear both (one reset per layer) to re-open it.`
|
|
1066
|
+
);
|
|
1067
|
+
if (!layer) return false;
|
|
1068
|
+
|
|
1069
|
+
const engineArgs = ['--unset', key, ...layer.args];
|
|
1070
|
+
if (!(await confirmCommand(ctx, rl, engineArgs))) return false;
|
|
1071
|
+
return commitWrite(ctx, state, key, engineArgs, layer.layer);
|
|
1072
|
+
}
|
|
1073
|
+
|
|
1074
|
+
// --- Main menu (Step C) -----------------------------------------------------
|
|
1075
|
+
|
|
1076
|
+
// runMenu(ctx, io) -> Promise<number> (process exit code)
|
|
1077
|
+
// ctx = { schema, schemaPath, projectDir, pin }
|
|
1078
|
+
// io optional { reader } to inject a createReader() reader (tests)
|
|
1079
|
+
// Steps A/B/B2 first: read the state and print the grouped summary before any
|
|
1080
|
+
// prompt. Then the Step C loop: Edit / Reset / View again / Quit. Quit, EOF and
|
|
1081
|
+
// Ctrl-C all exit 0 and write nothing themselves; the closing line is "No
|
|
1082
|
+
// changes." unless a write happened this session, then "Done." After every
|
|
1083
|
+
// successful editKey/resetKey the state is re-read.
|
|
1084
|
+
async function runMenu(ctx, io) {
|
|
1085
|
+
let state = readState(ctx);
|
|
1086
|
+
if (!state.ok) {
|
|
1087
|
+
process.stderr.write(state.stderr || `Error: engine --list exited ${state.status}\n`);
|
|
1088
|
+
return 1;
|
|
1089
|
+
}
|
|
1090
|
+
if (state.stderr) process.stderr.write(state.stderr);
|
|
1091
|
+
printSummary(state.rows, ctx.schema, { trailer: state.trailer });
|
|
1092
|
+
if (ctx.pin) {
|
|
1093
|
+
process.stdout.write(`Writes are pinned to the ${ctx.pin} layer.\n`);
|
|
1094
|
+
}
|
|
1095
|
+
|
|
1096
|
+
const rl = (io && io.reader) || createReader();
|
|
1097
|
+
const session = { wrote: false };
|
|
1098
|
+
const closing = () => (session.wrote ? 'Done.' : 'No changes.');
|
|
1099
|
+
const onSigint = () => rl.close();
|
|
1100
|
+
process.on('SIGINT', onSigint);
|
|
1101
|
+
|
|
1102
|
+
const refresh = () => {
|
|
1103
|
+
const next = readState(ctx);
|
|
1104
|
+
if (!next.ok) {
|
|
1105
|
+
process.stderr.write(next.stderr || `Error: engine --list exited ${next.status}\n`);
|
|
1106
|
+
return;
|
|
1107
|
+
}
|
|
1108
|
+
if (next.stderr) process.stderr.write(next.stderr);
|
|
1109
|
+
state = next;
|
|
1110
|
+
};
|
|
1111
|
+
|
|
1112
|
+
try {
|
|
1113
|
+
for (;;) {
|
|
1114
|
+
const choice = await pickFromList(
|
|
1115
|
+
rl,
|
|
1116
|
+
'What would you like to do?',
|
|
1117
|
+
[
|
|
1118
|
+
{ label: 'Edit a preference', value: 'edit' },
|
|
1119
|
+
{ label: 'Reset a preference', value: 'reset' },
|
|
1120
|
+
{ label: 'View again', value: 'view' },
|
|
1121
|
+
{ label: session.wrote ? 'Quit' : 'Quit (No changes)', value: 'quit' },
|
|
1122
|
+
],
|
|
1123
|
+
{ backLabel: null }
|
|
1124
|
+
);
|
|
1125
|
+
|
|
1126
|
+
if (choice.value === 'quit') {
|
|
1127
|
+
process.stdout.write(`${closing()}\n`);
|
|
1128
|
+
return 0;
|
|
1129
|
+
}
|
|
1130
|
+
if (choice.value === 'view') {
|
|
1131
|
+
refresh();
|
|
1132
|
+
printSummary(state.rows, ctx.schema, { trailer: state.trailer });
|
|
1133
|
+
continue;
|
|
1134
|
+
}
|
|
1135
|
+
|
|
1136
|
+
const key = await chooseKey(ctx, rl, state, choice.value);
|
|
1137
|
+
if (key === null) continue;
|
|
1138
|
+
const wrote =
|
|
1139
|
+
choice.value === 'edit'
|
|
1140
|
+
? await editKey(ctx, rl, state, key)
|
|
1141
|
+
: await resetKey(ctx, rl, state, key);
|
|
1142
|
+
if (wrote) {
|
|
1143
|
+
session.wrote = true;
|
|
1144
|
+
refresh();
|
|
1145
|
+
}
|
|
1146
|
+
}
|
|
1147
|
+
} catch (err) {
|
|
1148
|
+
if (err instanceof EofError) {
|
|
1149
|
+
process.stdout.write(`${closing()}\n`);
|
|
1150
|
+
return 0;
|
|
1151
|
+
}
|
|
1152
|
+
throw err;
|
|
1153
|
+
} finally {
|
|
1154
|
+
process.removeListener('SIGINT', onSigint);
|
|
1155
|
+
rl.close();
|
|
1156
|
+
}
|
|
1157
|
+
}
|
|
1158
|
+
|
|
1159
|
+
async function main(argv) {
|
|
1160
|
+
const args = parseArgs(argv);
|
|
1161
|
+
if (args.error) {
|
|
1162
|
+
process.stderr.write(`Error: ${args.error}\n${USAGE}\n`);
|
|
1163
|
+
return 2;
|
|
1164
|
+
}
|
|
1165
|
+
if (args.help) {
|
|
1166
|
+
process.stdout.write(`${USAGE}\n`);
|
|
1167
|
+
return 0;
|
|
1168
|
+
}
|
|
1169
|
+
const schemaPath = args.schema || DEFAULT_SCHEMA;
|
|
1170
|
+
const schema = loadSchema(schemaPath);
|
|
1171
|
+
return runMenu({
|
|
1172
|
+
schema,
|
|
1173
|
+
schemaPath,
|
|
1174
|
+
projectDir: resolveProjectDir(process.cwd(), args.project),
|
|
1175
|
+
pin: args.pin,
|
|
1176
|
+
});
|
|
1177
|
+
}
|
|
1178
|
+
|
|
1179
|
+
module.exports = {
|
|
1180
|
+
ENGINE,
|
|
1181
|
+
DEFAULT_SCHEMA,
|
|
1182
|
+
parseArgs,
|
|
1183
|
+
runEngine,
|
|
1184
|
+
loadSchema,
|
|
1185
|
+
resolveEntry,
|
|
1186
|
+
parseValues,
|
|
1187
|
+
permittedLayers,
|
|
1188
|
+
parseListOutput,
|
|
1189
|
+
groupRows,
|
|
1190
|
+
resolveProjectDir,
|
|
1191
|
+
readState,
|
|
1192
|
+
formatRow,
|
|
1193
|
+
renderSummary,
|
|
1194
|
+
printSummary,
|
|
1195
|
+
EofError,
|
|
1196
|
+
createReader,
|
|
1197
|
+
ask,
|
|
1198
|
+
askNonEmpty,
|
|
1199
|
+
pickFromList,
|
|
1200
|
+
buildKeyOptions,
|
|
1201
|
+
renderKeyDetail,
|
|
1202
|
+
familyExamples,
|
|
1203
|
+
resolveFamilyKey,
|
|
1204
|
+
chooseKey,
|
|
1205
|
+
displayCommand,
|
|
1206
|
+
changedRows,
|
|
1207
|
+
storedLayers,
|
|
1208
|
+
editKey,
|
|
1209
|
+
resetKey,
|
|
1210
|
+
runMenu,
|
|
1211
|
+
main,
|
|
1212
|
+
};
|
|
1213
|
+
|
|
1214
|
+
if (require.main === module) {
|
|
1215
|
+
main(process.argv.slice(2)).then(
|
|
1216
|
+
(code) => process.exit(code),
|
|
1217
|
+
(err) => {
|
|
1218
|
+
process.stderr.write(`Error: ${err && err.message ? err.message : err}\n`);
|
|
1219
|
+
process.exit(1);
|
|
1220
|
+
}
|
|
1221
|
+
);
|
|
1222
|
+
}
|