@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/lib/doctor-site.js
CHANGED
|
@@ -1,13 +1,21 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Doctor checks
|
|
3
|
-
*
|
|
2
|
+
* Doctor checks for the current project folder: declared modules in
|
|
3
|
+
* stagepass.json vs. the Stagepass scripts the live Webflow site loads,
|
|
4
|
+
* plus a PHP check when the project opts into PHP.
|
|
5
|
+
* Read-only: one public GET of the live page, parsed and never executed.
|
|
4
6
|
*/
|
|
5
7
|
|
|
6
8
|
import path from 'node:path';
|
|
7
9
|
import fs from 'fs-extra';
|
|
8
10
|
import { detectSite, describeDetection } from './site-detect.js';
|
|
9
11
|
import { formatModules, sameModules } from './project-modules.js';
|
|
10
|
-
import {
|
|
12
|
+
import {
|
|
13
|
+
declaredModules,
|
|
14
|
+
phpFromStagepassConfig,
|
|
15
|
+
readStagepassJson,
|
|
16
|
+
resolveProjectOrigin,
|
|
17
|
+
} from './project-sync.js';
|
|
18
|
+
import { resolveCommand } from './exec.js';
|
|
11
19
|
|
|
12
20
|
/**
|
|
13
21
|
* @param {object} [opts]
|
|
@@ -15,6 +23,8 @@ import { declaredModules, readStagepassJson, resolveProjectOrigin } from './proj
|
|
|
15
23
|
* @param {boolean} [opts.skipSite]
|
|
16
24
|
* @param {(site: string) => Promise<any>} [opts.detectSite]
|
|
17
25
|
* @param {Function} [opts.loadProjects]
|
|
26
|
+
* @param {typeof resolveCommand} [opts.resolveCommand]
|
|
27
|
+
* @param {Function} [opts.exec]
|
|
18
28
|
* @returns {Promise<{ id: string, status: 'ok'|'warn'|'fail'|'info', message: string, detail?: string }[]>}
|
|
19
29
|
*/
|
|
20
30
|
export async function runProjectSiteChecks(opts = {}) {
|
|
@@ -39,8 +49,43 @@ export async function runProjectSiteChecks(opts = {}) {
|
|
|
39
49
|
checks.push({ id: 'project.modules', status: 'ok', message: `Declared modules: ${formatModules(declared)}` });
|
|
40
50
|
}
|
|
41
51
|
|
|
52
|
+
const php = phpFromStagepassConfig(sp.data);
|
|
53
|
+
if (php === false) {
|
|
54
|
+
checks.push({
|
|
55
|
+
id: 'project.php',
|
|
56
|
+
status: 'info',
|
|
57
|
+
message: 'PHP is off for this project (no php_fastcgi on link)',
|
|
58
|
+
});
|
|
59
|
+
} else if (php === true) {
|
|
60
|
+
const resolve = opts.resolveCommand || resolveCommand;
|
|
61
|
+
const phpPath = await resolve('php', opts.exec);
|
|
62
|
+
const apiDir = path.join(cwd, 'api');
|
|
63
|
+
const apiOk = await fs.pathExists(apiDir);
|
|
64
|
+
if (!phpPath) {
|
|
65
|
+
checks.push({
|
|
66
|
+
id: 'project.php',
|
|
67
|
+
status: 'fail',
|
|
68
|
+
message: 'Project enables PHP but php was not found on PATH',
|
|
69
|
+
detail: 'Install PHP (stagepass setup) or set "php": false in stagepass.json',
|
|
70
|
+
});
|
|
71
|
+
} else if (!apiOk) {
|
|
72
|
+
checks.push({
|
|
73
|
+
id: 'project.php',
|
|
74
|
+
status: 'warn',
|
|
75
|
+
message: `PHP enabled (php=${phpPath}) but api/ folder is missing`,
|
|
76
|
+
detail: 'Create api/ for handlers served via php_fastcgi',
|
|
77
|
+
});
|
|
78
|
+
} else {
|
|
79
|
+
checks.push({
|
|
80
|
+
id: 'project.php',
|
|
81
|
+
status: 'ok',
|
|
82
|
+
message: `PHP enabled for this project (php=${phpPath}, api/ present)`,
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
42
87
|
if (opts.skipSite) return checks;
|
|
43
|
-
const found = await resolveProjectOrigin(cwd, sp.data, { loadProjects: opts.loadProjects
|
|
88
|
+
const found = await resolveProjectOrigin(cwd, sp.data, { loadProjects: opts.loadProjects });
|
|
44
89
|
if (!found) {
|
|
45
90
|
checks.push({
|
|
46
91
|
id: 'project.site',
|
|
@@ -70,10 +115,21 @@ export async function runProjectSiteChecks(opts = {}) {
|
|
|
70
115
|
});
|
|
71
116
|
return checks;
|
|
72
117
|
}
|
|
118
|
+
let siteMsg =
|
|
119
|
+
det.loader.cdn === 'self'
|
|
120
|
+
? `Live site ${found.origin} loads a self-hosted @stagepass/loader`
|
|
121
|
+
: `Live site ${found.origin} loads @stagepass/loader@${det.loader.version}`;
|
|
122
|
+
if (
|
|
123
|
+
det.loader.cdn === 'self' &&
|
|
124
|
+
det.loader.version &&
|
|
125
|
+
det.loader.version !== 'latest'
|
|
126
|
+
) {
|
|
127
|
+
siteMsg += ` (version ${det.loader.version})`;
|
|
128
|
+
}
|
|
73
129
|
checks.push({
|
|
74
130
|
id: 'project.site',
|
|
75
131
|
status: 'ok',
|
|
76
|
-
message:
|
|
132
|
+
message: siteMsg,
|
|
77
133
|
detail: describeDetection(det),
|
|
78
134
|
});
|
|
79
135
|
if (declared && !sameModules(declared, det.list)) {
|
package/src/lib/doctor.js
CHANGED
|
@@ -15,6 +15,7 @@ import {
|
|
|
15
15
|
CHROME_LNA_HINT,
|
|
16
16
|
findUnpinnedCdnSnippets,
|
|
17
17
|
RECOMMENDED_CDN_SNIPPETS,
|
|
18
|
+
scanProjectUnpinnedCdn,
|
|
18
19
|
} from './cdn-snippets.js';
|
|
19
20
|
|
|
20
21
|
/**
|
|
@@ -419,7 +420,7 @@ export async function runDoctor(opts = {}) {
|
|
|
419
420
|
checks.push(
|
|
420
421
|
check(
|
|
421
422
|
`certs.tls.${hs.servername}`,
|
|
422
|
-
hs.status,
|
|
423
|
+
hs.status === 'skip' ? 'info' : hs.status,
|
|
423
424
|
hs.reason || `TLS check for ${hs.servername}`
|
|
424
425
|
)
|
|
425
426
|
);
|
|
@@ -461,7 +462,7 @@ export async function runDoctor(opts = {}) {
|
|
|
461
462
|
);
|
|
462
463
|
}
|
|
463
464
|
|
|
464
|
-
// CDN snippets
|
|
465
|
+
// CDN snippets: canonical docs + current project snippet files (read-only)
|
|
465
466
|
const snippets = opts.cdnSnippets || RECOMMENDED_CDN_SNIPPETS;
|
|
466
467
|
const unpinned = findUnpinnedCdnSnippets(snippets);
|
|
467
468
|
if (!unpinned.ok) {
|
|
@@ -469,7 +470,7 @@ export async function runDoctor(opts = {}) {
|
|
|
469
470
|
check(
|
|
470
471
|
'cdn',
|
|
471
472
|
'warn',
|
|
472
|
-
'Recommended CDN snippets include @latest
|
|
473
|
+
'Recommended CDN snippets include unpinned refs (@latest, @1, or bare package; prefer @2)',
|
|
473
474
|
unpinned.offenders.join(', ')
|
|
474
475
|
)
|
|
475
476
|
);
|
|
@@ -478,11 +479,54 @@ export async function runDoctor(opts = {}) {
|
|
|
478
479
|
check(
|
|
479
480
|
'cdn',
|
|
480
481
|
'ok',
|
|
481
|
-
'Recommended CDN snippets use a pinned major (@2), not @latest'
|
|
482
|
+
'Recommended CDN snippets use a pinned major (@2), not @latest/@1/bare'
|
|
482
483
|
)
|
|
483
484
|
);
|
|
484
485
|
}
|
|
485
486
|
|
|
487
|
+
if (opts.projectCdnScan !== false) {
|
|
488
|
+
try {
|
|
489
|
+
const projectCdn =
|
|
490
|
+
opts.projectCdnResult ||
|
|
491
|
+
(await scanProjectUnpinnedCdn({
|
|
492
|
+
cwd: opts.cwd,
|
|
493
|
+
files: opts.projectCdnFiles,
|
|
494
|
+
pathExists,
|
|
495
|
+
readFile,
|
|
496
|
+
}));
|
|
497
|
+
if (!projectCdn.ok) {
|
|
498
|
+
const detail = projectCdn.offenders
|
|
499
|
+
.map((o) => `${o.file}: ${o.match}`)
|
|
500
|
+
.join('; ');
|
|
501
|
+
checks.push(
|
|
502
|
+
check(
|
|
503
|
+
'cdn.project',
|
|
504
|
+
'warn',
|
|
505
|
+
'Project files contain unpinned Stagepass CDN URLs (@latest, @1, or bare; prefer @2)',
|
|
506
|
+
detail
|
|
507
|
+
)
|
|
508
|
+
);
|
|
509
|
+
} else {
|
|
510
|
+
checks.push(
|
|
511
|
+
check(
|
|
512
|
+
'cdn.project',
|
|
513
|
+
'ok',
|
|
514
|
+
'Project snippet files use pinned Stagepass CDN URLs (or none found)'
|
|
515
|
+
)
|
|
516
|
+
);
|
|
517
|
+
}
|
|
518
|
+
} catch (error) {
|
|
519
|
+
checks.push(
|
|
520
|
+
check(
|
|
521
|
+
'cdn.project',
|
|
522
|
+
'warn',
|
|
523
|
+
'Could not scan project files for CDN pins',
|
|
524
|
+
error instanceof Error ? error.message : String(error)
|
|
525
|
+
)
|
|
526
|
+
);
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
|
|
486
530
|
hints.push(CHROME_LNA_HINT);
|
|
487
531
|
|
|
488
532
|
// Current project folder: declared modules vs. live site (read-only GET)
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import chalk from 'chalk';
|
|
2
|
+
import { defaultExec } from './exec.js';
|
|
3
|
+
import { parseLsofListenOutput } from './doctor.js';
|
|
4
|
+
import { findProjectByRoot } from './projects.js';
|
|
5
|
+
import { getProjectsPath } from './paths.js';
|
|
6
|
+
import { confirm as defaultConfirm } from './prompts.js';
|
|
7
|
+
|
|
8
|
+
/** Caddy listening on TCP 443 (same lsof parse as doctor); null if unknown. */
|
|
9
|
+
export async function isStagepassRunning(opts = {}) {
|
|
10
|
+
const exec = opts.exec || defaultExec;
|
|
11
|
+
try {
|
|
12
|
+
const { stdout } = await exec('lsof', ['-nP', '-iTCP:443', '-sTCP:LISTEN']);
|
|
13
|
+
return parseLsofListenOutput(stdout).some(
|
|
14
|
+
(l) => /caddy/i.test(l.command) || /caddy/i.test(l.name)
|
|
15
|
+
);
|
|
16
|
+
} catch (error) {
|
|
17
|
+
const err = /** @type {{ code?: string|number }} */ (error);
|
|
18
|
+
return err.code === 1 || err.code === '1' ? false : null;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Before watch/dev: offer start when down; warn when cwd is unlinked unless `unlinked: 'skip'`.
|
|
24
|
+
* @returns {Promise<{ running: boolean|null, linked: boolean, started: boolean }>}
|
|
25
|
+
*/
|
|
26
|
+
export async function ensureStagepassReady(options = {}, deps = {}) {
|
|
27
|
+
const log = deps.log || console.log;
|
|
28
|
+
const warn = (msg) => log(chalk.yellow(msg));
|
|
29
|
+
const cwd = deps.cwd || process.cwd();
|
|
30
|
+
const projectsPath = deps.projectsPath || getProjectsPath();
|
|
31
|
+
const isRunningFn = deps.isStagepassRunning || isStagepassRunning;
|
|
32
|
+
const findLinked = deps.findProjectByRoot || findProjectByRoot;
|
|
33
|
+
const confirm = deps.confirm || defaultConfirm;
|
|
34
|
+
const interactive =
|
|
35
|
+
options.yes !== true &&
|
|
36
|
+
!(deps.ci ?? process.env.CI) &&
|
|
37
|
+
Boolean(
|
|
38
|
+
(deps.stdin || process.stdin).isTTY && (deps.stdout || process.stdout).isTTY
|
|
39
|
+
);
|
|
40
|
+
|
|
41
|
+
let running = await isRunningFn({ exec: deps.exec });
|
|
42
|
+
let started = false;
|
|
43
|
+
|
|
44
|
+
if (running === false) {
|
|
45
|
+
const shouldStart =
|
|
46
|
+
options.yes === true ||
|
|
47
|
+
(interactive &&
|
|
48
|
+
(await confirm('Stagepass is not running. Start it now?', {
|
|
49
|
+
default: true,
|
|
50
|
+
})));
|
|
51
|
+
|
|
52
|
+
if (shouldStart && typeof deps.start === 'function') {
|
|
53
|
+
try {
|
|
54
|
+
const code = await deps.start({
|
|
55
|
+
verbose: options.verbose,
|
|
56
|
+
interactive: options.yes !== true && interactive,
|
|
57
|
+
exec: deps.exec,
|
|
58
|
+
confirm,
|
|
59
|
+
});
|
|
60
|
+
// start() must return 0 on success; undefined/non-zero means not started.
|
|
61
|
+
if (typeof code !== 'number' || code !== 0) {
|
|
62
|
+
warn('Could not start Stagepass. Run: stagepass start');
|
|
63
|
+
running = await isRunningFn({ exec: deps.exec });
|
|
64
|
+
} else {
|
|
65
|
+
running = await isRunningFn({ exec: deps.exec });
|
|
66
|
+
if (running === true) {
|
|
67
|
+
started = true;
|
|
68
|
+
} else {
|
|
69
|
+
warn('Could not start Stagepass. Run: stagepass start');
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
} catch {
|
|
73
|
+
warn('Could not start Stagepass. Run: stagepass start');
|
|
74
|
+
}
|
|
75
|
+
} else if (!shouldStart) {
|
|
76
|
+
warn('Stagepass is not running. Start it with: stagepass start');
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
let linked = true;
|
|
81
|
+
if (deps.unlinked !== 'skip') {
|
|
82
|
+
linked = Boolean(await findLinked(cwd, projectsPath));
|
|
83
|
+
if (!linked) {
|
|
84
|
+
warn('This directory is not linked to a .sp domain. Run: stagepass link');
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
return { running, linked, started };
|
|
89
|
+
}
|
package/src/lib/exec.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { execFile as nodeExecFile } from 'node:child_process';
|
|
1
|
+
import { execFile as nodeExecFile, spawn } from 'node:child_process';
|
|
2
2
|
import { promisify } from 'node:util';
|
|
3
3
|
|
|
4
4
|
const execFileAsync = promisify(nodeExecFile);
|
|
@@ -14,42 +14,90 @@ const execFileAsync = promisify(nodeExecFile);
|
|
|
14
14
|
* @typedef {(file: string, args?: string[], options?: object) => Promise<ExecResult>} ExecFn
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
|
+
/**
|
|
18
|
+
* @param {NodeJS.ErrnoException & { stdout?: string, stderr?: string, code?: number|string, killed?: boolean, signal?: string }} err
|
|
19
|
+
* @returns {Error}
|
|
20
|
+
*/
|
|
21
|
+
function wrapExecError(err) {
|
|
22
|
+
const wrapped = new Error(err.message || String(err));
|
|
23
|
+
Object.assign(wrapped, {
|
|
24
|
+
code: err.code,
|
|
25
|
+
errno: err.errno,
|
|
26
|
+
stdout: err.stdout != null ? String(err.stdout) : '',
|
|
27
|
+
stderr: err.stderr != null ? String(err.stderr) : '',
|
|
28
|
+
killed: err.killed,
|
|
29
|
+
signal: err.signal,
|
|
30
|
+
});
|
|
31
|
+
return wrapped;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Wait for spawn exit when stdout/stderr are not captured pipes.
|
|
36
|
+
* Avoids execFile+encoding hanging while a daemon grandchild still holds
|
|
37
|
+
* inherited logfile FDs (public `stagepass start` non-verbose path).
|
|
38
|
+
* @param {string} file
|
|
39
|
+
* @param {string[]} args
|
|
40
|
+
* @param {object} options
|
|
41
|
+
* @returns {Promise<ExecResult>}
|
|
42
|
+
*/
|
|
43
|
+
function spawnWait(file, args, options) {
|
|
44
|
+
return new Promise((resolve, reject) => {
|
|
45
|
+
const child = spawn(file, args, { ...options, shell: false });
|
|
46
|
+
child.on('error', (error) => {
|
|
47
|
+
reject(wrapExecError(/** @type {NodeJS.ErrnoException} */ (error)));
|
|
48
|
+
});
|
|
49
|
+
child.on('close', (code, signal) => {
|
|
50
|
+
if (code === 0) {
|
|
51
|
+
resolve({ stdout: '', stderr: '', code: 0 });
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
const err = /** @type {NodeJS.ErrnoException & { code?: number|string, signal?: string }} */ (
|
|
55
|
+
new Error(`Command failed: ${file} ${args.join(' ')}`)
|
|
56
|
+
);
|
|
57
|
+
err.code = code == null ? signal || 1 : code;
|
|
58
|
+
err.signal = signal || undefined;
|
|
59
|
+
reject(wrapExecError(err));
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
|
|
17
64
|
/**
|
|
18
65
|
* Default execFile wrapper. Always uses argument arrays (never a shell string).
|
|
19
66
|
* @type {ExecFn}
|
|
20
67
|
*/
|
|
21
68
|
export async function defaultExec(file, args = [], options = {}) {
|
|
22
|
-
const
|
|
23
|
-
|
|
24
|
-
const
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
69
|
+
const stdio = options.stdio;
|
|
70
|
+
const inheritStdio = stdio === 'inherit';
|
|
71
|
+
const customStdio = inheritStdio || Array.isArray(stdio) || stdio === 'ignore';
|
|
72
|
+
|
|
73
|
+
// Capture-output path (doctor/certs/which): execFile + encoding.
|
|
74
|
+
// Custom stdio (start logfile FDs / inherit): spawn + wait for exit only.
|
|
75
|
+
if (customStdio) {
|
|
76
|
+
try {
|
|
77
|
+
return await spawnWait(file, args, { ...options, shell: false });
|
|
78
|
+
} catch (error) {
|
|
79
|
+
throw error instanceof Error ? error : wrapExecError(/** @type {any} */ (error));
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
32
83
|
try {
|
|
33
|
-
const result = await execFileAsync(file, args,
|
|
84
|
+
const result = await execFileAsync(file, args, {
|
|
85
|
+
encoding: 'utf8',
|
|
86
|
+
maxBuffer: 4 * 1024 * 1024,
|
|
87
|
+
...options,
|
|
88
|
+
shell: false,
|
|
89
|
+
});
|
|
34
90
|
return {
|
|
35
91
|
stdout: result?.stdout != null ? String(result.stdout) : '',
|
|
36
92
|
stderr: result?.stderr != null ? String(result.stderr) : '',
|
|
37
93
|
code: 0,
|
|
38
94
|
};
|
|
39
95
|
} catch (error) {
|
|
40
|
-
|
|
41
|
-
|
|
96
|
+
throw wrapExecError(
|
|
97
|
+
/** @type {NodeJS.ErrnoException & { stdout?: string, stderr?: string, code?: number|string, killed?: boolean, signal?: string }} */ (
|
|
98
|
+
error
|
|
99
|
+
)
|
|
42
100
|
);
|
|
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
101
|
}
|
|
54
102
|
}
|
|
55
103
|
|