@stagepass/cli 2.0.0 → 2.2.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 +87 -7
- package/TESTING-macos.md +58 -0
- package/bin/index.js +2 -2
- package/package.json +3 -1
- package/src/cli.js +204 -0
- package/src/commands/autostart.js +85 -0
- package/src/commands/certs.js +185 -0
- package/src/commands/daemon.js +126 -0
- package/src/commands/doctor.js +57 -0
- package/src/commands/new.js +455 -0
- package/src/commands/npm-run.js +84 -0
- package/src/commands/stop.js +15 -0
- package/src/commands/upgrade.js +276 -0
- package/src/lib/ai-guide.js +161 -0
- package/src/lib/autostart.js +360 -0
- package/src/lib/cdn-snippets.js +27 -0
- package/src/lib/certs.js +476 -0
- package/src/lib/doctor-site.js +89 -0
- package/src/lib/doctor.js +521 -0
- package/src/lib/exec.js +71 -0
- package/src/lib/paths.js +58 -0
- package/src/lib/platform.js +47 -0
- package/src/lib/project-modules.js +98 -0
- package/src/lib/project-name.js +47 -0
- package/src/lib/project-sync.js +394 -0
- package/src/lib/remote-url.js +63 -0
- package/src/lib/scaffold.js +212 -0
- package/src/lib/site-detect.js +242 -0
- package/src/lib/snippets.js +94 -0
- package/src/lib/spawn.js +30 -0
- package/src/lib/versions.js +27 -0
- package/templates/project/AGENTS.md.tmpl +39 -0
- package/templates/project/README.md.tmpl +44 -0
- package/templates/project/gitignore +7 -0
- package/templates/project/jsconfig.json.tmpl +14 -0
- package/templates/project/scripts/build.mjs +110 -0
- package/templates/project/src/main.js.tmpl +29 -0
- package/templates/project/src/main.ts.tmpl +29 -0
- package/templates/project/src/styles.css.tmpl +8 -0
- package/templates/project/src/styles.scss.tmpl +8 -0
- package/templates/project/stagepass.json.tmpl +8 -0
- package/templates/project/tsconfig.json.tmpl +13 -0
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import fs from 'fs-extra';
|
|
2
|
+
import chalk from 'chalk';
|
|
3
|
+
import { defaultExec } from '../lib/exec.js';
|
|
4
|
+
import { getCaddyfilePath, getLogsDir } from '../lib/paths.js';
|
|
5
|
+
import { runCertsCheck } from '../lib/certs.js';
|
|
6
|
+
import { loadProjects } from '../lib/projects.js';
|
|
7
|
+
import { getProjectsPath } from '../lib/paths.js';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Detect bind / permission failures that will not self-heal on immediate restart.
|
|
11
|
+
* LaunchAgent KeepAlive (SuccessfulExit=false) must not restart-loop on these.
|
|
12
|
+
* @param {string} detail
|
|
13
|
+
* @returns {boolean}
|
|
14
|
+
*/
|
|
15
|
+
export function isPermanentDaemonFailure(detail) {
|
|
16
|
+
return /permission denied|bind|address already in use|EADDRINUSE|EACCES/i.test(
|
|
17
|
+
String(detail || '')
|
|
18
|
+
);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Non-interactive foreground daemon for LaunchAgent use.
|
|
23
|
+
* Never uses sudo. Runs certs check (log only, no TLS handshake), then `caddy run`.
|
|
24
|
+
*
|
|
25
|
+
* Existing `stagepass start` also launches Caddy as the user (sudo is only for
|
|
26
|
+
* port conflict helpers). Binding TCP 443 as a user LaunchAgent is therefore
|
|
27
|
+
* expected to work on macOS the same way. Permanent startup failures (missing
|
|
28
|
+
* Caddyfile, port 443 bind) exit 0 after logging so launchd KeepAlive does not
|
|
29
|
+
* restart-loop; unexpected mid-run failures exit 1 so KeepAlive can retry.
|
|
30
|
+
*
|
|
31
|
+
* @param {object} [options]
|
|
32
|
+
* @param {import('../lib/exec.js').ExecFn} [options.exec]
|
|
33
|
+
* @param {(file: string, args: string[], opts?: object) => Promise<unknown>} [options.runForeground]
|
|
34
|
+
* @param {(msg: string) => void} [options.log]
|
|
35
|
+
* @returns {Promise<number>}
|
|
36
|
+
*/
|
|
37
|
+
export async function daemon(options = {}) {
|
|
38
|
+
const log =
|
|
39
|
+
options.log ||
|
|
40
|
+
((msg) => {
|
|
41
|
+
const line = `[stagepass daemon] ${msg}`;
|
|
42
|
+
console.error(line);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
const exec = options.exec || defaultExec;
|
|
46
|
+
const caddyfilePath = options.caddyfilePath || getCaddyfilePath();
|
|
47
|
+
const pathExists = options.pathExists || ((p) => fs.pathExists(p));
|
|
48
|
+
|
|
49
|
+
await fs.ensureDir(options.logsDir || getLogsDir());
|
|
50
|
+
|
|
51
|
+
if (!(await pathExists(caddyfilePath))) {
|
|
52
|
+
log(`Caddyfile missing: ${caddyfilePath}`);
|
|
53
|
+
log('Refusing to start. Run: stagepass setup && stagepass link');
|
|
54
|
+
// Exit 0: permanent until the user fixes config; avoid KeepAlive restart loop.
|
|
55
|
+
return 0;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// Non-interactive cert check (log only; never prompts; never repairs).
|
|
59
|
+
// Skip TLS handshakes: Caddy is not running yet during cold start.
|
|
60
|
+
try {
|
|
61
|
+
const projectsPath = options.projectsPath || getProjectsPath();
|
|
62
|
+
const doc = await loadProjects(projectsPath);
|
|
63
|
+
const domains = Object.keys(doc.projects || {});
|
|
64
|
+
const report = await runCertsCheck({
|
|
65
|
+
exec,
|
|
66
|
+
domains,
|
|
67
|
+
skipHandshake: true,
|
|
68
|
+
handshakeTimeoutMs: 2000,
|
|
69
|
+
});
|
|
70
|
+
log(`certs root: ${report.root.status} ${report.root.reason}`);
|
|
71
|
+
log(
|
|
72
|
+
`certs trust: ${report.trust?.trusted ? 'trusted' : 'NOT trusted'}${
|
|
73
|
+
report.trust?.error ? ` (${report.trust.error})` : ''
|
|
74
|
+
}`
|
|
75
|
+
);
|
|
76
|
+
if (report.orphans?.length) {
|
|
77
|
+
log(
|
|
78
|
+
`certs orphans: ${report.orphans.length} (report only; SHA-1 ${report.orphans
|
|
79
|
+
.map((o) => o.sha1 || '?')
|
|
80
|
+
.join(', ')})`
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
} catch (error) {
|
|
84
|
+
log(
|
|
85
|
+
`certs check failed (continuing): ${
|
|
86
|
+
error instanceof Error ? error.message : String(error)
|
|
87
|
+
}`
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const args = ['run', '--config', caddyfilePath, '--adapter', 'caddyfile'];
|
|
92
|
+
log(`starting: caddy ${args.join(' ')}`);
|
|
93
|
+
|
|
94
|
+
const runForeground =
|
|
95
|
+
options.runForeground ||
|
|
96
|
+
(async (file, fileArgs) => {
|
|
97
|
+
// Inherit stdio so LaunchAgent log redirects capture Caddy output.
|
|
98
|
+
return exec(file, fileArgs, { stdio: 'inherit' });
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
try {
|
|
102
|
+
await runForeground('caddy', args);
|
|
103
|
+
log('caddy exited cleanly');
|
|
104
|
+
return 0;
|
|
105
|
+
} catch (error) {
|
|
106
|
+
const err = /** @type {{ message?: string, stderr?: string, code?: string|number }} */ (
|
|
107
|
+
error
|
|
108
|
+
);
|
|
109
|
+
const detail = [err.message, err.stderr].filter(Boolean).join(' | ');
|
|
110
|
+
log(`caddy failed: ${detail}`);
|
|
111
|
+
if (isPermanentDaemonFailure(detail)) {
|
|
112
|
+
log(
|
|
113
|
+
'Port 443 bind/listen failed for the user LaunchAgent. Exiting without KeepAlive retry. Use interactive `stagepass start` (which can resolve conflicts with confirmation), or free port 443, then retry.'
|
|
114
|
+
);
|
|
115
|
+
if (process.stderr.isTTY) {
|
|
116
|
+
console.error(chalk.red(detail));
|
|
117
|
+
}
|
|
118
|
+
// Exit 0 so SuccessfulExit=false KeepAlive does not restart-loop.
|
|
119
|
+
return 0;
|
|
120
|
+
}
|
|
121
|
+
if (process.stderr.isTTY) {
|
|
122
|
+
console.error(chalk.red(detail));
|
|
123
|
+
}
|
|
124
|
+
return 1;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import chalk from 'chalk';
|
|
2
|
+
import { runDoctor } from '../lib/doctor.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @param {object} [options]
|
|
6
|
+
* @param {boolean} [options.json]
|
|
7
|
+
* @param {boolean} [options.verbose]
|
|
8
|
+
* @returns {Promise<number>} exit code
|
|
9
|
+
*/
|
|
10
|
+
export async function doctor(options = {}) {
|
|
11
|
+
const result = await runDoctor({
|
|
12
|
+
skipHandshake: options.skipHandshake === true,
|
|
13
|
+
skipSite: options.skipSite === true,
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
if (options.json) {
|
|
17
|
+
const payload = {
|
|
18
|
+
ok: result.exitCode === 0,
|
|
19
|
+
summary: result.summary,
|
|
20
|
+
checks: result.checks,
|
|
21
|
+
hints: result.hints,
|
|
22
|
+
};
|
|
23
|
+
console.log(JSON.stringify(payload, null, 2));
|
|
24
|
+
return result.exitCode;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
console.log(chalk.bold('\nStagepass doctor\n'));
|
|
28
|
+
|
|
29
|
+
for (const item of result.checks) {
|
|
30
|
+
const tag =
|
|
31
|
+
item.status === 'ok'
|
|
32
|
+
? chalk.green('ok')
|
|
33
|
+
: item.status === 'warn'
|
|
34
|
+
? chalk.yellow('warn')
|
|
35
|
+
: item.status === 'fail'
|
|
36
|
+
? chalk.red('fail')
|
|
37
|
+
: chalk.cyan(item.status);
|
|
38
|
+
console.log(`[${tag}] ${item.message}`);
|
|
39
|
+
if (item.detail && (options.verbose || item.status !== 'ok')) {
|
|
40
|
+
console.log(chalk.dim(` ${item.detail}`));
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
console.log('');
|
|
45
|
+
for (const hint of result.hints) {
|
|
46
|
+
console.log(chalk.cyan(`hint: ${hint}`));
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const { ok, warn, fail } = result.summary;
|
|
50
|
+
console.log(
|
|
51
|
+
chalk.bold(
|
|
52
|
+
`\nSummary: ${ok} ok, ${warn} warn, ${fail} fail (exit ${result.exitCode})`
|
|
53
|
+
)
|
|
54
|
+
);
|
|
55
|
+
|
|
56
|
+
return result.exitCode;
|
|
57
|
+
}
|