@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/lib/doctor-site.js
CHANGED
|
@@ -16,6 +16,7 @@ import {
|
|
|
16
16
|
resolveProjectOrigin,
|
|
17
17
|
} from './project-sync.js';
|
|
18
18
|
import { resolveCommand } from './exec.js';
|
|
19
|
+
import { checkPhpRuntime } from './php-runtime.js';
|
|
19
20
|
|
|
20
21
|
/**
|
|
21
22
|
* @param {object} [opts]
|
|
@@ -57,29 +58,33 @@ export async function runProjectSiteChecks(opts = {}) {
|
|
|
57
58
|
message: 'PHP is off for this project (no php_fastcgi on link)',
|
|
58
59
|
});
|
|
59
60
|
} else if (php === true) {
|
|
60
|
-
const
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
61
|
+
const runtime = await checkPhpRuntime({
|
|
62
|
+
exec: opts.exec,
|
|
63
|
+
resolveCommand: opts.resolveCommand || resolveCommand,
|
|
64
|
+
pathExists: opts.pathExists,
|
|
65
|
+
platform: opts.platform,
|
|
66
|
+
brewPrefix: opts.brewPrefix,
|
|
67
|
+
});
|
|
68
|
+
const apiOk = await fs.pathExists(path.join(cwd, 'api'));
|
|
69
|
+
if (!runtime.ok) {
|
|
65
70
|
checks.push({
|
|
66
71
|
id: 'project.php',
|
|
67
|
-
status:
|
|
68
|
-
message:
|
|
69
|
-
detail: 'Install PHP (stagepass setup) or set "php": false in stagepass.json',
|
|
72
|
+
status: runtime.status,
|
|
73
|
+
message: `Project enables PHP but ${runtime.message}`,
|
|
74
|
+
detail: runtime.detail || 'Install PHP (stagepass setup) or set "php": false in stagepass.json',
|
|
70
75
|
});
|
|
71
76
|
} else if (!apiOk) {
|
|
72
77
|
checks.push({
|
|
73
78
|
id: 'project.php',
|
|
74
79
|
status: 'warn',
|
|
75
|
-
message: `PHP
|
|
80
|
+
message: `PHP ${runtime.version.label} is installed but api/ is missing`,
|
|
76
81
|
detail: 'Create api/ for handlers served via php_fastcgi',
|
|
77
82
|
});
|
|
78
83
|
} else {
|
|
79
84
|
checks.push({
|
|
80
85
|
id: 'project.php',
|
|
81
86
|
status: 'ok',
|
|
82
|
-
message: `PHP enabled for this project (php=${phpPath}, api/ present)`,
|
|
87
|
+
message: `PHP ${runtime.version.label} enabled for this project (php=${runtime.phpPath}, api/ present)`,
|
|
83
88
|
});
|
|
84
89
|
}
|
|
85
90
|
}
|
package/src/lib/doctor.js
CHANGED
|
@@ -3,6 +3,8 @@ import { createRequire } from 'node:module';
|
|
|
3
3
|
import { runProjectSiteChecks } from './doctor-site.js';
|
|
4
4
|
import path from 'node:path';
|
|
5
5
|
import { defaultExec, resolveCommand } from './exec.js';
|
|
6
|
+
import { CADDY_UPGRADE_HINT, readCaddyVersion } from './caddy-version.js';
|
|
7
|
+
import { checkPhpRuntime } from './php-runtime.js';
|
|
6
8
|
import {
|
|
7
9
|
getCaddyfilePath,
|
|
8
10
|
getProjectsPath,
|
|
@@ -202,41 +204,36 @@ export async function runDoctor(opts = {}) {
|
|
|
202
204
|
}
|
|
203
205
|
}
|
|
204
206
|
|
|
205
|
-
// Caddy
|
|
207
|
+
// Caddy (v3 site blocks need 2.9+; unknown version keeps the previous ok check)
|
|
206
208
|
const caddyPath = await resolveCommand('caddy', exec);
|
|
207
209
|
if (!caddyPath) {
|
|
208
210
|
checks.push(check('caddy', 'fail', 'Caddy not found on PATH'));
|
|
209
211
|
} else {
|
|
210
|
-
|
|
212
|
+
const rel = await readCaddyVersion(exec);
|
|
213
|
+
if (rel.known && !rel.ok) {
|
|
214
|
+
checks.push(
|
|
215
|
+
check(
|
|
216
|
+
'caddy',
|
|
217
|
+
'warn',
|
|
218
|
+
`Caddy ${rel.version} is older than 2.9 (v3 site block needs 2.9 or newer)`,
|
|
219
|
+
CADDY_UPGRADE_HINT
|
|
220
|
+
)
|
|
221
|
+
);
|
|
222
|
+
} else if (rel.known) {
|
|
223
|
+
checks.push(check('caddy', 'ok', `Caddy ${rel.version} found (${caddyPath})`));
|
|
224
|
+
} else {
|
|
225
|
+
checks.push(check('caddy', 'ok', `Caddy found (${caddyPath})`));
|
|
226
|
+
}
|
|
211
227
|
}
|
|
212
228
|
|
|
213
|
-
// PHP / php-fpm
|
|
214
|
-
const
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
checks.push(check('php', 'fail', 'PHP / php-fpm not found'));
|
|
222
|
-
} else if (!phpFpmPath) {
|
|
223
|
-
checks.push(
|
|
224
|
-
check(
|
|
225
|
-
'php',
|
|
226
|
-
'warn',
|
|
227
|
-
`PHP found (${phpPath}) but php-fpm binary not found`,
|
|
228
|
-
'Stagepass Caddy blocks use php_fastcgi 127.0.0.1:9000'
|
|
229
|
-
)
|
|
230
|
-
);
|
|
231
|
-
} else {
|
|
232
|
-
checks.push(
|
|
233
|
-
check(
|
|
234
|
-
'php',
|
|
235
|
-
'ok',
|
|
236
|
-
`PHP/php-fpm found (${phpFpmPath}${phpPath ? `; php=${phpPath}` : ''})`
|
|
237
|
-
)
|
|
238
|
-
);
|
|
239
|
-
}
|
|
229
|
+
// PHP / php-fpm (version >= 8.2, php-fpm or php-cgi)
|
|
230
|
+
const phpRuntime = await checkPhpRuntime({
|
|
231
|
+
exec,
|
|
232
|
+
pathExists,
|
|
233
|
+
brewPrefix,
|
|
234
|
+
platform: opts.platform,
|
|
235
|
+
});
|
|
236
|
+
checks.push(check('php', phpRuntime.status, phpRuntime.message, phpRuntime.detail || undefined));
|
|
240
237
|
|
|
241
238
|
// dnsmasq
|
|
242
239
|
let dnsmasqPath = await resolveCommand('dnsmasq', exec);
|
|
@@ -637,6 +634,10 @@ export async function runDoctor(opts = {}) {
|
|
|
637
634
|
skipSite: opts.skipSite === true,
|
|
638
635
|
detectSite: opts.detectSite,
|
|
639
636
|
loadProjects: opts.loadProjects,
|
|
637
|
+
exec,
|
|
638
|
+
pathExists,
|
|
639
|
+
platform: opts.platform,
|
|
640
|
+
brewPrefix,
|
|
640
641
|
});
|
|
641
642
|
for (const c of projectChecks) checks.push(check(c.id, c.status, c.message, c.detail));
|
|
642
643
|
} catch (error) {
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Plan a missing PHP `.htaccess` for `stagepass upgrade`.
|
|
3
|
+
* Covered when the file contains the shipped line `RewriteRule "(^|/)\." - [F]`.
|
|
4
|
+
* Symlinks and directories are not followed and are not replaced.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import path from 'node:path';
|
|
8
|
+
import { fileURLToPath } from 'node:url';
|
|
9
|
+
import fs from 'fs-extra';
|
|
10
|
+
|
|
11
|
+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
12
|
+
|
|
13
|
+
/** Exact deny line from templates/project/htaccess. */
|
|
14
|
+
export const HTACCESS_DENY_LINE = 'RewriteRule "(^|/)\\." - [F]';
|
|
15
|
+
|
|
16
|
+
const HTACCESS_DOCS = 'https://stagepass.one/docs/php/ and api/README.md';
|
|
17
|
+
|
|
18
|
+
export function defaultHtaccessTemplatePath() {
|
|
19
|
+
return path.join(__dirname, '../../templates/project/htaccess');
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function htaccessHasDenyRule(source) {
|
|
23
|
+
return String(source).includes(HTACCESS_DENY_LINE);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Does not write. `php` must be the boolean from stagepass.json.
|
|
28
|
+
*
|
|
29
|
+
* @param {object} options
|
|
30
|
+
* @param {string} options.cwd
|
|
31
|
+
* @param {boolean} [options.php]
|
|
32
|
+
* @param {{ lstat: Function, readFile: Function }} [options.fse]
|
|
33
|
+
* @returns {Promise<{ action: 'skip' | 'create' | 'present' | 'hint' | 'blocked', content?: string, note?: string }>}
|
|
34
|
+
*/
|
|
35
|
+
export async function planHtaccessUpgrade(options) {
|
|
36
|
+
if (options.php !== true) return { action: 'skip' };
|
|
37
|
+
const fse = options.fse || fs;
|
|
38
|
+
const abs = path.join(options.cwd, '.htaccess');
|
|
39
|
+
/** @type {import('node:fs').Stats} */
|
|
40
|
+
let st;
|
|
41
|
+
try {
|
|
42
|
+
st = await fse.lstat(abs);
|
|
43
|
+
} catch (err) {
|
|
44
|
+
if (err && /** @type {{ code?: string }} */ (err).code === 'ENOENT') {
|
|
45
|
+
try {
|
|
46
|
+
const content = await fse.readFile(defaultHtaccessTemplatePath(), 'utf8');
|
|
47
|
+
return { action: 'create', content };
|
|
48
|
+
} catch (readErr) {
|
|
49
|
+
const detail = readErr instanceof Error ? readErr.message : String(readErr);
|
|
50
|
+
return {
|
|
51
|
+
action: 'blocked',
|
|
52
|
+
note: `Could not read the .htaccess template (${detail}). Nothing was written.`,
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return { action: 'blocked', note: '.htaccess could not be inspected; left unchanged.' };
|
|
57
|
+
}
|
|
58
|
+
if (st.isSymbolicLink()) {
|
|
59
|
+
return { action: 'blocked', note: '.htaccess is a symlink; left unchanged.' };
|
|
60
|
+
}
|
|
61
|
+
if (st.isDirectory()) {
|
|
62
|
+
return { action: 'blocked', note: '.htaccess is a directory; left unchanged.' };
|
|
63
|
+
}
|
|
64
|
+
if (!st.isFile()) {
|
|
65
|
+
return { action: 'blocked', note: '.htaccess is not a regular file; left unchanged.' };
|
|
66
|
+
}
|
|
67
|
+
/** @type {string} */
|
|
68
|
+
let raw;
|
|
69
|
+
try {
|
|
70
|
+
raw = await fse.readFile(abs, 'utf8');
|
|
71
|
+
} catch {
|
|
72
|
+
return { action: 'blocked', note: '.htaccess could not be read; left unchanged.' };
|
|
73
|
+
}
|
|
74
|
+
if (htaccessHasDenyRule(raw)) return { action: 'present' };
|
|
75
|
+
return {
|
|
76
|
+
action: 'hint',
|
|
77
|
+
note: `.htaccess exists and has no ${HTACCESS_DENY_LINE} line. Left unchanged. See ${HTACCESS_DOCS}.`,
|
|
78
|
+
};
|
|
79
|
+
}
|
package/src/lib/migrate.js
CHANGED
|
@@ -2,10 +2,12 @@ import fs from 'fs-extra';
|
|
|
2
2
|
import path from 'path';
|
|
3
3
|
import chalk from 'chalk';
|
|
4
4
|
import {
|
|
5
|
+
BLOCK_VERSION,
|
|
5
6
|
formatBackupStamp,
|
|
6
7
|
migrateLegacyContent,
|
|
7
8
|
parseBlocks,
|
|
8
9
|
} from './caddyfile.js';
|
|
10
|
+
import { CADDY_UPGRADE_HINT, readCaddyVersion } from './caddy-version.js';
|
|
9
11
|
import { ensureMigratedProject } from './projects.js';
|
|
10
12
|
import { getCaddyfilePath, getProjectsPath } from './paths.js';
|
|
11
13
|
|
|
@@ -17,6 +19,8 @@ import { getCaddyfilePath, getProjectsPath } from './paths.js';
|
|
|
17
19
|
* @param {string} [options.caddyfilePath]
|
|
18
20
|
* @param {string} [options.projectsPath]
|
|
19
21
|
* @param {boolean} [options.silent]
|
|
22
|
+
* @param {import('./exec.js').ExecFn} [options.exec]
|
|
23
|
+
* @param {Awaited<ReturnType<typeof readCaddyVersion>>} [options.caddy]
|
|
20
24
|
* @returns {Promise<{ changed: boolean, migrated: Array, skipped: Array, backupPath: string|null }>}
|
|
21
25
|
*/
|
|
22
26
|
export async function ensureCaddyfileMigrated(options = {}) {
|
|
@@ -25,10 +29,24 @@ export async function ensureCaddyfileMigrated(options = {}) {
|
|
|
25
29
|
const silent = options.silent === true;
|
|
26
30
|
|
|
27
31
|
if (!(await fs.pathExists(caddyfilePath))) {
|
|
28
|
-
return { changed: false, migrated: [], skipped: [], backupPath: null };
|
|
32
|
+
return { changed: false, migrated: [], upgraded: [], skipped: [], backupPath: null };
|
|
29
33
|
}
|
|
30
34
|
|
|
31
35
|
const original = await fs.readFile(caddyfilePath, 'utf-8');
|
|
36
|
+
const caddy = options.caddy || (await readCaddyVersion(options.exec));
|
|
37
|
+
const current = new RegExp(`# stagepass-block:\\s*${BLOCK_VERSION}\\b`);
|
|
38
|
+
const stale = parseBlocks(original).some((block) => !current.test(block.full));
|
|
39
|
+
if (caddy.known && !caddy.ok && stale) {
|
|
40
|
+
if (!silent) {
|
|
41
|
+
console.log(
|
|
42
|
+
chalk.yellow(
|
|
43
|
+
` Caddy ${caddy.version} is older than 2.9, so existing site blocks were left unchanged. ${CADDY_UPGRADE_HINT}`
|
|
44
|
+
)
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
return { changed: false, migrated: [], upgraded: [], skipped: [], backupPath: null };
|
|
48
|
+
}
|
|
49
|
+
|
|
32
50
|
const result = migrateLegacyContent(original, {
|
|
33
51
|
warn: silent
|
|
34
52
|
? () => {}
|
|
@@ -39,6 +57,7 @@ export async function ensureCaddyfileMigrated(options = {}) {
|
|
|
39
57
|
return {
|
|
40
58
|
changed: false,
|
|
41
59
|
migrated: [],
|
|
60
|
+
upgraded: [],
|
|
42
61
|
skipped: result.skipped,
|
|
43
62
|
backupPath: null,
|
|
44
63
|
};
|
|
@@ -52,7 +71,7 @@ export async function ensureCaddyfileMigrated(options = {}) {
|
|
|
52
71
|
await ensureMigratedProject(item.domain, item.root, projectsPath);
|
|
53
72
|
}
|
|
54
73
|
|
|
55
|
-
if (!silent) {
|
|
74
|
+
if (!silent && result.migrated.length > 0) {
|
|
56
75
|
console.log(
|
|
57
76
|
chalk.yellow(
|
|
58
77
|
` Migrated ${result.migrated.length} legacy Caddy block(s). Backup: ${path.basename(backupPath)}`
|
|
@@ -67,10 +86,18 @@ export async function ensureCaddyfileMigrated(options = {}) {
|
|
|
67
86
|
)
|
|
68
87
|
);
|
|
69
88
|
}
|
|
89
|
+
if (!silent && result.upgraded.length > 0) {
|
|
90
|
+
console.log(
|
|
91
|
+
chalk.dim(
|
|
92
|
+
` Updated ${result.upgraded.length} Caddy block(s) to stagepass-block ${BLOCK_VERSION}.`
|
|
93
|
+
)
|
|
94
|
+
);
|
|
95
|
+
}
|
|
70
96
|
|
|
71
97
|
return {
|
|
72
98
|
changed: true,
|
|
73
99
|
migrated: result.migrated,
|
|
100
|
+
upgraded: result.upgraded,
|
|
74
101
|
skipped: result.skipped,
|
|
75
102
|
backupPath,
|
|
76
103
|
};
|
|
@@ -5,5 +5,5 @@
|
|
|
5
5
|
* Sourced from packages/loader and packages/modules package.json at generate time.
|
|
6
6
|
* The published CLI ships this snapshot; it cannot read the monorepo at runtime.
|
|
7
7
|
*/
|
|
8
|
-
export const LOADER_VERSION = "2.
|
|
8
|
+
export const LOADER_VERSION = "2.5.0";
|
|
9
9
|
export const MODULES_VERSION = "2.2.2";
|
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared PHP 8.2+ / php-fpm (or php-cgi) check for new, doctor, dev, and watch.
|
|
3
|
+
* `exec` is injectable. Never throws on a missing binary.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import fs from 'fs-extra';
|
|
7
|
+
import path from 'node:path';
|
|
8
|
+
import { defaultExec, resolveCommand } from './exec.js';
|
|
9
|
+
import { phpFromStagepassConfig, readStagepassJson } from './project-sync.js';
|
|
10
|
+
import { ensurePhpFpm as defaultEnsurePhpFpm } from './php-fpm.js';
|
|
11
|
+
|
|
12
|
+
/** Drop C0/DEL and collapse whitespace so command output cannot break a line. */
|
|
13
|
+
export function plainLine(value, max = 240) {
|
|
14
|
+
const text = String(value ?? '')
|
|
15
|
+
.replace(/[\u0000-\u001F\u007F]/g, ' ')
|
|
16
|
+
.replace(/\s+/g, ' ')
|
|
17
|
+
.trim();
|
|
18
|
+
return text.length > max ? text.slice(0, max) : text;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* @param {string} text
|
|
23
|
+
* @returns {{ major: number, minor: number, patch: number, label: string } | null}
|
|
24
|
+
*/
|
|
25
|
+
export function parsePhpVersion(text) {
|
|
26
|
+
const match = plainLine(text).match(/PHP\s+(\d+)\.(\d+)\.(\d+)/);
|
|
27
|
+
if (!match) return null;
|
|
28
|
+
return {
|
|
29
|
+
major: Number(match[1]),
|
|
30
|
+
minor: Number(match[2]),
|
|
31
|
+
patch: Number(match[3]),
|
|
32
|
+
label: `${match[1]}.${match[2]}.${match[3]}`,
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* @param {{ major: number, minor: number } | null} version
|
|
38
|
+
*/
|
|
39
|
+
export function phpVersionOk(version) {
|
|
40
|
+
if (!version) return false;
|
|
41
|
+
if (version.major > 8) return true;
|
|
42
|
+
if (version.major < 8) return false;
|
|
43
|
+
return version.minor >= 2;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* @param {string} [platform]
|
|
48
|
+
*/
|
|
49
|
+
export function phpInstallHint(platform) {
|
|
50
|
+
if (platform === 'darwin') {
|
|
51
|
+
return 'Install on macOS: brew install php\nThen start it: brew services start php';
|
|
52
|
+
}
|
|
53
|
+
return 'Install PHP 8.2 or newer and php-fpm (or php-cgi). On Debian/Ubuntu: sudo apt install php-fpm, then start the php-fpm service.';
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* @param {import('./exec.js').ExecFn} exec
|
|
58
|
+
* @param {string} file
|
|
59
|
+
* @param {string[]} args
|
|
60
|
+
*/
|
|
61
|
+
async function capture(exec, file, args) {
|
|
62
|
+
try {
|
|
63
|
+
const result = await exec(file, args);
|
|
64
|
+
return {
|
|
65
|
+
stdout: result && result.stdout != null ? String(result.stdout) : '',
|
|
66
|
+
stderr: result && result.stderr != null ? String(result.stderr) : '',
|
|
67
|
+
};
|
|
68
|
+
} catch (err) {
|
|
69
|
+
const error = /** @type {{ stdout?: string, stderr?: string }} */ (err);
|
|
70
|
+
return {
|
|
71
|
+
stdout: error && error.stdout != null ? String(error.stdout) : '',
|
|
72
|
+
stderr: error && error.stderr != null ? String(error.stderr) : '',
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* @param {object} [opts]
|
|
79
|
+
* @param {import('./exec.js').ExecFn} [opts.exec]
|
|
80
|
+
* @param {typeof resolveCommand} [opts.resolveCommand]
|
|
81
|
+
* @param {(p: string) => Promise<boolean>} [opts.pathExists]
|
|
82
|
+
* @param {string|null} [opts.brewPrefix]
|
|
83
|
+
* @param {string} [opts.platform]
|
|
84
|
+
* @returns {Promise<{
|
|
85
|
+
* ok: boolean,
|
|
86
|
+
* status: 'ok'|'warn'|'fail',
|
|
87
|
+
* message: string,
|
|
88
|
+
* detail: string,
|
|
89
|
+
* version: { major: number, minor: number, patch: number, label: string } | null,
|
|
90
|
+
* phpPath: string|null,
|
|
91
|
+
* fastcgi: string|null,
|
|
92
|
+
* }>}
|
|
93
|
+
*/
|
|
94
|
+
export async function checkPhpRuntime(opts = {}) {
|
|
95
|
+
const exec = opts.exec || defaultExec;
|
|
96
|
+
const which = opts.resolveCommand || resolveCommand;
|
|
97
|
+
const pathExists = opts.pathExists || ((p) => fs.pathExists(p));
|
|
98
|
+
const platform = opts.platform || process.platform;
|
|
99
|
+
const hint = phpInstallHint(platform);
|
|
100
|
+
|
|
101
|
+
const phpPath = await which('php', exec);
|
|
102
|
+
let fpm = await which('php-fpm', exec);
|
|
103
|
+
const cgi = await which('php-cgi', exec);
|
|
104
|
+
if (!fpm && opts.brewPrefix) {
|
|
105
|
+
const candidate = path.join(opts.brewPrefix, 'sbin', 'php-fpm');
|
|
106
|
+
if (await pathExists(candidate)) fpm = candidate;
|
|
107
|
+
}
|
|
108
|
+
const fastcgi = fpm || cgi || null;
|
|
109
|
+
const fastcgiLabel = fpm ? 'php-fpm' : 'php-cgi';
|
|
110
|
+
|
|
111
|
+
/** @type {{ major: number, minor: number, patch: number, label: string } | null} */
|
|
112
|
+
let version = null;
|
|
113
|
+
if (phpPath) {
|
|
114
|
+
const out = await capture(exec, phpPath, ['-v']);
|
|
115
|
+
version = parsePhpVersion(`${out.stdout}\n${out.stderr}`);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const base = { version, phpPath, fastcgi };
|
|
119
|
+
|
|
120
|
+
if (!phpPath && !fastcgi) {
|
|
121
|
+
return {
|
|
122
|
+
...base,
|
|
123
|
+
ok: false,
|
|
124
|
+
status: 'fail',
|
|
125
|
+
message: 'PHP / php-fpm not found',
|
|
126
|
+
detail: hint,
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
if (phpPath && !version) {
|
|
130
|
+
return {
|
|
131
|
+
...base,
|
|
132
|
+
ok: false,
|
|
133
|
+
status: 'warn',
|
|
134
|
+
message: `PHP found (${plainLine(phpPath)}) but the version could not be read (need 8.2 or newer)`,
|
|
135
|
+
detail: hint,
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
if (version && !phpVersionOk(version)) {
|
|
139
|
+
return {
|
|
140
|
+
...base,
|
|
141
|
+
ok: false,
|
|
142
|
+
status: 'warn',
|
|
143
|
+
message: `PHP ${version.label} is older than 8.2`,
|
|
144
|
+
detail: hint,
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
if (!fastcgi) {
|
|
148
|
+
return {
|
|
149
|
+
...base,
|
|
150
|
+
ok: false,
|
|
151
|
+
status: 'warn',
|
|
152
|
+
message: `PHP ${version ? version.label : 'found'} (${plainLine(phpPath)}) but neither php-fpm nor php-cgi was found`,
|
|
153
|
+
detail: `Caddy php_fastcgi uses 127.0.0.1:9000. ${hint}`,
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
if (!phpPath || !version) {
|
|
157
|
+
return {
|
|
158
|
+
...base,
|
|
159
|
+
ok: false,
|
|
160
|
+
status: 'warn',
|
|
161
|
+
message: `${fastcgiLabel} found (${plainLine(fastcgi)}) but the php CLI was not found, so the version was not checked (need 8.2 or newer)`,
|
|
162
|
+
detail: hint,
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
return {
|
|
166
|
+
...base,
|
|
167
|
+
ok: true,
|
|
168
|
+
status: 'ok',
|
|
169
|
+
message: `PHP ${version.label} and ${fastcgiLabel} found (${plainLine(fastcgi)}; php=${plainLine(phpPath)})`,
|
|
170
|
+
detail: '',
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* @param {string} name validated project name
|
|
176
|
+
*/
|
|
177
|
+
export function phpOriginsHint(name) {
|
|
178
|
+
const safe = plainLine(name).replace(/[^a-z0-9-]/gi, '') || 'your-project';
|
|
179
|
+
return `Copy api/.env.example to .env in the project root (do not commit .env). Set ALLOWED_ORIGINS to exact origins with no trailing slash, including https://${safe}.sp`;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Warning for new / dev / watch. Does not include a stack trace.
|
|
184
|
+
* @param {{ message: string, detail?: string }} result
|
|
185
|
+
* @param {string} [domain] linked name.sp
|
|
186
|
+
*/
|
|
187
|
+
export function phpProjectWarning(result, domain) {
|
|
188
|
+
const host = plainLine(domain).replace(/[^a-z0-9.-]/gi, '') || '<project>.sp';
|
|
189
|
+
const lines = [result.message];
|
|
190
|
+
if (result.detail) lines.push(result.detail);
|
|
191
|
+
lines.push(
|
|
192
|
+
`JavaScript build and live reload will continue. https://${host}/api/action.php needs PHP before it can answer.`
|
|
193
|
+
);
|
|
194
|
+
return lines.join('\n');
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* Warn when this project opted into PHP and the runtime is missing or too old.
|
|
199
|
+
* Does not throw and does not stop the JS build.
|
|
200
|
+
*
|
|
201
|
+
* @param {string} cwd
|
|
202
|
+
* @param {object} [opts]
|
|
203
|
+
* @param {typeof fs} [opts.fs]
|
|
204
|
+
* @param {import('./exec.js').ExecFn} [opts.exec]
|
|
205
|
+
* @param {(msg: string) => void} [opts.warn]
|
|
206
|
+
* @param {string} [opts.domain]
|
|
207
|
+
* @param {boolean} [opts.ensure] start php-fpm when the runtime looks usable
|
|
208
|
+
*/
|
|
209
|
+
export async function warnPhpProject(cwd, opts = {}) {
|
|
210
|
+
const fse = opts.fs || fs;
|
|
211
|
+
const warn = opts.warn || (() => {});
|
|
212
|
+
let data = null;
|
|
213
|
+
try {
|
|
214
|
+
const sp = await readStagepassJson(cwd, fse);
|
|
215
|
+
data = sp.data;
|
|
216
|
+
} catch {
|
|
217
|
+
return { applicable: false };
|
|
218
|
+
}
|
|
219
|
+
if (phpFromStagepassConfig(data) !== true) return { applicable: false };
|
|
220
|
+
|
|
221
|
+
let result;
|
|
222
|
+
try {
|
|
223
|
+
result = await checkPhpRuntime(opts);
|
|
224
|
+
} catch {
|
|
225
|
+
warn('PHP could not be checked. JavaScript build and live reload will continue.');
|
|
226
|
+
return { applicable: true, ok: false };
|
|
227
|
+
}
|
|
228
|
+
if (!result.ok) {
|
|
229
|
+
warn(phpProjectWarning(result, opts.domain));
|
|
230
|
+
return { applicable: true, ok: false, result };
|
|
231
|
+
}
|
|
232
|
+
if (opts.ensure !== false) {
|
|
233
|
+
const ensure = opts.ensurePhpFpm || defaultEnsurePhpFpm;
|
|
234
|
+
try {
|
|
235
|
+
const started = await ensure({ exec: opts.exec, stdio: 'ignore' });
|
|
236
|
+
if (!started || started.ok === false) {
|
|
237
|
+
const detail = plainLine(started && started.detail);
|
|
238
|
+
warn(
|
|
239
|
+
detail
|
|
240
|
+
? `PHP-FPM was not started (${detail}). JavaScript build and live reload will continue.`
|
|
241
|
+
: 'PHP-FPM was not started. JavaScript build and live reload will continue.'
|
|
242
|
+
);
|
|
243
|
+
}
|
|
244
|
+
} catch {
|
|
245
|
+
warn('PHP-FPM was not started. JavaScript build and live reload will continue.');
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
return { applicable: true, ok: true, result };
|
|
249
|
+
}
|
package/src/lib/scaffold.js
CHANGED
|
@@ -47,22 +47,31 @@ export function scaffoldTemplateMap(lang, styles) {
|
|
|
47
47
|
}
|
|
48
48
|
|
|
49
49
|
/**
|
|
50
|
-
*
|
|
50
|
+
* Boilerplate files when PHP is enabled.
|
|
51
|
+
* Dot paths are renamed in the package (npm pack drops dotfiles) and restored on write.
|
|
52
|
+
* `phpExamples` is ignored: sample actions live in api/actions/.
|
|
51
53
|
* @param {{ php?: boolean, phpExamples?: boolean }} options
|
|
52
54
|
* @returns {{ template: string, out: string }[]}
|
|
53
55
|
*/
|
|
54
56
|
export function scaffoldPhpTemplateMap(options = {}) {
|
|
55
57
|
if (options.php !== true) return [];
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
58
|
+
return [
|
|
59
|
+
{ template: 'api/README.md.tmpl', out: 'api/README.md' },
|
|
60
|
+
{ template: 'api/action.php', out: 'api/action.php' },
|
|
61
|
+
{ template: 'api/actions.php', out: 'api/actions.php' },
|
|
62
|
+
{ template: 'api/actions/ping.php', out: 'api/actions/ping.php' },
|
|
63
|
+
{ template: 'api/actions/contact.php', out: 'api/actions/contact.php' },
|
|
64
|
+
{ template: 'api/actions/boom.php', out: 'api/actions/boom.php' },
|
|
65
|
+
{ template: 'api/lib/bootstrap.php', out: 'api/lib/bootstrap.php' },
|
|
66
|
+
{ template: 'api/lib/config.php', out: 'api/lib/config.php' },
|
|
67
|
+
{ template: 'api/lib/request.php', out: 'api/lib/request.php' },
|
|
68
|
+
{ template: 'api/lib/response.php', out: 'api/lib/response.php' },
|
|
69
|
+
{ template: 'api/lib/security.php', out: 'api/lib/security.php' },
|
|
70
|
+
{ template: 'api/lib/validate.php', out: 'api/lib/validate.php' },
|
|
71
|
+
{ template: 'api/env.example', out: 'api/.env.example' },
|
|
72
|
+
{ template: 'php-private/config.php', out: '.php/config.php' },
|
|
73
|
+
{ template: 'htaccess', out: '.htaccess' },
|
|
74
|
+
];
|
|
66
75
|
}
|
|
67
76
|
|
|
68
77
|
/**
|
|
@@ -77,7 +86,7 @@ export function scaffoldPhpTemplateMap(options = {}) {
|
|
|
77
86
|
* @property {string} [origin] Staging / primary live host (stagepass.json `origin`)
|
|
78
87
|
* @property {string} [production] Optional production host (stagepass.json `production`)
|
|
79
88
|
* @property {boolean} [php] Enable PHP FastCGI for the linked .sp site (default false for new)
|
|
80
|
-
* @property {boolean} [phpExamples]
|
|
89
|
+
* @property {boolean} [phpExamples] Deprecated. Accepted and ignored; actions ship in api/actions/.
|
|
81
90
|
* @property {boolean} git
|
|
82
91
|
* @property {string} [remote]
|
|
83
92
|
* @property {boolean} install
|
|
@@ -213,10 +222,9 @@ export async function planScaffoldFiles(options, deps = {}) {
|
|
|
213
222
|
? [
|
|
214
223
|
'## PHP',
|
|
215
224
|
'',
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
: 'Optional samples: re-run scaffolding with `--php --php-examples`, or copy from the Stagepass docs.',
|
|
225
|
+
`PHP is on. Caddy serves \`https://${options.name}.sp/api/action.php\` with \`php_fastcgi\`.`,
|
|
226
|
+
'Read `api/README.md`. Copy `api/.env.example` to `.env` in the project root (do not commit it) and set `ALLOWED_ORIGINS` to exact origins, including that `.sp` URL. From Webflow, call the PHP host with its absolute URL, not `window.location.origin`.',
|
|
227
|
+
'Apache uses the project `.htaccess`. nginx needs the snippet in `api/README.md` before the PHP location.',
|
|
220
228
|
'',
|
|
221
229
|
].join('\n')
|
|
222
230
|
: [
|
|
@@ -6,7 +6,7 @@ Local Stagepass project for Webflow.
|
|
|
6
6
|
|
|
7
7
|
```bash
|
|
8
8
|
npm install
|
|
9
|
-
|
|
9
|
+
stagepass build
|
|
10
10
|
stagepass link {{NAME}}
|
|
11
11
|
```
|
|
12
12
|
|
|
@@ -16,9 +16,11 @@ stagepass link {{NAME}}
|
|
|
16
16
|
|
|
17
17
|
| Script | Description |
|
|
18
18
|
| :--- | :--- |
|
|
19
|
-
| `
|
|
20
|
-
| `
|
|
21
|
-
| `
|
|
19
|
+
| `stagepass build` | Production bundle: content hashes, SRI, and `dist/manifest.json` |
|
|
20
|
+
| `stagepass watch` | Rebuild stable `dist/main.js` and `dist/styles.css` only |
|
|
21
|
+
| `stagepass dev` | Watch plus live reload |
|
|
22
|
+
|
|
23
|
+
Hashes and `manifest.json` come only from `stagepass build`. `stagepass watch` and `stagepass dev` write only the stable files and leave the manifest as the last production build. `stagepass upgrade` replaces `scripts/build.mjs` when it is still an unmodified scaffold from an older CLI. A script you have edited is left in place; the CLI prints what `stagepass build`, `stagepass watch` and `stagepass dev` do now.
|
|
22
24
|
|
|
23
25
|
## Webflow Custom Code
|
|
24
26
|
|
|
@@ -38,11 +40,11 @@ Optional exact pin (replace `@2` in the URL if you need a locked version): `@{{L
|
|
|
38
40
|
{{FOOTER_SNIPPET}}
|
|
39
41
|
```
|
|
40
42
|
|
|
41
|
-
After each production upload, set `data-src` / `data-href` to your hosted URLs. For content-hashed uploads, read `file` from `dist/manifest.json
|
|
43
|
+
After each production upload, set `data-src` / `data-href` to your hosted URLs. For content-hashed uploads, read `file` from `dist/manifest.json` (last `stagepass build`). Local swapping uses the stable `dist/main.js` and `dist/styles.css` copies.
|
|
42
44
|
|
|
43
45
|
### Subresource Integrity (SRI)
|
|
44
46
|
|
|
45
|
-
`
|
|
47
|
+
`stagepass build` writes a sha384 `integrity` per output in `dist/manifest.json`:
|
|
46
48
|
|
|
47
49
|
```json
|
|
48
50
|
{
|