@stagepass/cli 2.4.0 → 2.5.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,12 @@ import { defaultExec } from '../lib/exec.js';
5
5
  import {
6
6
  buildWebflowSnippets,
7
7
  printWebflowSnippets,
8
+ readSnippetGitRef,
8
9
  } from '../lib/snippets.js';
9
10
 
10
11
  import readline from 'node:readline/promises';
11
12
  import { stdin as input, stdout as output } from 'node:process';
12
- import { STAGEPASS_PACKAGES_VERSION } from '../lib/versions.js';
13
+ import { SNIPPET_LOADER_VERSION } from '../lib/cdn-base.js';
13
14
  import {
14
15
  PACKAGES_MAJOR_RANGE,
15
16
  fetchPublished,
@@ -22,6 +23,7 @@ import {
22
23
  import { detectSite, normalizeSiteUrl } from '../lib/site-detect.js';
23
24
  import {
24
25
  applyProjectSync,
26
+ phpFromStagepassConfig,
25
27
  planProjectSync,
26
28
  readStagepassJson,
27
29
  resolveUpgradeModules,
@@ -30,6 +32,7 @@ import {
30
32
  defaultBuildScriptTemplatePath,
31
33
  planBuildScriptUpgrade,
32
34
  } from '../lib/build-script-upgrade.js';
35
+ import { planHtaccessUpgrade } from '../lib/htaccess-upgrade.js';
33
36
 
34
37
  const STAGEPASS_SCOPE = '@stagepass/';
35
38
 
@@ -167,11 +170,56 @@ export function applyStagepassVersions(pkg, target) {
167
170
  return { next, changes };
168
171
  }
169
172
 
173
+ function packageHas(pkg, name) {
174
+ for (const section of ['dependencies', 'devDependencies']) {
175
+ const block = pkg?.[section];
176
+ if (block && typeof block === 'object' && !Array.isArray(block) && Object.prototype.hasOwnProperty.call(block, name)) {
177
+ return true;
178
+ }
179
+ }
180
+ return false;
181
+ }
182
+
183
+ /**
184
+ * Add type packages the project does not already depend on.
185
+ * Loader is always required. `@stagepass/modules` only when the declared list is non-empty.
186
+ * Does not run npm install.
187
+ * @param {object} pkg
188
+ * @param {string[]} modules normalized declared list
189
+ * @param {Record<string, string>} specs name to version spec
190
+ * @returns {{ next: object, added: { name: string, to: string, section: 'devDependencies' }[] }}
191
+ */
192
+ export function ensureTypeDevDependencies(pkg, modules, specs) {
193
+ const next = structuredClone(pkg);
194
+ /** @type {{ name: string, to: string, section: 'devDependencies' }[]} */
195
+ const added = [];
196
+ const names = ['@stagepass/loader'];
197
+ if (Array.isArray(modules) && modules.length > 0) names.push('@stagepass/modules');
198
+ for (const name of names) {
199
+ if (packageHas(next, name)) continue;
200
+ const to = specs?.[name];
201
+ if (!to) continue;
202
+ if (next.devDependencies == null) next.devDependencies = {};
203
+ if (typeof next.devDependencies !== 'object' || Array.isArray(next.devDependencies)) continue;
204
+ next.devDependencies[name] = to;
205
+ added.push({ name, to, section: 'devDependencies' });
206
+ }
207
+ return { next, added };
208
+ }
209
+
170
210
  /**
171
211
  * Upgrade @stagepass/* deps in the current project's package.json and sync
172
- * the declared modules into stagepass.json, AGENTS.md, the Cursor rule and
173
- * tsconfig/jsconfig types. Replaces scripts/build.mjs when it is still an
174
- * unmodified scaffold from an earlier CLI.
212
+ * the declared modules into stagepass.json, AGENTS.stagepass.md, the short
213
+ * pointer in AGENTS.md, the Cursor rule and editor types (a plain `types`
214
+ * array, or stagepass.d.ts). Adds missing `@stagepass/loader` and, when
215
+ * modules are declared, `@stagepass/modules` devDependencies. Replaces
216
+ * scripts/build.mjs when it is still an unmodified scaffold from an earlier
217
+ * CLI, and replaces a hand-edited AGENTS.stagepass.md, when the user passed
218
+ * `--yes` or confirmed on a TTY. Without a TTY and without `--yes`, prints
219
+ * a hint and leaves that file. A managed AGENTS.stagepass.md is refreshed
220
+ * without asking. When stagepass.json has `"php": true`
221
+ * and the project root has no `.htaccess`, writes the shipped template.
222
+ * It does not copy `api/` when that boilerplate is missing.
175
223
  *
176
224
  * @param {object} [options]
177
225
  * @param {boolean} [options.dryRun]
@@ -235,40 +283,23 @@ export async function upgrade(options = {}, deps = {}) {
235
283
  log(chalk.yellow(`npm registry not reachable: using ${PACKAGES_MAJOR_RANGE} (resolves to the newest published 2.x).`));
236
284
  }
237
285
 
238
- const { next, changes } = applyStagepassVersions(pkg, specs);
286
+ let { next, changes } = applyStagepassVersions(pkg, specs);
239
287
 
240
- if (changes.length === 0) {
241
- if (names.length === 0) {
242
- log(chalk.yellow('No @stagepass/* packages found in package.json.'));
243
- } else {
244
- log(chalk.green('@stagepass/* already at the newest published ranges.'));
245
- }
246
- } else {
288
+ if (changes.length > 0) {
247
289
  log(chalk.bold('Upgrade @stagepass/* (published versions only)'));
248
290
  for (const change of changes) {
249
291
  log(` ${change.name}: ${change.from} → ${change.to} (${change.section})`);
250
292
  }
251
- }
252
-
253
- // Per-module types need @stagepass/modules >= 2.2.0 resolvable from the range.
254
- /** @type {boolean | undefined} */
255
- let perModuleTypes;
256
- const modulesSpec =
257
- next?.dependencies?.['@stagepass/modules'] || next?.devDependencies?.['@stagepass/modules'];
258
- if (modulesSpec) {
259
- const fromRegistry = rangeHasPerModuleTypes(
260
- published['@stagepass/modules'] ? published['@stagepass/modules'].versions : null,
261
- String(modulesSpec)
262
- );
263
- perModuleTypes =
264
- fromRegistry != null ? fromRegistry : (await installedHasPerModuleTypes(cwd, fse)) === true;
293
+ } else if (names.length > 0) {
294
+ log(chalk.green('@stagepass/* already at the newest published ranges.'));
265
295
  }
266
296
 
267
297
  // Declared modules: live site first, then stagepass.json, then ask.
268
298
  const sp = await readStagepassJson(cwd, fse);
269
299
  if (sp.error) log(chalk.yellow(sp.error));
270
300
  const isTty = deps.isTTY != null ? Boolean(deps.isTTY) : Boolean(input.isTTY && output.isTTY);
271
- const interactive = isTty && options.yes !== true;
301
+ const assumeYes = options.yes === true;
302
+ const interactive = isTty && !assumeYes;
272
303
  /** @type {import('node:readline/promises').Interface | null} */
273
304
  let rl = null;
274
305
  const getRl = () => {
@@ -312,6 +343,53 @@ export async function upgrade(options = {}, deps = {}) {
312
343
  return 1;
313
344
  }
314
345
 
346
+ const typeNames = ['@stagepass/loader'];
347
+ if (resolved.modules.length > 0) typeNames.push('@stagepass/modules');
348
+ const missingTypes = typeNames.filter((name) => !packageHas(next, name));
349
+ /** @type {{ name: string, to: string, section: 'devDependencies' }[]} */
350
+ let added = [];
351
+ if (missingTypes.length > 0) {
352
+ let extra;
353
+ try {
354
+ extra = await resolveUpgradeTargets(options.version, missingTypes, {
355
+ exec: deps.exec || defaultExec,
356
+ fetchPublished: deps.fetchPublished,
357
+ });
358
+ } catch (error) {
359
+ log(chalk.red(error instanceof Error ? error.message : String(error)));
360
+ return 1;
361
+ }
362
+ Object.assign(published, extra.published);
363
+ const typeSpecs = Object.fromEntries(Object.entries(extra.targets).map(([name, target]) => [name, target.spec]));
364
+ const ensured = ensureTypeDevDependencies(next, resolved.modules, typeSpecs);
365
+ next = ensured.next;
366
+ added = ensured.added;
367
+ const alreadyOffline = Object.values(targets).some((target) => target.offline);
368
+ if (!alreadyOffline && Object.values(extra.targets).some((target) => target.offline)) {
369
+ log(chalk.yellow(`npm registry not reachable: using ${PACKAGES_MAJOR_RANGE} (resolves to the newest published 2.x).`));
370
+ }
371
+ }
372
+ if (added.length > 0) {
373
+ log(chalk.bold('Type packages (devDependencies)'));
374
+ for (const item of added) log(` ${item.name}: add ${item.to} (${item.section})`);
375
+ } else if (names.length === 0 && changes.length === 0) {
376
+ log(chalk.yellow('No @stagepass/* packages found in package.json.'));
377
+ }
378
+
379
+ // Per-module types need @stagepass/modules >= 2.2.0 resolvable from the range.
380
+ /** @type {boolean | undefined} */
381
+ let perModuleTypes;
382
+ const modulesSpec =
383
+ next?.dependencies?.['@stagepass/modules'] || next?.devDependencies?.['@stagepass/modules'];
384
+ if (modulesSpec) {
385
+ const fromRegistry = rangeHasPerModuleTypes(
386
+ published['@stagepass/modules'] ? published['@stagepass/modules'].versions : null,
387
+ String(modulesSpec)
388
+ );
389
+ perModuleTypes =
390
+ fromRegistry != null ? fromRegistry : (await installedHasPerModuleTypes(cwd, fse)) === true;
391
+ }
392
+
315
393
  const plan = await planProjectSync(
316
394
  {
317
395
  cwd,
@@ -319,6 +397,10 @@ export async function upgrade(options = {}, deps = {}) {
319
397
  origin: options.site ? resolved.origin : null,
320
398
  pkg: next,
321
399
  perModuleTypes,
400
+ interactive,
401
+ assumeYes,
402
+ confirm: confirmFn,
403
+ cursorRule: options.cursorRule === true,
322
404
  },
323
405
  fse
324
406
  );
@@ -363,6 +445,7 @@ export async function upgrade(options = {}, deps = {}) {
363
445
  cwd,
364
446
  currentSource: currentBuild,
365
447
  interactive,
448
+ assumeYes,
366
449
  confirm: confirmFn,
367
450
  fse,
368
451
  });
@@ -378,8 +461,24 @@ export async function upgrade(options = {}, deps = {}) {
378
461
  closeRl();
379
462
  }
380
463
 
464
+ // Missing PHP .htaccess only. An existing path is never replaced.
465
+ const htaccessPlan = await planHtaccessUpgrade({
466
+ cwd,
467
+ php: phpFromStagepassConfig(sp.data),
468
+ fse,
469
+ });
470
+ if (htaccessPlan.action === 'create') {
471
+ plan.writes.push({
472
+ file: '.htaccess',
473
+ content: htaccessPlan.content,
474
+ action: 'created',
475
+ });
476
+ } else if (htaccessPlan.note) {
477
+ plan.notes.push(htaccessPlan.note);
478
+ }
479
+
381
480
  log(chalk.bold(`\nProject files for modules: ${resolved.modules.length ? resolved.modules.join(', ') : 'none'}`));
382
- if (plan.writes.length === 0) {
481
+ if (plan.writes.length === 0 && buildUpgrade.action !== 'hint') {
383
482
  log(' Everything is up to date.');
384
483
  }
385
484
  for (const w of plan.writes) {
@@ -391,7 +490,11 @@ export async function upgrade(options = {}, deps = {}) {
391
490
  if (buildUpgrade.action === 'custom') {
392
491
  log(chalk.yellow(buildUpgrade.note));
393
492
  } else if (buildUpgrade.action !== 'update' && buildUpgrade.note) {
394
- const warn = buildUpgrade.action === 'declined' || buildUpgrade.action === 'symlink' || buildUpgrade.action === 'unreadable';
493
+ const warn =
494
+ buildUpgrade.action === 'declined' ||
495
+ buildUpgrade.action === 'symlink' ||
496
+ buildUpgrade.action === 'unreadable' ||
497
+ buildUpgrade.action === 'hint';
395
498
  const line = ` ${buildUpgrade.note}`;
396
499
  log(warn ? chalk.yellow(line) : line);
397
500
  }
@@ -403,11 +506,36 @@ export async function upgrade(options = {}, deps = {}) {
403
506
  );
404
507
  }
405
508
 
509
+ if (!snippetVersion) {
510
+ snippetVersion = await resolveSnippetLoaderVersion({
511
+ published: await getLoaderPub(),
512
+ cwd,
513
+ bundled: SNIPPET_LOADER_VERSION,
514
+ fse,
515
+ });
516
+ }
517
+ let snippets;
518
+ try {
519
+ const readGitRef = deps.readGitRef || readSnippetGitRef;
520
+ const gitRef = await readGitRef(cwd);
521
+ snippets = buildWebflowSnippets({
522
+ version: snippetVersion,
523
+ moduleList: resolved.modules,
524
+ gitRemote: gitRef && gitRef.gitRemote,
525
+ gitBranch: gitRef && gitRef.gitBranch,
526
+ projectName: pkg.name,
527
+ });
528
+ } catch (err) {
529
+ log(chalk.red(err instanceof Error ? err.message : String(err)));
530
+ return 1;
531
+ }
532
+
406
533
  if (options.dryRun) {
407
534
  log(chalk.dim('\nDry run: no files were modified.'));
408
535
  } else {
409
536
  try {
410
- if (changes.length > 0) {
537
+ const pkgChanged = changes.length > 0 || added.length > 0;
538
+ if (pkgChanged) {
411
539
  try {
412
540
  if ((await fse.lstat(pkgPath)).isSymbolicLink()) {
413
541
  log(chalk.red('package.json is a symlink; refusing to write.'));
@@ -426,7 +554,7 @@ export async function upgrade(options = {}, deps = {}) {
426
554
  } catch (err) {
427
555
  // Restore package.json if project-sync failed after the pkg write.
428
556
  try {
429
- if (changes.length > 0) await fse.writeFile(pkgPath, raw, 'utf8');
557
+ if (changes.length > 0 || added.length > 0) await fse.writeFile(pkgPath, raw, 'utf8');
430
558
  } catch {
431
559
  /* best effort */
432
560
  }
@@ -436,22 +564,16 @@ export async function upgrade(options = {}, deps = {}) {
436
564
  }
437
565
  }
438
566
 
439
- if (!snippetVersion) {
440
- snippetVersion = await resolveSnippetLoaderVersion({
441
- published: await getLoaderPub(),
442
- cwd,
443
- bundled: STAGEPASS_PACKAGES_VERSION,
444
- fse,
445
- });
446
- }
447
- const snippets = buildWebflowSnippets({
448
- version: snippetVersion,
449
- moduleList: resolved.modules,
450
- });
451
567
  printWebflowSnippets(snippets, log);
452
568
 
453
569
  log(chalk.bold('\nNext step (not run automatically):'));
454
570
  log(' npm install');
571
+ const typesTouched =
572
+ added.length > 0 ||
573
+ plan.writes.some(
574
+ (item) => item.file === 'tsconfig.json' || item.file === 'jsconfig.json' || item.file.endsWith('stagepass.d.ts')
575
+ );
576
+ if (typesTouched) log(' TypeScript: Restart TS Server');
455
577
 
456
578
  return 0;
457
579
  }