@switchbot/openapi-cli 2.7.2 → 3.1.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 +481 -103
- package/dist/api/client.js +23 -1
- package/dist/commands/agent-bootstrap.js +47 -2
- package/dist/commands/auth.js +354 -0
- package/dist/commands/batch.js +20 -4
- package/dist/commands/capabilities.js +155 -65
- package/dist/commands/config.js +109 -0
- package/dist/commands/daemon.js +367 -0
- package/dist/commands/devices.js +62 -11
- package/dist/commands/doctor.js +417 -8
- package/dist/commands/events.js +3 -3
- package/dist/commands/explain.js +1 -2
- package/dist/commands/health.js +113 -0
- package/dist/commands/install.js +246 -0
- package/dist/commands/mcp.js +888 -7
- package/dist/commands/plan.js +379 -103
- package/dist/commands/policy.js +586 -0
- package/dist/commands/rules.js +875 -0
- package/dist/commands/scenes.js +140 -0
- package/dist/commands/schema.js +0 -2
- package/dist/commands/status-sync.js +131 -0
- package/dist/commands/uninstall.js +237 -0
- package/dist/commands/upgrade-check.js +88 -0
- package/dist/config.js +14 -0
- package/dist/credentials/backends/file.js +101 -0
- package/dist/credentials/backends/linux.js +129 -0
- package/dist/credentials/backends/macos.js +129 -0
- package/dist/credentials/backends/windows.js +215 -0
- package/dist/credentials/keychain.js +88 -0
- package/dist/credentials/prime.js +52 -0
- package/dist/devices/catalog.js +4 -10
- package/dist/index.js +30 -1
- package/dist/install/default-steps.js +257 -0
- package/dist/install/preflight.js +212 -0
- package/dist/install/steps.js +67 -0
- package/dist/lib/command-keywords.js +17 -0
- package/dist/lib/daemon-state.js +46 -0
- package/dist/lib/destructive-mode.js +12 -0
- package/dist/lib/devices.js +1 -1
- package/dist/lib/plan-store.js +68 -0
- package/dist/policy/add-rule.js +124 -0
- package/dist/policy/diff.js +91 -0
- package/dist/policy/examples/policy.example.yaml +99 -0
- package/dist/policy/format.js +57 -0
- package/dist/policy/load.js +61 -0
- package/dist/policy/migrate.js +67 -0
- package/dist/policy/schema/v0.2.json +331 -0
- package/dist/policy/schema.js +18 -0
- package/dist/policy/validate.js +262 -0
- package/dist/rules/action.js +205 -0
- package/dist/rules/audit-query.js +89 -0
- package/dist/rules/conflict-analyzer.js +203 -0
- package/dist/rules/cron-scheduler.js +186 -0
- package/dist/rules/destructive.js +52 -0
- package/dist/rules/engine.js +757 -0
- package/dist/rules/matcher.js +230 -0
- package/dist/rules/pid-file.js +95 -0
- package/dist/rules/quiet-hours.js +45 -0
- package/dist/rules/suggest.js +95 -0
- package/dist/rules/throttle.js +116 -0
- package/dist/rules/types.js +34 -0
- package/dist/rules/webhook-listener.js +223 -0
- package/dist/rules/webhook-token.js +90 -0
- package/dist/status-sync/manager.js +268 -0
- package/dist/utils/audit.js +12 -2
- package/dist/utils/health.js +101 -0
- package/dist/utils/output.js +72 -23
- package/dist/utils/retry.js +81 -0
- package/package.json +12 -4
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `switchbot install` — one-command bootstrap (Phase 3B in-repo).
|
|
3
|
+
*
|
|
4
|
+
* Collapses the 7-step Quickstart (credentials → policy → skill link →
|
|
5
|
+
* doctor verify) into a single orchestrated command with automatic
|
|
6
|
+
* rollback on any step failure. The step library
|
|
7
|
+
* (`src/install/default-steps.ts`) does the heavy lifting; this file
|
|
8
|
+
* composes the steps based on user flags, drives the step runner, and
|
|
9
|
+
* formats the outcome.
|
|
10
|
+
*
|
|
11
|
+
* Design notes:
|
|
12
|
+
* - `switchbot install` assumes the CLI is already on PATH (the user
|
|
13
|
+
* ran `npm i -g @switchbot/openapi-cli` to get here). We do not
|
|
14
|
+
* re-install the CLI from inside itself.
|
|
15
|
+
* - Doctor verification is NOT a step — if it failed, an automatic
|
|
16
|
+
* rollback would destroy good state. Instead we print a "next: run
|
|
17
|
+
* `switchbot doctor`" hint after success.
|
|
18
|
+
*/
|
|
19
|
+
import { InvalidArgumentError } from 'commander';
|
|
20
|
+
import fs from 'node:fs';
|
|
21
|
+
import path from 'node:path';
|
|
22
|
+
import { resolvePolicyPath } from '../policy/load.js';
|
|
23
|
+
import { runInstall } from '../install/steps.js';
|
|
24
|
+
import { runPreflight } from '../install/preflight.js';
|
|
25
|
+
import { stepPromptCredentials, stepWriteKeychain, stepScaffoldPolicy, stepSymlinkSkill, stepDoctorVerify, } from '../install/default-steps.js';
|
|
26
|
+
import { isJsonMode, printJson } from '../utils/output.js';
|
|
27
|
+
import { getActiveProfile } from '../lib/request-context.js';
|
|
28
|
+
import chalk from 'chalk';
|
|
29
|
+
const AGENT_VALUES = ['claude-code', 'cursor', 'copilot', 'none'];
|
|
30
|
+
function parseAgent(value) {
|
|
31
|
+
if (!value)
|
|
32
|
+
return 'claude-code';
|
|
33
|
+
if (!AGENT_VALUES.includes(value)) {
|
|
34
|
+
throw new InvalidArgumentError(`--agent must be one of ${AGENT_VALUES.join(', ')} (got "${value}")`);
|
|
35
|
+
}
|
|
36
|
+
return value;
|
|
37
|
+
}
|
|
38
|
+
function parseSkipList(value) {
|
|
39
|
+
if (!value)
|
|
40
|
+
return new Set();
|
|
41
|
+
return new Set(value
|
|
42
|
+
.split(',')
|
|
43
|
+
.map((s) => s.trim())
|
|
44
|
+
.filter(Boolean));
|
|
45
|
+
}
|
|
46
|
+
function printRecipe(ctx) {
|
|
47
|
+
if (!ctx.skillRecipePrinted)
|
|
48
|
+
return;
|
|
49
|
+
const lines = [];
|
|
50
|
+
lines.push('');
|
|
51
|
+
lines.push(chalk.bold(`Skill-install recipe for agent=${ctx.agent}:`));
|
|
52
|
+
switch (ctx.agent) {
|
|
53
|
+
case 'claude-code':
|
|
54
|
+
lines.push(' # re-run with --skill-path pointing at your local clone of openclaw-switchbot-skill', ' switchbot install --agent claude-code --skill-path /path/to/openclaw-switchbot-skill');
|
|
55
|
+
break;
|
|
56
|
+
case 'cursor':
|
|
57
|
+
lines.push(' # Cursor expects a rules file, not a skill directory. See:', ' # openclaw-switchbot-skill/docs/agents/cursor.md');
|
|
58
|
+
break;
|
|
59
|
+
case 'copilot':
|
|
60
|
+
lines.push(' # Copilot merges instructions into .github/copilot-instructions.md. See:', ' # openclaw-switchbot-skill/docs/agents/copilot.md');
|
|
61
|
+
break;
|
|
62
|
+
case 'none':
|
|
63
|
+
lines.push(' (none — skill step skipped)');
|
|
64
|
+
break;
|
|
65
|
+
}
|
|
66
|
+
console.error(lines.join('\n'));
|
|
67
|
+
}
|
|
68
|
+
function printDryRun(steps, ctx) {
|
|
69
|
+
if (isJsonMode()) {
|
|
70
|
+
printJson({
|
|
71
|
+
dryRun: true,
|
|
72
|
+
profile: ctx.profile,
|
|
73
|
+
agent: ctx.agent,
|
|
74
|
+
skillPath: ctx.skillPath ?? null,
|
|
75
|
+
policyPath: ctx.policyPath,
|
|
76
|
+
steps: steps.map((s) => ({ name: s.name, description: s.description })),
|
|
77
|
+
});
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
console.log(chalk.bold('switchbot install — dry run'));
|
|
81
|
+
console.log(` profile: ${ctx.profile}`);
|
|
82
|
+
console.log(` agent: ${ctx.agent}`);
|
|
83
|
+
console.log(` skill: ${ctx.skillPath ?? '(none — recipe will be printed)'}`);
|
|
84
|
+
console.log(` policy: ${ctx.policyPath}`);
|
|
85
|
+
console.log('');
|
|
86
|
+
console.log(chalk.bold('Steps that would run (in order):'));
|
|
87
|
+
for (const s of steps) {
|
|
88
|
+
console.log(` • ${s.name}${s.description ? ` — ${s.description}` : ''}`);
|
|
89
|
+
}
|
|
90
|
+
console.log('');
|
|
91
|
+
console.log(chalk.dim('No changes made. Re-run without --dry-run to apply.'));
|
|
92
|
+
}
|
|
93
|
+
export function registerInstallCommand(program) {
|
|
94
|
+
program
|
|
95
|
+
.command('install')
|
|
96
|
+
.description('One-command bootstrap: credentials + policy + skill link (rolls back on failure)')
|
|
97
|
+
.option('--agent <name>', `target agent: ${AGENT_VALUES.join(' | ')} (default: claude-code)`)
|
|
98
|
+
.option('--skill-path <dir>', 'local clone of openclaw-switchbot-skill (enables auto-link)')
|
|
99
|
+
.option('--token-file <path>', 'two-line credential file (token, secret); read once and deleted on success')
|
|
100
|
+
.option('--skip <names>', 'comma-separated list of step names to skip (e.g. "scaffold-policy,symlink-skill")')
|
|
101
|
+
.option('--force', 'replace an existing skill symlink pointing at a different path; allow link even without SKILL.md')
|
|
102
|
+
.option('--verify', 'after a successful install, run `switchbot doctor --json` as a warn-only post-check')
|
|
103
|
+
.addHelpText('after', `
|
|
104
|
+
The global --dry-run flag previews the step list without making changes.
|
|
105
|
+
Global --json emits the install report as JSON to stdout.
|
|
106
|
+
|
|
107
|
+
Exit codes:
|
|
108
|
+
0 success
|
|
109
|
+
2 preflight check failed (nothing changed)
|
|
110
|
+
3 step failed; rollback completed
|
|
111
|
+
4 step failed; rollback had residue (see output)
|
|
112
|
+
|
|
113
|
+
Examples:
|
|
114
|
+
# Interactive install, Claude Code skill not linked (recipe printed):
|
|
115
|
+
switchbot install
|
|
116
|
+
|
|
117
|
+
# Full install with skill link:
|
|
118
|
+
switchbot install --skill-path ../openclaw-switchbot-skill
|
|
119
|
+
|
|
120
|
+
# Non-interactive (CI) install:
|
|
121
|
+
printf '%s\\n%s\\n' "$TOKEN" "$SECRET" > /tmp/sb-creds
|
|
122
|
+
switchbot install --token-file /tmp/sb-creds --skill-path ./skill
|
|
123
|
+
`)
|
|
124
|
+
.action(async (opts, command) => {
|
|
125
|
+
const agent = parseAgent(opts.agent);
|
|
126
|
+
const profile = getActiveProfile() ?? 'default';
|
|
127
|
+
const skip = parseSkipList(opts.skip);
|
|
128
|
+
const skillPath = opts.skillPath ? path.resolve(opts.skillPath) : undefined;
|
|
129
|
+
const tokenFile = opts.tokenFile ? path.resolve(opts.tokenFile) : undefined;
|
|
130
|
+
const force = Boolean(opts.force);
|
|
131
|
+
const verify = Boolean(opts.verify);
|
|
132
|
+
const globalOpts = command.parent?.opts() ?? {};
|
|
133
|
+
const dryRun = Boolean(globalOpts.dryRun);
|
|
134
|
+
// Pre-flight: read-only checks, never mutate anything.
|
|
135
|
+
const pf = await runPreflight({
|
|
136
|
+
agent,
|
|
137
|
+
expectSkillLink: agent === 'claude-code' && Boolean(skillPath),
|
|
138
|
+
});
|
|
139
|
+
if (!pf.ok) {
|
|
140
|
+
if (isJsonMode()) {
|
|
141
|
+
printJson({ ok: false, stage: 'preflight', preflight: pf });
|
|
142
|
+
}
|
|
143
|
+
else {
|
|
144
|
+
console.error(chalk.red('✗ preflight failed — nothing changed'));
|
|
145
|
+
for (const c of pf.checks) {
|
|
146
|
+
const mark = c.status === 'fail' ? chalk.red('✗') : c.status === 'warn' ? chalk.yellow('!') : chalk.green('✓');
|
|
147
|
+
console.error(` ${mark} ${c.name}: ${c.message}`);
|
|
148
|
+
if (c.hint)
|
|
149
|
+
console.error(` hint: ${c.hint}`);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
process.exit(2);
|
|
153
|
+
}
|
|
154
|
+
const ctx = {
|
|
155
|
+
profile,
|
|
156
|
+
agent,
|
|
157
|
+
skillPath,
|
|
158
|
+
tokenFile,
|
|
159
|
+
policyPath: resolvePolicyPath(),
|
|
160
|
+
nonInteractive: !process.stdin.isTTY && !tokenFile,
|
|
161
|
+
};
|
|
162
|
+
const allSteps = [
|
|
163
|
+
stepPromptCredentials(),
|
|
164
|
+
stepWriteKeychain(),
|
|
165
|
+
stepScaffoldPolicy(),
|
|
166
|
+
stepSymlinkSkill({ force }),
|
|
167
|
+
];
|
|
168
|
+
const steps = allSteps.filter((s) => !skip.has(s.name));
|
|
169
|
+
if (dryRun) {
|
|
170
|
+
printDryRun(steps, ctx);
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
const report = await runInstall(steps, { context: ctx });
|
|
174
|
+
// Delete the token file now that credentials are committed.
|
|
175
|
+
if (report.ok && tokenFile) {
|
|
176
|
+
try {
|
|
177
|
+
fs.unlinkSync(tokenFile);
|
|
178
|
+
}
|
|
179
|
+
catch {
|
|
180
|
+
// non-fatal: credentials are already in the keychain
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
// A7: opt-in post-install verification. Doctor is NEVER part of the
|
|
184
|
+
// rollback chain — a failing doctor after a good install would
|
|
185
|
+
// destroy working state. So we run it AFTER runInstall resolves, as
|
|
186
|
+
// a warn-only check. The outcome is reported but never flips the
|
|
187
|
+
// command's exit code.
|
|
188
|
+
if (report.ok && verify) {
|
|
189
|
+
const cliPath = process.argv[1] ?? '';
|
|
190
|
+
const step = stepDoctorVerify({ cliPath });
|
|
191
|
+
await step.execute(ctx);
|
|
192
|
+
}
|
|
193
|
+
if (isJsonMode()) {
|
|
194
|
+
printJson({
|
|
195
|
+
ok: report.ok,
|
|
196
|
+
profile: ctx.profile,
|
|
197
|
+
agent: ctx.agent,
|
|
198
|
+
report,
|
|
199
|
+
preflight: pf,
|
|
200
|
+
policyPath: ctx.policyPath,
|
|
201
|
+
policyScaffolded: ctx.policyScaffoldResult && !ctx.policyScaffoldResult.skipped,
|
|
202
|
+
skillLinkPath: ctx.skillLinkPath,
|
|
203
|
+
skillLinkCreated: Boolean(ctx.skillLinkCreated),
|
|
204
|
+
verify: verify ? { ok: ctx.doctorOk ?? null, report: ctx.doctorReport ?? null } : undefined,
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
else if (report.ok) {
|
|
208
|
+
console.log(chalk.green('✓ install complete'));
|
|
209
|
+
if (ctx.skillLinkCreated)
|
|
210
|
+
console.log(` linked skill: ${ctx.skillLinkPath}`);
|
|
211
|
+
if (ctx.policyScaffoldResult?.skipped === false)
|
|
212
|
+
console.log(` wrote policy: ${ctx.policyScaffoldResult.policyPath}`);
|
|
213
|
+
printRecipe(ctx);
|
|
214
|
+
if (verify) {
|
|
215
|
+
if (ctx.doctorOk) {
|
|
216
|
+
console.log(chalk.green('✓ doctor --json: all green'));
|
|
217
|
+
}
|
|
218
|
+
else {
|
|
219
|
+
console.log(chalk.yellow('! doctor --json reported issues — install is committed; run `switchbot doctor` to inspect'));
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
console.log('');
|
|
223
|
+
console.log(chalk.bold('Next:'));
|
|
224
|
+
console.log(' switchbot doctor # verify the setup');
|
|
225
|
+
console.log(' switchbot devices list # smoke test');
|
|
226
|
+
}
|
|
227
|
+
else {
|
|
228
|
+
console.error(chalk.red(`✗ install failed at step: ${report.failedAt}`));
|
|
229
|
+
const residue = report.outcomes.some((o) => o.status === 'rollback-failed');
|
|
230
|
+
for (const o of report.outcomes) {
|
|
231
|
+
const tag = o.status === 'succeeded' ? chalk.green('✓') :
|
|
232
|
+
o.status === 'failed' ? chalk.red('✗') :
|
|
233
|
+
o.status === 'rolled-back' ? chalk.yellow('↺') :
|
|
234
|
+
o.status === 'rollback-failed' ? chalk.red('!!') :
|
|
235
|
+
chalk.dim('·');
|
|
236
|
+
const msg = o.status === 'failed' || o.status === 'rollback-failed' ? ` — ${o.error}` : '';
|
|
237
|
+
console.error(` ${tag} ${o.step} [${o.status}]${msg}`);
|
|
238
|
+
}
|
|
239
|
+
if (residue) {
|
|
240
|
+
console.error(chalk.red('Rollback left residue. Run `switchbot uninstall` to clean up or review output above.'));
|
|
241
|
+
process.exit(4);
|
|
242
|
+
}
|
|
243
|
+
process.exit(3);
|
|
244
|
+
}
|
|
245
|
+
});
|
|
246
|
+
}
|