@stagepass/cli 2.2.6 → 2.3.0-beta.1
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 +61 -13
- package/TESTING-macos.md +35 -8
- package/bin/index.js +4 -1
- package/package.json +9 -8
- package/src/cli.js +62 -7
- package/src/commands/certs.js +375 -19
- package/src/commands/daemon.js +75 -7
- package/src/commands/dev.js +419 -0
- package/src/commands/link.js +66 -25
- package/src/commands/new.js +397 -41
- package/src/commands/npm-run.js +8 -0
- package/src/commands/reload.js +2 -2
- package/src/commands/setup.js +5 -5
- package/src/commands/start.js +386 -83
- package/src/commands/stop.js +50 -31
- package/src/commands/unlink.js +8 -6
- package/src/commands/upgrade.js +51 -12
- package/src/lib/apply-site-block.js +48 -0
- package/src/lib/autostart.js +300 -3
- package/src/lib/caddyfile.js +72 -13
- package/src/lib/cdn-snippets.js +145 -3
- package/src/lib/certs.js +334 -47
- package/src/lib/dev-port.js +45 -0
- package/src/lib/doctor-site.js +61 -5
- package/src/lib/doctor.js +48 -4
- package/src/lib/ensure-running.js +89 -0
- package/src/lib/exec.js +72 -24
- package/src/lib/live-server.js +484 -0
- package/src/lib/node-engine.js +55 -0
- package/src/lib/notify.js +62 -0
- package/src/lib/origins.js +27 -5
- package/src/lib/package-versions.js +9 -0
- package/src/lib/paths.js +18 -0
- package/src/lib/php-fpm.js +33 -0
- package/src/lib/project-sync.js +165 -20
- package/src/lib/projects.js +137 -7
- package/src/lib/prompts.js +19 -0
- package/src/lib/registry.js +27 -6
- package/src/lib/scaffold.js +288 -11
- package/src/lib/site-detect.js +78 -25
- package/src/lib/snippets.js +14 -11
- package/src/lib/spinner.js +60 -0
- package/src/lib/versions.js +20 -8
- package/templates/project/AGENTS.md.tmpl +4 -3
- package/templates/project/README.md.tmpl +35 -6
- package/templates/project/api/README.md.tmpl +7 -0
- package/templates/project/examples/php/README.md.tmpl +18 -0
- package/templates/project/examples/php/form.php +105 -0
- package/templates/project/examples/php/proxy.php +171 -0
- package/templates/project/scripts/build.mjs +200 -37
package/src/commands/reload.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import chalk from 'chalk';
|
|
2
2
|
import { execa } from 'execa';
|
|
3
|
-
import
|
|
3
|
+
import { createSpinner } from '../lib/spinner.js';
|
|
4
4
|
import fs from 'fs-extra';
|
|
5
5
|
import { getCaddyfilePath, getProjectsPath } from '../lib/paths.js';
|
|
6
6
|
import {
|
|
@@ -12,7 +12,7 @@ export async function reload(options = {}) {
|
|
|
12
12
|
const caddyFilePath = getCaddyfilePath();
|
|
13
13
|
const projectsPath = getProjectsPath();
|
|
14
14
|
|
|
15
|
-
const spinner =
|
|
15
|
+
const spinner = createSpinner('Reloading Caddy...').start();
|
|
16
16
|
|
|
17
17
|
if (!(await fs.pathExists(caddyFilePath))) {
|
|
18
18
|
spinner.fail('No Caddyfile found. Run "stagepass link" first.');
|
package/src/commands/setup.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { execa } from 'execa';
|
|
2
|
-
import
|
|
2
|
+
import { createSpinner } from '../lib/spinner.js';
|
|
3
3
|
import chalk from 'chalk';
|
|
4
4
|
import fs from 'fs-extra';
|
|
5
5
|
import path from 'path';
|
|
@@ -21,7 +21,7 @@ export async function setup(options = {}) {
|
|
|
21
21
|
const execOpts = options.verbose ? { stdio: 'inherit' } : {};
|
|
22
22
|
|
|
23
23
|
// 1. Check Homebrew
|
|
24
|
-
const brewSpinner =
|
|
24
|
+
const brewSpinner = createSpinner('Checking system requirements...').start();
|
|
25
25
|
try {
|
|
26
26
|
await execa('brew', ['--version']);
|
|
27
27
|
brewSpinner.succeed('System requirements met.');
|
|
@@ -31,7 +31,7 @@ export async function setup(options = {}) {
|
|
|
31
31
|
}
|
|
32
32
|
|
|
33
33
|
// 2. Install Dependencies
|
|
34
|
-
const installSpinner =
|
|
34
|
+
const installSpinner = createSpinner('Installing dependencies...').start();
|
|
35
35
|
try {
|
|
36
36
|
await execa('brew', ['install', 'caddy', 'php', 'dnsmasq'], execOpts);
|
|
37
37
|
installSpinner.succeed('Core dependencies installed.');
|
|
@@ -41,7 +41,7 @@ export async function setup(options = {}) {
|
|
|
41
41
|
}
|
|
42
42
|
|
|
43
43
|
// 3. Configure Dnsmasq (DIRECT WRITE STRATEGY)
|
|
44
|
-
const dnsSpinner =
|
|
44
|
+
const dnsSpinner = createSpinner('Configuring local DNS (.sp)...').start();
|
|
45
45
|
try {
|
|
46
46
|
const { stdout: brewPrefixRaw } = await execa('brew', ['--prefix']);
|
|
47
47
|
const brewPrefix = brewPrefixRaw.trim();
|
|
@@ -82,7 +82,7 @@ export async function setup(options = {}) {
|
|
|
82
82
|
}
|
|
83
83
|
|
|
84
84
|
// 5. Restart DNS Services
|
|
85
|
-
const restartSpinner =
|
|
85
|
+
const restartSpinner = createSpinner('Restarting DNS services...').start();
|
|
86
86
|
try {
|
|
87
87
|
// Dnsmasq Restart
|
|
88
88
|
try {
|
package/src/commands/start.js
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
import { execa } from 'execa';
|
|
2
1
|
import chalk from 'chalk';
|
|
3
2
|
import path from 'path';
|
|
4
3
|
import fs from 'fs-extra';
|
|
5
|
-
import
|
|
6
|
-
import
|
|
4
|
+
import { createSpinner } from '../lib/spinner.js';
|
|
5
|
+
import os from 'node:os';
|
|
7
6
|
import {
|
|
8
7
|
getCaddyfilePath,
|
|
9
8
|
getServerLogPath,
|
|
@@ -14,137 +13,441 @@ import {
|
|
|
14
13
|
ensureCaddyfileMigrated,
|
|
15
14
|
warnIfAllowAllOriginsActive,
|
|
16
15
|
} from '../lib/migrate.js';
|
|
16
|
+
import { defaultExec } from '../lib/exec.js';
|
|
17
|
+
import { confirm as defaultConfirm } from '../lib/prompts.js';
|
|
18
|
+
import { parseLsofListenOutput } from '../lib/doctor.js';
|
|
19
|
+
import { runCertsCheck, domainsNeedingSiteCertRenewal } from '../lib/certs.js';
|
|
20
|
+
import { loadProjects, clearStaleDevPorts, getProject } from '../lib/projects.js';
|
|
21
|
+
import { applySiteBlock } from '../lib/apply-site-block.js';
|
|
22
|
+
import {
|
|
23
|
+
getAutostartStatus,
|
|
24
|
+
buildLaunchctlKickstartArgs,
|
|
25
|
+
buildLaunchctlBootstrapArgs,
|
|
26
|
+
LAUNCH_AGENT_LABEL,
|
|
27
|
+
} from '../lib/autostart.js';
|
|
28
|
+
import { ensurePhpFpm } from '../lib/php-fpm.js';
|
|
29
|
+
import { certsTrust, certsRenew } from './certs.js';
|
|
30
|
+
|
|
31
|
+
const PORT_IGNORE = ['Google Chrome', 'Chrome Helper', 'OneDrive', 'Music', 'Safari', 'caddy'];
|
|
17
32
|
|
|
18
|
-
|
|
33
|
+
/**
|
|
34
|
+
* @param {unknown} pid
|
|
35
|
+
* @returns {boolean}
|
|
36
|
+
*/
|
|
37
|
+
export function isSafePid(pid) {
|
|
38
|
+
return typeof pid === 'string'
|
|
39
|
+
? /^[1-9][0-9]*$/.test(pid)
|
|
40
|
+
: typeof pid === 'number' && Number.isInteger(pid) && pid > 0;
|
|
41
|
+
}
|
|
19
42
|
|
|
20
|
-
|
|
43
|
+
/**
|
|
44
|
+
* Inspect TCP 443 without sudo (same approach as doctor).
|
|
45
|
+
* @param {object} [opts]
|
|
46
|
+
* @param {import('../lib/exec.js').ExecFn} [opts.exec]
|
|
47
|
+
* @returns {Promise<{ pid: string, name: string }|null>}
|
|
48
|
+
*/
|
|
49
|
+
export async function findPort443Conflict(opts = {}) {
|
|
50
|
+
const exec = opts.exec || defaultExec;
|
|
21
51
|
try {
|
|
22
|
-
const
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
const
|
|
28
|
-
const
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
const ignoreList = ['Google Chrome', 'Chrome Helper', 'OneDrive', 'Music', 'Safari', 'caddy'];
|
|
35
|
-
if (ignoreList.some(ignore => name.includes(ignore))) {
|
|
36
|
-
return null;
|
|
52
|
+
const { stdout } = await exec('lsof', [
|
|
53
|
+
'-nP',
|
|
54
|
+
'-iTCP:443',
|
|
55
|
+
'-sTCP:LISTEN',
|
|
56
|
+
]);
|
|
57
|
+
const listeners = parseLsofListenOutput(stdout);
|
|
58
|
+
for (const listener of listeners) {
|
|
59
|
+
const name = listener.name || path.basename(listener.command || '');
|
|
60
|
+
if (PORT_IGNORE.some((ignore) => name.includes(ignore))) continue;
|
|
61
|
+
if (/^caddy$/i.test(name)) continue;
|
|
62
|
+
return { pid: String(listener.pid), name };
|
|
37
63
|
}
|
|
64
|
+
return null;
|
|
65
|
+
} catch (error) {
|
|
66
|
+
const err = /** @type {{ code?: string|number }} */ (error);
|
|
67
|
+
if (err.code === 1 || err.code === '1') return null;
|
|
68
|
+
return null;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
38
71
|
|
|
39
|
-
|
|
72
|
+
/** process.kill; sudo kill only on EPERM. ESRCH=ok; else ok:false (no throw). */
|
|
73
|
+
async function signalProcess(pid, signal, exec, kill = process.kill) {
|
|
74
|
+
try {
|
|
75
|
+
kill(Number(pid), /** @type {NodeJS.Signals} */ (`SIG${signal}`));
|
|
76
|
+
return { ok: true };
|
|
40
77
|
} catch (e) {
|
|
41
|
-
|
|
78
|
+
const code = /** @type {{ code?: string }} */ (e)?.code;
|
|
79
|
+
if (code === 'ESRCH') return { ok: true };
|
|
80
|
+
if (code !== 'EPERM') {
|
|
81
|
+
return { ok: false, detail: `Failed to signal PID ${pid}: ${e?.message ?? e}` };
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
try {
|
|
85
|
+
await exec('sudo', ['kill', signal === 'KILL' ? '-KILL' : '-TERM', pid], {
|
|
86
|
+
stdio: 'inherit',
|
|
87
|
+
});
|
|
88
|
+
return { ok: true };
|
|
89
|
+
} catch (e) {
|
|
90
|
+
return { ok: false, detail: `sudo kill failed for PID ${pid}: ${e?.message ?? e}` };
|
|
42
91
|
}
|
|
43
92
|
}
|
|
44
93
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
94
|
+
/**
|
|
95
|
+
* Resolve a confirmed port conflict: TERM first, KILL only after a second confirm
|
|
96
|
+
* if the listener is still present. Never signals without confirmation.
|
|
97
|
+
* Privilege-scoped: own processes without sudo; sudo only after unprivileged failure.
|
|
98
|
+
*
|
|
99
|
+
* @param {{ pid: string, name: string }} proc
|
|
100
|
+
* @param {object} opts
|
|
101
|
+
* @param {import('../lib/exec.js').ExecFn} [opts.exec]
|
|
102
|
+
* @param {typeof process.kill} [opts.kill]
|
|
103
|
+
* @param {(message: string, o?: object) => Promise<boolean>} [opts.confirm]
|
|
104
|
+
* @param {boolean} [opts.interactive]
|
|
105
|
+
* @param {(ms: number) => Promise<void>} [opts.sleep]
|
|
106
|
+
* @param {() => Promise<{ pid: string, name: string }|null>} [opts.recheck]
|
|
107
|
+
* @returns {Promise<{ ok: boolean, cancelled?: boolean, detail?: string }>}
|
|
108
|
+
*/
|
|
109
|
+
export async function resolvePort443Conflict(proc, opts = {}) {
|
|
110
|
+
const exec = opts.exec || defaultExec;
|
|
111
|
+
const kill = opts.kill || process.kill;
|
|
112
|
+
const confirm = opts.confirm || defaultConfirm;
|
|
113
|
+
const sleep =
|
|
114
|
+
opts.sleep || ((ms) => new Promise((r) => setTimeout(r, ms)));
|
|
115
|
+
const recheck = opts.recheck || (() => findPort443Conflict({ exec }));
|
|
116
|
+
const pid = String(proc?.pid ?? '');
|
|
117
|
+
|
|
118
|
+
if (!isSafePid(pid)) {
|
|
119
|
+
return {
|
|
120
|
+
ok: false,
|
|
121
|
+
detail: `Refusing to signal process: invalid PID "${proc?.pid}".`,
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
console.log(
|
|
126
|
+
chalk.yellow(
|
|
127
|
+
`\n⚠️ Port 443 is blocked by: ${chalk.bold(proc.name)} (PID ${pid})`
|
|
128
|
+
)
|
|
129
|
+
);
|
|
130
|
+
|
|
131
|
+
const shouldTerm = await confirm(
|
|
132
|
+
`Send SIGTERM to ${proc.name} (PID ${pid}) to free port 443?`,
|
|
133
|
+
{ default: false }
|
|
134
|
+
);
|
|
135
|
+
if (!shouldTerm) {
|
|
136
|
+
return { ok: false, cancelled: true };
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
let signaled;
|
|
140
|
+
// Prefer right-free service stops before any sudo (nginx: valet, then brew).
|
|
141
|
+
if (String(proc.name).includes('nginx')) {
|
|
142
|
+
try {
|
|
143
|
+
await exec('valet', ['stop']);
|
|
144
|
+
} catch {
|
|
145
|
+
try {
|
|
146
|
+
await exec('brew', ['services', 'stop', 'nginx']);
|
|
147
|
+
} catch {
|
|
148
|
+
signaled = await signalProcess(pid, 'TERM', exec, kill);
|
|
149
|
+
}
|
|
50
150
|
}
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
151
|
+
} else if (String(proc.name).includes('httpd')) {
|
|
152
|
+
try {
|
|
153
|
+
await exec('sudo', ['apachectl', 'stop'], { stdio: 'inherit' });
|
|
154
|
+
} catch {
|
|
155
|
+
signaled = await signalProcess(pid, 'TERM', exec, kill);
|
|
54
156
|
}
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
} catch (error) {
|
|
58
|
-
return false;
|
|
157
|
+
} else {
|
|
158
|
+
signaled = await signalProcess(pid, 'TERM', exec, kill);
|
|
59
159
|
}
|
|
160
|
+
if (signaled?.ok === false) return signaled;
|
|
161
|
+
|
|
162
|
+
await sleep(400);
|
|
163
|
+
let still = await recheck();
|
|
164
|
+
if (!still || String(still.pid) !== pid) {
|
|
165
|
+
return { ok: true, detail: 'SIGTERM' };
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
const shouldKill = await confirm(
|
|
169
|
+
`${proc.name} (PID ${pid}) is still listening. Send SIGKILL?`,
|
|
170
|
+
{ default: false }
|
|
171
|
+
);
|
|
172
|
+
if (!shouldKill) {
|
|
173
|
+
return {
|
|
174
|
+
ok: false,
|
|
175
|
+
cancelled: true,
|
|
176
|
+
detail: 'Still listening after SIGTERM; SIGKILL not confirmed.',
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
const killed = await signalProcess(pid, 'KILL', exec, kill);
|
|
181
|
+
if (!killed.ok) return killed;
|
|
182
|
+
await sleep(200);
|
|
183
|
+
still = await recheck();
|
|
184
|
+
if (still && String(still.pid) === pid) {
|
|
185
|
+
return {
|
|
186
|
+
ok: false,
|
|
187
|
+
detail: `PID ${pid} still listening after SIGKILL.`,
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
return { ok: true, detail: 'SIGKILL' };
|
|
60
191
|
}
|
|
61
192
|
|
|
62
|
-
|
|
193
|
+
/**
|
|
194
|
+
* Summarize cert findings for start output.
|
|
195
|
+
* @param {Awaited<ReturnType<typeof runCertsCheck>>} report
|
|
196
|
+
*/
|
|
197
|
+
export function summarizeCertFindings(report) {
|
|
198
|
+
const issues = [];
|
|
199
|
+
if (report.root?.status === 'fail') {
|
|
200
|
+
issues.push({ kind: 'root', message: report.root.reason });
|
|
201
|
+
}
|
|
202
|
+
if (report.trust && !report.trust.trusted) {
|
|
203
|
+
issues.push({ kind: 'trust', message: 'Caddy local root is not trusted in the System keychain' });
|
|
204
|
+
}
|
|
205
|
+
const needing = domainsNeedingSiteCertRenewal(report.handshakes || []);
|
|
206
|
+
for (const domain of needing) {
|
|
207
|
+
issues.push({ kind: 'site', domain, message: `TLS fail for ${domain}` });
|
|
208
|
+
}
|
|
209
|
+
if (report.orphans?.length) {
|
|
210
|
+
issues.push({
|
|
211
|
+
kind: 'orphans',
|
|
212
|
+
message: `${report.orphans.length} orphaned Caddy root(s) in keychain (certs renew --remove-orphans)`,
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
return issues;
|
|
216
|
+
}
|
|
63
217
|
|
|
218
|
+
/**
|
|
219
|
+
* @param {object} [options]
|
|
220
|
+
*/
|
|
64
221
|
export async function start(options = {}) {
|
|
65
222
|
const caddyDir = getStagepassDir();
|
|
66
223
|
const caddyFile = getCaddyfilePath();
|
|
67
224
|
const logFile = getServerLogPath();
|
|
68
225
|
const projectsPath = getProjectsPath();
|
|
226
|
+
const exec = options.exec || defaultExec;
|
|
227
|
+
const confirm = options.confirm || defaultConfirm;
|
|
228
|
+
const interactive =
|
|
229
|
+
options.interactive === true ||
|
|
230
|
+
(options.interactive !== false &&
|
|
231
|
+
(typeof options.confirm === 'function' || process.stdin.isTTY));
|
|
69
232
|
|
|
70
233
|
console.log(chalk.bold('\n🚀 Starting Stagepass...'));
|
|
71
234
|
|
|
72
|
-
// 1.
|
|
235
|
+
// 1. Read-only certs check (always)
|
|
236
|
+
let certReport = null;
|
|
73
237
|
try {
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
238
|
+
const doc = await loadProjects(projectsPath);
|
|
239
|
+
const domains = Object.keys(doc.projects || {});
|
|
240
|
+
certReport = await (options.runCertsCheck || runCertsCheck)({
|
|
241
|
+
exec,
|
|
242
|
+
domains,
|
|
243
|
+
skipHandshake: options.skipHandshake === true,
|
|
244
|
+
});
|
|
245
|
+
const findings = summarizeCertFindings(certReport);
|
|
246
|
+
if (findings.length > 0) {
|
|
247
|
+
console.log(chalk.yellow('\nCertificate findings:'));
|
|
248
|
+
for (const f of findings) {
|
|
249
|
+
console.log(chalk.yellow(` • ${f.message}`));
|
|
250
|
+
}
|
|
251
|
+
if (interactive) {
|
|
252
|
+
const trustIssue = findings.some((f) => f.kind === 'trust');
|
|
253
|
+
const siteIssue = findings.some((f) => f.kind === 'site');
|
|
254
|
+
if (trustIssue) {
|
|
255
|
+
const doTrust = await confirm('Run `stagepass certs trust` now?', {
|
|
256
|
+
default: false,
|
|
257
|
+
});
|
|
258
|
+
if (doTrust) {
|
|
259
|
+
await (options.certsTrust || certsTrust)({
|
|
260
|
+
yes: true,
|
|
261
|
+
exec,
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
if (siteIssue) {
|
|
266
|
+
const doRenew = await confirm(
|
|
267
|
+
'Run `stagepass certs renew` for failing site certificates?',
|
|
268
|
+
{ default: false }
|
|
269
|
+
);
|
|
270
|
+
if (doRenew) {
|
|
271
|
+
await (options.certsRenew || certsRenew)({
|
|
272
|
+
yes: true,
|
|
273
|
+
exec,
|
|
274
|
+
confirm,
|
|
275
|
+
});
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
} else {
|
|
279
|
+
console.log(
|
|
280
|
+
chalk.dim(
|
|
281
|
+
'Non-interactive start: fix with `stagepass certs trust` / `stagepass certs renew` (and `--remove-orphans` / `--root` when needed).'
|
|
282
|
+
)
|
|
283
|
+
);
|
|
284
|
+
}
|
|
285
|
+
} else {
|
|
286
|
+
console.log(chalk.dim('Certificates: ok'));
|
|
287
|
+
}
|
|
288
|
+
} catch (error) {
|
|
289
|
+
console.log(
|
|
290
|
+
chalk.yellow(
|
|
291
|
+
`Certificate check skipped: ${
|
|
292
|
+
error instanceof Error ? error.message : String(error)
|
|
293
|
+
}`
|
|
294
|
+
)
|
|
295
|
+
);
|
|
78
296
|
}
|
|
79
297
|
|
|
80
|
-
// 2. Port
|
|
81
|
-
const conflict =
|
|
298
|
+
// 2. Port conflict (no sudo until a confirmed signal needs it; never sudo -v)
|
|
299
|
+
const conflict =
|
|
300
|
+
(await (options.findPort443Conflict || findPort443Conflict)({ exec })) ||
|
|
301
|
+
null;
|
|
82
302
|
if (conflict) {
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
if (shouldKill) {
|
|
92
|
-
const killSpinner = ora('Freeing port...').start();
|
|
93
|
-
const success = await killProcess(conflict);
|
|
94
|
-
if (success) killSpinner.succeed('Port freed.');
|
|
95
|
-
else {
|
|
96
|
-
killSpinner.fail('Could not free port.');
|
|
97
|
-
return;
|
|
98
|
-
}
|
|
99
|
-
} else {
|
|
100
|
-
return;
|
|
303
|
+
if (!interactive && typeof options.confirm !== 'function') {
|
|
304
|
+
console.log(
|
|
305
|
+
chalk.red(
|
|
306
|
+
`Port 443 is blocked by ${conflict.name} (PID ${conflict.pid}). Refusing to signal without confirmation.`
|
|
307
|
+
)
|
|
308
|
+
);
|
|
309
|
+
return 1;
|
|
101
310
|
}
|
|
311
|
+
const resolved = await (options.resolvePort443Conflict ||
|
|
312
|
+
resolvePort443Conflict)(conflict, {
|
|
313
|
+
exec,
|
|
314
|
+
kill: options.kill,
|
|
315
|
+
confirm,
|
|
316
|
+
interactive,
|
|
317
|
+
sleep: options.sleep,
|
|
318
|
+
});
|
|
319
|
+
if (!resolved.ok) {
|
|
320
|
+
if (resolved.detail) console.log(chalk.red(resolved.detail));
|
|
321
|
+
return 1;
|
|
322
|
+
}
|
|
323
|
+
console.log(chalk.green(`Port freed (${resolved.detail}).`));
|
|
102
324
|
}
|
|
103
325
|
|
|
104
|
-
//
|
|
326
|
+
// 3. Migrate legacy Caddyfile blocks before boot; drop dead live-reload ports
|
|
105
327
|
await fs.ensureDir(caddyDir);
|
|
106
328
|
if (await fs.pathExists(caddyFile)) {
|
|
107
329
|
await ensureCaddyfileMigrated({ caddyfilePath: caddyFile, projectsPath });
|
|
330
|
+
const stale = await clearStaleDevPorts(projectsPath);
|
|
331
|
+
for (const domain of stale) {
|
|
332
|
+
const project = await getProject(domain, projectsPath);
|
|
333
|
+
if (project) {
|
|
334
|
+
await applySiteBlock(domain, project, { caddyfilePath: caddyFile });
|
|
335
|
+
}
|
|
336
|
+
}
|
|
108
337
|
await warnIfAllowAllOriginsActive({ caddyfilePath: caddyFile });
|
|
109
338
|
}
|
|
110
339
|
|
|
111
|
-
//
|
|
112
|
-
const spinner =
|
|
113
|
-
|
|
114
|
-
|
|
340
|
+
// 4. Boot services (autostart-aware)
|
|
341
|
+
const spinner = createSpinner('Booting background services...').start();
|
|
342
|
+
|
|
343
|
+
/** @type {number|null} */
|
|
344
|
+
let logFd = null;
|
|
115
345
|
let stdioMode = 'ignore';
|
|
116
346
|
if (options.verbose) {
|
|
117
347
|
spinner.stop();
|
|
118
348
|
console.log(chalk.dim('--- Verbose Logs ---'));
|
|
119
349
|
stdioMode = 'inherit';
|
|
120
350
|
} else {
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
stdioMode = ['ignore', logStream, logStream];
|
|
351
|
+
logFd = fs.openSync(logFile, 'w');
|
|
352
|
+
stdioMode = ['ignore', logFd, logFd];
|
|
124
353
|
}
|
|
125
354
|
|
|
126
355
|
try {
|
|
127
|
-
//
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
356
|
+
// 2.3: ensurePhpFpm prefers `brew services start php`, then `restart`
|
|
357
|
+
// (2.2.7 always used `restart`). Keep start-first; both stay non-sudo.
|
|
358
|
+
const php = await ensurePhpFpm({
|
|
359
|
+
exec,
|
|
360
|
+
stdio: stdioMode === 'inherit' ? 'inherit' : 'ignore',
|
|
361
|
+
});
|
|
362
|
+
if (!php.ok && options.verbose) {
|
|
363
|
+
console.log(chalk.yellow(`PHP-FPM: ${php.detail || 'failed'}`));
|
|
364
|
+
}
|
|
131
365
|
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
366
|
+
const status =
|
|
367
|
+
(await (options.getAutostartStatus || getAutostartStatus)({ exec })) ||
|
|
368
|
+
{};
|
|
369
|
+
const uid = options.uid ?? process.getuid?.() ?? os.userInfo().uid;
|
|
135
370
|
|
|
136
|
-
|
|
371
|
+
if (status.loaded || status.plistExists) {
|
|
372
|
+
if (status.plistExists && !status.loaded) {
|
|
373
|
+
try {
|
|
374
|
+
await exec(
|
|
375
|
+
'launchctl',
|
|
376
|
+
buildLaunchctlBootstrapArgs(uid, status.plistPath)
|
|
377
|
+
);
|
|
378
|
+
console.log(
|
|
379
|
+
chalk.dim(
|
|
380
|
+
`Bootstrapped LaunchAgent ${LAUNCH_AGENT_LABEL} (was installed but not loaded).`
|
|
381
|
+
)
|
|
382
|
+
);
|
|
383
|
+
} catch (error) {
|
|
384
|
+
if (!options.verbose) spinner.fail('Failed to bootstrap LaunchAgent.');
|
|
385
|
+
console.log(
|
|
386
|
+
chalk.red(error instanceof Error ? error.message : String(error))
|
|
387
|
+
);
|
|
388
|
+
return 1;
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
try {
|
|
392
|
+
await exec('launchctl', buildLaunchctlKickstartArgs(uid));
|
|
393
|
+
if (!options.verbose) {
|
|
394
|
+
spinner.succeed(
|
|
395
|
+
chalk.green(
|
|
396
|
+
`Stagepass is active (launchctl kickstart ${LAUNCH_AGENT_LABEL}).`
|
|
397
|
+
)
|
|
398
|
+
);
|
|
399
|
+
console.log(chalk.dim(` Logs: ${logFile}`));
|
|
400
|
+
} else {
|
|
401
|
+
console.log(
|
|
402
|
+
chalk.green(`✔ kickstart gui/${uid}/${LAUNCH_AGENT_LABEL}`)
|
|
403
|
+
);
|
|
404
|
+
}
|
|
405
|
+
return 0;
|
|
406
|
+
} catch (error) {
|
|
407
|
+
if (!options.verbose) spinner.fail('launchctl kickstart failed.');
|
|
408
|
+
console.log(
|
|
409
|
+
chalk.red(error instanceof Error ? error.message : String(error))
|
|
410
|
+
);
|
|
411
|
+
console.log(chalk.dim('Falling back to direct caddy start...'));
|
|
412
|
+
if (!options.verbose) spinner.start('Booting background services...');
|
|
413
|
+
}
|
|
414
|
+
}
|
|
137
415
|
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
416
|
+
// Direct caddy start (no LaunchAgent)
|
|
417
|
+
try {
|
|
418
|
+
await exec('caddy', ['stop'], { stdio: 'ignore' });
|
|
419
|
+
} catch {
|
|
420
|
+
// ignore
|
|
141
421
|
}
|
|
142
422
|
|
|
423
|
+
await exec('caddy', ['start', '--config', caddyFile], {
|
|
424
|
+
stdio: stdioMode,
|
|
425
|
+
});
|
|
426
|
+
|
|
427
|
+
if (!options.verbose) {
|
|
428
|
+
spinner.succeed(chalk.green('Stagepass is active.'));
|
|
429
|
+
console.log(chalk.dim(` Logs: ${logFile}`));
|
|
430
|
+
} else {
|
|
431
|
+
console.log(chalk.green('✔ Stagepass is active.'));
|
|
432
|
+
}
|
|
433
|
+
return 0;
|
|
143
434
|
} catch (error) {
|
|
144
|
-
spinner.fail('Failed to start.');
|
|
435
|
+
if (!options.verbose) spinner.fail('Failed to start.');
|
|
436
|
+
else console.log(chalk.red('Failed to start.'));
|
|
145
437
|
if (!options.verbose) {
|
|
146
|
-
|
|
147
|
-
|
|
438
|
+
console.log(chalk.red('See error log for details:'));
|
|
439
|
+
console.log(chalk.dim(logFile));
|
|
440
|
+
} else if (error instanceof Error) {
|
|
441
|
+
console.log(chalk.red(error.message));
|
|
442
|
+
}
|
|
443
|
+
return 1;
|
|
444
|
+
} finally {
|
|
445
|
+
if (logFd != null) {
|
|
446
|
+
try {
|
|
447
|
+
fs.closeSync(logFd);
|
|
448
|
+
} catch {
|
|
449
|
+
/* ignore */
|
|
450
|
+
}
|
|
148
451
|
}
|
|
149
452
|
}
|
|
150
|
-
}
|
|
453
|
+
}
|