@stagepass/cli 2.3.1 → 2.4.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 +55 -15
- package/package.json +1 -1
- package/src/cli.js +48 -5
- package/src/commands/dev.js +14 -0
- package/src/commands/link.js +11 -1
- package/src/commands/new.js +39 -16
- package/src/commands/npm-run.js +14 -0
- package/src/commands/reload.js +5 -1
- package/src/commands/start.js +1 -1
- package/src/commands/upgrade.js +103 -32
- package/src/lib/build-script-upgrade.js +249 -0
- package/src/lib/caddy-version.js +31 -0
- package/src/lib/caddyfile.js +76 -36
- package/src/lib/doctor-site.js +15 -10
- package/src/lib/doctor.js +30 -29
- package/src/lib/htaccess-upgrade.js +79 -0
- package/src/lib/migrate.js +29 -2
- package/src/lib/package-versions.js +1 -1
- package/src/lib/php-runtime.js +249 -0
- package/src/lib/scaffold.js +24 -16
- package/templates/project/README.md.tmpl +8 -6
- package/templates/project/api/README.md.tmpl +206 -5
- package/templates/project/api/action.php +103 -0
- package/templates/project/api/actions/boom.php +21 -0
- package/templates/project/api/actions/contact.php +46 -0
- package/templates/project/api/actions/ping.php +23 -0
- package/templates/project/api/actions.php +59 -0
- package/templates/project/api/env.example +25 -0
- package/templates/project/api/lib/bootstrap.php +44 -0
- package/templates/project/api/lib/config.php +179 -0
- package/templates/project/api/lib/request.php +94 -0
- package/templates/project/api/lib/response.php +97 -0
- package/templates/project/api/lib/security.php +114 -0
- package/templates/project/api/lib/validate.php +69 -0
- package/templates/project/gitignore +4 -0
- package/templates/project/htaccess +7 -0
- package/templates/project/php-private/config.php +24 -0
- package/templates/project/scripts/build.mjs +128 -8
- package/templates/project/examples/php/README.md.tmpl +0 -18
- package/templates/project/examples/php/form.php +0 -105
- package/templates/project/examples/php/proxy.php +0 -171
package/src/commands/upgrade.js
CHANGED
|
@@ -22,10 +22,16 @@ import {
|
|
|
22
22
|
import { detectSite, normalizeSiteUrl } from '../lib/site-detect.js';
|
|
23
23
|
import {
|
|
24
24
|
applyProjectSync,
|
|
25
|
+
phpFromStagepassConfig,
|
|
25
26
|
planProjectSync,
|
|
26
27
|
readStagepassJson,
|
|
27
28
|
resolveUpgradeModules,
|
|
28
29
|
} from '../lib/project-sync.js';
|
|
30
|
+
import {
|
|
31
|
+
defaultBuildScriptTemplatePath,
|
|
32
|
+
planBuildScriptUpgrade,
|
|
33
|
+
} from '../lib/build-script-upgrade.js';
|
|
34
|
+
import { planHtaccessUpgrade } from '../lib/htaccess-upgrade.js';
|
|
29
35
|
|
|
30
36
|
const STAGEPASS_SCOPE = '@stagepass/';
|
|
31
37
|
|
|
@@ -166,7 +172,9 @@ export function applyStagepassVersions(pkg, target) {
|
|
|
166
172
|
/**
|
|
167
173
|
* Upgrade @stagepass/* deps in the current project's package.json and sync
|
|
168
174
|
* the declared modules into stagepass.json, AGENTS.md, the Cursor rule and
|
|
169
|
-
* tsconfig/jsconfig types.
|
|
175
|
+
* tsconfig/jsconfig types. Replaces scripts/build.mjs when it is still an
|
|
176
|
+
* unmodified scaffold from an earlier CLI. When stagepass.json has `"php": true`
|
|
177
|
+
* and the project root has no `.htaccess`, writes the shipped template.
|
|
170
178
|
*
|
|
171
179
|
* @param {object} [options]
|
|
172
180
|
* @param {boolean} [options.dryRun]
|
|
@@ -270,6 +278,22 @@ export async function upgrade(options = {}, deps = {}) {
|
|
|
270
278
|
if (!rl) rl = deps.createReadline?.() || readline.createInterface({ input, output });
|
|
271
279
|
return rl;
|
|
272
280
|
};
|
|
281
|
+
const closeRl = () => {
|
|
282
|
+
if (!rl) return;
|
|
283
|
+
rl.close();
|
|
284
|
+
rl = null;
|
|
285
|
+
};
|
|
286
|
+
const ask = async (question, def) => {
|
|
287
|
+
const answer = await getRl().question(`${question} (${def}): `);
|
|
288
|
+
const trimmed = String(answer || '').trim();
|
|
289
|
+
return trimmed === '' ? def : trimmed;
|
|
290
|
+
};
|
|
291
|
+
const confirmFn = async (question, defYes) => {
|
|
292
|
+
const answer = await getRl().question(`${question} (${defYes ? 'Y/n' : 'y/N'}): `);
|
|
293
|
+
const t = String(answer || '').trim().toLowerCase();
|
|
294
|
+
if (t === '') return defYes;
|
|
295
|
+
return ['y', 'yes'].includes(t);
|
|
296
|
+
};
|
|
273
297
|
let resolved;
|
|
274
298
|
try {
|
|
275
299
|
resolved = await resolveUpgradeModules(
|
|
@@ -279,21 +303,12 @@ export async function upgrade(options = {}, deps = {}) {
|
|
|
279
303
|
loadProjects: deps.loadProjects,
|
|
280
304
|
interactive,
|
|
281
305
|
log,
|
|
282
|
-
ask
|
|
283
|
-
|
|
284
|
-
const trimmed = String(answer || '').trim();
|
|
285
|
-
return trimmed === '' ? def : trimmed;
|
|
286
|
-
},
|
|
287
|
-
confirm: async (question, defYes) => {
|
|
288
|
-
const answer = await getRl().question(`${question} (${defYes ? 'Y/n' : 'y/N'}): `);
|
|
289
|
-
const t = String(answer || '').trim().toLowerCase();
|
|
290
|
-
if (t === '') return defYes;
|
|
291
|
-
return ['y', 'yes'].includes(t);
|
|
292
|
-
},
|
|
306
|
+
ask,
|
|
307
|
+
confirm: confirmFn,
|
|
293
308
|
}
|
|
294
309
|
);
|
|
295
310
|
} finally {
|
|
296
|
-
|
|
311
|
+
closeRl();
|
|
297
312
|
}
|
|
298
313
|
if (!resolved.ok) {
|
|
299
314
|
log(chalk.red(resolved.error));
|
|
@@ -311,25 +326,7 @@ export async function upgrade(options = {}, deps = {}) {
|
|
|
311
326
|
fse
|
|
312
327
|
);
|
|
313
328
|
|
|
314
|
-
|
|
315
|
-
if (plan.writes.length === 0) {
|
|
316
|
-
log(' Everything is up to date.');
|
|
317
|
-
}
|
|
318
|
-
for (const w of plan.writes) {
|
|
319
|
-
log(` ${w.file}: ${w.action}`);
|
|
320
|
-
}
|
|
321
|
-
for (const note of plan.notes) {
|
|
322
|
-
log(chalk.yellow(` note: ${note}`));
|
|
323
|
-
}
|
|
324
|
-
if (perModuleTypes === false && resolved.modules.length > 0 && !resolved.modules.includes('all')) {
|
|
325
|
-
log(
|
|
326
|
-
chalk.yellow(
|
|
327
|
-
' note: the published @stagepass/modules has no per-module types yet (needs 2.2.0); types use "@stagepass/modules". Run `stagepass upgrade` again once it is published.'
|
|
328
|
-
)
|
|
329
|
-
);
|
|
330
|
-
}
|
|
331
|
-
|
|
332
|
-
// Snippet: --version pin of loader on 2.x wins; resolve before writing so a failure changes nothing.
|
|
329
|
+
// Refuse an unpublished --version before asking about scripts/build.mjs or writing.
|
|
333
330
|
let loaderPub = '@stagepass/loader' in published ? published['@stagepass/loader'] : undefined;
|
|
334
331
|
const getLoaderPub = async () => {
|
|
335
332
|
if (loaderPub !== undefined) return loaderPub;
|
|
@@ -351,6 +348,80 @@ export async function upgrade(options = {}, deps = {}) {
|
|
|
351
348
|
if (t.resolved && satisfiesSimple(t.resolved, PACKAGES_MAJOR_RANGE)) snippetVersion = t.resolved;
|
|
352
349
|
}
|
|
353
350
|
|
|
351
|
+
/** @type {{ action: string, from?: string, content?: string, note?: string }} */
|
|
352
|
+
let buildUpgrade = { action: 'skipped' };
|
|
353
|
+
try {
|
|
354
|
+
let currentBuild;
|
|
355
|
+
try {
|
|
356
|
+
currentBuild = await fse.readFile(defaultBuildScriptTemplatePath(), 'utf8');
|
|
357
|
+
} catch (error) {
|
|
358
|
+
buildUpgrade = {
|
|
359
|
+
action: 'unreadable',
|
|
360
|
+
note: `Could not read the scaffold build script (${error instanceof Error ? error.message : String(error)}). The project script was left unchanged.`,
|
|
361
|
+
};
|
|
362
|
+
currentBuild = null;
|
|
363
|
+
}
|
|
364
|
+
if (currentBuild != null) {
|
|
365
|
+
buildUpgrade = await planBuildScriptUpgrade({
|
|
366
|
+
cwd,
|
|
367
|
+
currentSource: currentBuild,
|
|
368
|
+
interactive,
|
|
369
|
+
confirm: confirmFn,
|
|
370
|
+
fse,
|
|
371
|
+
});
|
|
372
|
+
}
|
|
373
|
+
if (buildUpgrade.action === 'update' && buildUpgrade.content) {
|
|
374
|
+
plan.writes.push({
|
|
375
|
+
file: 'scripts/build.mjs',
|
|
376
|
+
content: buildUpgrade.content,
|
|
377
|
+
action: `replace unmodified scaffold (${buildUpgrade.from})`,
|
|
378
|
+
});
|
|
379
|
+
}
|
|
380
|
+
} finally {
|
|
381
|
+
closeRl();
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
// Missing PHP .htaccess only. An existing path is never replaced.
|
|
385
|
+
const htaccessPlan = await planHtaccessUpgrade({
|
|
386
|
+
cwd,
|
|
387
|
+
php: phpFromStagepassConfig(sp.data),
|
|
388
|
+
fse,
|
|
389
|
+
});
|
|
390
|
+
if (htaccessPlan.action === 'create') {
|
|
391
|
+
plan.writes.push({
|
|
392
|
+
file: '.htaccess',
|
|
393
|
+
content: htaccessPlan.content,
|
|
394
|
+
action: 'created',
|
|
395
|
+
});
|
|
396
|
+
} else if (htaccessPlan.note) {
|
|
397
|
+
plan.notes.push(htaccessPlan.note);
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
log(chalk.bold(`\nProject files for modules: ${resolved.modules.length ? resolved.modules.join(', ') : 'none'}`));
|
|
401
|
+
if (plan.writes.length === 0) {
|
|
402
|
+
log(' Everything is up to date.');
|
|
403
|
+
}
|
|
404
|
+
for (const w of plan.writes) {
|
|
405
|
+
log(` ${w.file}: ${w.action}`);
|
|
406
|
+
}
|
|
407
|
+
for (const note of plan.notes) {
|
|
408
|
+
log(chalk.yellow(` note: ${note}`));
|
|
409
|
+
}
|
|
410
|
+
if (buildUpgrade.action === 'custom') {
|
|
411
|
+
log(chalk.yellow(buildUpgrade.note));
|
|
412
|
+
} else if (buildUpgrade.action !== 'update' && buildUpgrade.note) {
|
|
413
|
+
const warn = buildUpgrade.action === 'declined' || buildUpgrade.action === 'symlink' || buildUpgrade.action === 'unreadable';
|
|
414
|
+
const line = ` ${buildUpgrade.note}`;
|
|
415
|
+
log(warn ? chalk.yellow(line) : line);
|
|
416
|
+
}
|
|
417
|
+
if (perModuleTypes === false && resolved.modules.length > 0 && !resolved.modules.includes('all')) {
|
|
418
|
+
log(
|
|
419
|
+
chalk.yellow(
|
|
420
|
+
' note: the published @stagepass/modules has no per-module types yet (needs 2.2.0); types use "@stagepass/modules". Run `stagepass upgrade` again once it is published.'
|
|
421
|
+
)
|
|
422
|
+
);
|
|
423
|
+
}
|
|
424
|
+
|
|
354
425
|
if (options.dryRun) {
|
|
355
426
|
log(chalk.dim('\nDry run: no files were modified.'));
|
|
356
427
|
} else {
|
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Detect an unmodified scaffold `scripts/build.mjs` and render the current one.
|
|
3
|
+
*
|
|
4
|
+
* Known hashes are SHA-256 of the LF-normalized template bytes (placeholders
|
|
5
|
+
* intact) from git history of `templates/project/scripts/build.mjs`. When the
|
|
6
|
+
* template changes, append the previous file's hash here or older projects
|
|
7
|
+
* will be treated as hand-edited.
|
|
8
|
+
*
|
|
9
|
+
* Project copies differ only by the three scaffold tokens and by CRLF. Those
|
|
10
|
+
* are normalized before hashing. Any other edit stays custom and is not overwritten.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { createHash } from 'node:crypto';
|
|
14
|
+
import path from 'node:path';
|
|
15
|
+
import { fileURLToPath } from 'node:url';
|
|
16
|
+
import fs from 'fs-extra';
|
|
17
|
+
import { applyTemplate } from './scaffold.js';
|
|
18
|
+
|
|
19
|
+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
20
|
+
|
|
21
|
+
/** Project-relative path written by `stagepass new`. */
|
|
22
|
+
export const BUILD_SCRIPT_REL = 'scripts/build.mjs';
|
|
23
|
+
|
|
24
|
+
export const BUILD_SCRIPT_DOC_URL =
|
|
25
|
+
'https://github.com/arobertherz/stagepass/blob/master/docs/cli.md#upgrade';
|
|
26
|
+
|
|
27
|
+
export const BUILD_SCRIPT_CUSTOM_HINT = [
|
|
28
|
+
'scripts/build.mjs has local edits, so stagepass upgrade left it unchanged.',
|
|
29
|
+
`What the current scaffold does (${BUILD_SCRIPT_DOC_URL}):`,
|
|
30
|
+
' stagepass watch and stagepass dev write only stable dist/main.js and dist/styles.css. Hashed files and manifest.json stay as the last stagepass build left them.',
|
|
31
|
+
' stagepass build writes content hashes, stable copies, and SRI in dist/manifest.json, then deletes hashed files the previous manifest listed that this build does not. CLI 2.2.x manifests store those filenames as strings; those names are included. Only plain <base>.<8 hex><ext> files directly in dist/ are removed.',
|
|
32
|
+
' When stagepass.json has "php": true, stagepass build also copies api/, .php/config.php, and a project-root .htaccess into dist. It skips .env, .php/.env, .php/rate-limit, logs, pid files, and key, certificate, or database files (.pem, .key, .p12, .pfx, .crt, .cer, .sqlite, .db). It keeps .htaccess and .env.example. A dist/.htaccess stays when the project has none. Watch does not copy them.',
|
|
33
|
+
'Port that behavior into your script by hand. The Scripts section of a README from stagepass new describes the same stagepass build, stagepass watch and stagepass dev commands.',
|
|
34
|
+
].join('\n');
|
|
35
|
+
|
|
36
|
+
export const BUILD_SCRIPT_MISSING_NOTE =
|
|
37
|
+
'No scripts/build.mjs. stagepass build, stagepass watch and stagepass dev use the scaffold from stagepass new. See ' +
|
|
38
|
+
BUILD_SCRIPT_DOC_URL;
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Distinct blobs of the scaffold before the template bundled with this CLI.
|
|
42
|
+
* @type {{ id: string, commit: string, sha256: string }[]}
|
|
43
|
+
*/
|
|
44
|
+
export const KNOWN_SCAFFOLD_BUILD_SCRIPTS = [
|
|
45
|
+
{
|
|
46
|
+
id: '2.1.0-2.2.7',
|
|
47
|
+
commit: '8a98239',
|
|
48
|
+
sha256: 'f297b451318a5d37a26552fd2b284d1c8fc32d982d324d7554ea51010cc8da8b',
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
id: '2.3.0-sri',
|
|
52
|
+
commit: 'f7afa80',
|
|
53
|
+
sha256: 'a3af9742800b4652603f42683f78028cb51fb31791d904d61eb474ba3ab2d2cd',
|
|
54
|
+
},
|
|
55
|
+
{
|
|
56
|
+
id: '2.3.0',
|
|
57
|
+
commit: 'bcc87fc',
|
|
58
|
+
sha256: '10ee2eaa94b0b357bedb37bf89120b6ac971273e4e1fdc8c45eafb7d102bab22',
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
id: 'watch-stable',
|
|
62
|
+
commit: '688dca9',
|
|
63
|
+
sha256: '8f0bc02d4a58af8ad1b830bf53c4f712db6761f5155f4d11053f02ca203b88da',
|
|
64
|
+
},
|
|
65
|
+
{
|
|
66
|
+
id: 'pre-php',
|
|
67
|
+
commit: '9cb8c49',
|
|
68
|
+
sha256: 'f6507c32b6b59e97930bd9dbe1e4d272efcd2678213929b7bc1eb3aa0a167459',
|
|
69
|
+
},
|
|
70
|
+
{
|
|
71
|
+
id: '2.4.0',
|
|
72
|
+
commit: '983f090',
|
|
73
|
+
sha256: '98578f87225fd03d08b7e5d5bbdecc07cbb6ccbbc754642c6e880bb2f53559bb',
|
|
74
|
+
},
|
|
75
|
+
{
|
|
76
|
+
id: '2.4.0-keys',
|
|
77
|
+
commit: '6966954',
|
|
78
|
+
sha256: 'cba431ef9e0ff257ecced77613f6157c96d64f2a2db7679fcaa6467795d5ca9c',
|
|
79
|
+
},
|
|
80
|
+
];
|
|
81
|
+
|
|
82
|
+
const TOKEN_SPECS = [
|
|
83
|
+
{
|
|
84
|
+
key: 'ENTRY_JS',
|
|
85
|
+
placeholder: '{{ENTRY_JS}}',
|
|
86
|
+
re: /^const ENTRY_JS = process\.env\.STAGEPASS_ENTRY_JS \|\| '([^'\\]*)';$/m,
|
|
87
|
+
restore: "const ENTRY_JS = process.env.STAGEPASS_ENTRY_JS || '{{ENTRY_JS}}';",
|
|
88
|
+
},
|
|
89
|
+
{
|
|
90
|
+
key: 'ENTRY_CSS',
|
|
91
|
+
placeholder: '{{ENTRY_CSS}}',
|
|
92
|
+
re: /^const ENTRY_CSS = process\.env\.STAGEPASS_ENTRY_CSS \|\| '([^'\\]*)';$/m,
|
|
93
|
+
restore: "const ENTRY_CSS = process.env.STAGEPASS_ENTRY_CSS || '{{ENTRY_CSS}}';",
|
|
94
|
+
},
|
|
95
|
+
{
|
|
96
|
+
key: 'USE_SASS',
|
|
97
|
+
placeholder: '{{USE_SASS}}',
|
|
98
|
+
re: /^const USE_SASS = \(process\.env\.STAGEPASS_USE_SASS \|\| '([^'\\]*)'\) === 'true';$/m,
|
|
99
|
+
restore: "const USE_SASS = (process.env.STAGEPASS_USE_SASS || '{{USE_SASS}}') === 'true';",
|
|
100
|
+
},
|
|
101
|
+
];
|
|
102
|
+
|
|
103
|
+
/** Absolute path of the scaffold shipped in this package. */
|
|
104
|
+
export function defaultBuildScriptTemplatePath() {
|
|
105
|
+
return path.join(__dirname, '../../templates/project/scripts/build.mjs');
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* @param {string} source
|
|
110
|
+
* @returns {string}
|
|
111
|
+
*/
|
|
112
|
+
export function normalizeNewlines(source) {
|
|
113
|
+
return String(source).replace(/\r\n/g, '\n').replace(/\r/g, '\n');
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* LF newlines, and the three scaffold assignments restored to placeholders.
|
|
118
|
+
* @param {string} source
|
|
119
|
+
* @returns {string}
|
|
120
|
+
*/
|
|
121
|
+
export function canonicalizeBuildScript(source) {
|
|
122
|
+
let text = normalizeNewlines(source);
|
|
123
|
+
for (const spec of TOKEN_SPECS) {
|
|
124
|
+
text = text.replace(spec.re, spec.restore);
|
|
125
|
+
}
|
|
126
|
+
return text;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* @param {string} source
|
|
131
|
+
* @returns {string}
|
|
132
|
+
*/
|
|
133
|
+
export function hashBuildScript(source) {
|
|
134
|
+
return createHash('sha256').update(canonicalizeBuildScript(source), 'utf8').digest('hex');
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Entry paths currently written in a scaffold script.
|
|
139
|
+
* @param {string} source
|
|
140
|
+
* @returns {Record<string, string>}
|
|
141
|
+
*/
|
|
142
|
+
export function readScaffoldTokens(source) {
|
|
143
|
+
const text = normalizeNewlines(source);
|
|
144
|
+
/** @type {Record<string, string>} */
|
|
145
|
+
const tokens = {};
|
|
146
|
+
for (const spec of TOKEN_SPECS) {
|
|
147
|
+
const match = spec.re.exec(text);
|
|
148
|
+
tokens[spec.key] = match ? match[1] : spec.placeholder;
|
|
149
|
+
}
|
|
150
|
+
return tokens;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* @param {string} currentSource template bytes from this package
|
|
155
|
+
* @param {string} projectSource
|
|
156
|
+
* @returns {string}
|
|
157
|
+
*/
|
|
158
|
+
export function renderCurrentBuildScript(currentSource, projectSource) {
|
|
159
|
+
return applyTemplate(currentSource, readScaffoldTokens(projectSource));
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* @param {string} source project or template bytes
|
|
164
|
+
* @param {string} currentSource template bundled with this CLI
|
|
165
|
+
* @returns {{ status: 'current' | 'stock' | 'custom', hash: string, id?: string, commit?: string }}
|
|
166
|
+
*/
|
|
167
|
+
export function classifyBuildScript(source, currentSource) {
|
|
168
|
+
const hash = hashBuildScript(source);
|
|
169
|
+
if (hash === hashBuildScript(currentSource)) return { status: 'current', hash };
|
|
170
|
+
const known = KNOWN_SCAFFOLD_BUILD_SCRIPTS.find((item) => item.sha256 === hash);
|
|
171
|
+
if (known) return { status: 'stock', hash, id: known.id, commit: known.commit };
|
|
172
|
+
return { status: 'custom', hash };
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* @param {string} cwd
|
|
177
|
+
* @param {string} currentSource
|
|
178
|
+
* @param {{ lstat: Function, readFile: Function }} [fse]
|
|
179
|
+
* @returns {Promise<{ status: string, hash?: string, id?: string, commit?: string, raw?: string, error?: string }>}
|
|
180
|
+
*/
|
|
181
|
+
export async function inspectProjectBuildScript(cwd, currentSource, fse = fs) {
|
|
182
|
+
const abs = path.join(cwd, BUILD_SCRIPT_REL);
|
|
183
|
+
/** @type {import('node:fs').Stats} */
|
|
184
|
+
let st;
|
|
185
|
+
try {
|
|
186
|
+
st = await fse.lstat(abs);
|
|
187
|
+
} catch (err) {
|
|
188
|
+
if (err && /** @type {{ code?: string }} */ (err).code === 'ENOENT') return { status: 'missing' };
|
|
189
|
+
return {
|
|
190
|
+
status: 'unreadable',
|
|
191
|
+
error: err instanceof Error ? err.message : String(err),
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
if (st.isSymbolicLink()) return { status: 'symlink' };
|
|
195
|
+
if (!st.isFile()) return { status: 'unreadable', error: 'not a file' };
|
|
196
|
+
/** @type {string} */
|
|
197
|
+
let raw;
|
|
198
|
+
try {
|
|
199
|
+
raw = await fse.readFile(abs, 'utf8');
|
|
200
|
+
} catch (err) {
|
|
201
|
+
return {
|
|
202
|
+
status: 'unreadable',
|
|
203
|
+
error: err instanceof Error ? err.message : String(err),
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
const classified = classifyBuildScript(raw, currentSource);
|
|
207
|
+
return { ...classified, raw };
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* Decide whether upgrade should replace scripts/build.mjs.
|
|
212
|
+
* Does not write. `interactive` false (no TTY, or `--yes`) accepts a stock script.
|
|
213
|
+
*
|
|
214
|
+
* @param {object} options
|
|
215
|
+
* @param {string} options.cwd
|
|
216
|
+
* @param {string} options.currentSource
|
|
217
|
+
* @param {boolean} options.interactive
|
|
218
|
+
* @param {(question: string, defYes: boolean) => Promise<boolean>} [options.confirm]
|
|
219
|
+
* @param {{ lstat: Function, readFile: Function }} [options.fse]
|
|
220
|
+
*/
|
|
221
|
+
export async function planBuildScriptUpgrade(options) {
|
|
222
|
+
const fse = options.fse || fs;
|
|
223
|
+
const found = await inspectProjectBuildScript(options.cwd, options.currentSource, fse);
|
|
224
|
+
if (found.status === 'missing') return { action: 'missing', note: BUILD_SCRIPT_MISSING_NOTE };
|
|
225
|
+
if (found.status === 'symlink') {
|
|
226
|
+
return { action: 'symlink', note: 'scripts/build.mjs is a symlink; left unchanged.' };
|
|
227
|
+
}
|
|
228
|
+
if (found.status === 'unreadable') {
|
|
229
|
+
return {
|
|
230
|
+
action: 'unreadable',
|
|
231
|
+
note: `scripts/build.mjs could not be read; left unchanged. ${found.error || ''}`.trim(),
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
if (found.status === 'current') {
|
|
235
|
+
return { action: 'current', note: 'scripts/build.mjs already matches the current scaffold.' };
|
|
236
|
+
}
|
|
237
|
+
if (found.status === 'custom') return { action: 'custom', note: BUILD_SCRIPT_CUSTOM_HINT };
|
|
238
|
+
const content = renderCurrentBuildScript(options.currentSource, found.raw || '');
|
|
239
|
+
if (!options.interactive) return { action: 'update', from: found.id, content };
|
|
240
|
+
if (typeof options.confirm !== 'function') {
|
|
241
|
+
throw new Error('confirm is required when upgrade asks about scripts/build.mjs');
|
|
242
|
+
}
|
|
243
|
+
const yes = await options.confirm(
|
|
244
|
+
`Replace scripts/build.mjs with the current scaffold? It matches the unmodified ${found.id} template.`,
|
|
245
|
+
true
|
|
246
|
+
);
|
|
247
|
+
if (!yes) return { action: 'declined', note: 'scripts/build.mjs left unchanged.' };
|
|
248
|
+
return { action: 'update', from: found.id, content };
|
|
249
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { defaultExec } from './exec.js';
|
|
2
|
+
|
|
3
|
+
/** v3 site blocks (`match status 2xx`) need Caddy 2.9+. */
|
|
4
|
+
export const CADDY_UPGRADE_HINT = 'brew upgrade caddy';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Read `caddy version` (`v2.10.2 h1:...`) and whether it is at least 2.9.0.
|
|
8
|
+
* Not found, or output that does not parse, is `{ known: false }` so callers
|
|
9
|
+
* keep the previous behaviour.
|
|
10
|
+
*
|
|
11
|
+
* @param {import('./exec.js').ExecFn} [exec]
|
|
12
|
+
* @returns {Promise<{ known: true, version: string, ok: boolean } | { known: false }>}
|
|
13
|
+
*/
|
|
14
|
+
export async function readCaddyVersion(exec = defaultExec) {
|
|
15
|
+
let text = '';
|
|
16
|
+
try {
|
|
17
|
+
const result = await exec('caddy', ['version']);
|
|
18
|
+
text = `${result && result.stdout ? result.stdout : ''}\n${result && result.stderr ? result.stderr : ''}`;
|
|
19
|
+
} catch {
|
|
20
|
+
return { known: false };
|
|
21
|
+
}
|
|
22
|
+
const match = text.match(/v(\d+)\.(\d+)\.(\d+)/);
|
|
23
|
+
if (!match) return { known: false };
|
|
24
|
+
const major = Number(match[1]);
|
|
25
|
+
const minor = Number(match[2]);
|
|
26
|
+
return {
|
|
27
|
+
known: true,
|
|
28
|
+
version: `${major}.${minor}.${match[3]}`,
|
|
29
|
+
ok: major > 2 || (major === 2 && minor >= 9),
|
|
30
|
+
};
|
|
31
|
+
}
|
package/src/lib/caddyfile.js
CHANGED
|
@@ -3,8 +3,11 @@ import { escapeCaddyQuotedString, validateRoot } from './paths.js';
|
|
|
3
3
|
import { buildOriginAllowlistRegex, validateOriginList } from './origins.js';
|
|
4
4
|
import { isValidDevPort, normalizeDevPort } from './dev-port.js';
|
|
5
5
|
|
|
6
|
-
/**
|
|
7
|
-
|
|
6
|
+
/**
|
|
7
|
+
* Marker written inside every Stagepass-managed site block.
|
|
8
|
+
* v3: success responses set one ACAO (deferred replace) so PHP CORS is not duplicated.
|
|
9
|
+
*/
|
|
10
|
+
export const BLOCK_VERSION = 'v3';
|
|
8
11
|
|
|
9
12
|
/**
|
|
10
13
|
* Sensitive path / extension blocking (always on).
|
|
@@ -98,16 +101,7 @@ export function renderSiteBlock(domain, root, options = {}) {
|
|
|
98
101
|
lines.push(' php_fastcgi 127.0.0.1:9000');
|
|
99
102
|
}
|
|
100
103
|
lines.push(' file_server');
|
|
101
|
-
|
|
102
|
-
if (allowAllOrigins) {
|
|
103
|
-
lines.push(' header Access-Control-Allow-Origin *');
|
|
104
|
-
} else {
|
|
105
|
-
lines.push(
|
|
106
|
-
' header @cors_origin Access-Control-Allow-Origin "{http.request.header.Origin}"',
|
|
107
|
-
' header @cors_origin Vary Origin'
|
|
108
|
-
);
|
|
109
|
-
}
|
|
110
|
-
|
|
104
|
+
lines.push(...deferredSuccessCorsLines(allowAllOrigins));
|
|
111
105
|
lines.push(' }', '}', `# END: ${domain}`);
|
|
112
106
|
|
|
113
107
|
return lines.join('\n');
|
|
@@ -140,16 +134,33 @@ function liveReloadHandleLines(port, allowAllOrigins) {
|
|
|
140
134
|
' flush_interval -1',
|
|
141
135
|
' }',
|
|
142
136
|
];
|
|
137
|
+
lines.push(...deferredSuccessCorsLines(allowAllOrigins));
|
|
138
|
+
lines.push(' }');
|
|
139
|
+
return lines;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* One ACAO on HTTP 2xx, applied when the response is written.
|
|
144
|
+
* `>` replaces an upstream value (PHP) instead of adding a second one.
|
|
145
|
+
* Non-2xx responses are left alone so a PHP 403 does not gain ACAO.
|
|
146
|
+
* @param {boolean} allowAllOrigins
|
|
147
|
+
*/
|
|
148
|
+
function deferredSuccessCorsLines(allowAllOrigins) {
|
|
143
149
|
if (allowAllOrigins) {
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
'
|
|
148
|
-
'
|
|
149
|
-
|
|
150
|
+
return [
|
|
151
|
+
' header {',
|
|
152
|
+
' >Access-Control-Allow-Origin *',
|
|
153
|
+
' match status 2xx',
|
|
154
|
+
' }',
|
|
155
|
+
];
|
|
150
156
|
}
|
|
151
|
-
|
|
152
|
-
|
|
157
|
+
return [
|
|
158
|
+
' header @cors_origin {',
|
|
159
|
+
' >Access-Control-Allow-Origin "{http.request.header.Origin}"',
|
|
160
|
+
' >Vary Origin',
|
|
161
|
+
' match status 2xx',
|
|
162
|
+
' }',
|
|
163
|
+
];
|
|
153
164
|
}
|
|
154
165
|
|
|
155
166
|
function allowAllCorsLines() {
|
|
@@ -190,6 +201,15 @@ function allowlistCorsLines(origins) {
|
|
|
190
201
|
];
|
|
191
202
|
}
|
|
192
203
|
|
|
204
|
+
/**
|
|
205
|
+
* @param {string} blockText
|
|
206
|
+
* @returns {string|null} `v2`, `v3`, … or null when the block has no marker
|
|
207
|
+
*/
|
|
208
|
+
function blockMarkerVersion(blockText) {
|
|
209
|
+
const m = blockText.match(/# stagepass-block:\s*(v\d+)/);
|
|
210
|
+
return m ? m[1] : null;
|
|
211
|
+
}
|
|
212
|
+
|
|
193
213
|
/**
|
|
194
214
|
* Parse Stagepass START/END blocks from a Caddyfile.
|
|
195
215
|
* @param {string} content
|
|
@@ -302,9 +322,10 @@ export function removeBlock(content, domain) {
|
|
|
302
322
|
}
|
|
303
323
|
|
|
304
324
|
/**
|
|
305
|
-
*
|
|
325
|
+
* Rewrite blocks that are not the current marker.
|
|
306
326
|
*
|
|
307
|
-
*
|
|
327
|
+
* No marker (pre-v2): allowAllOrigins=true so existing Webflow embedding keeps working.
|
|
328
|
+
* An older marker (v2): same options comment, current header rules.
|
|
308
329
|
*
|
|
309
330
|
* @param {string} content
|
|
310
331
|
* @param {object} [opts]
|
|
@@ -313,24 +334,34 @@ export function removeBlock(content, domain) {
|
|
|
313
334
|
export function migrateLegacyContent(content, opts = {}) {
|
|
314
335
|
const warn = opts.warn || (() => {});
|
|
315
336
|
const blocks = parseBlocks(content);
|
|
316
|
-
const
|
|
317
|
-
|
|
318
|
-
if (
|
|
319
|
-
return {
|
|
337
|
+
const pending = blocks.filter((b) => blockMarkerVersion(b.full) !== BLOCK_VERSION);
|
|
338
|
+
|
|
339
|
+
if (pending.length === 0) {
|
|
340
|
+
return {
|
|
341
|
+
content,
|
|
342
|
+
changed: false,
|
|
343
|
+
migrated: [],
|
|
344
|
+
upgraded: [],
|
|
345
|
+
skipped: [],
|
|
346
|
+
warnings: [],
|
|
347
|
+
};
|
|
320
348
|
}
|
|
321
349
|
|
|
322
350
|
let next = content;
|
|
323
351
|
const migrated = [];
|
|
352
|
+
const upgraded = [];
|
|
324
353
|
const skipped = [];
|
|
325
354
|
const warnings = [];
|
|
326
355
|
|
|
327
|
-
const ordered = [...
|
|
356
|
+
const ordered = [...pending].sort((a, b) => b.index - a.index);
|
|
328
357
|
|
|
329
358
|
for (const block of ordered) {
|
|
359
|
+
const legacy = blockMarkerVersion(block.full) == null;
|
|
360
|
+
const kind = legacy ? 'legacy block' : 'block';
|
|
330
361
|
if (!block.root) {
|
|
331
362
|
const reason = 'could not parse root path';
|
|
332
363
|
skipped.push({ domain: block.domain, reason });
|
|
333
|
-
const msg = `Left
|
|
364
|
+
const msg = `Left ${kind} ${block.domain} unchanged (${reason}).`;
|
|
334
365
|
warnings.push(msg);
|
|
335
366
|
warn(msg);
|
|
336
367
|
continue;
|
|
@@ -340,7 +371,7 @@ export function migrateLegacyContent(content, opts = {}) {
|
|
|
340
371
|
if (!rootCheck.ok) {
|
|
341
372
|
const reason = rootCheck.error;
|
|
342
373
|
skipped.push({ domain: block.domain, reason });
|
|
343
|
-
const msg = `Left
|
|
374
|
+
const msg = `Left ${kind} ${block.domain} unchanged (${reason}).`;
|
|
344
375
|
warnings.push(msg);
|
|
345
376
|
warn(msg);
|
|
346
377
|
continue;
|
|
@@ -348,14 +379,20 @@ export function migrateLegacyContent(content, opts = {}) {
|
|
|
348
379
|
|
|
349
380
|
let newBlock;
|
|
350
381
|
try {
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
origins: []
|
|
354
|
-
|
|
382
|
+
// Legacy (no marker) keeps allow-all. A versioned block keeps its options comment.
|
|
383
|
+
const renderOptions = legacy
|
|
384
|
+
? { allowAllOrigins: true, origins: [] }
|
|
385
|
+
: {
|
|
386
|
+
allowAllOrigins: block.options.allowAllOrigins === true,
|
|
387
|
+
origins: block.options.origins || [],
|
|
388
|
+
php: block.options.php,
|
|
389
|
+
devPort: block.options.devPort,
|
|
390
|
+
};
|
|
391
|
+
newBlock = renderSiteBlock(block.domain, rootCheck.path, renderOptions);
|
|
355
392
|
} catch (e) {
|
|
356
393
|
const reason = e.message || String(e);
|
|
357
394
|
skipped.push({ domain: block.domain, reason });
|
|
358
|
-
const msg = `Left
|
|
395
|
+
const msg = `Left ${kind} ${block.domain} unchanged (${reason}).`;
|
|
359
396
|
warnings.push(msg);
|
|
360
397
|
warn(msg);
|
|
361
398
|
continue;
|
|
@@ -365,7 +402,9 @@ export function migrateLegacyContent(content, opts = {}) {
|
|
|
365
402
|
next.slice(0, block.index) +
|
|
366
403
|
newBlock.trim() +
|
|
367
404
|
next.slice(block.index + block.length);
|
|
368
|
-
|
|
405
|
+
const item = { domain: block.domain, root: rootCheck.path };
|
|
406
|
+
if (legacy) migrated.push(item);
|
|
407
|
+
else upgraded.push(item);
|
|
369
408
|
}
|
|
370
409
|
|
|
371
410
|
next = next.replace(/\n{3,}/g, '\n\n');
|
|
@@ -377,8 +416,9 @@ export function migrateLegacyContent(content, opts = {}) {
|
|
|
377
416
|
|
|
378
417
|
return {
|
|
379
418
|
content: next,
|
|
380
|
-
changed: migrated.length > 0,
|
|
419
|
+
changed: migrated.length + upgraded.length > 0,
|
|
381
420
|
migrated,
|
|
421
|
+
upgraded,
|
|
382
422
|
skipped,
|
|
383
423
|
warnings,
|
|
384
424
|
};
|