@stagepass/cli 2.4.1 → 2.6.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.
@@ -5,11 +5,13 @@ import { defaultExec } from '../lib/exec.js';
5
5
  import {
6
6
  buildWebflowSnippets,
7
7
  printWebflowSnippets,
8
+ readSnippetGitRef,
9
+ sanitizeSnippetVersion,
8
10
  } from '../lib/snippets.js';
9
11
 
10
12
  import readline from 'node:readline/promises';
11
13
  import { stdin as input, stdout as output } from 'node:process';
12
- import { STAGEPASS_PACKAGES_VERSION } from '../lib/versions.js';
14
+ import { SNIPPET_LOADER_VERSION } from '../lib/cdn-base.js';
13
15
  import {
14
16
  PACKAGES_MAJOR_RANGE,
15
17
  fetchPublished,
@@ -22,6 +24,7 @@ import {
22
24
  import { detectSite, normalizeSiteUrl } from '../lib/site-detect.js';
23
25
  import {
24
26
  applyProjectSync,
27
+ cdnFromStagepassConfig,
25
28
  phpFromStagepassConfig,
26
29
  planProjectSync,
27
30
  readStagepassJson,
@@ -169,12 +172,58 @@ export function applyStagepassVersions(pkg, target) {
169
172
  return { next, changes };
170
173
  }
171
174
 
175
+ function packageHas(pkg, name) {
176
+ for (const section of ['dependencies', 'devDependencies']) {
177
+ const block = pkg?.[section];
178
+ if (block && typeof block === 'object' && !Array.isArray(block) && Object.prototype.hasOwnProperty.call(block, name)) {
179
+ return true;
180
+ }
181
+ }
182
+ return false;
183
+ }
184
+
185
+ /**
186
+ * Add type packages the project does not already depend on.
187
+ * Loader is always required. `@stagepass/modules` only when the declared list is non-empty.
188
+ * Does not run npm install.
189
+ * @param {object} pkg
190
+ * @param {string[]} modules normalized declared list
191
+ * @param {Record<string, string>} specs name to version spec
192
+ * @returns {{ next: object, added: { name: string, to: string, section: 'devDependencies' }[] }}
193
+ */
194
+ export function ensureTypeDevDependencies(pkg, modules, specs) {
195
+ const next = structuredClone(pkg);
196
+ /** @type {{ name: string, to: string, section: 'devDependencies' }[]} */
197
+ const added = [];
198
+ const names = ['@stagepass/loader'];
199
+ if (Array.isArray(modules) && modules.length > 0) names.push('@stagepass/modules');
200
+ for (const name of names) {
201
+ if (packageHas(next, name)) continue;
202
+ const to = specs?.[name];
203
+ if (!to) continue;
204
+ if (next.devDependencies == null) next.devDependencies = {};
205
+ if (typeof next.devDependencies !== 'object' || Array.isArray(next.devDependencies)) continue;
206
+ next.devDependencies[name] = to;
207
+ added.push({ name, to, section: 'devDependencies' });
208
+ }
209
+ return { next, added };
210
+ }
211
+
172
212
  /**
173
213
  * Upgrade @stagepass/* deps in the current project's package.json and sync
174
- * the declared modules into stagepass.json, AGENTS.md, the Cursor rule and
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`
214
+ * the declared modules into stagepass.json, AGENTS.stagepass.md, the short
215
+ * pointer in AGENTS.md, the Cursor rule and editor types (a plain `types`
216
+ * array, or stagepass.d.ts). Adds missing `@stagepass/loader` and, when
217
+ * modules are declared, `@stagepass/modules` devDependencies. Replaces
218
+ * scripts/build.mjs when it is still an unmodified scaffold from an earlier
219
+ * CLI, and replaces a hand-edited AGENTS.stagepass.md, when the user passed
220
+ * `--yes` or confirmed on a TTY. Without a TTY and without `--yes`, prints
221
+ * a hint and leaves that file. A managed AGENTS.stagepass.md is refreshed
222
+ * without asking. When stagepass.json has `"php": true`
177
223
  * and the project root has no `.htaccess`, writes the shipped template.
224
+ * It does not copy `api/` when that boilerplate is missing.
225
+ * `AGENTS.stagepass.md` and the printed Webflow snippets share one sanitized
226
+ * loader pin and the CDN recorded in `stagepass.json` (`stagepass` when missing).
178
227
  *
179
228
  * @param {object} [options]
180
229
  * @param {boolean} [options.dryRun]
@@ -238,40 +287,23 @@ export async function upgrade(options = {}, deps = {}) {
238
287
  log(chalk.yellow(`npm registry not reachable: using ${PACKAGES_MAJOR_RANGE} (resolves to the newest published 2.x).`));
239
288
  }
240
289
 
241
- const { next, changes } = applyStagepassVersions(pkg, specs);
290
+ let { next, changes } = applyStagepassVersions(pkg, specs);
242
291
 
243
- if (changes.length === 0) {
244
- if (names.length === 0) {
245
- log(chalk.yellow('No @stagepass/* packages found in package.json.'));
246
- } else {
247
- log(chalk.green('@stagepass/* already at the newest published ranges.'));
248
- }
249
- } else {
292
+ if (changes.length > 0) {
250
293
  log(chalk.bold('Upgrade @stagepass/* (published versions only)'));
251
294
  for (const change of changes) {
252
295
  log(` ${change.name}: ${change.from} → ${change.to} (${change.section})`);
253
296
  }
254
- }
255
-
256
- // Per-module types need @stagepass/modules >= 2.2.0 resolvable from the range.
257
- /** @type {boolean | undefined} */
258
- let perModuleTypes;
259
- const modulesSpec =
260
- next?.dependencies?.['@stagepass/modules'] || next?.devDependencies?.['@stagepass/modules'];
261
- if (modulesSpec) {
262
- const fromRegistry = rangeHasPerModuleTypes(
263
- published['@stagepass/modules'] ? published['@stagepass/modules'].versions : null,
264
- String(modulesSpec)
265
- );
266
- perModuleTypes =
267
- fromRegistry != null ? fromRegistry : (await installedHasPerModuleTypes(cwd, fse)) === true;
297
+ } else if (names.length > 0) {
298
+ log(chalk.green('@stagepass/* already at the newest published ranges.'));
268
299
  }
269
300
 
270
301
  // Declared modules: live site first, then stagepass.json, then ask.
271
302
  const sp = await readStagepassJson(cwd, fse);
272
303
  if (sp.error) log(chalk.yellow(sp.error));
273
304
  const isTty = deps.isTTY != null ? Boolean(deps.isTTY) : Boolean(input.isTTY && output.isTTY);
274
- const interactive = isTty && options.yes !== true;
305
+ const assumeYes = options.yes === true;
306
+ const interactive = isTty && !assumeYes;
275
307
  /** @type {import('node:readline/promises').Interface | null} */
276
308
  let rl = null;
277
309
  const getRl = () => {
@@ -315,18 +347,55 @@ export async function upgrade(options = {}, deps = {}) {
315
347
  return 1;
316
348
  }
317
349
 
318
- const plan = await planProjectSync(
319
- {
320
- cwd,
321
- modules: resolved.modules,
322
- origin: options.site ? resolved.origin : null,
323
- pkg: next,
324
- perModuleTypes,
325
- },
326
- fse
327
- );
350
+ const typeNames = ['@stagepass/loader'];
351
+ if (resolved.modules.length > 0) typeNames.push('@stagepass/modules');
352
+ const missingTypes = typeNames.filter((name) => !packageHas(next, name));
353
+ /** @type {{ name: string, to: string, section: 'devDependencies' }[]} */
354
+ let added = [];
355
+ if (missingTypes.length > 0) {
356
+ let extra;
357
+ try {
358
+ extra = await resolveUpgradeTargets(options.version, missingTypes, {
359
+ exec: deps.exec || defaultExec,
360
+ fetchPublished: deps.fetchPublished,
361
+ });
362
+ } catch (error) {
363
+ log(chalk.red(error instanceof Error ? error.message : String(error)));
364
+ return 1;
365
+ }
366
+ Object.assign(published, extra.published);
367
+ const typeSpecs = Object.fromEntries(Object.entries(extra.targets).map(([name, target]) => [name, target.spec]));
368
+ const ensured = ensureTypeDevDependencies(next, resolved.modules, typeSpecs);
369
+ next = ensured.next;
370
+ added = ensured.added;
371
+ const alreadyOffline = Object.values(targets).some((target) => target.offline);
372
+ if (!alreadyOffline && Object.values(extra.targets).some((target) => target.offline)) {
373
+ log(chalk.yellow(`npm registry not reachable: using ${PACKAGES_MAJOR_RANGE} (resolves to the newest published 2.x).`));
374
+ }
375
+ }
376
+ if (added.length > 0) {
377
+ log(chalk.bold('Type packages (devDependencies)'));
378
+ for (const item of added) log(` ${item.name}: add ${item.to} (${item.section})`);
379
+ } else if (names.length === 0 && changes.length === 0) {
380
+ log(chalk.yellow('No @stagepass/* packages found in package.json.'));
381
+ }
328
382
 
329
- // Refuse an unpublished --version before asking about scripts/build.mjs or writing.
383
+ // Per-module types need @stagepass/modules >= 2.2.0 resolvable from the range.
384
+ /** @type {boolean | undefined} */
385
+ let perModuleTypes;
386
+ const modulesSpec =
387
+ next?.dependencies?.['@stagepass/modules'] || next?.devDependencies?.['@stagepass/modules'];
388
+ if (modulesSpec) {
389
+ const fromRegistry = rangeHasPerModuleTypes(
390
+ published['@stagepass/modules'] ? published['@stagepass/modules'].versions : null,
391
+ String(modulesSpec)
392
+ );
393
+ perModuleTypes =
394
+ fromRegistry != null ? fromRegistry : (await installedHasPerModuleTypes(cwd, fse)) === true;
395
+ }
396
+
397
+ // One sanitized pin and one CDN for the guide and the printed snippets.
398
+ // Refuse an unpublished --version before asking about project files or writing.
330
399
  let loaderPub = '@stagepass/loader' in published ? published['@stagepass/loader'] : undefined;
331
400
  const getLoaderPub = async () => {
332
401
  if (loaderPub !== undefined) return loaderPub;
@@ -347,6 +416,38 @@ export async function upgrade(options = {}, deps = {}) {
347
416
  }
348
417
  if (t.resolved && satisfiesSimple(t.resolved, PACKAGES_MAJOR_RANGE)) snippetVersion = t.resolved;
349
418
  }
419
+ if (!snippetVersion) {
420
+ snippetVersion = await resolveSnippetLoaderVersion({
421
+ published: await getLoaderPub(),
422
+ cwd,
423
+ bundled: SNIPPET_LOADER_VERSION,
424
+ fse,
425
+ });
426
+ }
427
+ try {
428
+ snippetVersion = sanitizeSnippetVersion(snippetVersion);
429
+ } catch (err) {
430
+ log(chalk.red(err instanceof Error ? err.message : String(err)));
431
+ return 1;
432
+ }
433
+ const cdn = cdnFromStagepassConfig(sp.data);
434
+
435
+ const plan = await planProjectSync(
436
+ {
437
+ cwd,
438
+ modules: resolved.modules,
439
+ origin: options.site ? resolved.origin : null,
440
+ pkg: next,
441
+ perModuleTypes,
442
+ interactive,
443
+ assumeYes,
444
+ confirm: confirmFn,
445
+ cursorRule: options.cursorRule === true,
446
+ cdn,
447
+ loaderVersion: snippetVersion,
448
+ },
449
+ fse
450
+ );
350
451
 
351
452
  /** @type {{ action: string, from?: string, content?: string, note?: string }} */
352
453
  let buildUpgrade = { action: 'skipped' };
@@ -366,6 +467,7 @@ export async function upgrade(options = {}, deps = {}) {
366
467
  cwd,
367
468
  currentSource: currentBuild,
368
469
  interactive,
470
+ assumeYes,
369
471
  confirm: confirmFn,
370
472
  fse,
371
473
  });
@@ -398,7 +500,7 @@ export async function upgrade(options = {}, deps = {}) {
398
500
  }
399
501
 
400
502
  log(chalk.bold(`\nProject files for modules: ${resolved.modules.length ? resolved.modules.join(', ') : 'none'}`));
401
- if (plan.writes.length === 0) {
503
+ if (plan.writes.length === 0 && buildUpgrade.action !== 'hint') {
402
504
  log(' Everything is up to date.');
403
505
  }
404
506
  for (const w of plan.writes) {
@@ -410,7 +512,11 @@ export async function upgrade(options = {}, deps = {}) {
410
512
  if (buildUpgrade.action === 'custom') {
411
513
  log(chalk.yellow(buildUpgrade.note));
412
514
  } else if (buildUpgrade.action !== 'update' && buildUpgrade.note) {
413
- const warn = buildUpgrade.action === 'declined' || buildUpgrade.action === 'symlink' || buildUpgrade.action === 'unreadable';
515
+ const warn =
516
+ buildUpgrade.action === 'declined' ||
517
+ buildUpgrade.action === 'symlink' ||
518
+ buildUpgrade.action === 'unreadable' ||
519
+ buildUpgrade.action === 'hint';
414
520
  const line = ` ${buildUpgrade.note}`;
415
521
  log(warn ? chalk.yellow(line) : line);
416
522
  }
@@ -422,11 +528,29 @@ export async function upgrade(options = {}, deps = {}) {
422
528
  );
423
529
  }
424
530
 
531
+ let snippets;
532
+ try {
533
+ const readGitRef = deps.readGitRef || readSnippetGitRef;
534
+ const gitRef = await readGitRef(cwd);
535
+ snippets = buildWebflowSnippets({
536
+ version: snippetVersion,
537
+ moduleList: resolved.modules,
538
+ gitRemote: gitRef && gitRef.gitRemote,
539
+ gitBranch: gitRef && gitRef.gitBranch,
540
+ projectName: pkg.name,
541
+ cdn,
542
+ });
543
+ } catch (err) {
544
+ log(chalk.red(err instanceof Error ? err.message : String(err)));
545
+ return 1;
546
+ }
547
+
425
548
  if (options.dryRun) {
426
549
  log(chalk.dim('\nDry run: no files were modified.'));
427
550
  } else {
428
551
  try {
429
- if (changes.length > 0) {
552
+ const pkgChanged = changes.length > 0 || added.length > 0;
553
+ if (pkgChanged) {
430
554
  try {
431
555
  if ((await fse.lstat(pkgPath)).isSymbolicLink()) {
432
556
  log(chalk.red('package.json is a symlink; refusing to write.'));
@@ -445,7 +569,7 @@ export async function upgrade(options = {}, deps = {}) {
445
569
  } catch (err) {
446
570
  // Restore package.json if project-sync failed after the pkg write.
447
571
  try {
448
- if (changes.length > 0) await fse.writeFile(pkgPath, raw, 'utf8');
572
+ if (changes.length > 0 || added.length > 0) await fse.writeFile(pkgPath, raw, 'utf8');
449
573
  } catch {
450
574
  /* best effort */
451
575
  }
@@ -455,22 +579,16 @@ export async function upgrade(options = {}, deps = {}) {
455
579
  }
456
580
  }
457
581
 
458
- if (!snippetVersion) {
459
- snippetVersion = await resolveSnippetLoaderVersion({
460
- published: await getLoaderPub(),
461
- cwd,
462
- bundled: STAGEPASS_PACKAGES_VERSION,
463
- fse,
464
- });
465
- }
466
- const snippets = buildWebflowSnippets({
467
- version: snippetVersion,
468
- moduleList: resolved.modules,
469
- });
470
582
  printWebflowSnippets(snippets, log);
471
583
 
472
584
  log(chalk.bold('\nNext step (not run automatically):'));
473
585
  log(' npm install');
586
+ const typesTouched =
587
+ added.length > 0 ||
588
+ plan.writes.some(
589
+ (item) => item.file === 'tsconfig.json' || item.file === 'jsconfig.json' || item.file.endsWith('stagepass.d.ts')
590
+ );
591
+ if (typesTouched) log(' TypeScript: Restart TS Server');
474
592
 
475
593
  return 0;
476
594
  }