@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
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { defaultExec } from './exec.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Ensure Homebrew PHP-FPM is running (non-interactive, never sudo).
|
|
5
|
+
* Uses `brew services start php`, falling back to `restart` if start fails.
|
|
6
|
+
*
|
|
7
|
+
* @param {object} [opts]
|
|
8
|
+
* @param {import('./exec.js').ExecFn} [opts.exec]
|
|
9
|
+
* @param {'ignore'|'inherit'} [opts.stdio]
|
|
10
|
+
* @returns {Promise<{ ok: boolean, action: string, detail?: string }>}
|
|
11
|
+
*/
|
|
12
|
+
export async function ensurePhpFpm(opts = {}) {
|
|
13
|
+
const exec = opts.exec || defaultExec;
|
|
14
|
+
const stdio = opts.stdio || 'ignore';
|
|
15
|
+
|
|
16
|
+
try {
|
|
17
|
+
await exec('brew', ['services', 'start', 'php'], { stdio });
|
|
18
|
+
return { ok: true, action: 'start' };
|
|
19
|
+
} catch (startError) {
|
|
20
|
+
try {
|
|
21
|
+
await exec('brew', ['services', 'restart', 'php'], { stdio });
|
|
22
|
+
return { ok: true, action: 'restart' };
|
|
23
|
+
} catch (restartError) {
|
|
24
|
+
const detail = [
|
|
25
|
+
startError instanceof Error ? startError.message : String(startError),
|
|
26
|
+
restartError instanceof Error ? restartError.message : String(restartError),
|
|
27
|
+
]
|
|
28
|
+
.filter(Boolean)
|
|
29
|
+
.join(' | ');
|
|
30
|
+
return { ok: false, action: 'failed', detail };
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
package/src/lib/project-sync.js
CHANGED
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Sync generated project files with declared Stagepass modules
|
|
2
|
+
* Sync generated project files with the declared Stagepass modules
|
|
3
|
+
* (used by `stagepass upgrade` and `stagepass doctor`).
|
|
4
|
+
*
|
|
5
|
+
* Writes only:
|
|
6
|
+
* - stagepass.json `modules` (and `origin` when passed via --site)
|
|
7
|
+
* - AGENTS.md managed block (created when missing)
|
|
8
|
+
* - .cursor/rules/stagepass.mdc (created when missing, managed block refreshed)
|
|
9
|
+
* - tsconfig.json / jsconfig.json `compilerOptions.types` (plain JSON only)
|
|
3
10
|
*/
|
|
4
11
|
|
|
5
12
|
import path from 'node:path';
|
|
@@ -18,9 +25,11 @@ import {
|
|
|
18
25
|
typesForModules,
|
|
19
26
|
} from './project-modules.js';
|
|
20
27
|
import { loadProjects } from './projects.js';
|
|
28
|
+
import { validateOriginEntry, validateOriginList } from './origins.js';
|
|
21
29
|
import { describeDetection, normalizeSiteUrl, SITE_PORT_ERROR } from './site-detect.js';
|
|
22
30
|
|
|
23
31
|
/**
|
|
32
|
+
* Read and parse stagepass.json (null when missing or invalid).
|
|
24
33
|
* @param {string} cwd
|
|
25
34
|
* @param {typeof fs} [fse]
|
|
26
35
|
* @returns {Promise<{ exists: boolean, data: Record<string, unknown> | null, raw: string, error?: string }>}
|
|
@@ -41,6 +50,7 @@ export async function readStagepassJson(cwd, fse = fs) {
|
|
|
41
50
|
}
|
|
42
51
|
|
|
43
52
|
/**
|
|
53
|
+
* Declared modules from stagepass.json (`null` when missing or legacy boolean).
|
|
44
54
|
* @param {Record<string, unknown> | null} data
|
|
45
55
|
* @returns {string[] | null}
|
|
46
56
|
*/
|
|
@@ -50,11 +60,56 @@ export function declaredModules(data) {
|
|
|
50
60
|
return result.ok ? result.modules : null;
|
|
51
61
|
}
|
|
52
62
|
|
|
63
|
+
/**
|
|
64
|
+
* CORS origins declared in stagepass.json for `stagepass link` with no --origin flags.
|
|
65
|
+
* Reads `origin` (staging) and optional `production`. Invalid entries are skipped.
|
|
66
|
+
*
|
|
67
|
+
* @param {Record<string, unknown> | null} data
|
|
68
|
+
* @returns {string[]}
|
|
69
|
+
*/
|
|
70
|
+
export function originsFromStagepassConfig(data) {
|
|
71
|
+
if (!data || typeof data !== 'object') return [];
|
|
72
|
+
const raw = [];
|
|
73
|
+
for (const key of ['origin', 'production']) {
|
|
74
|
+
const value = data[key];
|
|
75
|
+
if (typeof value !== 'string' || value.trim() === '') continue;
|
|
76
|
+
// Stored config may have incidental spaces; CLI flags are validated raw (AC-10).
|
|
77
|
+
const check = validateOriginEntry(value.trim());
|
|
78
|
+
if (check.ok) raw.push(check.value);
|
|
79
|
+
}
|
|
80
|
+
const validated = validateOriginList(raw);
|
|
81
|
+
return validated.ok ? validated.origins : [];
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* PHP toggle from stagepass.json (`undefined` when unset).
|
|
86
|
+
* @param {Record<string, unknown> | null} data
|
|
87
|
+
* @returns {boolean|undefined}
|
|
88
|
+
*/
|
|
89
|
+
export function phpFromStagepassConfig(data) {
|
|
90
|
+
if (!data || typeof data !== 'object') return undefined;
|
|
91
|
+
if (data.php === true) return true;
|
|
92
|
+
if (data.php === false) return false;
|
|
93
|
+
return undefined;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Validate a staging/production hostname for `stagepass new` (origins.js rules).
|
|
98
|
+
* @param {string} input
|
|
99
|
+
* @returns {{ ok: true, value: string } | { ok: false, error: string }}
|
|
100
|
+
*/
|
|
101
|
+
export function validateProjectOriginHost(input) {
|
|
102
|
+
return validateOriginEntry(input);
|
|
103
|
+
}
|
|
104
|
+
|
|
53
105
|
function warnPortSkip(log, source) {
|
|
54
106
|
if (log) log(`Skipped ${source}: it contains a port. Use the host only, e.g. my-site.webflow.io.`);
|
|
55
107
|
}
|
|
56
108
|
|
|
57
109
|
/**
|
|
110
|
+
* Live origin for a project: stagepass.json `origin`, else the first concrete
|
|
111
|
+
* (non-wildcard) origin stored by `stagepass link` for this project folder.
|
|
112
|
+
*
|
|
58
113
|
* @param {string} cwd
|
|
59
114
|
* @param {Record<string, unknown> | null} data
|
|
60
115
|
* @param {{ loadProjects?: typeof loadProjects, realpath?: (p: string) => Promise<string> }} [deps]
|
|
@@ -99,11 +154,15 @@ export async function resolveProjectOrigin(cwd, data, deps = {}) {
|
|
|
99
154
|
}
|
|
100
155
|
|
|
101
156
|
/**
|
|
157
|
+
* Decide the declared modules list for `upgrade`.
|
|
158
|
+
* Order: --site-modules flag, live site detection, existing stagepass.json,
|
|
159
|
+
* interactive prompt. The npm dependency is only mentioned as a hint.
|
|
160
|
+
*
|
|
102
161
|
* @param {object} input
|
|
103
162
|
* @param {string} input.cwd
|
|
104
|
-
* @param {Record<string, unknown> | null} input.data
|
|
105
|
-
* @param {object} input.pkg
|
|
106
|
-
* @param {object} input.options
|
|
163
|
+
* @param {Record<string, unknown> | null} input.data stagepass.json
|
|
164
|
+
* @param {object} input.pkg package.json
|
|
165
|
+
* @param {object} input.options { siteModules?, site?, skipSite?, yes? }
|
|
107
166
|
* @param {object} deps
|
|
108
167
|
* @param {(site: string) => Promise<any>} deps.detectSite
|
|
109
168
|
* @param {(question: string, def: string) => Promise<string>} [deps.ask]
|
|
@@ -137,7 +196,7 @@ export async function resolveUpgradeModules(input, deps) {
|
|
|
137
196
|
if (!check.ok) return { ok: false, error: check.error };
|
|
138
197
|
origin = check.host;
|
|
139
198
|
} else {
|
|
140
|
-
const found = await resolveProjectOrigin(cwd, data, { loadProjects: deps.loadProjects
|
|
199
|
+
const found = await resolveProjectOrigin(cwd, data, { loadProjects: deps.loadProjects });
|
|
141
200
|
origin = found ? found.origin : null;
|
|
142
201
|
}
|
|
143
202
|
|
|
@@ -169,7 +228,9 @@ export async function resolveUpgradeModules(input, deps) {
|
|
|
169
228
|
source = 'stagepass.json';
|
|
170
229
|
}
|
|
171
230
|
|
|
172
|
-
// Legacy `"modules": true` meant
|
|
231
|
+
// Legacy `"modules": true` (CLI 2.0/2.1) meant "modules included", typed via
|
|
232
|
+
// `@stagepass/modules` (all modules). Keep that as `["all"]` so an upgrade
|
|
233
|
+
// without a live site result never removes types the project relied on.
|
|
173
234
|
if (!candidate && data?.modules === true) {
|
|
174
235
|
candidate = ['all'];
|
|
175
236
|
source = 'legacy "modules": true in stagepass.json';
|
|
@@ -216,7 +277,9 @@ export async function resolveUpgradeModules(input, deps) {
|
|
|
216
277
|
}
|
|
217
278
|
|
|
218
279
|
/**
|
|
219
|
-
*
|
|
280
|
+
* Update `compilerOptions.types` in a tsconfig/jsconfig source.
|
|
281
|
+
*
|
|
282
|
+
* @param {string} raw file contents
|
|
220
283
|
* @param {string[]} modules
|
|
221
284
|
* @param {{ modulesDependency: boolean, perModuleTypes?: boolean }} opts
|
|
222
285
|
* @returns {{ status: 'updated' | 'unchanged' | 'skipped', content?: string, reason?: string, types?: string[] }}
|
|
@@ -258,13 +321,24 @@ export function updateConfigTypes(raw, modules, opts) {
|
|
|
258
321
|
return { status: 'updated', content: `${JSON.stringify(json, null, 2)}${trailing}`, types: nextTypes };
|
|
259
322
|
}
|
|
260
323
|
|
|
324
|
+
/** @param {string} abs @param {typeof fs} fse */
|
|
325
|
+
async function isSymlink(abs, fse) {
|
|
326
|
+
try {
|
|
327
|
+
return (await fse.lstat(abs)).isSymbolicLink();
|
|
328
|
+
} catch {
|
|
329
|
+
return false;
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
|
|
261
333
|
/**
|
|
334
|
+
* Plan file changes for the declared modules (pure, no writes).
|
|
335
|
+
*
|
|
262
336
|
* @param {object} input
|
|
263
337
|
* @param {string} input.cwd
|
|
264
338
|
* @param {string[]} input.modules
|
|
265
|
-
* @param {string | null} [input.origin]
|
|
266
|
-
* @param {object} input.pkg
|
|
267
|
-
* @param {boolean} [input.perModuleTypes]
|
|
339
|
+
* @param {string | null} [input.origin] stored in stagepass.json when set
|
|
340
|
+
* @param {object} input.pkg package.json
|
|
341
|
+
* @param {boolean} [input.perModuleTypes] false when @stagepass/modules has no per-module types (umbrella entry)
|
|
268
342
|
* @param {typeof fs} [fse]
|
|
269
343
|
* @returns {Promise<{ writes: { file: string, content: string, action: string }[], notes: string[] }>}
|
|
270
344
|
*/
|
|
@@ -278,12 +352,29 @@ export async function planProjectSync(input, fse = fs) {
|
|
|
278
352
|
pkg?.dependencies?.['@stagepass/modules'] || pkg?.devDependencies?.['@stagepass/modules']
|
|
279
353
|
);
|
|
280
354
|
const perModuleTypes = input.perModuleTypes;
|
|
355
|
+
/** Skip target when it (or a path ancestor under cwd) is a symlink. */
|
|
356
|
+
const skipSymlink = async (rel) => {
|
|
357
|
+
const abs = path.join(cwd, rel);
|
|
358
|
+
const root = path.resolve(cwd);
|
|
359
|
+
let cur = abs;
|
|
360
|
+
for (;;) {
|
|
361
|
+
if (await isSymlink(cur, fse)) {
|
|
362
|
+
notes.push(`${rel} is a symlink; left unchanged`);
|
|
363
|
+
return true;
|
|
364
|
+
}
|
|
365
|
+
const parent = path.dirname(cur);
|
|
366
|
+
if (parent === cur || path.resolve(parent) === root) break;
|
|
367
|
+
if (!path.resolve(parent).startsWith(root + path.sep) && path.resolve(parent) !== root) break;
|
|
368
|
+
cur = parent;
|
|
369
|
+
}
|
|
370
|
+
return false;
|
|
371
|
+
};
|
|
281
372
|
|
|
282
373
|
// stagepass.json
|
|
283
374
|
const sp = await readStagepassJson(cwd, fse);
|
|
284
375
|
if (sp.exists && !sp.data) {
|
|
285
376
|
notes.push(`stagepass.json skipped: ${sp.error}`);
|
|
286
|
-
} else {
|
|
377
|
+
} else if (!(await skipSymlink('stagepass.json'))) {
|
|
287
378
|
/** @type {Record<string, unknown>} */
|
|
288
379
|
let data;
|
|
289
380
|
if (sp.data) {
|
|
@@ -307,7 +398,9 @@ export async function planProjectSync(input, fse = fs) {
|
|
|
307
398
|
// AGENTS.md
|
|
308
399
|
const block = buildManagedBlock(modules);
|
|
309
400
|
const agentsPath = path.join(cwd, 'AGENTS.md');
|
|
310
|
-
if (await
|
|
401
|
+
if (await skipSymlink('AGENTS.md')) {
|
|
402
|
+
// note already added
|
|
403
|
+
} else if (await fse.pathExists(agentsPath)) {
|
|
311
404
|
const raw = await fse.readFile(agentsPath, 'utf8');
|
|
312
405
|
const result = upsertManagedBlock(raw, block);
|
|
313
406
|
if (result.action === 'invalid') {
|
|
@@ -330,7 +423,9 @@ export async function planProjectSync(input, fse = fs) {
|
|
|
330
423
|
|
|
331
424
|
// Cursor rule
|
|
332
425
|
const rulePath = path.join(cwd, CURSOR_RULE_PATH);
|
|
333
|
-
if (await
|
|
426
|
+
if (await skipSymlink(CURSOR_RULE_PATH)) {
|
|
427
|
+
// note already added
|
|
428
|
+
} else if (await fse.pathExists(rulePath)) {
|
|
334
429
|
const raw = await fse.readFile(rulePath, 'utf8');
|
|
335
430
|
if (raw.includes(BLOCK_BEGIN)) {
|
|
336
431
|
const result = upsertManagedBlock(raw, block);
|
|
@@ -352,6 +447,7 @@ export async function planProjectSync(input, fse = fs) {
|
|
|
352
447
|
const file = path.join(cwd, name);
|
|
353
448
|
if (!(await fse.pathExists(file))) continue;
|
|
354
449
|
sawConfig = true;
|
|
450
|
+
if (await skipSymlink(name)) continue;
|
|
355
451
|
const raw = await fse.readFile(file, 'utf8');
|
|
356
452
|
const result = updateConfigTypes(raw, modules, { modulesDependency, perModuleTypes });
|
|
357
453
|
if (result.status === 'skipped') {
|
|
@@ -372,19 +468,68 @@ export async function planProjectSync(input, fse = fs) {
|
|
|
372
468
|
}
|
|
373
469
|
|
|
374
470
|
/**
|
|
471
|
+
* Write planned changes. On failure restores prior file contents (INV-4).
|
|
375
472
|
* @param {string} cwd
|
|
376
473
|
* @param {{ file: string, content: string }[]} writes
|
|
377
474
|
* @param {typeof fs} [fse]
|
|
378
475
|
*/
|
|
379
476
|
export async function applyProjectSync(cwd, writes, fse = fs) {
|
|
380
477
|
const root = path.resolve(cwd);
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
478
|
+
const rootReal = await fse.realpath(root);
|
|
479
|
+
/** @type {{ abs: string, previous: string | null, createdDir: string | null }[]} */
|
|
480
|
+
const journal = [];
|
|
481
|
+
try {
|
|
482
|
+
for (const w of writes) {
|
|
483
|
+
const abs = path.resolve(root, w.file);
|
|
484
|
+
const rel = path.relative(root, abs);
|
|
485
|
+
if (rel.startsWith('..') || path.isAbsolute(rel)) {
|
|
486
|
+
throw new Error(`Refusing to write outside the project: ${w.file}`);
|
|
487
|
+
}
|
|
488
|
+
if (await isSymlink(abs, fse)) {
|
|
489
|
+
throw new Error(`Refusing to write through symlink: ${w.file}`);
|
|
490
|
+
}
|
|
491
|
+
const parent = path.dirname(abs);
|
|
492
|
+
let parentProbe = parent;
|
|
493
|
+
for (;;) {
|
|
494
|
+
try {
|
|
495
|
+
const parentReal = await fse.realpath(parentProbe);
|
|
496
|
+
if (parentReal !== rootReal && !parentReal.startsWith(rootReal + path.sep)) {
|
|
497
|
+
throw new Error(`Refusing to write outside the project: ${w.file}`);
|
|
498
|
+
}
|
|
499
|
+
break;
|
|
500
|
+
} catch (err) {
|
|
501
|
+
if (err instanceof Error && err.message.startsWith('Refusing')) throw err;
|
|
502
|
+
const up = path.dirname(parentProbe);
|
|
503
|
+
if (up === parentProbe) throw err;
|
|
504
|
+
parentProbe = up;
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
let previous = null;
|
|
508
|
+
try {
|
|
509
|
+
previous = await fse.readFile(abs, 'utf8');
|
|
510
|
+
} catch (err) {
|
|
511
|
+
if (!(err && /** @type {{ code?: string }} */ (err).code === 'ENOENT')) throw err;
|
|
512
|
+
}
|
|
513
|
+
let createdDir = null;
|
|
514
|
+
if (!(await fse.pathExists(parent))) {
|
|
515
|
+
await fse.ensureDir(parent);
|
|
516
|
+
createdDir = parent;
|
|
517
|
+
} else {
|
|
518
|
+
await fse.ensureDir(parent);
|
|
519
|
+
}
|
|
520
|
+
await fse.writeFile(abs, w.content, 'utf8');
|
|
521
|
+
journal.push({ abs, previous, createdDir });
|
|
522
|
+
}
|
|
523
|
+
} catch (err) {
|
|
524
|
+
for (const entry of [...journal].reverse()) {
|
|
525
|
+
try {
|
|
526
|
+
if (entry.previous == null) await fse.remove(entry.abs);
|
|
527
|
+
else await fse.writeFile(entry.abs, entry.previous, 'utf8');
|
|
528
|
+
if (entry.createdDir) await fse.remove(entry.createdDir);
|
|
529
|
+
} catch {
|
|
530
|
+
/* best effort */
|
|
531
|
+
}
|
|
386
532
|
}
|
|
387
|
-
|
|
388
|
-
await fse.writeFile(abs, w.content, 'utf8');
|
|
533
|
+
throw err;
|
|
389
534
|
}
|
|
390
535
|
}
|
package/src/lib/projects.js
CHANGED
|
@@ -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
|
-
|
|
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 {
|
|
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
|
-
|
|
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
|
-
|
|
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,19 @@
|
|
|
1
|
+
import { confirm as defaultConfirm } from '@inquirer/prompts';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Confirm prompt (defaults to `@inquirer/prompts` confirm).
|
|
5
|
+
* Injectable for unit tests.
|
|
6
|
+
*
|
|
7
|
+
* @param {string} message
|
|
8
|
+
* @param {object} [options]
|
|
9
|
+
* @param {boolean} [options.default]
|
|
10
|
+
* @param {(opts: { message: string, default?: boolean }) => Promise<boolean>} [options.confirm]
|
|
11
|
+
* @returns {Promise<boolean>}
|
|
12
|
+
*/
|
|
13
|
+
export async function confirm(message, options = {}) {
|
|
14
|
+
const confirmFn = options.confirm || defaultConfirm;
|
|
15
|
+
return confirmFn({
|
|
16
|
+
message: String(message || ''),
|
|
17
|
+
default: options.default === true,
|
|
18
|
+
});
|
|
19
|
+
}
|
package/src/lib/registry.js
CHANGED
|
@@ -1,4 +1,11 @@
|
|
|
1
|
-
/**
|
|
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
|
-
* @
|
|
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
|
-
* @
|
|
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
|
|
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
|