@bbcsoftwarefactory/cli 0.1.1 → 1.1.2
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/dist/audit-ledger.d.ts +2 -0
- package/dist/audit-ledger.js +71 -0
- package/dist/bbc-install.d.ts +2 -0
- package/dist/bbc-install.js +1250 -0
- package/dist/bbc.js +87 -3
- package/dist/change-lane/classification.js +3 -1
- package/dist/check-issue-claim.d.ts +2 -0
- package/dist/check-issue-claim.js +146 -0
- package/dist/check-worktree-guard.d.ts +2 -0
- package/dist/check-worktree-guard.js +114 -0
- package/dist/error-scan.d.ts +3 -0
- package/dist/error-scan.js +216 -0
- package/dist/export-public.d.ts +9 -0
- package/dist/export-public.js +274 -0
- package/dist/fetch-issue-attachments.d.ts +22 -0
- package/dist/fetch-issue-attachments.js +325 -0
- package/dist/issue-claim/evaluation.d.ts +66 -0
- package/dist/issue-claim/evaluation.js +259 -0
- package/dist/issue-claim/loaders.d.ts +5 -0
- package/dist/issue-claim/loaders.js +116 -0
- package/dist/list-zed-commands.d.ts +7 -0
- package/dist/list-zed-commands.js +288 -0
- package/dist/plan-issue-triage.d.ts +3 -0
- package/dist/plan-issue-triage.js +404 -0
- package/dist/question-gate/ledger.d.ts +5 -0
- package/dist/question-gate/ledger.js +115 -0
- package/dist/question-gate/rules.d.ts +40 -0
- package/dist/question-gate/rules.js +483 -0
- package/dist/recap/board.d.ts +5 -0
- package/dist/recap/board.js +384 -0
- package/dist/recap/git-and-github.d.ts +11 -0
- package/dist/recap/git-and-github.js +620 -0
- package/dist/recap/report-and-hud.d.ts +2 -0
- package/dist/recap/report-and-hud.js +294 -0
- package/dist/recap/time-and-refs.d.ts +27 -0
- package/dist/recap/time-and-refs.js +148 -0
- package/dist/recap.d.ts +2 -0
- package/dist/recap.js +217 -0
- package/dist/render-workflow.js +12 -14
- package/dist/resolve-containers.d.ts +2 -0
- package/dist/resolve-containers.js +96 -0
- package/dist/review-loop.d.ts +7 -0
- package/dist/review-loop.js +622 -0
- package/dist/save-counts.d.ts +2 -0
- package/dist/save-counts.js +191 -0
- package/dist/summarize-logs.d.ts +2 -0
- package/dist/summarize-logs.js +41 -0
- package/dist/sweep-github.d.ts +10 -0
- package/dist/sweep-github.js +453 -0
- package/dist/sweep-issues.d.ts +56 -0
- package/dist/sweep-issues.js +650 -0
- package/dist/sweep.d.ts +2 -0
- package/dist/sweep.js +301 -0
- package/dist/triage-question.d.ts +2 -0
- package/dist/triage-question.js +189 -0
- package/dist/validate-skills.d.ts +2 -0
- package/dist/validate-skills.js +48 -0
- package/dist/watch-live.d.ts +7 -0
- package/dist/watch-live.js +83 -0
- package/dist/watcher/config.d.ts +11 -0
- package/dist/watcher/config.js +265 -0
- package/dist/watcher/live-classifier.d.ts +52 -0
- package/dist/watcher/live-classifier.js +510 -0
- package/dist/watcher/safe-fs.d.ts +9 -0
- package/dist/watcher/safe-fs.js +159 -0
- package/dist/watcher/stream.d.ts +27 -0
- package/dist/watcher/stream.js +340 -0
- package/dist/watcher/summarizer.d.ts +19 -0
- package/dist/watcher/summarizer.js +121 -0
- package/dist/wave3/process.d.ts +14 -0
- package/dist/wave3/process.js +84 -0
- package/dist/wave4/process.d.ts +14 -0
- package/dist/wave4/process.js +128 -0
- package/dist/workflow/render.d.ts +1 -1
- package/dist/workflow/render.js +1 -1
- package/dist/workflow-auditor/audit.d.ts +27 -0
- package/dist/workflow-auditor/audit.js +583 -0
- package/dist/workflow-auditor/ledger-io.d.ts +5 -0
- package/dist/workflow-auditor/ledger-io.js +99 -0
- package/dist/workflow-auditor/skills-validator.d.ts +10 -0
- package/dist/workflow-auditor/skills-validator.js +533 -0
- package/dist/worktree-guard/evaluation.d.ts +19 -0
- package/dist/worktree-guard/evaluation.js +84 -0
- package/dist/worktree-guard/git.d.ts +3 -0
- package/dist/worktree-guard/git.js +61 -0
- package/package.json +32 -33
|
@@ -0,0 +1,1250 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* LAYER: Interface
|
|
3
|
+
* Contains: Installer CLI (doctor, detect, install, verify, uninstall) over installer/clients.json.
|
|
4
|
+
* Rules: Plan before writing; refuse on conflict; never overwrite without --force; undo only manifest-owned entries.
|
|
5
|
+
* BigO: O(n^2) score:1
|
|
6
|
+
* keywords: [BbcInstall, ClientCatalog, InstallManifest, HookMerge, LayoutVerifier]
|
|
7
|
+
* GOAL: Install, verify, and cleanly uninstall the skill/command collection across supported coding agents.
|
|
8
|
+
*/
|
|
9
|
+
import { createHash } from 'node:crypto';
|
|
10
|
+
import { copyFileSync, existsSync, mkdirSync, readdirSync, readFileSync, renameSync, rmSync, rmdirSync, statSync, writeFileSync, } from 'node:fs';
|
|
11
|
+
import { homedir } from 'node:os';
|
|
12
|
+
import { dirname, join, relative, resolve, sep } from 'node:path';
|
|
13
|
+
import { fileURLToPath } from 'node:url';
|
|
14
|
+
import { runExecutable, splitCommandWords, whichCommand } from "./wave4/process.js";
|
|
15
|
+
export const bbcInstallUsage = 'bbc bbc-install <doctor|detect|install|verify|uninstall> [options]';
|
|
16
|
+
const DEFAULT_SOURCE = fileURLToPath(new URL('../../..', import.meta.url));
|
|
17
|
+
const STATE_DIR = '.bbc-install';
|
|
18
|
+
const GATE = 'question-gate';
|
|
19
|
+
const HOOK_MARK = 'triage-question';
|
|
20
|
+
const TRIAGE_PROBE = 'Should I run tests locally?';
|
|
21
|
+
const VERSION_RE = /\d+(?:\.\d+)+/;
|
|
22
|
+
const IGNORED_PARTS = new Set(['__pycache__']);
|
|
23
|
+
const MARK = { ok: '🟢', warn: '🟡', fail: '🔴' };
|
|
24
|
+
const SYMBOL = {
|
|
25
|
+
create: '+',
|
|
26
|
+
add: '+',
|
|
27
|
+
update: '~',
|
|
28
|
+
replace: '!',
|
|
29
|
+
unchanged: '=',
|
|
30
|
+
present: '=',
|
|
31
|
+
};
|
|
32
|
+
const DEFAULT_CLI = 'npx -y @bbcsoftwarefactory/cli';
|
|
33
|
+
const NODE_HINT = 'https://nodejs.org/ (Node.js >= 22.18.0)';
|
|
34
|
+
const DEPENDENCIES = [
|
|
35
|
+
{
|
|
36
|
+
name: 'node',
|
|
37
|
+
command: 'node',
|
|
38
|
+
required: true,
|
|
39
|
+
neededFor: 'running the bbc CLI and hooks (Node.js >= 22.18.0)',
|
|
40
|
+
hint: NODE_HINT,
|
|
41
|
+
},
|
|
42
|
+
{
|
|
43
|
+
name: 'git',
|
|
44
|
+
command: 'git',
|
|
45
|
+
required: true,
|
|
46
|
+
neededFor: 'cloning and updating the collection; most skills read Git state',
|
|
47
|
+
hint: 'https://git-scm.com/downloads',
|
|
48
|
+
},
|
|
49
|
+
{
|
|
50
|
+
name: 'gh',
|
|
51
|
+
command: 'gh',
|
|
52
|
+
required: false,
|
|
53
|
+
neededFor: 'GitHub commands and issue skills (/daily, /issue-clean-and-close, /recap, issue-reviewer)',
|
|
54
|
+
hint: 'https://cli.github.com/',
|
|
55
|
+
},
|
|
56
|
+
{
|
|
57
|
+
name: 'ssh',
|
|
58
|
+
command: 'ssh',
|
|
59
|
+
required: false,
|
|
60
|
+
neededFor: 'watcher-observability only',
|
|
61
|
+
hint: 'install the OpenSSH client',
|
|
62
|
+
},
|
|
63
|
+
{
|
|
64
|
+
name: 'bash',
|
|
65
|
+
command: 'bash',
|
|
66
|
+
required: false,
|
|
67
|
+
neededFor: 'the bundled test suites and the copy-paste examples',
|
|
68
|
+
hint: 'Git for Windows includes Git Bash',
|
|
69
|
+
},
|
|
70
|
+
{
|
|
71
|
+
name: 'npx',
|
|
72
|
+
command: 'npx',
|
|
73
|
+
required: false,
|
|
74
|
+
neededFor: 'running @bbcsoftwarefactory/cli hooks and npx skills add',
|
|
75
|
+
hint: NODE_HINT,
|
|
76
|
+
},
|
|
77
|
+
];
|
|
78
|
+
class UsageError extends Error {
|
|
79
|
+
constructor(message) {
|
|
80
|
+
super(message);
|
|
81
|
+
this.name = 'UsageError';
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
function toPosix(p) {
|
|
85
|
+
return p.replaceAll('\\', '/');
|
|
86
|
+
}
|
|
87
|
+
function expandHomePath(raw) {
|
|
88
|
+
const home = process.env.HOME || homedir();
|
|
89
|
+
if (raw === '~')
|
|
90
|
+
return home;
|
|
91
|
+
if (raw.startsWith('~/') || raw.startsWith('~\\'))
|
|
92
|
+
return join(home, raw.slice(2));
|
|
93
|
+
return raw;
|
|
94
|
+
}
|
|
95
|
+
class InstallTarget {
|
|
96
|
+
home;
|
|
97
|
+
scope;
|
|
98
|
+
root;
|
|
99
|
+
source;
|
|
100
|
+
cliRunner;
|
|
101
|
+
constructor(args) {
|
|
102
|
+
this.home = args.home ? resolve(expandHomePath(args.home)) : resolve(process.env.HOME || homedir());
|
|
103
|
+
this.scope = args.globalScope ? 'global' : 'project';
|
|
104
|
+
const projectDir = resolve(args.project || process.cwd());
|
|
105
|
+
this.root = this.scope === 'global' ? this.home : projectDir;
|
|
106
|
+
this.source = args.source ? resolve(args.source) : resolve(DEFAULT_SOURCE);
|
|
107
|
+
this.cliRunner = args.cliRunner || DEFAULT_CLI;
|
|
108
|
+
}
|
|
109
|
+
rel(targetPath) {
|
|
110
|
+
return toPosix(relative(this.root, targetPath));
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
function isPlainObject(value) {
|
|
114
|
+
return value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
115
|
+
}
|
|
116
|
+
function isSubPath(parent, candidate) {
|
|
117
|
+
const prefix = parent.endsWith(sep) ? parent : `${parent}${sep}`;
|
|
118
|
+
return candidate.startsWith(prefix);
|
|
119
|
+
}
|
|
120
|
+
function loadClients(sourceRoot) {
|
|
121
|
+
const localClients = join(sourceRoot, 'installer', 'clients.json');
|
|
122
|
+
const fallbackClients = join(DEFAULT_SOURCE, 'installer', 'clients.json');
|
|
123
|
+
const filePath = existsSync(localClients) ? localClients : fallbackClients;
|
|
124
|
+
const parsed = JSON.parse(readFileSync(filePath, 'utf8'));
|
|
125
|
+
return parsed.clients;
|
|
126
|
+
}
|
|
127
|
+
function discoverBundles(source) {
|
|
128
|
+
const found = new Map();
|
|
129
|
+
for (const [kind, folder] of [
|
|
130
|
+
['skill', 'skills'],
|
|
131
|
+
['command', 'commands'],
|
|
132
|
+
]) {
|
|
133
|
+
const base = join(source, folder);
|
|
134
|
+
if (!existsSync(base) || !statSync(base).isDirectory())
|
|
135
|
+
continue;
|
|
136
|
+
const entries = readdirSync(base, { withFileTypes: true }).sort((a, b) => a.name.localeCompare(b.name));
|
|
137
|
+
for (const entry of entries) {
|
|
138
|
+
if (!entry.isDirectory())
|
|
139
|
+
continue;
|
|
140
|
+
const full = join(base, entry.name);
|
|
141
|
+
if (!existsSync(join(full, 'SKILL.md')))
|
|
142
|
+
continue;
|
|
143
|
+
if (found.has(entry.name)) {
|
|
144
|
+
throw new UsageError(`${entry.name} exists as both a skill and a command`);
|
|
145
|
+
}
|
|
146
|
+
found.set(entry.name, { kind, path: full });
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
if (found.size === 0) {
|
|
150
|
+
throw new UsageError(`no skills found under ${source}; pass --source <clone of this collection>`);
|
|
151
|
+
}
|
|
152
|
+
return found;
|
|
153
|
+
}
|
|
154
|
+
function selectNames(found, kind, value) {
|
|
155
|
+
const names = [];
|
|
156
|
+
for (const [name, bundle] of found.entries()) {
|
|
157
|
+
if (bundle.kind === kind)
|
|
158
|
+
names.push(name);
|
|
159
|
+
}
|
|
160
|
+
if (value === undefined || value === 'all')
|
|
161
|
+
return names;
|
|
162
|
+
if (value === 'none')
|
|
163
|
+
return [];
|
|
164
|
+
const wanted = value
|
|
165
|
+
.split(',')
|
|
166
|
+
.map((item) => item.trim())
|
|
167
|
+
.filter((item) => item.length > 0);
|
|
168
|
+
const nameSet = new Set(names);
|
|
169
|
+
const unknown = wanted.filter((item) => !nameSet.has(item));
|
|
170
|
+
if (unknown.length > 0) {
|
|
171
|
+
throw new UsageError(`unknown ${kind}: ${unknown.join(', ')} (available: ${names.join(', ')})`);
|
|
172
|
+
}
|
|
173
|
+
return wanted;
|
|
174
|
+
}
|
|
175
|
+
function detectClients(clients, home, project) {
|
|
176
|
+
return clients.map((client) => {
|
|
177
|
+
const evidence = [];
|
|
178
|
+
for (const name of client.detect.commands) {
|
|
179
|
+
if (whichCommand(name))
|
|
180
|
+
evidence.push(`command ${name}`);
|
|
181
|
+
}
|
|
182
|
+
for (const rel of client.detect.home) {
|
|
183
|
+
if (existsSync(join(home, rel)))
|
|
184
|
+
evidence.push(`~/${rel}`);
|
|
185
|
+
}
|
|
186
|
+
for (const rel of client.detect.project) {
|
|
187
|
+
if (existsSync(join(project, rel)))
|
|
188
|
+
evidence.push(`./${rel}`);
|
|
189
|
+
}
|
|
190
|
+
return {
|
|
191
|
+
id: client.id,
|
|
192
|
+
name: client.name,
|
|
193
|
+
detected: evidence.length > 0,
|
|
194
|
+
evidence,
|
|
195
|
+
};
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
function resolveClients(clients, value, target) {
|
|
199
|
+
const ids = clients.map((c) => c.id);
|
|
200
|
+
if (!value) {
|
|
201
|
+
throw new UsageError(`--client is required: ${ids.join(', ')} or all-detected`);
|
|
202
|
+
}
|
|
203
|
+
if (value === 'all-detected') {
|
|
204
|
+
const detectedIds = new Set(detectClients(clients, target.home, target.root)
|
|
205
|
+
.filter((row) => row.detected === true)
|
|
206
|
+
.map((row) => String(row.id)));
|
|
207
|
+
return clients.filter((c) => detectedIds.has(c.id));
|
|
208
|
+
}
|
|
209
|
+
const wanted = value
|
|
210
|
+
.split(',')
|
|
211
|
+
.map((item) => item.trim())
|
|
212
|
+
.filter((item) => item.length > 0);
|
|
213
|
+
const idSet = new Set(ids);
|
|
214
|
+
const unknown = wanted.filter((item) => !idSet.has(item));
|
|
215
|
+
if (unknown.length > 0) {
|
|
216
|
+
throw new UsageError(`unknown client: ${unknown.join(', ')} (available: ${ids.join(', ')}, all-detected)`);
|
|
217
|
+
}
|
|
218
|
+
const wantedSet = new Set(wanted);
|
|
219
|
+
return clients.filter((c) => wantedSet.has(c.id));
|
|
220
|
+
}
|
|
221
|
+
function fileDigest(filePath) {
|
|
222
|
+
return createHash('sha256').update(readFileSync(filePath)).digest('hex');
|
|
223
|
+
}
|
|
224
|
+
function collectFilesFiltered(dir, rootDir, out) {
|
|
225
|
+
const entries = readdirSync(dir, { withFileTypes: true }).sort((a, b) => a.name.localeCompare(b.name));
|
|
226
|
+
for (const entry of entries) {
|
|
227
|
+
if (IGNORED_PARTS.has(entry.name))
|
|
228
|
+
continue;
|
|
229
|
+
const full = join(dir, entry.name);
|
|
230
|
+
if (entry.isDirectory()) {
|
|
231
|
+
collectFilesFiltered(full, rootDir, out);
|
|
232
|
+
}
|
|
233
|
+
else if (entry.isFile()) {
|
|
234
|
+
out.push(toPosix(relative(rootDir, full)));
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
function treeDigest(targetPath) {
|
|
239
|
+
if (statSync(targetPath).isFile()) {
|
|
240
|
+
return fileDigest(targetPath);
|
|
241
|
+
}
|
|
242
|
+
const files = [];
|
|
243
|
+
collectFilesFiltered(targetPath, targetPath, files);
|
|
244
|
+
files.sort();
|
|
245
|
+
const hash = createHash('sha256');
|
|
246
|
+
for (const rel of files) {
|
|
247
|
+
hash.update(Buffer.from(`${rel}\0`, 'utf8'));
|
|
248
|
+
hash.update(Buffer.from(fileDigest(join(targetPath, rel)), 'ascii'));
|
|
249
|
+
}
|
|
250
|
+
return hash.digest('hex');
|
|
251
|
+
}
|
|
252
|
+
function writeTextAtomic(filePath, text) {
|
|
253
|
+
const temp = `${filePath}.bbc-install.tmp`;
|
|
254
|
+
writeFileSync(temp, text, 'utf8');
|
|
255
|
+
renameSync(temp, filePath);
|
|
256
|
+
}
|
|
257
|
+
function writeJsonAtomic(filePath, doc) {
|
|
258
|
+
writeTextAtomic(filePath, `${JSON.stringify(doc, null, 2)}\n`);
|
|
259
|
+
}
|
|
260
|
+
function readJsonObject(filePath) {
|
|
261
|
+
try {
|
|
262
|
+
const parsed = JSON.parse(readFileSync(filePath, 'utf8'));
|
|
263
|
+
return isPlainObject(parsed) ? parsed : null;
|
|
264
|
+
}
|
|
265
|
+
catch {
|
|
266
|
+
return null;
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
function removePath(targetPath) {
|
|
270
|
+
if (!existsSync(targetPath))
|
|
271
|
+
return;
|
|
272
|
+
rmSync(targetPath, { recursive: true, force: true });
|
|
273
|
+
}
|
|
274
|
+
function copyTreeFiltered(srcDir, dstDir) {
|
|
275
|
+
mkdirSync(dstDir, { recursive: true });
|
|
276
|
+
const entries = readdirSync(srcDir, { withFileTypes: true });
|
|
277
|
+
for (const entry of entries) {
|
|
278
|
+
if (IGNORED_PARTS.has(entry.name))
|
|
279
|
+
continue;
|
|
280
|
+
const src = join(srcDir, entry.name);
|
|
281
|
+
const dst = join(dstDir, entry.name);
|
|
282
|
+
if (entry.isDirectory()) {
|
|
283
|
+
copyTreeFiltered(src, dst);
|
|
284
|
+
}
|
|
285
|
+
else if (entry.isFile()) {
|
|
286
|
+
copyFileSync(src, dst);
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
function fillCommandTemplate(value, command) {
|
|
291
|
+
if (Array.isArray(value))
|
|
292
|
+
return value.map((item) => fillCommandTemplate(item, command));
|
|
293
|
+
if (isPlainObject(value)) {
|
|
294
|
+
const out = {};
|
|
295
|
+
for (const [k, v] of Object.entries(value)) {
|
|
296
|
+
out[k] = fillCommandTemplate(v, command);
|
|
297
|
+
}
|
|
298
|
+
return out;
|
|
299
|
+
}
|
|
300
|
+
return value === '{command}' ? command : value;
|
|
301
|
+
}
|
|
302
|
+
function manifestPath(target) {
|
|
303
|
+
return join(target.root, STATE_DIR, 'manifest.json');
|
|
304
|
+
}
|
|
305
|
+
function loadManifest(target) {
|
|
306
|
+
const filePath = manifestPath(target);
|
|
307
|
+
if (!existsSync(filePath)) {
|
|
308
|
+
return { version: 1, scope: target.scope, entries: [] };
|
|
309
|
+
}
|
|
310
|
+
const doc = readJsonObject(filePath);
|
|
311
|
+
if (!doc || !Array.isArray(doc.entries)) {
|
|
312
|
+
throw new UsageError(`${target.rel(filePath)} is unreadable; nothing was changed`);
|
|
313
|
+
}
|
|
314
|
+
return {
|
|
315
|
+
version: typeof doc.version === 'number' ? doc.version : 1,
|
|
316
|
+
scope: typeof doc.scope === 'string' ? doc.scope : target.scope,
|
|
317
|
+
entries: doc.entries,
|
|
318
|
+
};
|
|
319
|
+
}
|
|
320
|
+
function findEntry(manifest, kind, entryPath) {
|
|
321
|
+
return manifest.entries.find((e) => e.type === kind && e.path === entryPath);
|
|
322
|
+
}
|
|
323
|
+
function saveManifest(target, manifest) {
|
|
324
|
+
const stateDir = join(target.root, STATE_DIR);
|
|
325
|
+
mkdirSync(stateDir, { recursive: true });
|
|
326
|
+
const ignoreFile = join(stateDir, '.gitignore');
|
|
327
|
+
if (!existsSync(ignoreFile)) {
|
|
328
|
+
writeFileSync(ignoreFile, '*\n', 'utf8');
|
|
329
|
+
}
|
|
330
|
+
writeJsonAtomic(manifestPath(target), manifest);
|
|
331
|
+
}
|
|
332
|
+
function utcStampNow() {
|
|
333
|
+
const now = new Date();
|
|
334
|
+
const pad2 = (n) => String(n).padStart(2, '0');
|
|
335
|
+
const pad3 = (n) => String(n).padStart(3, '0');
|
|
336
|
+
return (`${now.getUTCFullYear()}${pad2(now.getUTCMonth() + 1)}${pad2(now.getUTCDate())}` +
|
|
337
|
+
`T${pad2(now.getUTCHours())}${pad2(now.getUTCMinutes())}${pad2(now.getUTCSeconds())}` +
|
|
338
|
+
`${pad3(now.getUTCMilliseconds())}000Z`);
|
|
339
|
+
}
|
|
340
|
+
class Backups {
|
|
341
|
+
target;
|
|
342
|
+
stamp;
|
|
343
|
+
constructor(target) {
|
|
344
|
+
this.target = target;
|
|
345
|
+
this.stamp = utcStampNow();
|
|
346
|
+
}
|
|
347
|
+
keep(filePath, move = false) {
|
|
348
|
+
const rel = this.target.rel(filePath);
|
|
349
|
+
const dest = join(this.target.root, STATE_DIR, 'backups', this.stamp, rel);
|
|
350
|
+
mkdirSync(dirname(dest), { recursive: true });
|
|
351
|
+
if (move) {
|
|
352
|
+
renameSync(filePath, dest);
|
|
353
|
+
}
|
|
354
|
+
else if (statSync(filePath).isDirectory()) {
|
|
355
|
+
copyTreeFiltered(filePath, dest);
|
|
356
|
+
}
|
|
357
|
+
else {
|
|
358
|
+
copyFileSync(filePath, dest);
|
|
359
|
+
}
|
|
360
|
+
return this.target.rel(dest);
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
function probeCommand(command) {
|
|
364
|
+
const found = whichCommand(command);
|
|
365
|
+
if (!found)
|
|
366
|
+
return [null, ''];
|
|
367
|
+
const flag = command === 'ssh' ? '-V' : '--version';
|
|
368
|
+
const res = runExecutable([found, flag], { timeoutMs: 10_000 });
|
|
369
|
+
if (res.error)
|
|
370
|
+
return [found, ''];
|
|
371
|
+
const combined = Buffer.concat([res.stdout, res.stderr]).toString('utf8');
|
|
372
|
+
const match = VERSION_RE.exec(combined);
|
|
373
|
+
return [found, match ? match[0] : ''];
|
|
374
|
+
}
|
|
375
|
+
function runDoctor(args) {
|
|
376
|
+
const checks = [];
|
|
377
|
+
const launcher = splitCommandWords(args.cliRunner || DEFAULT_CLI)[0] ?? 'npx';
|
|
378
|
+
const [launcherPath, launcherVersion] = probeCommand(launcher);
|
|
379
|
+
checks.push({
|
|
380
|
+
name: launcher,
|
|
381
|
+
status: launcherPath ? 'ok' : 'fail',
|
|
382
|
+
required: true,
|
|
383
|
+
version: launcherVersion,
|
|
384
|
+
needed_for: 'the question-gate hook command (--cli to change it)',
|
|
385
|
+
hint: NODE_HINT,
|
|
386
|
+
});
|
|
387
|
+
for (const dep of DEPENDENCIES) {
|
|
388
|
+
if (dep.name === launcher)
|
|
389
|
+
continue;
|
|
390
|
+
const [depPath, depVersion] = probeCommand(dep.command);
|
|
391
|
+
const status = depPath ? 'ok' : dep.required ? 'fail' : 'warn';
|
|
392
|
+
checks.push({
|
|
393
|
+
name: dep.name,
|
|
394
|
+
status,
|
|
395
|
+
required: dep.required,
|
|
396
|
+
version: depVersion,
|
|
397
|
+
needed_for: dep.neededFor,
|
|
398
|
+
hint: dep.hint,
|
|
399
|
+
});
|
|
400
|
+
}
|
|
401
|
+
const failed = checks.filter((c) => c.status === 'fail').map((c) => c.name);
|
|
402
|
+
const report = { verdict: failed.length > 0 ? 'missing-required' : 'ready', checks };
|
|
403
|
+
if (args.json) {
|
|
404
|
+
process.stdout.write(`${JSON.stringify(report, null, 2)}\n`);
|
|
405
|
+
return failed.length > 0 ? 1 : 0;
|
|
406
|
+
}
|
|
407
|
+
for (const check of checks) {
|
|
408
|
+
let line = `${MARK[check.status]} ${check.name} ${check.version}`.trimEnd();
|
|
409
|
+
if (check.status !== 'ok') {
|
|
410
|
+
const kind = check.required ? 'required' : 'optional';
|
|
411
|
+
line += ` not found (${kind}: ${check.needed_for}). Install: ${check.hint}`;
|
|
412
|
+
}
|
|
413
|
+
process.stdout.write(`${line}\n`);
|
|
414
|
+
}
|
|
415
|
+
if (failed.length > 0) {
|
|
416
|
+
process.stdout.write(`${MARK.fail} missing required: ${failed.join(', ')}\n`);
|
|
417
|
+
return 1;
|
|
418
|
+
}
|
|
419
|
+
process.stdout.write(`${MARK.ok} ready to install\n`);
|
|
420
|
+
return 0;
|
|
421
|
+
}
|
|
422
|
+
function runDetect(args) {
|
|
423
|
+
const target = new InstallTarget(args);
|
|
424
|
+
const rows = detectClients(loadClients(target.source), target.home, target.root);
|
|
425
|
+
if (args.json) {
|
|
426
|
+
process.stdout.write(`${JSON.stringify({ clients: rows }, null, 2)}\n`);
|
|
427
|
+
return 0;
|
|
428
|
+
}
|
|
429
|
+
for (const row of rows) {
|
|
430
|
+
const ok = row.detected === true;
|
|
431
|
+
const mark = ok ? MARK.ok : MARK.fail;
|
|
432
|
+
const evidence = Array.isArray(row.evidence) ? row.evidence : [];
|
|
433
|
+
const detail = ok ? evidence.join(', ') : 'not detected';
|
|
434
|
+
process.stdout.write(`${mark} ${String(row.id).padEnd(12, ' ')} ${String(row.name)}: ${detail}\n`);
|
|
435
|
+
}
|
|
436
|
+
return 0;
|
|
437
|
+
}
|
|
438
|
+
class InstallPlan {
|
|
439
|
+
actions = [];
|
|
440
|
+
byKey = new Map();
|
|
441
|
+
conflicts = [];
|
|
442
|
+
manual = [];
|
|
443
|
+
shared(key, clientId) {
|
|
444
|
+
const existing = this.byKey.get(key);
|
|
445
|
+
if (!existing)
|
|
446
|
+
return false;
|
|
447
|
+
if (!existing.clients.includes(clientId))
|
|
448
|
+
existing.clients.push(clientId);
|
|
449
|
+
return true;
|
|
450
|
+
}
|
|
451
|
+
add(key, action) {
|
|
452
|
+
this.byKey.set(key, action);
|
|
453
|
+
this.actions.push(action);
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
function planCopy(target, plan, manifest, force, src, rel, clientId, kind) {
|
|
457
|
+
if (plan.shared(rel, clientId))
|
|
458
|
+
return;
|
|
459
|
+
const dst = join(target.root, rel);
|
|
460
|
+
const digest = treeDigest(src);
|
|
461
|
+
const record = findEntry(manifest, kind, rel);
|
|
462
|
+
const action = {
|
|
463
|
+
type: kind,
|
|
464
|
+
src,
|
|
465
|
+
path: rel,
|
|
466
|
+
digest,
|
|
467
|
+
clients: [clientId],
|
|
468
|
+
owned: record !== undefined,
|
|
469
|
+
mode: 'create',
|
|
470
|
+
};
|
|
471
|
+
if (!existsSync(dst)) {
|
|
472
|
+
action.mode = 'create';
|
|
473
|
+
}
|
|
474
|
+
else if (statSync(dst).isDirectory() !== (kind === 'dir')) {
|
|
475
|
+
plan.conflicts.push(`${rel} exists but is not a ${kind === 'dir' ? 'folder' : 'file'}`);
|
|
476
|
+
return;
|
|
477
|
+
}
|
|
478
|
+
else {
|
|
479
|
+
const current = treeDigest(dst);
|
|
480
|
+
if (current === digest) {
|
|
481
|
+
action.mode = 'unchanged';
|
|
482
|
+
}
|
|
483
|
+
else if (record !== undefined && record.digest === current) {
|
|
484
|
+
action.mode = 'update';
|
|
485
|
+
}
|
|
486
|
+
else if (force) {
|
|
487
|
+
action.mode = 'replace';
|
|
488
|
+
}
|
|
489
|
+
else {
|
|
490
|
+
plan.conflicts.push(`${rel} already exists with different content. Review it, then rerun with --force ` +
|
|
491
|
+
`(the current copy is backed up under ${STATE_DIR}/backups/)`);
|
|
492
|
+
return;
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
plan.add(rel, action);
|
|
496
|
+
}
|
|
497
|
+
function hookCommand(target, client) {
|
|
498
|
+
const spec = client.hook;
|
|
499
|
+
return `${target.cliRunner} triage-question --stdin --format ${spec.format}`;
|
|
500
|
+
}
|
|
501
|
+
function hookEntries(doc, event) {
|
|
502
|
+
const hooks = doc.hooks;
|
|
503
|
+
if (hooks === undefined)
|
|
504
|
+
return [];
|
|
505
|
+
if (!isPlainObject(hooks))
|
|
506
|
+
return null;
|
|
507
|
+
const eventList = hooks[event];
|
|
508
|
+
if (eventList === undefined)
|
|
509
|
+
return [];
|
|
510
|
+
return Array.isArray(eventList) ? eventList : null;
|
|
511
|
+
}
|
|
512
|
+
function planHook(target, plan, manifest, client) {
|
|
513
|
+
const spec = client.hook;
|
|
514
|
+
const rel = spec.file[target.scope];
|
|
515
|
+
const filePath = join(target.root, rel);
|
|
516
|
+
let doc = {};
|
|
517
|
+
if (existsSync(filePath)) {
|
|
518
|
+
const parsed = readJsonObject(filePath);
|
|
519
|
+
if (!parsed) {
|
|
520
|
+
plan.conflicts.push(`${rel} is not a valid JSON object; fix or remove it, then rerun`);
|
|
521
|
+
return;
|
|
522
|
+
}
|
|
523
|
+
doc = parsed;
|
|
524
|
+
}
|
|
525
|
+
const entries = hookEntries(doc, spec.event);
|
|
526
|
+
if (entries === null) {
|
|
527
|
+
plan.conflicts.push(`${rel} has an unexpected "hooks" shape; add the ${GATE} hook by hand`);
|
|
528
|
+
return;
|
|
529
|
+
}
|
|
530
|
+
const present = entries.some((entry) => JSON.stringify(entry).includes(HOOK_MARK));
|
|
531
|
+
plan.add(rel, {
|
|
532
|
+
type: 'hook',
|
|
533
|
+
path: rel,
|
|
534
|
+
event: spec.event,
|
|
535
|
+
root: spec.root,
|
|
536
|
+
entry: fillCommandTemplate(spec.entry, hookCommand(target, client)),
|
|
537
|
+
clients: [client.id],
|
|
538
|
+
mode: present ? 'present' : 'add',
|
|
539
|
+
created: !existsSync(filePath),
|
|
540
|
+
owned: findEntry(manifest, 'hook', rel) !== undefined,
|
|
541
|
+
});
|
|
542
|
+
}
|
|
543
|
+
function agentsBlock(skillsDir) {
|
|
544
|
+
return (`<!-- bbc-install:${GATE} ${skillsDir} -->\n` +
|
|
545
|
+
`Before finishing a turn with a question or asking the user what to do next, read ` +
|
|
546
|
+
`\`${skillsDir}/${GATE}/SKILL.md\` and run \`${DEFAULT_CLI} triage-question --question "<draft>"\`.\n` +
|
|
547
|
+
`<!-- /bbc-install:${GATE} ${skillsDir} -->\n`);
|
|
548
|
+
}
|
|
549
|
+
function planAgents(target, plan, manifest, client) {
|
|
550
|
+
const skillsDir = client.skills[target.scope];
|
|
551
|
+
const key = `AGENTS.md#${skillsDir}`;
|
|
552
|
+
if (plan.shared(key, client.id))
|
|
553
|
+
return;
|
|
554
|
+
const filePath = join(target.root, 'AGENTS.md');
|
|
555
|
+
const block = agentsBlock(skillsDir);
|
|
556
|
+
const text = existsSync(filePath) && statSync(filePath).isFile() ? readFileSync(filePath, 'utf8') : '';
|
|
557
|
+
const record = manifest.entries.find((e) => e.type === 'agents' && e.key === skillsDir);
|
|
558
|
+
const markerLine = block.split('\n')[0] ?? '';
|
|
559
|
+
plan.add(key, {
|
|
560
|
+
type: 'agents',
|
|
561
|
+
path: 'AGENTS.md',
|
|
562
|
+
key: skillsDir,
|
|
563
|
+
block,
|
|
564
|
+
clients: [client.id],
|
|
565
|
+
mode: text.includes(markerLine) ? 'present' : 'add',
|
|
566
|
+
owned: record !== undefined,
|
|
567
|
+
});
|
|
568
|
+
}
|
|
569
|
+
function buildInstallPlan(target, clients, names, found, manifest, force) {
|
|
570
|
+
const plan = new InstallPlan();
|
|
571
|
+
for (const client of clients) {
|
|
572
|
+
const skillsDir = client.skills[target.scope];
|
|
573
|
+
for (const name of names) {
|
|
574
|
+
const bundle = found.get(name);
|
|
575
|
+
planCopy(target, plan, manifest, force, bundle.path, `${skillsDir}/${name}`, client.id, 'dir');
|
|
576
|
+
}
|
|
577
|
+
const commandsDir = client.commands_dir?.[target.scope];
|
|
578
|
+
if (commandsDir) {
|
|
579
|
+
for (const name of names) {
|
|
580
|
+
const bundle = found.get(name);
|
|
581
|
+
const adapter = join(bundle.path, 'adapters', 'gemini.toml');
|
|
582
|
+
if (bundle.kind === 'command' && existsSync(adapter) && statSync(adapter).isFile()) {
|
|
583
|
+
planCopy(target, plan, manifest, force, adapter, `${commandsDir}/${name}.toml`, client.id, 'file');
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
if (names.includes(GATE) && client.hook) {
|
|
588
|
+
planHook(target, plan, manifest, client);
|
|
589
|
+
}
|
|
590
|
+
if (names.includes(GATE) && client.agents_md && target.scope === 'project') {
|
|
591
|
+
planAgents(target, plan, manifest, client);
|
|
592
|
+
}
|
|
593
|
+
if (names.includes(GATE) && client.agents_md && target.scope === 'global') {
|
|
594
|
+
plan.manual.push(`${client.name}: add the ${GATE} rule from AGENTS.md to your global agent rules.`);
|
|
595
|
+
}
|
|
596
|
+
for (const step of client.manual) {
|
|
597
|
+
plan.manual.push(`${client.name}: ${step}`);
|
|
598
|
+
}
|
|
599
|
+
}
|
|
600
|
+
return plan;
|
|
601
|
+
}
|
|
602
|
+
function makeParents(target, manifest, targetPath) {
|
|
603
|
+
const missing = [];
|
|
604
|
+
let parent = dirname(targetPath);
|
|
605
|
+
while (!existsSync(parent)) {
|
|
606
|
+
missing.push(parent);
|
|
607
|
+
const next = dirname(parent);
|
|
608
|
+
if (next === parent)
|
|
609
|
+
break;
|
|
610
|
+
parent = next;
|
|
611
|
+
}
|
|
612
|
+
for (let i = missing.length - 1; i >= 0; i -= 1) {
|
|
613
|
+
const folder = missing[i];
|
|
614
|
+
mkdirSync(folder);
|
|
615
|
+
const rel = target.rel(folder);
|
|
616
|
+
if (findEntry(manifest, 'mkdir', rel) === undefined) {
|
|
617
|
+
manifest.entries.push({ type: 'mkdir', path: rel });
|
|
618
|
+
}
|
|
619
|
+
}
|
|
620
|
+
}
|
|
621
|
+
function remember(manifest, action, extra) {
|
|
622
|
+
let record = action.type !== 'agents'
|
|
623
|
+
? findEntry(manifest, action.type, action.path)
|
|
624
|
+
: manifest.entries.find((e) => e.type === 'agents' && e.key === action.key);
|
|
625
|
+
if (!record) {
|
|
626
|
+
record = { type: action.type, path: action.path, clients: [], ...extra };
|
|
627
|
+
manifest.entries.push(record);
|
|
628
|
+
}
|
|
629
|
+
else {
|
|
630
|
+
Object.assign(record, extra);
|
|
631
|
+
}
|
|
632
|
+
const merged = new Set([...(record.clients ?? []), ...action.clients]);
|
|
633
|
+
record.clients = Array.from(merged).sort();
|
|
634
|
+
}
|
|
635
|
+
function applyCopy(target, manifest, backups, action) {
|
|
636
|
+
if (action.mode === 'unchanged') {
|
|
637
|
+
if (action.owned)
|
|
638
|
+
remember(manifest, action, {});
|
|
639
|
+
return;
|
|
640
|
+
}
|
|
641
|
+
const dst = join(target.root, action.path);
|
|
642
|
+
let backup = null;
|
|
643
|
+
if (action.mode === 'replace') {
|
|
644
|
+
backup = backups.keep(dst, true);
|
|
645
|
+
}
|
|
646
|
+
else if (action.mode === 'update') {
|
|
647
|
+
removePath(dst);
|
|
648
|
+
}
|
|
649
|
+
makeParents(target, manifest, dst);
|
|
650
|
+
if (action.type === 'dir') {
|
|
651
|
+
copyTreeFiltered(action.src, dst);
|
|
652
|
+
}
|
|
653
|
+
else {
|
|
654
|
+
copyFileSync(action.src, dst);
|
|
655
|
+
}
|
|
656
|
+
const extra = { digest: action.digest };
|
|
657
|
+
if (backup)
|
|
658
|
+
extra.backup = backup;
|
|
659
|
+
remember(manifest, action, extra);
|
|
660
|
+
}
|
|
661
|
+
function applyHook(target, manifest, backups, action) {
|
|
662
|
+
if (action.mode === 'present') {
|
|
663
|
+
if (action.owned)
|
|
664
|
+
remember(manifest, action, {});
|
|
665
|
+
return;
|
|
666
|
+
}
|
|
667
|
+
const filePath = join(target.root, action.path);
|
|
668
|
+
let backup = null;
|
|
669
|
+
let doc;
|
|
670
|
+
if (action.created) {
|
|
671
|
+
doc = structuredClone(action.root ?? {});
|
|
672
|
+
makeParents(target, manifest, filePath);
|
|
673
|
+
}
|
|
674
|
+
else {
|
|
675
|
+
doc = readJsonObject(filePath) ?? {};
|
|
676
|
+
backup = backups.keep(filePath);
|
|
677
|
+
}
|
|
678
|
+
const hooksObj = isPlainObject(doc.hooks) ? doc.hooks : {};
|
|
679
|
+
doc.hooks = hooksObj;
|
|
680
|
+
const eventName = action.event;
|
|
681
|
+
const list = Array.isArray(hooksObj[eventName]) ? hooksObj[eventName] : [];
|
|
682
|
+
list.push(action.entry);
|
|
683
|
+
hooksObj[eventName] = list;
|
|
684
|
+
writeJsonAtomic(filePath, doc);
|
|
685
|
+
remember(manifest, action, {
|
|
686
|
+
event: eventName,
|
|
687
|
+
entry: action.entry,
|
|
688
|
+
root: action.root,
|
|
689
|
+
created: action.created,
|
|
690
|
+
backup,
|
|
691
|
+
written: fileDigest(filePath),
|
|
692
|
+
});
|
|
693
|
+
}
|
|
694
|
+
function applyAgents(target, manifest, action) {
|
|
695
|
+
if (action.mode === 'present') {
|
|
696
|
+
if (action.owned)
|
|
697
|
+
remember(manifest, action, {});
|
|
698
|
+
return;
|
|
699
|
+
}
|
|
700
|
+
const filePath = join(target.root, 'AGENTS.md');
|
|
701
|
+
const created = !(existsSync(filePath) && statSync(filePath).isFile());
|
|
702
|
+
const text = created ? '' : readFileSync(filePath, 'utf8');
|
|
703
|
+
const separator = !text || text.endsWith('\n\n') ? '' : text.endsWith('\n') ? '\n' : '\n\n';
|
|
704
|
+
writeTextAtomic(filePath, `${text}${separator}${action.block}`);
|
|
705
|
+
remember(manifest, action, {
|
|
706
|
+
key: action.key,
|
|
707
|
+
appended: `${separator}${action.block}`,
|
|
708
|
+
created,
|
|
709
|
+
});
|
|
710
|
+
}
|
|
711
|
+
function printPlan(target, plan) {
|
|
712
|
+
process.stdout.write(`Plan for ${target.scope} ${toPosix(target.root)}:\n`);
|
|
713
|
+
for (const action of plan.actions) {
|
|
714
|
+
let label = action.type !== 'hook' ? action.path : `${action.path} ${action.event ?? ''} -> ${GATE}`;
|
|
715
|
+
if (action.type === 'agents') {
|
|
716
|
+
label = `AGENTS.md rule for ${action.key ?? ''}`;
|
|
717
|
+
}
|
|
718
|
+
const sym = SYMBOL[action.mode] ?? '?';
|
|
719
|
+
process.stdout.write(` ${sym} ${label} (${action.mode}; ${action.clients.join(', ')})\n`);
|
|
720
|
+
}
|
|
721
|
+
}
|
|
722
|
+
function finishInstall(args, target, plan, report, code) {
|
|
723
|
+
if (args.json) {
|
|
724
|
+
process.stdout.write(`${JSON.stringify(report, null, 2)}\n`);
|
|
725
|
+
return code;
|
|
726
|
+
}
|
|
727
|
+
printPlan(target, plan);
|
|
728
|
+
for (const conflict of plan.conflicts) {
|
|
729
|
+
process.stdout.write(`${MARK.fail} conflict: ${conflict}\n`);
|
|
730
|
+
}
|
|
731
|
+
if (report.verdict === 'refused') {
|
|
732
|
+
process.stdout.write(`${MARK.fail} nothing was written\n`);
|
|
733
|
+
return code;
|
|
734
|
+
}
|
|
735
|
+
if (report.verdict === 'planned') {
|
|
736
|
+
process.stdout.write(`${MARK.ok} dry run: nothing was written\n`);
|
|
737
|
+
return code;
|
|
738
|
+
}
|
|
739
|
+
for (const step of plan.manual) {
|
|
740
|
+
process.stdout.write(`${MARK.warn} manual: ${step}\n`);
|
|
741
|
+
}
|
|
742
|
+
const scope = target.scope === 'global' ? '--global' : `--project "${toPosix(target.root)}"`;
|
|
743
|
+
const clientIds = Array.isArray(report.clients) ? report.clients.join(',') : '';
|
|
744
|
+
process.stdout.write(`${MARK.ok} installed. Check it with: bbc-install verify --client ${clientIds} ${scope}\n`);
|
|
745
|
+
return code;
|
|
746
|
+
}
|
|
747
|
+
function runInstall(args) {
|
|
748
|
+
const target = new InstallTarget(args);
|
|
749
|
+
if (!existsSync(target.root) || !statSync(target.root).isDirectory()) {
|
|
750
|
+
throw new UsageError(`${target.root} is not a directory`);
|
|
751
|
+
}
|
|
752
|
+
if (target.root === target.source || isSubPath(target.source, target.root)) {
|
|
753
|
+
throw new UsageError('the target is this collection itself; run from your project or pass --project <dir>');
|
|
754
|
+
}
|
|
755
|
+
const found = discoverBundles(target.source);
|
|
756
|
+
const names = [
|
|
757
|
+
...selectNames(found, 'skill', args.skill),
|
|
758
|
+
...selectNames(found, 'command', args.command),
|
|
759
|
+
];
|
|
760
|
+
const clients = resolveClients(loadClients(target.source), args.client, target);
|
|
761
|
+
if (clients.length === 0) {
|
|
762
|
+
throw new UsageError('no client selected (all-detected found none); pass --client <id>');
|
|
763
|
+
}
|
|
764
|
+
const manifest = loadManifest(target);
|
|
765
|
+
const plan = buildInstallPlan(target, clients, names, found, manifest, args.force);
|
|
766
|
+
const cleanActions = plan.actions.map((a) => ({
|
|
767
|
+
type: a.type,
|
|
768
|
+
path: a.path,
|
|
769
|
+
clients: a.clients,
|
|
770
|
+
mode: a.mode,
|
|
771
|
+
owned: a.owned,
|
|
772
|
+
...(a.digest !== undefined ? { digest: a.digest } : {}),
|
|
773
|
+
...(a.event !== undefined ? { event: a.event } : {}),
|
|
774
|
+
...(a.entry !== undefined ? { entry: a.entry } : {}),
|
|
775
|
+
...(a.created !== undefined ? { created: a.created } : {}),
|
|
776
|
+
...(a.key !== undefined ? { key: a.key } : {}),
|
|
777
|
+
}));
|
|
778
|
+
const report = {
|
|
779
|
+
scope: target.scope,
|
|
780
|
+
root: toPosix(target.root),
|
|
781
|
+
clients: clients.map((c) => c.id),
|
|
782
|
+
actions: cleanActions,
|
|
783
|
+
conflicts: plan.conflicts,
|
|
784
|
+
manual: plan.manual,
|
|
785
|
+
};
|
|
786
|
+
if (plan.conflicts.length > 0) {
|
|
787
|
+
report.verdict = 'refused';
|
|
788
|
+
return finishInstall(args, target, plan, report, 1);
|
|
789
|
+
}
|
|
790
|
+
if (args.dryRun) {
|
|
791
|
+
report.verdict = 'planned';
|
|
792
|
+
return finishInstall(args, target, plan, report, 0);
|
|
793
|
+
}
|
|
794
|
+
const backups = new Backups(target);
|
|
795
|
+
for (const action of plan.actions) {
|
|
796
|
+
if (action.type === 'dir' || action.type === 'file') {
|
|
797
|
+
applyCopy(target, manifest, backups, action);
|
|
798
|
+
}
|
|
799
|
+
else if (action.type === 'hook') {
|
|
800
|
+
applyHook(target, manifest, backups, action);
|
|
801
|
+
}
|
|
802
|
+
else {
|
|
803
|
+
applyAgents(target, manifest, action);
|
|
804
|
+
}
|
|
805
|
+
}
|
|
806
|
+
if (manifest.entries.some((e) => e.type !== 'mkdir')) {
|
|
807
|
+
saveManifest(target, manifest);
|
|
808
|
+
}
|
|
809
|
+
report.verdict = 'installed';
|
|
810
|
+
return finishInstall(args, target, plan, report, 0);
|
|
811
|
+
}
|
|
812
|
+
function installedNames(manifest, client, target) {
|
|
813
|
+
const prefix = `${client.skills[target.scope]}/`;
|
|
814
|
+
const names = [];
|
|
815
|
+
for (const entry of manifest.entries) {
|
|
816
|
+
if (entry.type === 'dir' &&
|
|
817
|
+
Array.isArray(entry.clients) &&
|
|
818
|
+
entry.clients.includes(client.id) &&
|
|
819
|
+
entry.path.startsWith(prefix)) {
|
|
820
|
+
names.push(entry.path.slice(prefix.length));
|
|
821
|
+
}
|
|
822
|
+
}
|
|
823
|
+
return names;
|
|
824
|
+
}
|
|
825
|
+
function checkBundle(target, skillsDir, name, src, out) {
|
|
826
|
+
const dst = join(target.root, skillsDir, name);
|
|
827
|
+
const rel = `${skillsDir}/${name}`;
|
|
828
|
+
if (!existsSync(dst) || !statSync(dst).isDirectory()) {
|
|
829
|
+
out.push(['fail', `${rel} is missing`]);
|
|
830
|
+
return;
|
|
831
|
+
}
|
|
832
|
+
if (existsSync(join(dst, name, 'SKILL.md'))) {
|
|
833
|
+
out.push(['fail', `${rel}/${name} is an extra folder level; move its contents up one level`]);
|
|
834
|
+
}
|
|
835
|
+
if (!existsSync(join(dst, 'SKILL.md'))) {
|
|
836
|
+
out.push(['fail', `${rel}/SKILL.md is missing`]);
|
|
837
|
+
return;
|
|
838
|
+
}
|
|
839
|
+
const srcFiles = [];
|
|
840
|
+
collectFilesFiltered(src, src, srcFiles);
|
|
841
|
+
srcFiles.sort();
|
|
842
|
+
const missing = srcFiles.filter((fileRel) => !existsSync(join(dst, fileRel)));
|
|
843
|
+
if (missing.length > 0) {
|
|
844
|
+
const sample = missing.slice(0, 3).join(', ') + (missing.length > 3 ? ' ...' : '');
|
|
845
|
+
out.push(['fail', `${rel} is incomplete: ${sample}`]);
|
|
846
|
+
return;
|
|
847
|
+
}
|
|
848
|
+
if (treeDigest(dst) !== treeDigest(src)) {
|
|
849
|
+
out.push(['warn', `${rel} differs from the source (edited, or installed from another version)`]);
|
|
850
|
+
return;
|
|
851
|
+
}
|
|
852
|
+
out.push(['ok', rel]);
|
|
853
|
+
}
|
|
854
|
+
function checkNesting(target, skillsDir, out) {
|
|
855
|
+
for (const extra of ['skills', 'commands']) {
|
|
856
|
+
const nested = join(target.root, skillsDir, extra);
|
|
857
|
+
if (!existsSync(nested) || !statSync(nested).isDirectory())
|
|
858
|
+
continue;
|
|
859
|
+
const hasSkill = readdirSync(nested, { withFileTypes: true }).some((child) => child.isDirectory() && existsSync(join(nested, child.name, 'SKILL.md')));
|
|
860
|
+
if (hasSkill) {
|
|
861
|
+
out.push(['fail', `${skillsDir}/${extra}/ is an extra level; skills must sit directly in ${skillsDir}/`]);
|
|
862
|
+
}
|
|
863
|
+
}
|
|
864
|
+
}
|
|
865
|
+
function checkHook(target, client, out) {
|
|
866
|
+
const spec = client.hook;
|
|
867
|
+
const rel = spec.file[target.scope];
|
|
868
|
+
const filePath = join(target.root, rel);
|
|
869
|
+
const doc = existsSync(filePath) && statSync(filePath).isFile() ? readJsonObject(filePath) : null;
|
|
870
|
+
if (!doc) {
|
|
871
|
+
out.push(['fail', `${rel} is missing or not valid JSON`]);
|
|
872
|
+
return;
|
|
873
|
+
}
|
|
874
|
+
const entries = hookEntries(doc, spec.event) ?? [];
|
|
875
|
+
const serialized = JSON.stringify(entries);
|
|
876
|
+
const cmdMatches = Array.from(serialized.matchAll(/"command":\s*"((?:[^"\\]|\\.)*)"/g))
|
|
877
|
+
.map((m) => m[1] ?? '')
|
|
878
|
+
.filter((c) => c.includes(HOOK_MARK));
|
|
879
|
+
if (cmdMatches.length === 0) {
|
|
880
|
+
out.push(['fail', `${rel} has no ${spec.event} hook running ${HOOK_MARK}`]);
|
|
881
|
+
return;
|
|
882
|
+
}
|
|
883
|
+
const command = JSON.parse(`"${cmdMatches[0]}"`);
|
|
884
|
+
const words = splitCommandWords(command);
|
|
885
|
+
const localScript = words.find((w) => /\.(?:py|ts|js|mjs|cjs)$/i.test(w));
|
|
886
|
+
if (localScript) {
|
|
887
|
+
const resolvedScript = localScript.startsWith('/') || /^[A-Za-z]:[\\/]/.test(localScript)
|
|
888
|
+
? localScript
|
|
889
|
+
: join(target.root, localScript);
|
|
890
|
+
if (!existsSync(resolvedScript) || !statSync(resolvedScript).isFile()) {
|
|
891
|
+
out.push(['fail', `${rel} ${spec.event} hook points to a missing script: ${command}`]);
|
|
892
|
+
return;
|
|
893
|
+
}
|
|
894
|
+
}
|
|
895
|
+
out.push(['ok', `${rel} ${spec.event} -> ${command}`]);
|
|
896
|
+
const cmdHead = words[0] ?? '';
|
|
897
|
+
if (cmdHead && !whichCommand(cmdHead)) {
|
|
898
|
+
out.push(['warn', `${cmdHead} is not on PATH, so the hook cannot start (see doctor)`]);
|
|
899
|
+
}
|
|
900
|
+
}
|
|
901
|
+
function checkTriage(target, skillsDir, out) {
|
|
902
|
+
if (!existsSync(join(target.root, skillsDir, GATE, 'SKILL.md')))
|
|
903
|
+
return;
|
|
904
|
+
const sourceCli = join(target.source, 'packages', 'cli', 'src', 'bbc.ts');
|
|
905
|
+
const selfCli = fileURLToPath(new URL('./bbc.ts', import.meta.url));
|
|
906
|
+
const localCli = existsSync(sourceCli) && existsSync(join(target.source, 'node_modules'))
|
|
907
|
+
? sourceCli
|
|
908
|
+
: existsSync(selfCli)
|
|
909
|
+
? selfCli
|
|
910
|
+
: null;
|
|
911
|
+
const argv = localCli
|
|
912
|
+
? [process.execPath, localCli, 'triage-question', '--question', TRIAGE_PROBE]
|
|
913
|
+
: ['bbc', 'triage-question', '--question', TRIAGE_PROBE];
|
|
914
|
+
const res = runExecutable(argv, { timeoutMs: 30_000 });
|
|
915
|
+
const code = res.error ? null : res.code;
|
|
916
|
+
if (code === 1) {
|
|
917
|
+
out.push(['ok', `triage-question blocks "${TRIAGE_PROBE}" (exit 1)`]);
|
|
918
|
+
return;
|
|
919
|
+
}
|
|
920
|
+
out.push(['fail', `triage-question returned ${String(code)} for "${TRIAGE_PROBE}" (expected 1)`]);
|
|
921
|
+
}
|
|
922
|
+
function verifyClient(target, client, found, args, manifest) {
|
|
923
|
+
const out = [];
|
|
924
|
+
const skillsDir = client.skills[target.scope];
|
|
925
|
+
const installed = installedNames(manifest, client, target);
|
|
926
|
+
const names = args.skill === undefined && args.command === undefined && installed.length > 0
|
|
927
|
+
? installed
|
|
928
|
+
: [...selectNames(found, 'skill', args.skill), ...selectNames(found, 'command', args.command)];
|
|
929
|
+
checkNesting(target, skillsDir, out);
|
|
930
|
+
for (const name of names) {
|
|
931
|
+
const bundle = found.get(name);
|
|
932
|
+
if (!bundle) {
|
|
933
|
+
out.push(['warn', `${skillsDir}/${name} is installed but no longer in the source`]);
|
|
934
|
+
continue;
|
|
935
|
+
}
|
|
936
|
+
checkBundle(target, skillsDir, name, bundle.path, out);
|
|
937
|
+
}
|
|
938
|
+
const commandsDir = client.commands_dir?.[target.scope];
|
|
939
|
+
if (commandsDir) {
|
|
940
|
+
for (const name of names) {
|
|
941
|
+
const bundle = found.get(name);
|
|
942
|
+
const adapter = bundle ? join(bundle.path, 'adapters', 'gemini.toml') : null;
|
|
943
|
+
if (adapter && existsSync(adapter) && !existsSync(join(target.root, commandsDir, `${name}.toml`))) {
|
|
944
|
+
out.push(['fail', `${commandsDir}/${name}.toml is missing (copy of adapters/gemini.toml)`]);
|
|
945
|
+
}
|
|
946
|
+
}
|
|
947
|
+
}
|
|
948
|
+
if (names.includes(GATE) && client.hook) {
|
|
949
|
+
checkHook(target, client, out);
|
|
950
|
+
}
|
|
951
|
+
if (names.includes(GATE) && client.agents_md && target.scope === 'project') {
|
|
952
|
+
const agentsPath = join(target.root, 'AGENTS.md');
|
|
953
|
+
const text = existsSync(agentsPath) && statSync(agentsPath).isFile() ? readFileSync(agentsPath, 'utf8') : '';
|
|
954
|
+
const marker = agentsBlock(skillsDir).split('\n')[0] ?? '';
|
|
955
|
+
out.push(text.includes(marker)
|
|
956
|
+
? ['ok', `AGENTS.md rule for ${skillsDir}`]
|
|
957
|
+
: ['warn', `AGENTS.md has no ${GATE} rule for ${skillsDir}`]);
|
|
958
|
+
}
|
|
959
|
+
if (names.includes(GATE)) {
|
|
960
|
+
checkTriage(target, skillsDir, out);
|
|
961
|
+
}
|
|
962
|
+
return {
|
|
963
|
+
client: client.id,
|
|
964
|
+
failed: out.some(([status]) => status === 'fail'),
|
|
965
|
+
checks: out.map(([status, detail]) => ({ status, detail })),
|
|
966
|
+
manual: client.manual.map((step) => `manual: ${step}`),
|
|
967
|
+
};
|
|
968
|
+
}
|
|
969
|
+
function runVerify(args) {
|
|
970
|
+
const target = new InstallTarget(args);
|
|
971
|
+
const found = discoverBundles(target.source);
|
|
972
|
+
const clients = resolveClients(loadClients(target.source), args.client, target);
|
|
973
|
+
const manifest = loadManifest(target);
|
|
974
|
+
const report = clients.map((c) => verifyClient(target, c, found, args, manifest));
|
|
975
|
+
const failed = report.some((row) => row.failed === true);
|
|
976
|
+
if (args.json) {
|
|
977
|
+
process.stdout.write(`${JSON.stringify({ verdict: failed ? 'broken' : 'installed', clients: report }, null, 2)}\n`);
|
|
978
|
+
return failed ? 1 : 0;
|
|
979
|
+
}
|
|
980
|
+
for (let i = 0; i < clients.length; i += 1) {
|
|
981
|
+
const client = clients[i];
|
|
982
|
+
const row = report[i];
|
|
983
|
+
process.stdout.write(`${client.name} (${target.scope} ${toPosix(target.root)}):\n`);
|
|
984
|
+
for (const check of row.checks) {
|
|
985
|
+
process.stdout.write(` ${MARK[check.status]} ${check.detail}\n`);
|
|
986
|
+
}
|
|
987
|
+
for (const step of row.manual) {
|
|
988
|
+
process.stdout.write(` ${MARK.warn} ${step} (not verified by this CLI)\n`);
|
|
989
|
+
}
|
|
990
|
+
}
|
|
991
|
+
process.stdout.write(failed ? `${MARK.fail} broken install\n` : `${MARK.ok} files and hooks verified\n`);
|
|
992
|
+
return failed ? 1 : 0;
|
|
993
|
+
}
|
|
994
|
+
function undoCopy(target, entry, force) {
|
|
995
|
+
const full = join(target.root, entry.path);
|
|
996
|
+
if (existsSync(full) && treeDigest(full) !== entry.digest && !force) {
|
|
997
|
+
return ['fail', `${entry.path} was modified after install; kept (use --force to remove it)`];
|
|
998
|
+
}
|
|
999
|
+
if (entry.backup) {
|
|
1000
|
+
const backup = join(target.root, entry.backup);
|
|
1001
|
+
removePath(full);
|
|
1002
|
+
renameSync(backup, full);
|
|
1003
|
+
return ['ok', `${entry.path} restored from backup`];
|
|
1004
|
+
}
|
|
1005
|
+
removePath(full);
|
|
1006
|
+
return ['ok', `${entry.path} removed`];
|
|
1007
|
+
}
|
|
1008
|
+
function undoHook(target, entry) {
|
|
1009
|
+
const full = join(target.root, entry.path);
|
|
1010
|
+
if (!existsSync(full) || !statSync(full).isFile()) {
|
|
1011
|
+
return ['ok', `${entry.path} already gone`];
|
|
1012
|
+
}
|
|
1013
|
+
if (fileDigest(full) === entry.written) {
|
|
1014
|
+
if (entry.created) {
|
|
1015
|
+
rmSync(full);
|
|
1016
|
+
return ['ok', `${entry.path} removed`];
|
|
1017
|
+
}
|
|
1018
|
+
if (!entry.backup) {
|
|
1019
|
+
return ['fail', `${entry.path} has no recorded backup path`];
|
|
1020
|
+
}
|
|
1021
|
+
const backup = join(target.root, entry.backup);
|
|
1022
|
+
copyFileSync(backup, full);
|
|
1023
|
+
rmSync(backup);
|
|
1024
|
+
return ['ok', `${entry.path} restored from backup`];
|
|
1025
|
+
}
|
|
1026
|
+
const doc = readJsonObject(full);
|
|
1027
|
+
const eventName = entry.event;
|
|
1028
|
+
const entries = doc && eventName ? hookEntries(doc, eventName) : null;
|
|
1029
|
+
if (!doc || !eventName || entries === null) {
|
|
1030
|
+
return ['fail', `${entry.path} is no longer valid JSON; remove the ${GATE} hook by hand`];
|
|
1031
|
+
}
|
|
1032
|
+
const entryJson = JSON.stringify(entry.entry);
|
|
1033
|
+
const kept = entries.filter((item) => JSON.stringify(item) !== entryJson);
|
|
1034
|
+
const hooksObj = isPlainObject(doc.hooks) ? doc.hooks : {};
|
|
1035
|
+
hooksObj[eventName] = kept;
|
|
1036
|
+
if (kept.length === 0) {
|
|
1037
|
+
delete hooksObj[eventName];
|
|
1038
|
+
}
|
|
1039
|
+
if (Object.keys(hooksObj).length === 0) {
|
|
1040
|
+
delete doc.hooks;
|
|
1041
|
+
}
|
|
1042
|
+
if (entry.created && JSON.stringify(doc) === JSON.stringify(entry.root ?? {})) {
|
|
1043
|
+
rmSync(full);
|
|
1044
|
+
return ['ok', `${entry.path} removed`];
|
|
1045
|
+
}
|
|
1046
|
+
writeJsonAtomic(full, doc);
|
|
1047
|
+
return ['ok', `${entry.path}: ${GATE} hook removed, other hooks kept`];
|
|
1048
|
+
}
|
|
1049
|
+
function undoAgents(target, entry) {
|
|
1050
|
+
const full = join(target.root, entry.path);
|
|
1051
|
+
if (!existsSync(full) || !statSync(full).isFile()) {
|
|
1052
|
+
return ['ok', 'AGENTS.md already gone'];
|
|
1053
|
+
}
|
|
1054
|
+
let text = readFileSync(full, 'utf8');
|
|
1055
|
+
const appended = entry.appended ?? '';
|
|
1056
|
+
if (appended && text.endsWith(appended)) {
|
|
1057
|
+
text = text.slice(0, text.length - appended.length);
|
|
1058
|
+
}
|
|
1059
|
+
else if (appended && text.includes(appended)) {
|
|
1060
|
+
text = text.replace(appended, '');
|
|
1061
|
+
}
|
|
1062
|
+
else {
|
|
1063
|
+
return ['warn', `AGENTS.md rule for ${entry.key ?? ''} not found; nothing to remove`];
|
|
1064
|
+
}
|
|
1065
|
+
if (entry.created && !text) {
|
|
1066
|
+
rmSync(full);
|
|
1067
|
+
return ['ok', 'AGENTS.md removed'];
|
|
1068
|
+
}
|
|
1069
|
+
writeTextAtomic(full, text);
|
|
1070
|
+
return ['ok', `AGENTS.md rule for ${entry.key ?? ''} removed`];
|
|
1071
|
+
}
|
|
1072
|
+
function pruneFolders(target, manifest) {
|
|
1073
|
+
const folders = manifest.entries
|
|
1074
|
+
.filter((e) => e.type === 'mkdir')
|
|
1075
|
+
.sort((a, b) => b.path.split('/').length - a.path.split('/').length);
|
|
1076
|
+
const toDrop = new Set();
|
|
1077
|
+
for (const entry of folders) {
|
|
1078
|
+
const full = join(target.root, entry.path);
|
|
1079
|
+
if (existsSync(full) && statSync(full).isDirectory() && readdirSync(full).length === 0) {
|
|
1080
|
+
rmdirSync(full);
|
|
1081
|
+
}
|
|
1082
|
+
if (!existsSync(full)) {
|
|
1083
|
+
toDrop.add(entry);
|
|
1084
|
+
}
|
|
1085
|
+
}
|
|
1086
|
+
manifest.entries = manifest.entries.filter((e) => !toDrop.has(e));
|
|
1087
|
+
}
|
|
1088
|
+
function runUninstall(args) {
|
|
1089
|
+
const target = new InstallTarget(args);
|
|
1090
|
+
const manifest = loadManifest(target);
|
|
1091
|
+
const owned = Array.from(new Set(manifest.entries.flatMap((e) => e.clients ?? []))).sort();
|
|
1092
|
+
const wanted = args.client === 'all-installed'
|
|
1093
|
+
? new Set(owned)
|
|
1094
|
+
: new Set(resolveClients(loadClients(target.source), args.client, target).map((c) => c.id));
|
|
1095
|
+
const results = [];
|
|
1096
|
+
const undone = new Set();
|
|
1097
|
+
for (let i = manifest.entries.length - 1; i >= 0; i -= 1) {
|
|
1098
|
+
const entry = manifest.entries[i];
|
|
1099
|
+
if (entry.type === 'mkdir')
|
|
1100
|
+
continue;
|
|
1101
|
+
const entryClients = entry.clients ?? [];
|
|
1102
|
+
if (!entryClients.some((c) => wanted.has(c)))
|
|
1103
|
+
continue;
|
|
1104
|
+
const remaining = entryClients.filter((c) => !wanted.has(c));
|
|
1105
|
+
if (remaining.length > 0) {
|
|
1106
|
+
if (!args.dryRun)
|
|
1107
|
+
entry.clients = remaining;
|
|
1108
|
+
results.push(['ok', `${entry.path} kept for ${remaining.join(', ')}`]);
|
|
1109
|
+
continue;
|
|
1110
|
+
}
|
|
1111
|
+
if (args.dryRun) {
|
|
1112
|
+
results.push(['ok', `${entry.path} would be removed`]);
|
|
1113
|
+
continue;
|
|
1114
|
+
}
|
|
1115
|
+
const [status, detail] = entry.type === 'hook'
|
|
1116
|
+
? undoHook(target, entry)
|
|
1117
|
+
: entry.type === 'agents'
|
|
1118
|
+
? undoAgents(target, entry)
|
|
1119
|
+
: undoCopy(target, entry, args.force);
|
|
1120
|
+
results.push([status, detail]);
|
|
1121
|
+
if (status !== 'fail')
|
|
1122
|
+
undone.add(i);
|
|
1123
|
+
}
|
|
1124
|
+
if (!args.dryRun) {
|
|
1125
|
+
manifest.entries = manifest.entries.filter((_, idx) => !undone.has(idx));
|
|
1126
|
+
pruneFolders(target, manifest);
|
|
1127
|
+
if (manifest.entries.some((e) => e.type !== 'mkdir')) {
|
|
1128
|
+
saveManifest(target, manifest);
|
|
1129
|
+
}
|
|
1130
|
+
else {
|
|
1131
|
+
removePath(join(target.root, STATE_DIR));
|
|
1132
|
+
}
|
|
1133
|
+
}
|
|
1134
|
+
const failed = results.some(([status]) => status === 'fail');
|
|
1135
|
+
if (args.json) {
|
|
1136
|
+
const verdict = failed ? 'partial' : args.dryRun ? 'planned' : 'removed';
|
|
1137
|
+
process.stdout.write(`${JSON.stringify({ verdict, results: results.map(([status, detail]) => ({ status, detail })) }, null, 2)}\n`);
|
|
1138
|
+
return failed ? 1 : 0;
|
|
1139
|
+
}
|
|
1140
|
+
const display = results.length > 0 ? results : [['ok', 'nothing recorded for these clients']];
|
|
1141
|
+
for (const [status, detail] of display) {
|
|
1142
|
+
process.stdout.write(`${MARK[status]} ${detail}\n`);
|
|
1143
|
+
}
|
|
1144
|
+
return failed ? 1 : 0;
|
|
1145
|
+
}
|
|
1146
|
+
const HELP_TEXT = 'usage: bbc bbc-install [-h] {doctor,detect,install,verify,uninstall} ...\n\n' +
|
|
1147
|
+
'Install this collection into coding agents.\n';
|
|
1148
|
+
function parseInstallArgs(argv) {
|
|
1149
|
+
if (argv.includes('--help') || argv.includes('-h')) {
|
|
1150
|
+
return null;
|
|
1151
|
+
}
|
|
1152
|
+
const [rawAction, ...rest] = argv;
|
|
1153
|
+
if (rawAction !== 'doctor' &&
|
|
1154
|
+
rawAction !== 'detect' &&
|
|
1155
|
+
rawAction !== 'install' &&
|
|
1156
|
+
rawAction !== 'verify' &&
|
|
1157
|
+
rawAction !== 'uninstall') {
|
|
1158
|
+
throw new UsageError(rawAction ? `invalid choice: '${rawAction}' (choose from doctor, detect, install, verify, uninstall)` : 'missing subcommand');
|
|
1159
|
+
}
|
|
1160
|
+
const allowedFlags = new Set(['--home', '--project', '--source', '--json']);
|
|
1161
|
+
const allowedBools = new Set(['--json']);
|
|
1162
|
+
if (rawAction === 'doctor') {
|
|
1163
|
+
allowedFlags.add('--cli');
|
|
1164
|
+
}
|
|
1165
|
+
else if (rawAction === 'install' || rawAction === 'verify' || rawAction === 'uninstall') {
|
|
1166
|
+
allowedFlags.add('--client');
|
|
1167
|
+
allowedFlags.add('--global');
|
|
1168
|
+
allowedBools.add('--global');
|
|
1169
|
+
if (rawAction !== 'uninstall') {
|
|
1170
|
+
allowedFlags.add('--skill');
|
|
1171
|
+
allowedFlags.add('--command');
|
|
1172
|
+
}
|
|
1173
|
+
if (rawAction !== 'verify') {
|
|
1174
|
+
allowedFlags.add('--dry-run');
|
|
1175
|
+
allowedFlags.add('--force');
|
|
1176
|
+
allowedBools.add('--dry-run');
|
|
1177
|
+
allowedBools.add('--force');
|
|
1178
|
+
}
|
|
1179
|
+
if (rawAction === 'install') {
|
|
1180
|
+
allowedFlags.add('--cli');
|
|
1181
|
+
}
|
|
1182
|
+
}
|
|
1183
|
+
const values = {};
|
|
1184
|
+
const flags = new Set();
|
|
1185
|
+
for (let i = 0; i < rest.length; i += 1) {
|
|
1186
|
+
const token = rest[i];
|
|
1187
|
+
if (!allowedFlags.has(token)) {
|
|
1188
|
+
throw new UsageError(`unrecognized arguments: ${token}`);
|
|
1189
|
+
}
|
|
1190
|
+
if (allowedBools.has(token)) {
|
|
1191
|
+
flags.add(token);
|
|
1192
|
+
continue;
|
|
1193
|
+
}
|
|
1194
|
+
const val = rest[i + 1];
|
|
1195
|
+
if (val === undefined) {
|
|
1196
|
+
throw new UsageError(`argument ${token}: expected one argument`);
|
|
1197
|
+
}
|
|
1198
|
+
values[token.slice(2)] = val;
|
|
1199
|
+
i += 1;
|
|
1200
|
+
}
|
|
1201
|
+
return {
|
|
1202
|
+
action: rawAction,
|
|
1203
|
+
home: values.home,
|
|
1204
|
+
project: values.project,
|
|
1205
|
+
source: values.source,
|
|
1206
|
+
json: flags.has('--json'),
|
|
1207
|
+
cliRunner: values.cli,
|
|
1208
|
+
client: values.client,
|
|
1209
|
+
globalScope: flags.has('--global'),
|
|
1210
|
+
skill: values.skill,
|
|
1211
|
+
command: values.command,
|
|
1212
|
+
dryRun: flags.has('--dry-run'),
|
|
1213
|
+
force: flags.has('--force'),
|
|
1214
|
+
};
|
|
1215
|
+
}
|
|
1216
|
+
export function bbcInstall(argv) {
|
|
1217
|
+
let args;
|
|
1218
|
+
try {
|
|
1219
|
+
args = parseInstallArgs(argv);
|
|
1220
|
+
}
|
|
1221
|
+
catch (error) {
|
|
1222
|
+
const msg = error instanceof Error ? error.message : String(error);
|
|
1223
|
+
process.stderr.write(`${HELP_TEXT}bbc bbc-install: error: ${msg}\n`);
|
|
1224
|
+
return 2;
|
|
1225
|
+
}
|
|
1226
|
+
if (args === null) {
|
|
1227
|
+
process.stdout.write(`${HELP_TEXT}\n`);
|
|
1228
|
+
return 0;
|
|
1229
|
+
}
|
|
1230
|
+
try {
|
|
1231
|
+
if (args.action === 'doctor')
|
|
1232
|
+
return runDoctor(args);
|
|
1233
|
+
if (args.action === 'detect')
|
|
1234
|
+
return runDetect(args);
|
|
1235
|
+
if (args.action === 'install')
|
|
1236
|
+
return runInstall(args);
|
|
1237
|
+
if (args.action === 'verify')
|
|
1238
|
+
return runVerify(args);
|
|
1239
|
+
return runUninstall(args);
|
|
1240
|
+
}
|
|
1241
|
+
catch (error) {
|
|
1242
|
+
if (error instanceof UsageError) {
|
|
1243
|
+
process.stderr.write(`${MARK.fail} ${error.message}\n`);
|
|
1244
|
+
return 2;
|
|
1245
|
+
}
|
|
1246
|
+
const msg = error instanceof Error ? error.message : String(error);
|
|
1247
|
+
process.stderr.write(`${MARK.fail} ${msg}\n`);
|
|
1248
|
+
return 1;
|
|
1249
|
+
}
|
|
1250
|
+
}
|