@stagepass/cli 1.0.1 → 2.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.
Files changed (48) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +216 -0
  3. package/TESTING-macos.md +58 -0
  4. package/bin/index.js +2 -76
  5. package/package.json +10 -4
  6. package/src/cli.js +320 -0
  7. package/src/commands/autostart.js +85 -0
  8. package/src/commands/certs.js +185 -0
  9. package/src/commands/daemon.js +126 -0
  10. package/src/commands/doctor.js +56 -0
  11. package/src/commands/link.js +145 -57
  12. package/src/commands/new.js +368 -0
  13. package/src/commands/npm-run.js +84 -0
  14. package/src/commands/reload.js +14 -11
  15. package/src/commands/start.js +21 -4
  16. package/src/commands/stop.js +15 -0
  17. package/src/commands/unlink.js +17 -30
  18. package/src/commands/upgrade.js +198 -0
  19. package/src/lib/autostart.js +360 -0
  20. package/src/lib/caddyfile.js +338 -0
  21. package/src/lib/cdn-snippets.js +27 -0
  22. package/src/lib/certs.js +476 -0
  23. package/src/lib/doctor.js +503 -0
  24. package/src/lib/domain.js +125 -0
  25. package/src/lib/exec.js +71 -0
  26. package/src/lib/migrate.js +96 -0
  27. package/src/lib/origins.js +160 -0
  28. package/src/lib/paths.js +132 -0
  29. package/src/lib/platform.js +47 -0
  30. package/src/lib/project-name.js +47 -0
  31. package/src/lib/projects.js +181 -0
  32. package/src/lib/remote-url.js +63 -0
  33. package/src/lib/scaffold.js +186 -0
  34. package/src/lib/snippets.js +83 -0
  35. package/src/lib/spawn.js +30 -0
  36. package/templates/project/AGENTS.md.tmpl +37 -0
  37. package/templates/project/README.md.tmpl +44 -0
  38. package/templates/project/jsconfig.json.tmpl +14 -0
  39. package/templates/project/scripts/build.mjs +110 -0
  40. package/templates/project/src/main.js.tmpl +29 -0
  41. package/templates/project/src/main.ts.tmpl +29 -0
  42. package/templates/project/src/styles.css.tmpl +8 -0
  43. package/templates/project/src/styles.scss.tmpl +8 -0
  44. package/templates/project/stagepass.json.tmpl +8 -0
  45. package/templates/project/tsconfig.json.tmpl +13 -0
  46. package/src/commands/__tests__/link.test.js +0 -113
  47. package/src/commands/__tests__/reload.test.js +0 -72
  48. package/src/commands/__tests__/unlink.test.js +0 -90
package/src/cli.js ADDED
@@ -0,0 +1,320 @@
1
+ import { createRequire } from 'module';
2
+ import { Command } from 'commander';
3
+ import chalk from 'chalk';
4
+ import { setup as defaultSetup } from './commands/setup.js';
5
+ import { link as defaultLink } from './commands/link.js';
6
+ import { unlink as defaultUnlink } from './commands/unlink.js';
7
+ import { start as defaultStart } from './commands/start.js';
8
+ import { stop as defaultStop } from './commands/stop.js';
9
+ import { reload as defaultReload } from './commands/reload.js';
10
+ import { doctor as defaultDoctor } from './commands/doctor.js';
11
+ import {
12
+ certsCheck as defaultCertsCheck,
13
+ certsTrust as defaultCertsTrust,
14
+ } from './commands/certs.js';
15
+ import { autostart as defaultAutostart } from './commands/autostart.js';
16
+ import { daemon as defaultDaemon } from './commands/daemon.js';
17
+ import { upgrade as defaultUpgrade } from './commands/upgrade.js';
18
+ import {
19
+ watch as defaultWatch,
20
+ build as defaultBuild,
21
+ } from './commands/npm-run.js';
22
+ import { createNew as defaultCreateNew } from './commands/new.js';
23
+ import { assertSupportedPlatform } from './lib/platform.js';
24
+
25
+ const require = createRequire(import.meta.url);
26
+ const { version: packageVersion } = require('../package.json');
27
+
28
+ function collectRepeatable(value, previous) {
29
+ return previous.concat([value]);
30
+ }
31
+
32
+ /**
33
+ * Commander reserves root `--version` for the CLI version. Rewrite
34
+ * `stagepass upgrade --version <x>` to `--target` so the subcommand receives it.
35
+ * `stagepass --version` is unchanged.
36
+ *
37
+ * @param {string[]} argv
38
+ * @returns {string[]}
39
+ */
40
+ export function rewriteArgvForUpgrade(argv) {
41
+ const out = [...argv];
42
+ // Prefer the first bare `upgrade` token after node/script.
43
+ let idx = -1;
44
+ for (let i = 2; i < out.length; i++) {
45
+ if (out[i] === 'upgrade') {
46
+ idx = i;
47
+ break;
48
+ }
49
+ if (!String(out[i]).startsWith('-')) break;
50
+ }
51
+ if (idx === -1) return out;
52
+
53
+ for (let i = idx + 1; i < out.length; i++) {
54
+ if (out[i] === '--version' && out[i + 1] && !String(out[i + 1]).startsWith('-')) {
55
+ out[i] = '--target';
56
+ break;
57
+ }
58
+ if (out[i] === '--') break;
59
+ }
60
+ return out;
61
+ }
62
+
63
+ /**
64
+ * Build the Commander program (no parse). Injectable handlers for unit tests.
65
+ *
66
+ * @param {object} [deps]
67
+ * @param {typeof defaultSetup} [deps.setup]
68
+ * @param {typeof defaultLink} [deps.link]
69
+ * @param {typeof defaultUnlink} [deps.unlink]
70
+ * @param {typeof defaultStart} [deps.start]
71
+ * @param {typeof defaultStop} [deps.stop]
72
+ * @param {typeof defaultReload} [deps.reload]
73
+ * @param {typeof defaultDoctor} [deps.doctor]
74
+ * @param {typeof defaultCertsCheck} [deps.certsCheck]
75
+ * @param {typeof defaultCertsTrust} [deps.certsTrust]
76
+ * @param {typeof defaultAutostart} [deps.autostart]
77
+ * @param {typeof defaultDaemon} [deps.daemon]
78
+ * @param {typeof defaultUpgrade} [deps.upgrade]
79
+ * @param {typeof defaultWatch} [deps.watch]
80
+ * @param {typeof defaultBuild} [deps.build]
81
+ * @param {typeof defaultCreateNew} [deps.createNew]
82
+ * @param {string} [deps.version]
83
+ * @param {() => void} [deps.assertPlatform]
84
+ */
85
+ export function createProgram(deps = {}) {
86
+ const setup = deps.setup || defaultSetup;
87
+ const link = deps.link || defaultLink;
88
+ const unlink = deps.unlink || defaultUnlink;
89
+ const start = deps.start || defaultStart;
90
+ const stop = deps.stop || defaultStop;
91
+ const reload = deps.reload || defaultReload;
92
+ const doctor = deps.doctor || defaultDoctor;
93
+ const certsCheck = deps.certsCheck || defaultCertsCheck;
94
+ const certsTrust = deps.certsTrust || defaultCertsTrust;
95
+ const autostart = deps.autostart || defaultAutostart;
96
+ const daemon = deps.daemon || defaultDaemon;
97
+ const upgrade = deps.upgrade || defaultUpgrade;
98
+ const watch = deps.watch || defaultWatch;
99
+ const build = deps.build || defaultBuild;
100
+ const createNew = deps.createNew || defaultCreateNew;
101
+ const version = deps.version || packageVersion;
102
+ const assertPlatform =
103
+ deps.assertPlatform || (() => assertSupportedPlatform());
104
+
105
+ const program = new Command();
106
+
107
+ program
108
+ .name('stagepass')
109
+ .description('The missing link between Webflow and local dev.')
110
+ .version(version);
111
+
112
+ // Runs before every system command action. --help / --version never hit this.
113
+ program.hook('preAction', () => {
114
+ assertPlatform();
115
+ });
116
+
117
+ program
118
+ .command('setup')
119
+ .description('Install dependencies (Caddy, PHP, Dnsmasq) and configure .sp domain')
120
+ .option('-v, --verbose', 'Show detailed installation logs')
121
+ .action(async (options) => {
122
+ try {
123
+ await setup(options);
124
+ } catch (error) {
125
+ console.error(chalk.red('\nSetup failed.'));
126
+ if (options.verbose) console.error(error);
127
+ else console.error(chalk.dim('Run with -v for details.'));
128
+ }
129
+ });
130
+
131
+ program
132
+ .command('link')
133
+ .description('Link current directory to a .sp domain')
134
+ .argument('[domain]', 'Domain name (optional)')
135
+ .option('-v, --verbose', 'Show Caddy reload output')
136
+ .option(
137
+ '--origin <host>',
138
+ 'Allow CORS from this live hostname (repeatable; e.g. example.com or *.example.com)',
139
+ collectRepeatable,
140
+ []
141
+ )
142
+ .option(
143
+ '--remove-origin <host>',
144
+ 'Remove a previously stored CORS hostname (repeatable)',
145
+ collectRepeatable,
146
+ []
147
+ )
148
+ .option(
149
+ '--allow-all-origins',
150
+ 'Allow CORS from any Origin (Access-Control-Allow-Origin: *). Insecure; prefer --origin.'
151
+ )
152
+ .option(
153
+ '--no-allow-all-origins',
154
+ 'Disable allow-all CORS and use the origin allowlist'
155
+ )
156
+ .action(async (domain, options) => {
157
+ await link(domain, options);
158
+ });
159
+
160
+ program
161
+ .command('unlink')
162
+ .description('Unlink current directory from .sp domain')
163
+ .argument('[domain]', 'Domain name (optional, defaults to current folder name)')
164
+ .option('-v, --verbose', 'Show Caddy reload output')
165
+ .action(async (domain, options) => {
166
+ await unlink(domain, options);
167
+ });
168
+
169
+ program
170
+ .command('reload')
171
+ .description('Reload Caddy configuration')
172
+ .option('-v, --verbose', 'Show Caddy reload output')
173
+ .action(async (options) => {
174
+ await reload(options);
175
+ });
176
+
177
+ program
178
+ .command('start')
179
+ .description('Start background services (Caddy & PHP)')
180
+ .option('-v, --verbose', 'Show detailed Caddy server logs')
181
+ .action(async (options) => {
182
+ await start(options);
183
+ });
184
+
185
+ program
186
+ .command('stop')
187
+ .description('Stop all background services')
188
+ .option('-v, --verbose', 'Show detailed stop logs')
189
+ .action(async (options) => {
190
+ await stop(options);
191
+ });
192
+
193
+ // --- Package 2: CLI operations (doctor, certs, autostart, daemon) ---
194
+ // Keep this block self-contained for easy merges with other cli.js changes.
195
+ program
196
+ .command('doctor')
197
+ .description('Diagnose the local Stagepass stack (read-only; never uses sudo)')
198
+ .option('-v, --verbose', 'Show extra detail for ok checks')
199
+ .option('--json', 'Machine-readable JSON output')
200
+ .action(async (options) => {
201
+ const code = await doctor(options);
202
+ process.exitCode = code;
203
+ });
204
+
205
+ const certsCmd = program
206
+ .command('certs')
207
+ .description('Certificate helpers for the Caddy local CA');
208
+
209
+ certsCmd
210
+ .command('check')
211
+ .description('Check Caddy local CA validity, trust, and optional TLS handshakes (read-only)')
212
+ .option('-v, --verbose', 'Show detailed output')
213
+ .option('--json', 'Machine-readable JSON output')
214
+ .option('--skip-handshake', 'Skip per-domain TLS handshake checks')
215
+ .action(async (options) => {
216
+ const code = await certsCheck(options);
217
+ process.exitCode = code;
218
+ });
219
+
220
+ certsCmd
221
+ .command('trust')
222
+ .description('Trust the Caddy local root CA via `caddy trust` (confirmation required)')
223
+ .option('-y, --yes', 'Skip interactive confirmation')
224
+ .option('-v, --verbose', 'Show detailed output')
225
+ .action(async (options) => {
226
+ const code = await certsTrust(options);
227
+ process.exitCode = code;
228
+ });
229
+
230
+ program
231
+ .command('autostart')
232
+ .description('Manage login LaunchAgent for Stagepass (macOS)')
233
+ .argument('<action>', 'on | off | status')
234
+ .option('-v, --verbose', 'Show detailed output')
235
+ .option('--json', 'JSON output for status')
236
+ .action(async (action, options) => {
237
+ const normalized = String(action || '').toLowerCase();
238
+ if (!['on', 'off', 'status'].includes(normalized)) {
239
+ console.error(chalk.red('Usage: stagepass autostart on|off|status'));
240
+ process.exitCode = 1;
241
+ return;
242
+ }
243
+ const code = await autostart(normalized, options);
244
+ process.exitCode = code;
245
+ });
246
+
247
+ program
248
+ .command('daemon')
249
+ .description('Foreground daemon for LaunchAgent (non-interactive; never uses sudo)')
250
+ .action(async () => {
251
+ const code = await daemon();
252
+ process.exitCode = code;
253
+ });
254
+
255
+ // --- Package 3: project scaffold / npm wrappers / upgrade ---
256
+ // Keep this block self-contained for easy merges with other cli.js changes.
257
+ program
258
+ .command('upgrade')
259
+ .description(
260
+ 'Bump @stagepass/* in the current package.json and print Webflow snippets'
261
+ )
262
+ .option('--dry-run', 'Show changes without writing package.json')
263
+ .option(
264
+ '--target <version>',
265
+ 'Target version (default: this CLI version; dist-tags use npm view)'
266
+ )
267
+ // Public docs use `--version`; rewriteArgvForUpgrade maps it to `--target`
268
+ // so it does not collide with the root `stagepass --version` handler.
269
+ .option('-v, --verbose', 'Show detailed output')
270
+ .action(async (options) => {
271
+ const code = await upgrade({
272
+ ...options,
273
+ version: options.target,
274
+ });
275
+ process.exitCode = code;
276
+ });
277
+
278
+ program
279
+ .command('watch')
280
+ .description('Run `npm run watch` in the current project')
281
+ .option('-v, --verbose', 'Show the underlying npm command')
282
+ .action(async (options) => {
283
+ const code = await watch(options);
284
+ process.exitCode = code;
285
+ });
286
+
287
+ program
288
+ .command('build')
289
+ .description('Run `npm run build` in the current project')
290
+ .option('-v, --verbose', 'Show the underlying npm command')
291
+ .action(async (options) => {
292
+ const code = await build(options);
293
+ process.exitCode = code;
294
+ });
295
+
296
+ program
297
+ .command('new')
298
+ .description('Scaffold a new Stagepass Webflow project')
299
+ .argument('[name]', 'DNS-safe project name')
300
+ .option('--dir <dir>', 'Target directory (default: ./<name>)')
301
+ .option('--author <author>', 'Author for package.json (default: git user.name)')
302
+ .option('--lang <lang>', 'ts or js (default: ts)')
303
+ .option('--styles <styles>', 'scss or css (default: scss)')
304
+ .option('--modules', 'Include @stagepass/modules')
305
+ .option('--no-modules', 'Skip @stagepass/modules')
306
+ .option('--git', 'Initialize a git repository')
307
+ .option('--no-git', 'Do not initialize git')
308
+ .option('--remote <url>', 'Git remote URL (requires git)')
309
+ .option('--install', 'Run npm install after scaffolding')
310
+ .option('--no-install', 'Do not run npm install (default)')
311
+ .option('-y, --yes', 'Non-interactive; accept defaults for omitted flags')
312
+ .option('--dry-run', 'Print the file plan without writing')
313
+ .option('-v, --verbose', 'Show detailed output')
314
+ .action(async (name, options) => {
315
+ const code = await createNew(name, options);
316
+ process.exitCode = code;
317
+ });
318
+
319
+ return program;
320
+ }
@@ -0,0 +1,85 @@
1
+ import chalk from 'chalk';
2
+ import {
3
+ enableAutostart,
4
+ disableAutostart,
5
+ getAutostartStatus,
6
+ LAUNCH_AGENT_LABEL,
7
+ } from '../lib/autostart.js';
8
+
9
+ /**
10
+ * @param {'on'|'off'|'status'} action
11
+ * @param {object} [options]
12
+ * @param {boolean} [options.verbose]
13
+ * @param {boolean} [options.json]
14
+ * @returns {Promise<number>}
15
+ */
16
+ export async function autostart(action, options = {}) {
17
+ if (action === 'status') {
18
+ const status = await getAutostartStatus();
19
+ if (options.json) {
20
+ console.log(
21
+ JSON.stringify(
22
+ {
23
+ label: status.label,
24
+ plistPath: status.plistPath,
25
+ plistExists: status.plistExists,
26
+ loaded: status.loaded,
27
+ logsDir: status.logsDir,
28
+ },
29
+ null,
30
+ 2
31
+ )
32
+ );
33
+ } else {
34
+ console.log(chalk.bold('\nStagepass autostart status\n'));
35
+ console.log(`Label: ${status.label}`);
36
+ console.log(`Plist: ${status.plistPath}`);
37
+ console.log(`Exists: ${status.plistExists ? 'yes' : 'no'}`);
38
+ console.log(`Loaded: ${status.loaded ? 'yes' : 'no'}`);
39
+ console.log(`Logs: ${status.logsDir}`);
40
+ if (!status.loaded && status.printError && options.verbose) {
41
+ console.log(chalk.dim(`launchctl: ${status.printError}`));
42
+ }
43
+ console.log(
44
+ chalk.dim(
45
+ '\nNote: the LaunchAgent runs as your user (same as `caddy start` in stagepass start).'
46
+ )
47
+ );
48
+ console.log(
49
+ chalk.dim(
50
+ 'If port 443 bind fails, daemon logs the error and exits 0 so launchd KeepAlive does not restart-loop. Use stagepass start for interactive recovery.'
51
+ )
52
+ );
53
+ }
54
+ return 0;
55
+ }
56
+
57
+ if (action === 'on') {
58
+ console.log(chalk.bold('\nEnabling Stagepass autostart...'));
59
+ const result = await enableAutostart();
60
+ if (!result.ok) {
61
+ console.log(chalk.red(`Failed: ${result.error}`));
62
+ return 1;
63
+ }
64
+ console.log(chalk.green(`LaunchAgent installed: ${result.plistPath}`));
65
+ console.log(chalk.dim(`Label: ${LAUNCH_AGENT_LABEL}`));
66
+ console.log(chalk.dim(`Node: ${result.paths.nodePath}`));
67
+ console.log(chalk.dim(`CLI: ${result.paths.cliPath}`));
68
+ console.log(
69
+ chalk.dim(
70
+ 'Runs `stagepass daemon` at login (caddy run in the foreground). Never uses sudo.'
71
+ )
72
+ );
73
+ return 0;
74
+ }
75
+
76
+ if (action === 'off') {
77
+ console.log(chalk.bold('\nDisabling Stagepass autostart...'));
78
+ const result = await disableAutostart();
79
+ console.log(chalk.green(`LaunchAgent removed: ${result.plistPath}`));
80
+ return 0;
81
+ }
82
+
83
+ console.log(chalk.red(`Unknown autostart action: ${action}`));
84
+ return 1;
85
+ }
@@ -0,0 +1,185 @@
1
+ import chalk from 'chalk';
2
+ import inquirer from 'inquirer';
3
+ import { defaultExec } from '../lib/exec.js';
4
+ import { loadProjects } from '../lib/projects.js';
5
+ import { getProjectsPath } from '../lib/paths.js';
6
+ import { runCertsCheck, assertNoDestructiveCertOps } from '../lib/certs.js';
7
+
8
+ /**
9
+ * Read-only certificate report.
10
+ * @param {object} [options]
11
+ * @param {boolean} [options.json]
12
+ * @param {boolean} [options.verbose]
13
+ * @param {boolean} [options.skipHandshake]
14
+ * @returns {Promise<number>}
15
+ */
16
+ export async function certsCheck(options = {}) {
17
+ assertNoDestructiveCertOps();
18
+
19
+ const projectsPath = getProjectsPath();
20
+ const doc = await loadProjects(projectsPath);
21
+ const domains = Object.keys(doc.projects || {});
22
+
23
+ const report = await runCertsCheck({
24
+ domains: options.skipHandshake ? [] : domains,
25
+ skipHandshake: options.skipHandshake === true,
26
+ });
27
+
28
+ let exitCode = 0;
29
+ if (report.root.status === 'fail') exitCode = 1;
30
+ if (report.trust && !report.trust.trusted) exitCode = 1;
31
+ for (const hs of report.handshakes || []) {
32
+ if (hs.status === 'fail') exitCode = 1;
33
+ }
34
+
35
+ if (options.json) {
36
+ console.log(
37
+ JSON.stringify(
38
+ {
39
+ ok: exitCode === 0,
40
+ root: {
41
+ status: report.root.status,
42
+ path: report.root.path,
43
+ reason: report.root.reason,
44
+ fingerprint256: report.root.fingerprint256,
45
+ daysRemaining: report.root.validity?.daysRemaining ?? null,
46
+ },
47
+ trust: {
48
+ trusted: Boolean(report.trust?.trusted),
49
+ error: report.trust?.error || null,
50
+ orphans: report.orphans || [],
51
+ },
52
+ secondaryRoots: report.secondaryRoots || [],
53
+ handshakes: report.handshakes || [],
54
+ },
55
+ null,
56
+ 2
57
+ )
58
+ );
59
+ return exitCode;
60
+ }
61
+
62
+ console.log(chalk.bold('\nStagepass certs check\n'));
63
+
64
+ const rootTag =
65
+ report.root.status === 'ok'
66
+ ? chalk.green('ok')
67
+ : report.root.status === 'warn'
68
+ ? chalk.yellow('warn')
69
+ : chalk.red('fail');
70
+ console.log(`[${rootTag}] Root CA: ${report.root.reason}`);
71
+ console.log(chalk.dim(` ${report.root.path}`));
72
+ if (report.root.fingerprint256) {
73
+ console.log(chalk.dim(` SHA-256 ${report.root.fingerprint256}`));
74
+ }
75
+
76
+ if (report.trust?.trusted) {
77
+ console.log(`[${chalk.green('ok')}] System keychain trusts this root`);
78
+ } else {
79
+ console.log(
80
+ `[${chalk.red('fail')}] System keychain does not trust this root`
81
+ );
82
+ if (report.trust?.error) {
83
+ console.log(chalk.dim(` ${report.trust.error}`));
84
+ }
85
+ console.log(chalk.dim(' Fix: stagepass certs trust'));
86
+ }
87
+
88
+ if (report.orphans?.length) {
89
+ console.log(
90
+ `[${chalk.yellow('warn')}] Orphaned Caddy roots in keychain (report only; never deleted):`
91
+ );
92
+ for (const orphan of report.orphans) {
93
+ console.log(
94
+ chalk.dim(
95
+ ` SHA-1 ${orphan.sha1 || 'unknown'} SHA-256 ${orphan.sha256}`
96
+ )
97
+ );
98
+ }
99
+ } else {
100
+ console.log(`[${chalk.green('ok')}] No orphaned Caddy roots reported`);
101
+ }
102
+
103
+ for (const secondary of report.secondaryRoots || []) {
104
+ console.log(`[${chalk.yellow('warn')}] ${secondary.note}`);
105
+ console.log(chalk.dim(` ${secondary.path}`));
106
+ }
107
+
108
+ if ((report.handshakes || []).length === 0) {
109
+ console.log(chalk.dim('TLS handshake: skipped (no linked domains or --skip-handshake)'));
110
+ } else {
111
+ for (const hs of report.handshakes) {
112
+ const tag =
113
+ hs.status === 'ok'
114
+ ? chalk.green('ok')
115
+ : hs.status === 'warn'
116
+ ? chalk.yellow('warn')
117
+ : chalk.red('fail');
118
+ console.log(`[${tag}] TLS ${hs.servername}: ${hs.reason}`);
119
+ }
120
+ }
121
+
122
+ console.log(chalk.bold(`\nExit ${exitCode}`));
123
+ return exitCode;
124
+ }
125
+
126
+ /**
127
+ * Run `caddy trust` after explicit confirmation (or --yes).
128
+ * @param {object} [options]
129
+ * @param {boolean} [options.yes]
130
+ * @param {boolean} [options.verbose]
131
+ * @param {import('../lib/exec.js').ExecFn} [options.exec]
132
+ * @param {(q: object[]) => Promise<object>} [options.prompt]
133
+ * @returns {Promise<number>}
134
+ */
135
+ export async function certsTrust(options = {}) {
136
+ assertNoDestructiveCertOps();
137
+ const exec = options.exec || defaultExec;
138
+ const prompt = options.prompt || ((questions) => inquirer.prompt(questions));
139
+
140
+ let confirmed = options.yes === true;
141
+ if (!confirmed) {
142
+ const canPrompt = typeof options.prompt === 'function' || process.stdin.isTTY;
143
+ if (!canPrompt) {
144
+ console.log(
145
+ chalk.red(
146
+ 'certs trust requires interactive confirmation or --yes (refusing non-TTY without --yes).'
147
+ )
148
+ );
149
+ return 1;
150
+ }
151
+ const answers = await prompt([
152
+ {
153
+ type: 'confirm',
154
+ name: 'ok',
155
+ message:
156
+ 'Install/trust the Caddy local root CA in the System keychain via `caddy trust`?',
157
+ default: false,
158
+ },
159
+ ]);
160
+ confirmed = answers.ok === true;
161
+ }
162
+
163
+ if (!confirmed) {
164
+ console.log(chalk.dim('Cancelled.'));
165
+ return 1;
166
+ }
167
+
168
+ try {
169
+ // caddy trust may prompt for an admin password; always inherit stdio.
170
+ await exec('caddy', ['trust'], { stdio: 'inherit' });
171
+ console.log(chalk.green('caddy trust completed.'));
172
+ console.log(
173
+ chalk.dim(
174
+ 'Re-check with: stagepass certs check (restart browsers if needed)'
175
+ )
176
+ );
177
+ return 0;
178
+ } catch (error) {
179
+ console.log(chalk.red('caddy trust failed.'));
180
+ console.log(
181
+ chalk.dim(error instanceof Error ? error.message : String(error))
182
+ );
183
+ return 1;
184
+ }
185
+ }