@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,521 @@
|
|
|
1
|
+
import fs from 'fs-extra';
|
|
2
|
+
import { runProjectSiteChecks } from './doctor-site.js';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { defaultExec, resolveCommand } from './exec.js';
|
|
5
|
+
import {
|
|
6
|
+
getCaddyfilePath,
|
|
7
|
+
getProjectsPath,
|
|
8
|
+
getCaddyRootCaPath,
|
|
9
|
+
} from './paths.js';
|
|
10
|
+
import { parseBlocks } from './caddyfile.js';
|
|
11
|
+
import { loadProjects } from './projects.js';
|
|
12
|
+
import { runCertsCheck } from './certs.js';
|
|
13
|
+
import { getAutostartStatus } from './autostart.js';
|
|
14
|
+
import {
|
|
15
|
+
CHROME_LNA_HINT,
|
|
16
|
+
findUnpinnedCdnSnippets,
|
|
17
|
+
RECOMMENDED_CDN_SNIPPETS,
|
|
18
|
+
} from './cdn-snippets.js';
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* @typedef {'ok'|'warn'|'fail'|'info'} CheckStatus
|
|
22
|
+
* @typedef {{ id: string, status: CheckStatus, message: string, detail?: string }} DoctorCheck
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* @param {string} id
|
|
27
|
+
* @param {CheckStatus} status
|
|
28
|
+
* @param {string} message
|
|
29
|
+
* @param {string} [detail]
|
|
30
|
+
* @returns {DoctorCheck}
|
|
31
|
+
*/
|
|
32
|
+
function check(id, status, message, detail) {
|
|
33
|
+
return detail ? { id, status, message, detail } : { id, status, message };
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Parse `lsof -nP -iTCP:443 -sTCP:LISTEN` output.
|
|
38
|
+
* @param {string} stdout
|
|
39
|
+
* @returns {{ command: string, pid: string, name: string }[]}
|
|
40
|
+
*/
|
|
41
|
+
export function parseLsofListenOutput(stdout) {
|
|
42
|
+
const lines = String(stdout || '')
|
|
43
|
+
.split('\n')
|
|
44
|
+
.map((l) => l.trim())
|
|
45
|
+
.filter(Boolean);
|
|
46
|
+
const results = [];
|
|
47
|
+
for (const line of lines) {
|
|
48
|
+
if (line.startsWith('COMMAND')) continue;
|
|
49
|
+
const parts = line.split(/\s+/);
|
|
50
|
+
if (parts.length < 2) continue;
|
|
51
|
+
results.push({
|
|
52
|
+
command: parts[0],
|
|
53
|
+
pid: parts[1],
|
|
54
|
+
name: path.basename(parts[0]),
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
return results;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* @param {string} content
|
|
62
|
+
*/
|
|
63
|
+
export function assessResolverContent(content) {
|
|
64
|
+
const text = String(content || '');
|
|
65
|
+
if (!/nameserver\s+127\.0\.0\.1\b/m.test(text)) {
|
|
66
|
+
return {
|
|
67
|
+
status: /** @type {CheckStatus} */ ('fail'),
|
|
68
|
+
message: '/etc/resolver/sp exists but does not point to 127.0.0.1',
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
return {
|
|
72
|
+
status: /** @type {CheckStatus} */ ('ok'),
|
|
73
|
+
message: '/etc/resolver/sp points to 127.0.0.1',
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Structural checks for the Stagepass Caddyfile (not a full Caddy validate).
|
|
79
|
+
* @param {string} content
|
|
80
|
+
*/
|
|
81
|
+
export function assessCaddyfileStructure(content) {
|
|
82
|
+
const text = String(content || '').trim();
|
|
83
|
+
if (!text) {
|
|
84
|
+
return {
|
|
85
|
+
status: /** @type {CheckStatus} */ ('warn'),
|
|
86
|
+
message: 'Caddyfile is empty (no linked projects yet)',
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const blocks = parseBlocks(text);
|
|
91
|
+
if (blocks.length === 0) {
|
|
92
|
+
return {
|
|
93
|
+
status: /** @type {CheckStatus} */ ('warn'),
|
|
94
|
+
message: 'Caddyfile present but no Stagepass START/END site blocks found',
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const legacy = blocks.filter((b) => b.isLegacy);
|
|
99
|
+
const missingRoot = blocks.filter((b) => !b.root);
|
|
100
|
+
if (missingRoot.length > 0) {
|
|
101
|
+
return {
|
|
102
|
+
status: /** @type {CheckStatus} */ ('fail'),
|
|
103
|
+
message: `Caddyfile has ${missingRoot.length} block(s) without a parseable root path`,
|
|
104
|
+
detail: missingRoot.map((b) => b.domain).join(', '),
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
if (legacy.length > 0) {
|
|
109
|
+
return {
|
|
110
|
+
status: /** @type {CheckStatus} */ ('warn'),
|
|
111
|
+
message: `Caddyfile has ${legacy.length} legacy block(s); run stagepass start or reload to migrate`,
|
|
112
|
+
detail: legacy.map((b) => b.domain).join(', '),
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
return {
|
|
117
|
+
status: /** @type {CheckStatus} */ ('ok'),
|
|
118
|
+
message: `Caddyfile structure OK (${blocks.length} site block(s))`,
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Run all doctor checks (read-only). Never uses sudo. Never changes anything.
|
|
124
|
+
*
|
|
125
|
+
* @param {object} [opts]
|
|
126
|
+
* @returns {Promise<{ checks: DoctorCheck[], summary: { ok: number, warn: number, fail: number, info: number }, exitCode: number, hints: string[] }>}
|
|
127
|
+
*/
|
|
128
|
+
export async function runDoctor(opts = {}) {
|
|
129
|
+
const exec = opts.exec || defaultExec;
|
|
130
|
+
const pathExists = opts.pathExists || ((p) => fs.pathExists(p));
|
|
131
|
+
const readFile = opts.readFile || ((p, enc) => fs.readFile(p, enc));
|
|
132
|
+
const checks = /** @type {DoctorCheck[]} */ ([]);
|
|
133
|
+
const hints = /** @type {string[]} */ ([]);
|
|
134
|
+
|
|
135
|
+
// Homebrew
|
|
136
|
+
const brewPath = await resolveCommand('brew', exec);
|
|
137
|
+
if (!brewPath) {
|
|
138
|
+
checks.push(check('homebrew', 'fail', 'Homebrew not found on PATH'));
|
|
139
|
+
} else {
|
|
140
|
+
checks.push(check('homebrew', 'ok', `Homebrew found (${brewPath})`));
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
let brewPrefix = null;
|
|
144
|
+
if (brewPath) {
|
|
145
|
+
try {
|
|
146
|
+
const { stdout } = await exec('brew', ['--prefix']);
|
|
147
|
+
brewPrefix = stdout.trim() || null;
|
|
148
|
+
} catch {
|
|
149
|
+
brewPrefix = null;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// Caddy
|
|
154
|
+
const caddyPath = await resolveCommand('caddy', exec);
|
|
155
|
+
if (!caddyPath) {
|
|
156
|
+
checks.push(check('caddy', 'fail', 'Caddy not found on PATH'));
|
|
157
|
+
} else {
|
|
158
|
+
checks.push(check('caddy', 'ok', `Caddy found (${caddyPath})`));
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// PHP / php-fpm
|
|
162
|
+
const phpPath = await resolveCommand('php', exec);
|
|
163
|
+
let phpFpmPath = await resolveCommand('php-fpm', exec);
|
|
164
|
+
if (!phpFpmPath && brewPrefix) {
|
|
165
|
+
const candidate = path.join(brewPrefix, 'sbin', 'php-fpm');
|
|
166
|
+
if (await pathExists(candidate)) phpFpmPath = candidate;
|
|
167
|
+
}
|
|
168
|
+
if (!phpPath && !phpFpmPath) {
|
|
169
|
+
checks.push(check('php', 'fail', 'PHP / php-fpm not found'));
|
|
170
|
+
} else if (!phpFpmPath) {
|
|
171
|
+
checks.push(
|
|
172
|
+
check(
|
|
173
|
+
'php',
|
|
174
|
+
'warn',
|
|
175
|
+
`PHP found (${phpPath}) but php-fpm binary not found`,
|
|
176
|
+
'Stagepass Caddy blocks use php_fastcgi 127.0.0.1:9000'
|
|
177
|
+
)
|
|
178
|
+
);
|
|
179
|
+
} else {
|
|
180
|
+
checks.push(
|
|
181
|
+
check(
|
|
182
|
+
'php',
|
|
183
|
+
'ok',
|
|
184
|
+
`PHP/php-fpm found (${phpFpmPath}${phpPath ? `; php=${phpPath}` : ''})`
|
|
185
|
+
)
|
|
186
|
+
);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// dnsmasq
|
|
190
|
+
let dnsmasqPath = await resolveCommand('dnsmasq', exec);
|
|
191
|
+
if (!dnsmasqPath && brewPrefix) {
|
|
192
|
+
const candidate = path.join(brewPrefix, 'sbin', 'dnsmasq');
|
|
193
|
+
if (await pathExists(candidate)) dnsmasqPath = candidate;
|
|
194
|
+
}
|
|
195
|
+
if (!dnsmasqPath) {
|
|
196
|
+
checks.push(check('dnsmasq', 'fail', 'dnsmasq not found'));
|
|
197
|
+
} else {
|
|
198
|
+
checks.push(check('dnsmasq', 'ok', `dnsmasq found (${dnsmasqPath})`));
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// /etc/resolver/sp
|
|
202
|
+
const resolverPath = opts.resolverPath || '/etc/resolver/sp';
|
|
203
|
+
if (!(await pathExists(resolverPath))) {
|
|
204
|
+
checks.push(
|
|
205
|
+
check(
|
|
206
|
+
'resolver',
|
|
207
|
+
'fail',
|
|
208
|
+
`${resolverPath} missing`,
|
|
209
|
+
'Run stagepass setup to create the .sp resolver'
|
|
210
|
+
)
|
|
211
|
+
);
|
|
212
|
+
} else {
|
|
213
|
+
try {
|
|
214
|
+
const content = await readFile(resolverPath, 'utf8');
|
|
215
|
+
const assessed = assessResolverContent(content);
|
|
216
|
+
checks.push(check('resolver', assessed.status, assessed.message));
|
|
217
|
+
} catch (error) {
|
|
218
|
+
checks.push(
|
|
219
|
+
check(
|
|
220
|
+
'resolver',
|
|
221
|
+
'fail',
|
|
222
|
+
`Could not read ${resolverPath}`,
|
|
223
|
+
error instanceof Error ? error.message : String(error)
|
|
224
|
+
)
|
|
225
|
+
);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
// Port 443 listener (no sudo)
|
|
230
|
+
try {
|
|
231
|
+
const { stdout } = await exec('lsof', [
|
|
232
|
+
'-nP',
|
|
233
|
+
'-iTCP:443',
|
|
234
|
+
'-sTCP:LISTEN',
|
|
235
|
+
]);
|
|
236
|
+
const listeners = parseLsofListenOutput(stdout);
|
|
237
|
+
if (listeners.length === 0) {
|
|
238
|
+
checks.push(
|
|
239
|
+
check('port443', 'warn', 'Nothing listening on TCP 443', 'Run stagepass start')
|
|
240
|
+
);
|
|
241
|
+
} else {
|
|
242
|
+
const summary = listeners
|
|
243
|
+
.map((l) => `${l.command}(pid ${l.pid})`)
|
|
244
|
+
.join(', ');
|
|
245
|
+
const hasCaddy = listeners.some((l) =>
|
|
246
|
+
/caddy/i.test(l.command) || /caddy/i.test(l.name)
|
|
247
|
+
);
|
|
248
|
+
checks.push(
|
|
249
|
+
check(
|
|
250
|
+
'port443',
|
|
251
|
+
hasCaddy ? 'ok' : 'warn',
|
|
252
|
+
hasCaddy
|
|
253
|
+
? `Port 443 listener: ${summary}`
|
|
254
|
+
: `Port 443 occupied (no caddy seen): ${summary}`
|
|
255
|
+
)
|
|
256
|
+
);
|
|
257
|
+
}
|
|
258
|
+
} catch (error) {
|
|
259
|
+
const err = /** @type {{ code?: string|number, message?: string }} */ (error);
|
|
260
|
+
const msg = err.message || String(error);
|
|
261
|
+
if (
|
|
262
|
+
err.code === 'EPERM' ||
|
|
263
|
+
err.code === 'EACCES' ||
|
|
264
|
+
/permission denied/i.test(msg)
|
|
265
|
+
) {
|
|
266
|
+
checks.push(
|
|
267
|
+
check(
|
|
268
|
+
'port443',
|
|
269
|
+
'warn',
|
|
270
|
+
'Could not inspect port 443 (permission denied)',
|
|
271
|
+
'Doctor never uses sudo; try manually: lsof -nP -iTCP:443 -sTCP:LISTEN'
|
|
272
|
+
)
|
|
273
|
+
);
|
|
274
|
+
} else if (err.code === 1 || err.code === '1') {
|
|
275
|
+
// lsof exits 1 when no matches
|
|
276
|
+
checks.push(
|
|
277
|
+
check('port443', 'warn', 'Nothing listening on TCP 443', 'Run stagepass start')
|
|
278
|
+
);
|
|
279
|
+
} else {
|
|
280
|
+
checks.push(check('port443', 'warn', `Could not inspect port 443: ${msg}`));
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
// Caddyfile
|
|
285
|
+
const caddyfilePath = opts.caddyfilePath || getCaddyfilePath();
|
|
286
|
+
if (!(await pathExists(caddyfilePath))) {
|
|
287
|
+
checks.push(
|
|
288
|
+
check(
|
|
289
|
+
'caddyfile',
|
|
290
|
+
'fail',
|
|
291
|
+
`Stagepass Caddyfile missing (${caddyfilePath})`,
|
|
292
|
+
'Run stagepass setup / link'
|
|
293
|
+
)
|
|
294
|
+
);
|
|
295
|
+
} else {
|
|
296
|
+
try {
|
|
297
|
+
const content = await readFile(caddyfilePath, 'utf8');
|
|
298
|
+
const assessed = assessCaddyfileStructure(content);
|
|
299
|
+
checks.push(
|
|
300
|
+
check('caddyfile', assessed.status, assessed.message, assessed.detail)
|
|
301
|
+
);
|
|
302
|
+
} catch (error) {
|
|
303
|
+
checks.push(
|
|
304
|
+
check(
|
|
305
|
+
'caddyfile',
|
|
306
|
+
'fail',
|
|
307
|
+
'Could not read Caddyfile',
|
|
308
|
+
error instanceof Error ? error.message : String(error)
|
|
309
|
+
)
|
|
310
|
+
);
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
// Linked projects whose folders no longer exist
|
|
315
|
+
const projectsPath = opts.projectsPath || getProjectsPath();
|
|
316
|
+
try {
|
|
317
|
+
const doc = await loadProjects(projectsPath);
|
|
318
|
+
const domains = Object.keys(doc.projects || {});
|
|
319
|
+
const missing = [];
|
|
320
|
+
for (const domain of domains) {
|
|
321
|
+
const root = doc.projects[domain]?.root;
|
|
322
|
+
if (!root || !(await pathExists(root))) {
|
|
323
|
+
missing.push(`${domain} -> ${root || '(no root)'}`);
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
if (domains.length === 0) {
|
|
327
|
+
checks.push(check('projects', 'warn', 'No linked projects in projects.json'));
|
|
328
|
+
} else if (missing.length > 0) {
|
|
329
|
+
checks.push(
|
|
330
|
+
check(
|
|
331
|
+
'projects',
|
|
332
|
+
'fail',
|
|
333
|
+
`${missing.length} linked project folder(s) missing`,
|
|
334
|
+
missing.join('; ')
|
|
335
|
+
)
|
|
336
|
+
);
|
|
337
|
+
} else {
|
|
338
|
+
checks.push(
|
|
339
|
+
check('projects', 'ok', `${domains.length} linked project(s) exist on disk`)
|
|
340
|
+
);
|
|
341
|
+
}
|
|
342
|
+
} catch (error) {
|
|
343
|
+
checks.push(
|
|
344
|
+
check(
|
|
345
|
+
'projects',
|
|
346
|
+
'warn',
|
|
347
|
+
'Could not read projects.json',
|
|
348
|
+
error instanceof Error ? error.message : String(error)
|
|
349
|
+
)
|
|
350
|
+
);
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
// Certificates (check only)
|
|
354
|
+
const projectsDoc = await loadProjects(projectsPath);
|
|
355
|
+
const linkedDomains = Object.keys(projectsDoc.projects || {});
|
|
356
|
+
const certReport = await runCertsCheck({
|
|
357
|
+
exec,
|
|
358
|
+
rootPath: opts.rootCaPath || getCaddyRootCaPath(),
|
|
359
|
+
brewPrefix,
|
|
360
|
+
domains: opts.skipHandshake ? [] : linkedDomains,
|
|
361
|
+
skipHandshake: opts.skipHandshake === true,
|
|
362
|
+
now: opts.now,
|
|
363
|
+
pathExists,
|
|
364
|
+
readFile,
|
|
365
|
+
connect: opts.connect,
|
|
366
|
+
handshakeTimeoutMs: opts.handshakeTimeoutMs,
|
|
367
|
+
});
|
|
368
|
+
|
|
369
|
+
if (certReport.root.status === 'fail') {
|
|
370
|
+
checks.push(
|
|
371
|
+
check('certs.root', 'fail', certReport.root.reason, certReport.root.path)
|
|
372
|
+
);
|
|
373
|
+
} else if (certReport.root.status === 'warn') {
|
|
374
|
+
checks.push(
|
|
375
|
+
check('certs.root', 'warn', certReport.root.reason, certReport.root.path)
|
|
376
|
+
);
|
|
377
|
+
} else {
|
|
378
|
+
checks.push(
|
|
379
|
+
check('certs.root', 'ok', certReport.root.reason, certReport.root.path)
|
|
380
|
+
);
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
if (!certReport.trust?.trusted) {
|
|
384
|
+
checks.push(
|
|
385
|
+
check(
|
|
386
|
+
'certs.trust',
|
|
387
|
+
'fail',
|
|
388
|
+
'Caddy local root is not trusted in the System keychain',
|
|
389
|
+
certReport.trust?.error || 'Run: stagepass certs trust'
|
|
390
|
+
)
|
|
391
|
+
);
|
|
392
|
+
} else {
|
|
393
|
+
checks.push(
|
|
394
|
+
check('certs.trust', 'ok', 'Caddy local root is trusted in the System keychain')
|
|
395
|
+
);
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
if (certReport.orphans?.length > 0) {
|
|
399
|
+
const detail = certReport.orphans
|
|
400
|
+
.map((o) => `SHA-1=${o.sha1 || 'unknown'} SHA-256=${o.sha256}`)
|
|
401
|
+
.join('; ');
|
|
402
|
+
checks.push(
|
|
403
|
+
check(
|
|
404
|
+
'certs.orphans',
|
|
405
|
+
'warn',
|
|
406
|
+
`${certReport.orphans.length} orphaned Caddy root(s) in keychain (report only; not deleted)`,
|
|
407
|
+
detail
|
|
408
|
+
)
|
|
409
|
+
);
|
|
410
|
+
} else {
|
|
411
|
+
checks.push(check('certs.orphans', 'ok', 'No orphaned Caddy roots in keychain'));
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
for (const secondary of certReport.secondaryRoots || []) {
|
|
415
|
+
checks.push(check('certs.secondary', 'warn', secondary.note, secondary.path));
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
for (const hs of certReport.handshakes || []) {
|
|
419
|
+
checks.push(
|
|
420
|
+
check(
|
|
421
|
+
`certs.tls.${hs.servername}`,
|
|
422
|
+
hs.status,
|
|
423
|
+
hs.reason || `TLS check for ${hs.servername}`
|
|
424
|
+
)
|
|
425
|
+
);
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
// LaunchAgent
|
|
429
|
+
const autostart = await getAutostartStatus({
|
|
430
|
+
exec,
|
|
431
|
+
pathExists,
|
|
432
|
+
uid: opts.uid,
|
|
433
|
+
plistPath: opts.plistPath,
|
|
434
|
+
});
|
|
435
|
+
if (!autostart.plistExists && !autostart.loaded) {
|
|
436
|
+
checks.push(
|
|
437
|
+
check(
|
|
438
|
+
'autostart',
|
|
439
|
+
'warn',
|
|
440
|
+
'LaunchAgent not installed',
|
|
441
|
+
'Optional: stagepass autostart on'
|
|
442
|
+
)
|
|
443
|
+
);
|
|
444
|
+
} else if (autostart.plistExists && !autostart.loaded) {
|
|
445
|
+
checks.push(
|
|
446
|
+
check(
|
|
447
|
+
'autostart',
|
|
448
|
+
'warn',
|
|
449
|
+
`LaunchAgent plist present but not loaded (${autostart.label})`,
|
|
450
|
+
autostart.plistPath
|
|
451
|
+
)
|
|
452
|
+
);
|
|
453
|
+
} else {
|
|
454
|
+
checks.push(
|
|
455
|
+
check(
|
|
456
|
+
'autostart',
|
|
457
|
+
'ok',
|
|
458
|
+
`LaunchAgent loaded (${autostart.label})`,
|
|
459
|
+
autostart.plistPath
|
|
460
|
+
)
|
|
461
|
+
);
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
// CDN snippets (Stagepass-recommended; do not scan user sites)
|
|
465
|
+
const snippets = opts.cdnSnippets || RECOMMENDED_CDN_SNIPPETS;
|
|
466
|
+
const unpinned = findUnpinnedCdnSnippets(snippets);
|
|
467
|
+
if (!unpinned.ok) {
|
|
468
|
+
checks.push(
|
|
469
|
+
check(
|
|
470
|
+
'cdn',
|
|
471
|
+
'warn',
|
|
472
|
+
'Recommended CDN snippets include @latest (prefer major pin @2)',
|
|
473
|
+
unpinned.offenders.join(', ')
|
|
474
|
+
)
|
|
475
|
+
);
|
|
476
|
+
} else {
|
|
477
|
+
checks.push(
|
|
478
|
+
check(
|
|
479
|
+
'cdn',
|
|
480
|
+
'ok',
|
|
481
|
+
'Recommended CDN snippets use a pinned major (@2), not @latest'
|
|
482
|
+
)
|
|
483
|
+
);
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
hints.push(CHROME_LNA_HINT);
|
|
487
|
+
|
|
488
|
+
// Current project folder: declared modules vs. live site (read-only GET)
|
|
489
|
+
if (opts.projectChecks !== false) {
|
|
490
|
+
try {
|
|
491
|
+
const projectChecks = await runProjectSiteChecks({
|
|
492
|
+
cwd: opts.cwd,
|
|
493
|
+
skipSite: opts.skipSite === true,
|
|
494
|
+
detectSite: opts.detectSite,
|
|
495
|
+
loadProjects: opts.loadProjects,
|
|
496
|
+
});
|
|
497
|
+
for (const c of projectChecks) checks.push(check(c.id, c.status, c.message, c.detail));
|
|
498
|
+
} catch (error) {
|
|
499
|
+
checks.push(
|
|
500
|
+
check('project', 'warn', 'Project check failed', error instanceof Error ? error.message : String(error))
|
|
501
|
+
);
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
const summary = { ok: 0, warn: 0, fail: 0, info: 0 };
|
|
506
|
+
for (const c of checks) {
|
|
507
|
+
if (c.status === 'ok') summary.ok += 1;
|
|
508
|
+
else if (c.status === 'warn') summary.warn += 1;
|
|
509
|
+
else if (c.status === 'fail') summary.fail += 1;
|
|
510
|
+
else summary.info += 1;
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
return {
|
|
514
|
+
checks,
|
|
515
|
+
summary,
|
|
516
|
+
exitCode: summary.fail > 0 ? 1 : 0,
|
|
517
|
+
hints,
|
|
518
|
+
certReport,
|
|
519
|
+
autostart,
|
|
520
|
+
};
|
|
521
|
+
}
|
package/src/lib/exec.js
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { execFile as nodeExecFile } from 'node:child_process';
|
|
2
|
+
import { promisify } from 'node:util';
|
|
3
|
+
|
|
4
|
+
const execFileAsync = promisify(nodeExecFile);
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @typedef {object} ExecResult
|
|
8
|
+
* @property {string} stdout
|
|
9
|
+
* @property {string} stderr
|
|
10
|
+
* @property {number|null} [code]
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* @typedef {(file: string, args?: string[], options?: object) => Promise<ExecResult>} ExecFn
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Default execFile wrapper. Always uses argument arrays (never a shell string).
|
|
19
|
+
* @type {ExecFn}
|
|
20
|
+
*/
|
|
21
|
+
export async function defaultExec(file, args = [], options = {}) {
|
|
22
|
+
const inheritStdio = options.stdio === 'inherit';
|
|
23
|
+
// Always force shell:false after spreading options so callers cannot enable a shell.
|
|
24
|
+
const opts = inheritStdio
|
|
25
|
+
? { ...options, shell: false }
|
|
26
|
+
: {
|
|
27
|
+
encoding: 'utf8',
|
|
28
|
+
maxBuffer: 4 * 1024 * 1024,
|
|
29
|
+
...options,
|
|
30
|
+
shell: false,
|
|
31
|
+
};
|
|
32
|
+
try {
|
|
33
|
+
const result = await execFileAsync(file, args, opts);
|
|
34
|
+
return {
|
|
35
|
+
stdout: result?.stdout != null ? String(result.stdout) : '',
|
|
36
|
+
stderr: result?.stderr != null ? String(result.stderr) : '',
|
|
37
|
+
code: 0,
|
|
38
|
+
};
|
|
39
|
+
} catch (error) {
|
|
40
|
+
const err = /** @type {NodeJS.ErrnoException & { stdout?: string, stderr?: string, code?: number|string }} */ (
|
|
41
|
+
error
|
|
42
|
+
);
|
|
43
|
+
const wrapped = new Error(err.message || String(error));
|
|
44
|
+
Object.assign(wrapped, {
|
|
45
|
+
code: err.code,
|
|
46
|
+
errno: err.errno,
|
|
47
|
+
stdout: err.stdout != null ? String(err.stdout) : '',
|
|
48
|
+
stderr: err.stderr != null ? String(err.stderr) : '',
|
|
49
|
+
killed: /** @type {{ killed?: boolean }} */ (err).killed,
|
|
50
|
+
signal: /** @type {{ signal?: string }} */ (err).signal,
|
|
51
|
+
});
|
|
52
|
+
throw wrapped;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Resolve which executable exists via `command -v` style lookup without a shell.
|
|
58
|
+
* Uses `which` on PATH via execFile.
|
|
59
|
+
* @param {string} name
|
|
60
|
+
* @param {ExecFn} [exec]
|
|
61
|
+
* @returns {Promise<string|null>}
|
|
62
|
+
*/
|
|
63
|
+
export async function resolveCommand(name, exec = defaultExec) {
|
|
64
|
+
try {
|
|
65
|
+
const { stdout } = await exec('/usr/bin/env', ['which', name]);
|
|
66
|
+
const path = stdout.trim().split('\n')[0];
|
|
67
|
+
return path || null;
|
|
68
|
+
} catch {
|
|
69
|
+
return null;
|
|
70
|
+
}
|
|
71
|
+
}
|
package/src/lib/paths.js
CHANGED
|
@@ -18,6 +18,64 @@ export function getServerLogPath() {
|
|
|
18
18
|
return path.join(getStagepassDir(), 'server.log');
|
|
19
19
|
}
|
|
20
20
|
|
|
21
|
+
/** Daemon / autostart log directory (~/.stagepass/logs). */
|
|
22
|
+
export function getLogsDir() {
|
|
23
|
+
return path.join(getStagepassDir(), 'logs');
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function getDaemonStdoutLogPath() {
|
|
27
|
+
return path.join(getLogsDir(), 'daemon.stdout.log');
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function getDaemonStderrLogPath() {
|
|
31
|
+
return path.join(getLogsDir(), 'daemon.stderr.log');
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** LaunchAgent label (owned GitHub-io reverse DNS; not a third-party domain). */
|
|
35
|
+
export const LAUNCH_AGENT_LABEL = 'io.github.arobertherz.stagepass';
|
|
36
|
+
|
|
37
|
+
export function getLaunchAgentsDir() {
|
|
38
|
+
return path.join(os.homedir(), 'Library', 'LaunchAgents');
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function getLaunchAgentPlistPath() {
|
|
42
|
+
return path.join(getLaunchAgentsDir(), `${LAUNCH_AGENT_LABEL}.plist`);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Caddy default data directory on macOS (user instance).
|
|
47
|
+
* Honours XDG_DATA_HOME when set (same rules as Caddy itself).
|
|
48
|
+
* @param {{ home?: string, env?: NodeJS.ProcessEnv }} [opts]
|
|
49
|
+
*/
|
|
50
|
+
export function getCaddyDataDir(opts = {}) {
|
|
51
|
+
const env = opts.env || process.env;
|
|
52
|
+
const home = opts.home || os.homedir();
|
|
53
|
+
if (env.XDG_DATA_HOME) {
|
|
54
|
+
return path.join(env.XDG_DATA_HOME, 'caddy');
|
|
55
|
+
}
|
|
56
|
+
return path.join(home, 'Library', 'Application Support', 'Caddy');
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Path to Caddy's local root CA certificate.
|
|
61
|
+
* @param {{ home?: string, env?: NodeJS.ProcessEnv, dataDir?: string }} [opts]
|
|
62
|
+
*/
|
|
63
|
+
export function getCaddyRootCaPath(opts = {}) {
|
|
64
|
+
const dataDir = opts.dataDir || getCaddyDataDir(opts);
|
|
65
|
+
return path.join(dataDir, 'pki', 'authorities', 'local', 'root.crt');
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Common secondary data dir when Homebrew's Caddy service sets XDG_DATA_HOME.
|
|
70
|
+
* Report-only helper for doctor/certs (never delete).
|
|
71
|
+
* @param {{ brewPrefix?: string|null }} [opts]
|
|
72
|
+
*/
|
|
73
|
+
export function getBrewCaddyDataDir(opts = {}) {
|
|
74
|
+
const prefix = opts.brewPrefix;
|
|
75
|
+
if (!prefix) return null;
|
|
76
|
+
return path.join(prefix, 'var', 'lib', 'caddy');
|
|
77
|
+
}
|
|
78
|
+
|
|
21
79
|
/** System / dangerous roots that must never be linked as site roots. */
|
|
22
80
|
export const FORBIDDEN_ROOTS = Object.freeze([
|
|
23
81
|
'/',
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/** Env var that skips the macOS-only guard (tests/CI only). */
|
|
2
|
+
export const SKIP_PLATFORM_CHECK_ENV = 'STAGEPASS_SKIP_PLATFORM_CHECK';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @param {string} [platform=process.platform]
|
|
6
|
+
* @returns {boolean}
|
|
7
|
+
*/
|
|
8
|
+
export function isMacOS(platform = process.platform) {
|
|
9
|
+
return platform === 'darwin';
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Build the user-facing unsupported-platform message.
|
|
14
|
+
* @param {string} platform
|
|
15
|
+
* @returns {string}
|
|
16
|
+
*/
|
|
17
|
+
export function unsupportedPlatformMessage(platform) {
|
|
18
|
+
return `Stagepass CLI supports macOS only (detected: ${platform}).`;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Exit with a clear error when running system commands off macOS.
|
|
23
|
+
* No-op when `STAGEPASS_SKIP_PLATFORM_CHECK=1` (tests/CI only) or on darwin.
|
|
24
|
+
*
|
|
25
|
+
* @param {object} [options]
|
|
26
|
+
* @param {string} [options.platform]
|
|
27
|
+
* @param {NodeJS.ProcessEnv} [options.env]
|
|
28
|
+
* @param {(code: number) => void} [options.exit]
|
|
29
|
+
* @param {(msg: string) => void} [options.write]
|
|
30
|
+
*/
|
|
31
|
+
export function assertSupportedPlatform(options = {}) {
|
|
32
|
+
const platform = options.platform ?? process.platform;
|
|
33
|
+
const env = options.env ?? process.env;
|
|
34
|
+
const exit = options.exit ?? ((code) => process.exit(code));
|
|
35
|
+
const write = options.write ?? ((msg) => console.error(msg));
|
|
36
|
+
|
|
37
|
+
if (env[SKIP_PLATFORM_CHECK_ENV] === '1') {
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if (isMacOS(platform)) {
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
write(unsupportedPlatformMessage(platform));
|
|
46
|
+
exit(1);
|
|
47
|
+
}
|