@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,360 @@
|
|
|
1
|
+
import fs from 'fs-extra';
|
|
2
|
+
import os from 'node:os';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import {
|
|
5
|
+
LAUNCH_AGENT_LABEL,
|
|
6
|
+
getLaunchAgentPlistPath,
|
|
7
|
+
getLaunchAgentsDir,
|
|
8
|
+
getLogsDir,
|
|
9
|
+
getDaemonStdoutLogPath,
|
|
10
|
+
getDaemonStderrLogPath,
|
|
11
|
+
getCaddyfilePath,
|
|
12
|
+
} from './paths.js';
|
|
13
|
+
import { defaultExec, resolveCommand } from './exec.js';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Escape a string for use inside an XML plist text node.
|
|
17
|
+
* @param {string} value
|
|
18
|
+
*/
|
|
19
|
+
export function escapeXml(value) {
|
|
20
|
+
return String(value)
|
|
21
|
+
.replace(/&/g, '&')
|
|
22
|
+
.replace(/</g, '<')
|
|
23
|
+
.replace(/>/g, '>')
|
|
24
|
+
.replace(/"/g, '"')
|
|
25
|
+
.replace(/'/g, ''');
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Build LaunchAgent ProgramArguments for `stagepass daemon`.
|
|
30
|
+
* @param {{ nodePath: string, cliPath: string }} paths
|
|
31
|
+
* @returns {string[]}
|
|
32
|
+
*/
|
|
33
|
+
export function buildDaemonProgramArguments(paths) {
|
|
34
|
+
return [paths.nodePath, paths.cliPath, 'daemon'];
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* launchctl bootstrap arguments (gui domain).
|
|
39
|
+
* @param {number|string} uid
|
|
40
|
+
* @param {string} plistPath
|
|
41
|
+
* @returns {string[]}
|
|
42
|
+
*/
|
|
43
|
+
export function buildLaunchctlBootstrapArgs(uid, plistPath) {
|
|
44
|
+
return ['bootstrap', `gui/${uid}`, plistPath];
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* launchctl bootout arguments (gui domain).
|
|
49
|
+
* @param {number|string} uid
|
|
50
|
+
* @param {string} [label]
|
|
51
|
+
* @returns {string[]}
|
|
52
|
+
*/
|
|
53
|
+
export function buildLaunchctlBootoutArgs(uid, label = LAUNCH_AGENT_LABEL) {
|
|
54
|
+
return ['bootout', `gui/${uid}/${label}`];
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* launchctl print arguments (gui domain).
|
|
59
|
+
* @param {number|string} uid
|
|
60
|
+
* @param {string} [label]
|
|
61
|
+
* @returns {string[]}
|
|
62
|
+
*/
|
|
63
|
+
export function buildLaunchctlPrintArgs(uid, label = LAUNCH_AGENT_LABEL) {
|
|
64
|
+
return ['print', `gui/${uid}/${label}`];
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Render a LaunchAgent plist that runs `stagepass daemon` at login.
|
|
69
|
+
*
|
|
70
|
+
* Port 443: existing `stagepass start` runs `caddy start` as the user (sudo is
|
|
71
|
+
* only used for conflict detection / kill). A user LaunchAgent therefore has the
|
|
72
|
+
* same bind privileges as interactive `start` on macOS. If bind fails at
|
|
73
|
+
* runtime, `daemon` logs a clear error and exits 0 so KeepAlive does not loop.
|
|
74
|
+
*
|
|
75
|
+
* @param {object} opts
|
|
76
|
+
* @param {string} opts.nodePath
|
|
77
|
+
* @param {string} opts.cliPath
|
|
78
|
+
* @param {string} [opts.label]
|
|
79
|
+
* @param {string} [opts.stdoutPath]
|
|
80
|
+
* @param {string} [opts.stderrPath]
|
|
81
|
+
* @param {string} [opts.workingDirectory]
|
|
82
|
+
*/
|
|
83
|
+
export function renderLaunchAgentPlist(opts) {
|
|
84
|
+
const label = opts.label || LAUNCH_AGENT_LABEL;
|
|
85
|
+
const programArguments = buildDaemonProgramArguments({
|
|
86
|
+
nodePath: opts.nodePath,
|
|
87
|
+
cliPath: opts.cliPath,
|
|
88
|
+
});
|
|
89
|
+
const stdoutPath = opts.stdoutPath || getDaemonStdoutLogPath();
|
|
90
|
+
const stderrPath = opts.stderrPath || getDaemonStderrLogPath();
|
|
91
|
+
const workingDirectory = opts.workingDirectory || os.homedir();
|
|
92
|
+
|
|
93
|
+
const argXml = programArguments
|
|
94
|
+
.map((a) => `\t\t<string>${escapeXml(a)}</string>`)
|
|
95
|
+
.join('\n');
|
|
96
|
+
|
|
97
|
+
return `<?xml version="1.0" encoding="UTF-8"?>
|
|
98
|
+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
|
99
|
+
<plist version="1.0">
|
|
100
|
+
<dict>
|
|
101
|
+
\t<key>Label</key>
|
|
102
|
+
\t<string>${escapeXml(label)}</string>
|
|
103
|
+
\t<key>ProgramArguments</key>
|
|
104
|
+
\t<array>
|
|
105
|
+
${argXml}
|
|
106
|
+
\t</array>
|
|
107
|
+
\t<key>WorkingDirectory</key>
|
|
108
|
+
\t<string>${escapeXml(workingDirectory)}</string>
|
|
109
|
+
\t<key>RunAtLoad</key>
|
|
110
|
+
\t<true/>
|
|
111
|
+
\t<key>KeepAlive</key>
|
|
112
|
+
\t<dict>
|
|
113
|
+
\t\t<key>SuccessfulExit</key>
|
|
114
|
+
\t\t<false/>
|
|
115
|
+
\t</dict>
|
|
116
|
+
\t<key>StandardOutPath</key>
|
|
117
|
+
\t<string>${escapeXml(stdoutPath)}</string>
|
|
118
|
+
\t<key>StandardErrorPath</key>
|
|
119
|
+
\t<string>${escapeXml(stderrPath)}</string>
|
|
120
|
+
</dict>
|
|
121
|
+
</plist>
|
|
122
|
+
`;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Require an absolute path that exists on disk (LaunchAgent ProgramArguments).
|
|
127
|
+
* @param {string|null|undefined} candidate
|
|
128
|
+
* @param {string} label
|
|
129
|
+
* @param {(p: string) => Promise<boolean>} pathExists
|
|
130
|
+
* @returns {Promise<{ ok: true, path: string } | { ok: false, error: string }>}
|
|
131
|
+
*/
|
|
132
|
+
async function requireAbsoluteExistingPath(candidate, label, pathExists) {
|
|
133
|
+
if (typeof candidate !== 'string' || candidate.length === 0) {
|
|
134
|
+
return { ok: false, error: `Could not resolve absolute path to ${label}.` };
|
|
135
|
+
}
|
|
136
|
+
if (!path.isAbsolute(candidate)) {
|
|
137
|
+
return {
|
|
138
|
+
ok: false,
|
|
139
|
+
error: `${label} path must be absolute (got: ${candidate}).`,
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
if (!(await pathExists(candidate))) {
|
|
143
|
+
return {
|
|
144
|
+
ok: false,
|
|
145
|
+
error: `${label} path does not exist: ${candidate}`,
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
return { ok: true, path: candidate };
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Resolve absolute node + CLI entry paths for the LaunchAgent.
|
|
153
|
+
* @param {object} [opts]
|
|
154
|
+
* @param {string} [opts.execPath]
|
|
155
|
+
* @param {string} [opts.argv1]
|
|
156
|
+
* @param {import('./exec.js').ExecFn} [opts.exec]
|
|
157
|
+
* @param {(p: string) => Promise<string>} [opts.realpath]
|
|
158
|
+
* @param {(p: string) => Promise<boolean>} [opts.pathExists]
|
|
159
|
+
*/
|
|
160
|
+
export async function resolveAutostartPaths(opts = {}) {
|
|
161
|
+
const execPath = opts.execPath || process.execPath;
|
|
162
|
+
// Nullish coalescing so tests can inject '' / null without falling back to process.argv[1].
|
|
163
|
+
const argv1 = opts.argv1 !== undefined ? opts.argv1 : process.argv[1];
|
|
164
|
+
const realpath =
|
|
165
|
+
opts.realpath || ((p) => fs.realpath(p).then((r) => String(r)));
|
|
166
|
+
const exec = opts.exec || defaultExec;
|
|
167
|
+
const pathExists = opts.pathExists || ((p) => fs.pathExists(p));
|
|
168
|
+
|
|
169
|
+
const nodeResolved = path.resolve(execPath);
|
|
170
|
+
const nodeCheck = await requireAbsoluteExistingPath(
|
|
171
|
+
nodeResolved,
|
|
172
|
+
'node',
|
|
173
|
+
pathExists
|
|
174
|
+
);
|
|
175
|
+
if (!nodeCheck.ok) {
|
|
176
|
+
return {
|
|
177
|
+
ok: false,
|
|
178
|
+
error: nodeCheck.error,
|
|
179
|
+
nodePath: nodeResolved,
|
|
180
|
+
cliPath: null,
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
const nodePath = nodeCheck.path;
|
|
184
|
+
|
|
185
|
+
let cliPath = null;
|
|
186
|
+
|
|
187
|
+
if (argv1) {
|
|
188
|
+
try {
|
|
189
|
+
cliPath = await realpath(argv1);
|
|
190
|
+
} catch {
|
|
191
|
+
cliPath = path.resolve(argv1);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
if (!cliPath) {
|
|
196
|
+
const whichStagepass = await resolveCommand('stagepass', exec);
|
|
197
|
+
if (whichStagepass) {
|
|
198
|
+
try {
|
|
199
|
+
cliPath = await realpath(whichStagepass);
|
|
200
|
+
} catch {
|
|
201
|
+
cliPath = whichStagepass;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
const cliCheck = await requireAbsoluteExistingPath(
|
|
207
|
+
cliPath,
|
|
208
|
+
'stagepass CLI entrypoint',
|
|
209
|
+
pathExists
|
|
210
|
+
);
|
|
211
|
+
if (!cliCheck.ok) {
|
|
212
|
+
return {
|
|
213
|
+
ok: false,
|
|
214
|
+
error: cliCheck.error,
|
|
215
|
+
nodePath,
|
|
216
|
+
cliPath: cliPath || null,
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
return { ok: true, nodePath, cliPath: cliCheck.path, error: null };
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* @param {object} [opts]
|
|
225
|
+
* @param {import('./exec.js').ExecFn} [opts.exec]
|
|
226
|
+
* @param {number|string} [opts.uid]
|
|
227
|
+
* @param {string} [opts.plistPath]
|
|
228
|
+
*/
|
|
229
|
+
export async function getAutostartStatus(opts = {}) {
|
|
230
|
+
const exec = opts.exec || defaultExec;
|
|
231
|
+
const uid = opts.uid ?? process.getuid?.() ?? os.userInfo().uid;
|
|
232
|
+
const plistPath = opts.plistPath || getLaunchAgentPlistPath();
|
|
233
|
+
const pathExists = opts.pathExists || ((p) => fs.pathExists(p));
|
|
234
|
+
const plistExists = await pathExists(plistPath);
|
|
235
|
+
|
|
236
|
+
let loaded = false;
|
|
237
|
+
let printOutput = '';
|
|
238
|
+
let printError = null;
|
|
239
|
+
try {
|
|
240
|
+
const result = await exec('launchctl', buildLaunchctlPrintArgs(uid));
|
|
241
|
+
printOutput = result.stdout || '';
|
|
242
|
+
loaded = true;
|
|
243
|
+
} catch (error) {
|
|
244
|
+
printError = error instanceof Error ? error.message : String(error);
|
|
245
|
+
loaded = false;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
return {
|
|
249
|
+
label: LAUNCH_AGENT_LABEL,
|
|
250
|
+
plistPath,
|
|
251
|
+
plistExists,
|
|
252
|
+
loaded,
|
|
253
|
+
printOutput,
|
|
254
|
+
printError,
|
|
255
|
+
logsDir: getLogsDir(),
|
|
256
|
+
caddyfilePath: getCaddyfilePath(),
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* Write plist (idempotent) and bootstrap the LaunchAgent.
|
|
262
|
+
* @param {object} [opts]
|
|
263
|
+
*/
|
|
264
|
+
export async function enableAutostart(opts = {}) {
|
|
265
|
+
const exec = opts.exec || defaultExec;
|
|
266
|
+
const uid = opts.uid ?? process.getuid?.() ?? os.userInfo().uid;
|
|
267
|
+
const paths =
|
|
268
|
+
opts.paths ||
|
|
269
|
+
(await resolveAutostartPaths({
|
|
270
|
+
execPath: opts.execPath,
|
|
271
|
+
argv1: opts.argv1,
|
|
272
|
+
exec,
|
|
273
|
+
realpath: opts.realpath,
|
|
274
|
+
pathExists: opts.pathExists,
|
|
275
|
+
}));
|
|
276
|
+
|
|
277
|
+
if (!paths.ok) {
|
|
278
|
+
return { ok: false, error: paths.error };
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
// Defense in depth when callers inject `paths` directly.
|
|
282
|
+
if (!path.isAbsolute(paths.nodePath) || !path.isAbsolute(paths.cliPath)) {
|
|
283
|
+
return {
|
|
284
|
+
ok: false,
|
|
285
|
+
error: 'Autostart nodePath and cliPath must be absolute.',
|
|
286
|
+
};
|
|
287
|
+
}
|
|
288
|
+
const pathExists = opts.pathExists || ((p) => fs.pathExists(p));
|
|
289
|
+
if (!(await pathExists(paths.nodePath)) || !(await pathExists(paths.cliPath))) {
|
|
290
|
+
return {
|
|
291
|
+
ok: false,
|
|
292
|
+
error: 'Autostart nodePath or cliPath does not exist on disk.',
|
|
293
|
+
};
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
const plistPath = opts.plistPath || getLaunchAgentPlistPath();
|
|
297
|
+
const agentsDir = path.dirname(plistPath);
|
|
298
|
+
const ensureDir = opts.ensureDir || ((p) => fs.ensureDir(p));
|
|
299
|
+
const writeFile = opts.writeFile || ((p, c) => fs.writeFile(p, c, 'utf8'));
|
|
300
|
+
const logsDir = opts.logsDir || getLogsDir();
|
|
301
|
+
|
|
302
|
+
await ensureDir(agentsDir);
|
|
303
|
+
await ensureDir(logsDir);
|
|
304
|
+
|
|
305
|
+
const plist = renderLaunchAgentPlist({
|
|
306
|
+
nodePath: paths.nodePath,
|
|
307
|
+
cliPath: paths.cliPath,
|
|
308
|
+
stdoutPath: opts.stdoutPath || getDaemonStdoutLogPath(),
|
|
309
|
+
stderrPath: opts.stderrPath || getDaemonStderrLogPath(),
|
|
310
|
+
workingDirectory: opts.workingDirectory || os.homedir(),
|
|
311
|
+
});
|
|
312
|
+
|
|
313
|
+
await writeFile(plistPath, plist);
|
|
314
|
+
|
|
315
|
+
// Idempotent: bootout if already loaded, then bootstrap.
|
|
316
|
+
try {
|
|
317
|
+
await exec('launchctl', buildLaunchctlBootoutArgs(uid));
|
|
318
|
+
} catch {
|
|
319
|
+
// not loaded yet
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
try {
|
|
323
|
+
await exec('launchctl', buildLaunchctlBootstrapArgs(uid, plistPath));
|
|
324
|
+
} catch (error) {
|
|
325
|
+
return {
|
|
326
|
+
ok: false,
|
|
327
|
+
error: error instanceof Error ? error.message : String(error),
|
|
328
|
+
plistPath,
|
|
329
|
+
paths,
|
|
330
|
+
};
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
return { ok: true, plistPath, paths, error: null };
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/**
|
|
337
|
+
* Bootout and remove the LaunchAgent plist.
|
|
338
|
+
* @param {object} [opts]
|
|
339
|
+
*/
|
|
340
|
+
export async function disableAutostart(opts = {}) {
|
|
341
|
+
const exec = opts.exec || defaultExec;
|
|
342
|
+
const uid = opts.uid ?? process.getuid?.() ?? os.userInfo().uid;
|
|
343
|
+
const plistPath = opts.plistPath || getLaunchAgentPlistPath();
|
|
344
|
+
const pathExists = opts.pathExists || ((p) => fs.pathExists(p));
|
|
345
|
+
const remove = opts.remove || ((p) => fs.remove(p));
|
|
346
|
+
|
|
347
|
+
try {
|
|
348
|
+
await exec('launchctl', buildLaunchctlBootoutArgs(uid));
|
|
349
|
+
} catch {
|
|
350
|
+
// ignore if not loaded
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
if (await pathExists(plistPath)) {
|
|
354
|
+
await remove(plistPath);
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
return { ok: true, plistPath };
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
export { LAUNCH_AGENT_LABEL, getLaunchAgentPlistPath, getLaunchAgentsDir };
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Canonical CDN embed snippets recommended by Stagepass docs.
|
|
3
|
+
* Doctor checks these so generated / documented snippets never drift to @latest.
|
|
4
|
+
* Do not scan user sites.
|
|
5
|
+
*/
|
|
6
|
+
export const RECOMMENDED_CDN_SNIPPETS = Object.freeze([
|
|
7
|
+
'https://cdn.jsdelivr.net/npm/@stagepass/loader@2/dist/loader.min.js',
|
|
8
|
+
'https://cdn.jsdelivr.net/npm/@stagepass/modules@2/dist/inject.min.js',
|
|
9
|
+
'https://cdn.jsdelivr.net/npm/@stagepass/modules@2/dist/cookies.min.js',
|
|
10
|
+
'https://cdn.jsdelivr.net/npm/@stagepass/modules@2/dist/maintenance.min.js',
|
|
11
|
+
'https://cdn.jsdelivr.net/npm/@stagepass/modules@2/dist/all.min.js',
|
|
12
|
+
]);
|
|
13
|
+
|
|
14
|
+
const UNPINNED_RE = /@(?:latest)(?=[/"'?]|$)/i;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* @param {string[]} [snippets]
|
|
18
|
+
* @returns {{ ok: boolean, offenders: string[] }}
|
|
19
|
+
*/
|
|
20
|
+
export function findUnpinnedCdnSnippets(snippets = RECOMMENDED_CDN_SNIPPETS) {
|
|
21
|
+
const offenders = snippets.filter((s) => UNPINNED_RE.test(s));
|
|
22
|
+
return { ok: offenders.length === 0, offenders };
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** Chrome Local Network Access hint (public HTTPS → loopback). */
|
|
26
|
+
export const CHROME_LNA_HINT =
|
|
27
|
+
'Chrome may block public HTTPS pages from reaching local .sp hosts until Local Network Access is allowed for the site (site settings → Local network access).';
|