@stagepass/cli 2.2.7 → 2.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. package/README.md +37 -18
  2. package/TESTING-macos.md +28 -10
  3. package/bin/index.js +4 -1
  4. package/package.json +9 -8
  5. package/src/cli.js +62 -7
  6. package/src/commands/certs.js +375 -19
  7. package/src/commands/daemon.js +75 -7
  8. package/src/commands/dev.js +419 -0
  9. package/src/commands/link.js +64 -23
  10. package/src/commands/new.js +397 -41
  11. package/src/commands/npm-run.js +8 -0
  12. package/src/commands/start.js +220 -127
  13. package/src/commands/stop.js +10 -19
  14. package/src/commands/unlink.js +6 -4
  15. package/src/commands/upgrade.js +51 -12
  16. package/src/lib/apply-site-block.js +48 -0
  17. package/src/lib/caddyfile.js +72 -13
  18. package/src/lib/cdn-snippets.js +145 -3
  19. package/src/lib/certs.js +334 -47
  20. package/src/lib/dev-port.js +45 -0
  21. package/src/lib/doctor-site.js +61 -5
  22. package/src/lib/doctor.js +48 -4
  23. package/src/lib/ensure-running.js +89 -0
  24. package/src/lib/exec.js +72 -24
  25. package/src/lib/live-server.js +484 -0
  26. package/src/lib/node-engine.js +55 -0
  27. package/src/lib/notify.js +62 -0
  28. package/src/lib/origins.js +27 -5
  29. package/src/lib/package-versions.js +9 -0
  30. package/src/lib/paths.js +18 -0
  31. package/src/lib/php-fpm.js +33 -0
  32. package/src/lib/project-sync.js +165 -20
  33. package/src/lib/projects.js +137 -7
  34. package/src/lib/prompts.js +47 -0
  35. package/src/lib/registry.js +27 -6
  36. package/src/lib/scaffold.js +288 -11
  37. package/src/lib/site-detect.js +78 -25
  38. package/src/lib/snippets.js +14 -11
  39. package/src/lib/versions.js +20 -8
  40. package/templates/project/AGENTS.md.tmpl +4 -3
  41. package/templates/project/README.md.tmpl +35 -6
  42. package/templates/project/api/README.md.tmpl +7 -0
  43. package/templates/project/examples/php/README.md.tmpl +18 -0
  44. package/templates/project/examples/php/form.php +105 -0
  45. package/templates/project/examples/php/proxy.php +171 -0
  46. package/templates/project/scripts/build.mjs +200 -37
@@ -2,6 +2,10 @@
2
2
  /**
3
3
  * Stagepass project build: esbuild (+ optional sass) → content-hashed dist files + manifest.json.
4
4
  * Also writes stable dist/main.js and dist/styles.css copies for local Stagepass paths.
5
+ * Manifest entries include Subresource Integrity (sha384) for production Webflow tags.
6
+ *
7
+ * Watch mode rebuilds only the affected part (JS entry/imports → JS, CSS entry → CSS)
8
+ * and writes a dist file only when its bytes changed.
5
9
  */
6
10
  import { createHash } from 'node:crypto';
7
11
  import { watch as fsWatch, promises as fs } from 'node:fs';
@@ -18,6 +22,12 @@ const ENTRY_JS = process.env.STAGEPASS_ENTRY_JS || '{{ENTRY_JS}}';
18
22
  const ENTRY_CSS = process.env.STAGEPASS_ENTRY_CSS || '{{ENTRY_CSS}}';
19
23
  const USE_SASS = (process.env.STAGEPASS_USE_SASS || '{{USE_SASS}}') === 'true';
20
24
 
25
+ /** @type {{ 'main.js': { file: string, integrity: string } | null, 'styles.css': { file: string, integrity: string } | null }} */
26
+ const manifestState = {
27
+ 'main.js': null,
28
+ 'styles.css': null,
29
+ };
30
+
21
31
  async function ensureDist() {
22
32
  await fs.mkdir(distDir, { recursive: true });
23
33
  }
@@ -26,6 +36,32 @@ function hashContent(buf) {
26
36
  return createHash('sha256').update(buf).digest('hex').slice(0, 8);
27
37
  }
28
38
 
39
+ function sriSha384(buf) {
40
+ return `sha384-${createHash('sha384').update(buf).digest('base64')}`;
41
+ }
42
+
43
+ /**
44
+ * Write only when contents differ from the existing file (preserves mtime when unchanged).
45
+ * @param {string} filePath
46
+ * @param {Buffer} buf
47
+ * @returns {Promise<boolean>} true when written
48
+ */
49
+ async function writeIfChanged(filePath, buf) {
50
+ try {
51
+ const existing = await fs.readFile(filePath);
52
+ if (existing.equals(buf)) return false;
53
+ } catch {
54
+ /* missing or unreadable → write */
55
+ }
56
+ await fs.writeFile(filePath, buf);
57
+ return true;
58
+ }
59
+
60
+ /**
61
+ * @param {string} stableName
62
+ * @param {Buffer|string} contents
63
+ * @param {BufferEncoding} [encoding]
64
+ */
29
65
  async function writeHashed(stableName, contents, encoding = undefined) {
30
66
  const buf = Buffer.isBuffer(contents)
31
67
  ? contents
@@ -34,15 +70,30 @@ async function writeHashed(stableName, contents, encoding = undefined) {
34
70
  const ext = path.extname(stableName);
35
71
  const base = path.basename(stableName, ext);
36
72
  const hashedName = `${base}.${hash}${ext}`;
37
- await fs.writeFile(path.join(distDir, hashedName), buf);
38
- await fs.writeFile(path.join(distDir, stableName), buf);
39
- return hashedName;
73
+ await writeIfChanged(path.join(distDir, hashedName), buf);
74
+ await writeIfChanged(path.join(distDir, stableName), buf);
75
+ return {
76
+ file: hashedName,
77
+ integrity: sriSha384(buf),
78
+ };
40
79
  }
41
80
 
42
- async function buildJs() {
43
- const entry = path.join(root, ENTRY_JS);
44
- const result = await esbuild.build({
45
- entryPoints: [entry],
81
+ async function writeManifest() {
82
+ if (!manifestState['main.js'] || !manifestState['styles.css']) return;
83
+ const text = `${JSON.stringify(
84
+ {
85
+ 'main.js': manifestState['main.js'],
86
+ 'styles.css': manifestState['styles.css'],
87
+ },
88
+ null,
89
+ 2
90
+ )}\n`;
91
+ await writeIfChanged(path.join(distDir, 'manifest.json'), Buffer.from(text, 'utf8'));
92
+ }
93
+
94
+ function jsBuildOptions() {
95
+ return {
96
+ entryPoints: [path.join(root, ENTRY_JS)],
46
97
  bundle: true,
47
98
  write: false,
48
99
  format: 'iife',
@@ -50,9 +101,22 @@ async function buildJs() {
50
101
  target: ['es2018'],
51
102
  sourcemap: false,
52
103
  logLevel: 'warning',
53
- });
104
+ };
105
+ }
106
+
107
+ /**
108
+ * @param {Buffer} contents
109
+ */
110
+ async function applyJsOutput(contents) {
111
+ manifestState['main.js'] = await writeHashed('main.js', contents);
112
+ await writeManifest();
113
+ return manifestState['main.js'];
114
+ }
115
+
116
+ async function buildJsOnce() {
117
+ const result = await esbuild.build(jsBuildOptions());
54
118
  const out = result.outputFiles[0];
55
- return writeHashed('main.js', out.contents);
119
+ return applyJsOutput(out.contents);
56
120
  }
57
121
 
58
122
  async function buildCss() {
@@ -65,46 +129,145 @@ async function buildCss() {
65
129
  } else {
66
130
  css = await fs.readFile(entry, 'utf8');
67
131
  }
68
- return writeHashed('styles.css', css, 'utf8');
132
+ manifestState['styles.css'] = await writeHashed('styles.css', css, 'utf8');
133
+ await writeManifest();
134
+ return manifestState['styles.css'];
69
135
  }
70
136
 
71
137
  async function buildOnce() {
72
138
  await ensureDist();
73
- const mainFile = await buildJs();
74
- const stylesFile = await buildCss();
75
- const manifest = {
76
- 'main.js': mainFile,
77
- 'styles.css': stylesFile,
139
+ await buildJsOnce();
140
+ await buildCss();
141
+ return {
142
+ 'main.js': manifestState['main.js'],
143
+ 'styles.css': manifestState['styles.css'],
78
144
  };
79
- await fs.writeFile(
80
- path.join(distDir, 'manifest.json'),
81
- `${JSON.stringify(manifest, null, 2)}\n`,
82
- 'utf8'
83
- );
84
- return manifest;
85
145
  }
86
146
 
87
- const manifest = await buildOnce();
88
- console.log('[stagepass] build ok', manifest);
147
+ /**
148
+ * Serialize async rebuild work so overlapping esbuild / CSS events stay ordered.
149
+ * @type {Promise<void>}
150
+ */
151
+ let chain = Promise.resolve();
152
+ function enqueue(label, fn) {
153
+ chain = chain
154
+ .then(async () => {
155
+ try {
156
+ const next = await fn();
157
+ console.log(`[stagepass] rebuild ${label} ok`, next);
158
+ } catch (err) {
159
+ console.error(`[stagepass] rebuild ${label} failed`, err);
160
+ }
161
+ })
162
+ .catch(() => {});
163
+ return chain;
164
+ }
165
+
166
+ if (!watchMode) {
167
+ const manifest = await buildOnce();
168
+ console.log('[stagepass] build ok', manifest);
169
+ } else {
170
+ await ensureDist();
171
+
172
+ /** Skip the first onEnd from context.watch (we seed via rebuild below). */
173
+ let jsWatchReady = false;
174
+ /** @type {import('esbuild').BuildContext|null} */
175
+ let ctx = null;
176
+ /** @type {import('node:fs').FSWatcher|null} */
177
+ let cssWatcher = null;
178
+ let shuttingDown = false;
89
179
 
90
- if (watchMode) {
91
- let pending = false;
92
- const rebuild = async () => {
93
- if (pending) return;
94
- pending = true;
180
+ async function shutdown(code = 0) {
181
+ if (shuttingDown) return;
182
+ shuttingDown = true;
95
183
  try {
96
- const next = await buildOnce();
97
- console.log('[stagepass] rebuild ok', next);
98
- } catch (err) {
99
- console.error('[stagepass] rebuild failed', err);
100
- } finally {
101
- pending = false;
184
+ cssWatcher?.close();
185
+ } catch {
186
+ /* ignore */
102
187
  }
188
+ cssWatcher = null;
189
+ try {
190
+ if (ctx) await ctx.dispose();
191
+ } catch {
192
+ /* ignore */
193
+ }
194
+ ctx = null;
195
+ // Allow pending enqueue work to settle briefly, then exit hard.
196
+ await Promise.race([chain, new Promise((r) => setTimeout(r, 500))]);
197
+ process.exit(code);
198
+ }
199
+
200
+ for (const signal of ['SIGTERM', 'SIGINT']) {
201
+ process.on(signal, () => {
202
+ void shutdown(0);
203
+ });
204
+ }
205
+
206
+ ctx = await esbuild.context({
207
+ ...jsBuildOptions(),
208
+ plugins: [
209
+ {
210
+ name: 'stagepass-js-watch',
211
+ setup(build) {
212
+ build.onEnd((result) => {
213
+ if (!jsWatchReady || shuttingDown) return;
214
+ if (result.errors.length > 0) {
215
+ console.error('[stagepass] rebuild js failed');
216
+ return;
217
+ }
218
+ const out = result.outputFiles && result.outputFiles[0];
219
+ if (!out) {
220
+ enqueue('full', buildOnce);
221
+ return;
222
+ }
223
+ const contents = Buffer.from(out.contents);
224
+ enqueue('js', () => applyJsOutput(contents));
225
+ });
226
+ },
227
+ },
228
+ ],
229
+ });
230
+
231
+ // Initial full build (JS via context.rebuild so the graph is warm for watch).
232
+ const initial = await ctx.rebuild();
233
+ if (initial.errors.length > 0 || !initial.outputFiles?.[0]) {
234
+ await shutdown(1);
235
+ throw new Error('[stagepass] initial js build failed');
236
+ }
237
+ await applyJsOutput(initial.outputFiles[0].contents);
238
+ await buildCss();
239
+ console.log('[stagepass] build ok', {
240
+ 'main.js': manifestState['main.js'],
241
+ 'styles.css': manifestState['styles.css'],
242
+ });
243
+
244
+ jsWatchReady = true;
245
+ await ctx.watch();
246
+
247
+ let cssPending = false;
248
+ const scheduleCss = () => {
249
+ if (cssPending || shuttingDown) return;
250
+ cssPending = true;
251
+ setTimeout(() => {
252
+ cssPending = false;
253
+ if (shuttingDown) return;
254
+ enqueue('css', async () => {
255
+ await buildCss();
256
+ return manifestState['styles.css'];
257
+ });
258
+ }, 40);
103
259
  };
104
- for (const rel of [ENTRY_JS, ENTRY_CSS]) {
105
- fsWatch(path.join(root, rel), () => {
106
- rebuild().catch(() => {});
260
+
261
+ try {
262
+ cssWatcher = fsWatch(path.join(root, ENTRY_CSS), () => {
263
+ scheduleCss();
264
+ });
265
+ } catch (err) {
266
+ console.error('[stagepass] css watch failed; falling back to full rebuilds', err);
267
+ cssWatcher = fsWatch(path.join(root, ENTRY_CSS), () => {
268
+ enqueue('full', buildOnce);
107
269
  });
108
270
  }
271
+
109
272
  console.log('[stagepass] watching…');
110
273
  }