@stagepass/cli 2.2.7 → 2.3.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.
Files changed (46) hide show
  1. package/README.md +37 -18
  2. package/TESTING-macos.md +28 -10
  3. package/bin/index.js +4 -1
  4. package/package.json +9 -8
  5. package/src/cli.js +62 -7
  6. package/src/commands/certs.js +375 -19
  7. package/src/commands/daemon.js +75 -7
  8. package/src/commands/dev.js +419 -0
  9. package/src/commands/link.js +64 -23
  10. package/src/commands/new.js +397 -41
  11. package/src/commands/npm-run.js +8 -0
  12. package/src/commands/start.js +220 -127
  13. package/src/commands/stop.js +10 -19
  14. package/src/commands/unlink.js +6 -4
  15. package/src/commands/upgrade.js +51 -12
  16. package/src/lib/apply-site-block.js +48 -0
  17. package/src/lib/caddyfile.js +72 -13
  18. package/src/lib/cdn-snippets.js +145 -3
  19. package/src/lib/certs.js +334 -47
  20. package/src/lib/dev-port.js +45 -0
  21. package/src/lib/doctor-site.js +61 -5
  22. package/src/lib/doctor.js +48 -4
  23. package/src/lib/ensure-running.js +89 -0
  24. package/src/lib/exec.js +72 -24
  25. package/src/lib/live-server.js +484 -0
  26. package/src/lib/node-engine.js +55 -0
  27. package/src/lib/notify.js +62 -0
  28. package/src/lib/origins.js +27 -5
  29. package/src/lib/package-versions.js +9 -0
  30. package/src/lib/paths.js +18 -0
  31. package/src/lib/php-fpm.js +33 -0
  32. package/src/lib/project-sync.js +165 -20
  33. package/src/lib/projects.js +137 -7
  34. package/src/lib/prompts.js +47 -0
  35. package/src/lib/registry.js +27 -6
  36. package/src/lib/scaffold.js +288 -11
  37. package/src/lib/site-detect.js +78 -25
  38. package/src/lib/snippets.js +14 -11
  39. package/src/lib/versions.js +20 -8
  40. package/templates/project/AGENTS.md.tmpl +4 -3
  41. package/templates/project/README.md.tmpl +35 -6
  42. package/templates/project/api/README.md.tmpl +7 -0
  43. package/templates/project/examples/php/README.md.tmpl +18 -0
  44. package/templates/project/examples/php/form.php +105 -0
  45. package/templates/project/examples/php/proxy.php +171 -0
  46. package/templates/project/scripts/build.mjs +200 -37
@@ -2,9 +2,10 @@ import fs from 'fs-extra';
2
2
  import path from 'path';
3
3
  import { getProjectsPath } from './paths.js';
4
4
  import { validateOriginList } from './origins.js';
5
+ import { isPidAlive, isValidDevPort, normalizeDevPort } from './dev-port.js';
5
6
 
6
7
  /**
7
- * Per-project CORS settings stored in ~/.stagepass/projects.json
8
+ * Per-project CORS / PHP / live-reload settings stored in ~/.stagepass/projects.json
8
9
  *
9
10
  * Shape:
10
11
  * {
@@ -13,10 +14,16 @@ import { validateOriginList } from './origins.js';
13
14
  * "my-app.sp": {
14
15
  * "root": "/absolute/path",
15
16
  * "origins": ["example.com", "*.example.org"],
16
- * "allowAllOrigins": false
17
+ * "allowAllOrigins": false,
18
+ * "php": true,
19
+ * "devPort": 34567,
20
+ * "devPid": 12345
17
21
  * }
18
22
  * }
19
23
  * }
24
+ *
25
+ * `php` is optional: missing means PHP on (legacy linked projects keep php_fastcgi).
26
+ * `devPort` / `devPid` are set only while `stagepass dev` is running for that project.
20
27
  */
21
28
 
22
29
  const DEFAULT_DOC = { version: 1, projects: {} };
@@ -52,12 +59,14 @@ export async function saveProjects(doc, filePath = getProjectsPath()) {
52
59
  version: 1,
53
60
  projects: doc.projects || {},
54
61
  };
55
- await fs.writeFile(filePath, `${JSON.stringify(payload, null, 2)}\n`, 'utf-8');
62
+ const tmp = `${filePath}.${process.pid}.tmp`;
63
+ await fs.writeFile(tmp, `${JSON.stringify(payload, null, 2)}\n`, 'utf-8');
64
+ await fs.rename(tmp, filePath);
56
65
  }
57
66
 
58
67
  /**
59
68
  * @param {string} domain
60
- * @returns {Promise<{ root?: string, origins: string[], allowAllOrigins: boolean }|null>}
69
+ * @returns {Promise<{ root?: string, origins: string[], allowAllOrigins: boolean, php?: boolean, devPort?: number, devPid?: number }|null>}
61
70
  */
62
71
  export async function getProject(domain, filePath = getProjectsPath()) {
63
72
  const doc = await loadProjects(filePath);
@@ -66,6 +75,92 @@ export async function getProject(domain, filePath = getProjectsPath()) {
66
75
  return normalizeProjectEntry(entry);
67
76
  }
68
77
 
78
+ /**
79
+ * Find a linked project whose root matches cwd (realpath when possible).
80
+ * @param {string} cwd
81
+ * @param {string} [filePath]
82
+ * @returns {Promise<{ domain: string, project: ReturnType<typeof normalizeProjectEntry> }|null>}
83
+ */
84
+ export async function findProjectByRoot(cwd, filePath = getProjectsPath()) {
85
+ const doc = await loadProjects(filePath);
86
+ let target;
87
+ try {
88
+ target = fs.realpathSync(path.resolve(cwd));
89
+ } catch {
90
+ target = path.resolve(cwd);
91
+ }
92
+ for (const [domain, raw] of Object.entries(doc.projects || {})) {
93
+ if (!raw || typeof raw.root !== 'string') continue;
94
+ let root;
95
+ try {
96
+ root = fs.realpathSync(path.resolve(raw.root));
97
+ } catch {
98
+ root = path.resolve(raw.root);
99
+ }
100
+ if (root === target) {
101
+ return { domain, project: normalizeProjectEntry(raw) };
102
+ }
103
+ }
104
+ return null;
105
+ }
106
+
107
+ /**
108
+ * Persist live-reload port/pid for a domain (or clear when port is null).
109
+ * @param {string} domain
110
+ * @param {{ port: number|null, pid?: number|null }} update
111
+ * @param {string} [filePath]
112
+ */
113
+ export async function setProjectDevPort(domain, update, filePath = getProjectsPath()) {
114
+ const doc = await loadProjects(filePath);
115
+ const existing = doc.projects[domain];
116
+ if (!existing) {
117
+ throw new Error(`No projects.json entry for ${domain}. Run stagepass link first.`);
118
+ }
119
+ const next = { ...existing };
120
+ if (update.port == null) {
121
+ delete next.devPort;
122
+ delete next.devPid;
123
+ } else {
124
+ if (!isValidDevPort(update.port)) {
125
+ throw new Error(`Invalid devPort ${String(update.port)} (must be an integer 1024..65535).`);
126
+ }
127
+ next.devPort = update.port;
128
+ if (typeof update.pid === 'number' && Number.isInteger(update.pid) && update.pid > 0) {
129
+ next.devPid = update.pid;
130
+ } else {
131
+ delete next.devPid;
132
+ }
133
+ }
134
+ doc.projects[domain] = next;
135
+ await saveProjects(doc, filePath);
136
+ return normalizeProjectEntry(next);
137
+ }
138
+
139
+ /**
140
+ * Drop stale devPort/devPid when the recorded PID is dead.
141
+ * @param {string} [filePath]
142
+ * @returns {Promise<string[]>} domains that were cleaned
143
+ */
144
+ export async function clearStaleDevPorts(filePath = getProjectsPath()) {
145
+ const doc = await loadProjects(filePath);
146
+ const cleaned = [];
147
+ let changed = false;
148
+ for (const [domain, raw] of Object.entries(doc.projects || {})) {
149
+ if (!raw || (raw.devPort == null && raw.devPid == null)) continue;
150
+ const port = normalizeDevPort(raw.devPort);
151
+ const pid = raw.devPid;
152
+ const alive = isPidAlive(pid);
153
+ if (!port || !alive) {
154
+ delete raw.devPort;
155
+ delete raw.devPid;
156
+ cleaned.push(domain);
157
+ changed = true;
158
+ }
159
+ }
160
+ if (changed) await saveProjects(doc, filePath);
161
+ return cleaned;
162
+ }
163
+
69
164
  /**
70
165
  * Merge and persist project settings.
71
166
  *
@@ -73,6 +168,7 @@ export async function getProject(domain, filePath = getProjectsPath()) {
73
168
  * - originsFromFlags: when non-empty, merge into existing (deduped)
74
169
  * - originsToRemove: when non-empty, remove matching stored origins (after merge)
75
170
  * - allowAllOriginsFlag: true | false | undefined (undefined = keep existing / seed / default)
171
+ * - phpFlag: true | false | undefined (undefined = keep existing / seed / omit)
76
172
  * - seed: when no projects.json entry exists, start from these values (e.g. Caddyfile marker)
77
173
  * - isNew: when true and allowAllOriginsFlag is undefined, default allowAllOrigins to false
78
174
  * - forceAllowAllOrigins: migration helper; force true once
@@ -83,7 +179,8 @@ export async function getProject(domain, filePath = getProjectsPath()) {
83
179
  * @param {string[]} [update.originsFromFlags]
84
180
  * @param {string[]} [update.originsToRemove]
85
181
  * @param {boolean|undefined} [update.allowAllOriginsFlag]
86
- * @param {{ origins?: string[], allowAllOrigins?: boolean }|null} [update.seed]
182
+ * @param {boolean|undefined} [update.phpFlag]
183
+ * @param {{ origins?: string[], allowAllOrigins?: boolean, php?: boolean }|null} [update.seed]
87
184
  * @param {boolean} [update.isNew]
88
185
  * @param {boolean} [update.forceAllowAllOrigins]
89
186
  */
@@ -124,11 +221,31 @@ export async function upsertProject(domain, update, filePath = getProjectsPath()
124
221
  allowAllOrigins = true;
125
222
  }
126
223
 
127
- doc.projects[domain] = {
224
+ let php = existing.php;
225
+ if (update.phpFlag === true) php = true;
226
+ if (update.phpFlag === false) php = false;
227
+
228
+ /** @type {Record<string, unknown>} */
229
+ const next = {
128
230
  root: update.root,
129
231
  origins,
130
232
  allowAllOrigins,
131
233
  };
234
+ if (php === true || php === false) {
235
+ next.php = php;
236
+ }
237
+ // Preserve live-reload fields across link/upsert unless explicitly cleared elsewhere.
238
+ const prevDevPort = normalizeDevPort(existing.devPort);
239
+ if (prevDevPort) next.devPort = prevDevPort;
240
+ if (
241
+ typeof existing.devPid === 'number' &&
242
+ Number.isInteger(existing.devPid) &&
243
+ existing.devPid > 0
244
+ ) {
245
+ next.devPid = existing.devPid;
246
+ }
247
+
248
+ doc.projects[domain] = next;
132
249
 
133
250
  await saveProjects(doc, filePath);
134
251
  return normalizeProjectEntry(doc.projects[domain]);
@@ -173,9 +290,22 @@ export async function removeProject(domain, filePath = getProjectsPath()) {
173
290
 
174
291
  function normalizeProjectEntry(entry) {
175
292
  const originsResult = validateOriginList(entry.origins || []);
176
- return {
293
+ /** @type {{ root?: string, origins: string[], allowAllOrigins: boolean, php?: boolean, devPort?: number, devPid?: number }} */
294
+ const out = {
177
295
  root: entry.root,
178
296
  origins: originsResult.ok ? originsResult.origins : [],
179
297
  allowAllOrigins: entry.allowAllOrigins === true,
180
298
  };
299
+ if (entry.php === true) out.php = true;
300
+ if (entry.php === false) out.php = false;
301
+ const devPort = normalizeDevPort(entry.devPort);
302
+ if (devPort) out.devPort = devPort;
303
+ if (
304
+ typeof entry.devPid === 'number' &&
305
+ Number.isInteger(entry.devPid) &&
306
+ entry.devPid > 0
307
+ ) {
308
+ out.devPid = entry.devPid;
309
+ }
310
+ return out;
181
311
  }
@@ -0,0 +1,47 @@
1
+ import { confirm as defaultConfirm } from '@inquirer/prompts';
2
+
3
+ /**
4
+ * Confirm prompt (defaults to `@inquirer/prompts` confirm).
5
+ * Injectable for unit tests. Single choke point for every confirmation in
6
+ * the CLI, so an abort here ends the process before any caller's own
7
+ * try/catch can run: no stack trace, 130 for SIGINT, 1 for EOF/closed stdin.
8
+ *
9
+ * @param {string} message
10
+ * @param {object} [options]
11
+ * @param {boolean} [options.default]
12
+ * @param {(opts: { message: string, default?: boolean }) => Promise<boolean>} [options.confirm]
13
+ * @param {(code: number) => void} [options.exit]
14
+ * @param {(msg: string) => void} [options.write]
15
+ * @returns {Promise<boolean>}
16
+ */
17
+ export async function confirm(message, options = {}) {
18
+ const confirmFn = options.confirm || defaultConfirm;
19
+ const exit = options.exit ?? ((code) => process.exit(code));
20
+ const write = options.write ?? ((msg) => process.stderr.write(msg));
21
+ // Byte 0x03 is a readline event, not a process signal (raw mode has ISIG
22
+ // off). A real SIGINT is caught by signal-exit, which re-raises it and the
23
+ // process dies before this await's catch runs. Handle that signal here,
24
+ // synchronously, and only while a real prompt is on screen.
25
+ const onSigInt = options.confirm
26
+ ? null
27
+ : () => {
28
+ write('Aborted.\n');
29
+ exit(130);
30
+ };
31
+ if (onSigInt) process.on('SIGINT', onSigInt);
32
+ try {
33
+ return await confirmFn({
34
+ message: String(message || ''),
35
+ default: options.default === true,
36
+ });
37
+ } catch (error) {
38
+ if (error && error.name === 'ExitPromptError') {
39
+ write('Aborted.\n');
40
+ exit(/SIGINT/.test(String(error.message)) ? 130 : 1);
41
+ return false;
42
+ }
43
+ throw error;
44
+ } finally {
45
+ if (onSigInt) process.off('SIGINT', onSigInt);
46
+ }
47
+ }
@@ -1,4 +1,11 @@
1
- /** Read-only npm registry lookups for @stagepass/* packages. */
1
+ /**
2
+ * Read-only npm registry lookups for @stagepass/* packages.
3
+ *
4
+ * Versions of @stagepass/cli, loader and modules are decoupled, and a release
5
+ * can be staged on npm long before it is published. Generated projects and
6
+ * `stagepass upgrade` therefore never derive a dependency range from the CLI's
7
+ * own version: they only write ranges that a published version satisfies.
8
+ */
2
9
 
3
10
  import fs from 'fs-extra';
4
11
  import path from 'node:path';
@@ -6,16 +13,18 @@ import { defaultExec } from './exec.js';
6
13
  import { MODULE_IDS } from './project-modules.js';
7
14
  import { STAGEPASS_PACKAGES_VERSION } from './versions.js';
8
15
 
9
- /** Major range for new projects (matches `@2` CDN embed policy). */
16
+ /** Major range used for new projects (matches the `@2` CDN embed policy). */
10
17
  export const PACKAGES_MAJOR_RANGE = '^2';
11
18
 
12
- /** First @stagepass/modules release with per-module type entry points. */
19
+ /** First @stagepass/modules release with per-module type entry points. */
13
20
  export const PER_MODULE_TYPES_SINCE = '2.2.0';
14
21
 
15
22
  const SEMVER_RE = /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-([0-9A-Za-z.-]+))?$/;
16
23
  const PKG_NAME_RE = /^@stagepass\/[a-z0-9][a-z0-9._-]*$/;
17
24
 
18
25
  /**
26
+ * Parse a semver string (prerelease kept as a flag).
27
+ * Rejects whitespace padding, leading zeros, and build metadata (`+…`).
19
28
  * @param {string} version
20
29
  * @returns {{ major: number, minor: number, patch: number, pre: boolean } | null}
21
30
  */
@@ -26,6 +35,7 @@ export function parseSemver(version) {
26
35
  }
27
36
 
28
37
  /**
38
+ * Compare two semver strings (prerelease suffix ignored for ordering).
29
39
  * @param {string} a
30
40
  * @param {string} b
31
41
  * @returns {number}
@@ -38,6 +48,8 @@ export function compareSemver(a, b) {
38
48
  }
39
49
 
40
50
  /**
51
+ * Whether a stable version satisfies a simple range: exact `1.2.3`,
52
+ * caret `^2`, `^2.2`, `^2.2.0`, or tilde `~2.2.0`. Prereleases never match.
41
53
  * @param {string} version
42
54
  * @param {string} range
43
55
  * @returns {boolean}
@@ -70,6 +82,7 @@ export function satisfiesSimple(version, range) {
70
82
  }
71
83
 
72
84
  /**
85
+ * Highest published stable version satisfying a range.
73
86
  * @param {string[]} versions
74
87
  * @param {string} range
75
88
  * @returns {string | null}
@@ -83,6 +96,9 @@ export function maxSatisfying(versions, range) {
83
96
  }
84
97
 
85
98
  /**
99
+ * Published dist-tags and versions of a package, or null when npm or the
100
+ * network is unavailable (callers fall back to the major range).
101
+ *
86
102
  * @param {string} name
87
103
  * @param {{ exec?: import('./exec.js').ExecFn, timeoutMs?: number }} [deps]
88
104
  * @returns {Promise<{ distTags: Record<string, string>, versions: string[] } | null>}
@@ -126,7 +142,9 @@ export async function fetchPublished(name, deps = {}) {
126
142
  }
127
143
 
128
144
  /**
129
- * @param {string[] | null | undefined} versions
145
+ * Whether the @stagepass/modules version a range resolves to ships
146
+ * per-module types (`@stagepass/modules/<id>`).
147
+ * @param {string[] | null | undefined} versions published versions
130
148
  * @param {string} range
131
149
  * @returns {boolean | null} null when unknown (no registry data)
132
150
  */
@@ -138,7 +156,8 @@ export function rangeHasPerModuleTypes(versions, range) {
138
156
  }
139
157
 
140
158
  /**
141
- * @param {string} cwd
159
+ * Whether the installed @stagepass/modules ships per-module types.
160
+ * @param {string} cwd project directory
142
161
  * @param {typeof fs} [fse]
143
162
  * @returns {Promise<boolean | null>} null when not installed
144
163
  */
@@ -152,6 +171,7 @@ export async function installedHasPerModuleTypes(cwd, fse = fs) {
152
171
  }
153
172
 
154
173
  /**
174
+ * Installed @stagepass/loader version when it is a stable 2.x.
155
175
  * @param {string} cwd
156
176
  * @param {typeof fs} [fse]
157
177
  * @returns {Promise<string | null>}
@@ -169,7 +189,8 @@ export async function installedLoaderVersion(cwd, fse = fs) {
169
189
  }
170
190
 
171
191
  /**
172
- * Loader version for Webflow snippets (AC-14); never the CLI version.
192
+ * Loader version for Webflow snippets; never the CLI version.
193
+ * Prefers the newest published stable 2.x, then installed, then bundled.
173
194
  * @param {object} options
174
195
  * @param {{ distTags: Record<string, string>, versions: string[] } | null} options.published
175
196
  * @param {string} options.cwd