@stagepass/cli 2.2.0 → 2.2.6
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 +3 -3
- package/package.json +1 -1
- package/src/cli.js +1 -1
- package/src/commands/new.js +73 -17
- package/src/commands/upgrade.js +155 -65
- package/src/lib/doctor-site.js +3 -4
- package/src/lib/project-modules.js +11 -1
- package/src/lib/project-sync.js +29 -33
- package/src/lib/registry.js +184 -0
- package/src/lib/scaffold.js +9 -5
- package/src/lib/site-detect.js +43 -23
- package/src/lib/snippets.js +9 -12
- package/src/lib/versions.js +9 -11
- package/templates/project/src/main.js.tmpl +1 -1
package/README.md
CHANGED
|
@@ -93,7 +93,7 @@ Scaffold a project with flags for every choice (interactive prompts only on a TT
|
|
|
93
93
|
| `-y, --yes` | Non-interactive; use defaults for omitted flags. |
|
|
94
94
|
| `--dry-run` | Print the file plan without writing. |
|
|
95
95
|
|
|
96
|
-
The name must be DNS-safe (lowercase `a-z`, `0-9`, hyphens; 1 to 63 chars; no leading/trailing hyphen). Existing files are never overwritten. Generated projects include `scripts/build.mjs` (content-hashed `dist/` + `manifest.json`), Webflow head/footer snippets (jsDelivr `@2`, loader plus the declared modules), `stagepass.json`, an `AGENTS.md` with the full `sp` API reference, `.cursor/rules/stagepass.mdc`, and a `.gitignore`. `@stagepass/loader` / `@stagepass/modules` are added as `^2
|
|
96
|
+
The name must be DNS-safe (lowercase `a-z`, `0-9`, hyphens; 1 to 63 chars; no leading/trailing hyphen). Existing files are never overwritten. Generated projects include `scripts/build.mjs` (content-hashed `dist/` + `manifest.json`), Webflow head/footer snippets (jsDelivr `@2`, loader plus the declared modules), `stagepass.json`, an `AGENTS.md` with the full `sp` API reference, `.cursor/rules/stagepass.mdc`, and a `.gitignore`. `@stagepass/loader` / `@stagepass/modules` are added as `^2` (same major as the CDN pin `@2`; npm installs the newest published 2.x). Per-module types are written only when the published or installed `@stagepass/modules` ships them (2.2.0 and later); otherwise `types` lists the umbrella `@stagepass/modules`.
|
|
97
97
|
|
|
98
98
|
Interactively, `new` asks for the live site after the styles question, shows the detected modules and asks for confirmation; otherwise it asks for the list directly. Without a TTY (or with `--yes`) it uses `--site-modules`, then `--site` detection, else `[]` with a hint.
|
|
99
99
|
|
|
@@ -109,7 +109,7 @@ Interactively, `new` asks for the live site after the styles question, shows the
|
|
|
109
109
|
- `.cursor/rules/stagepass.mdc` (globs `src/**`) with the same content;
|
|
110
110
|
- `compilerOptions.types` in `tsconfig.json` / `jsconfig.json`: `@stagepass/loader` plus `@stagepass/modules/<module>` for each declared module (`@stagepass/modules` for `all`), so undeclared modules are type errors.
|
|
111
111
|
|
|
112
|
-
The legacy boolean `"modules": true|false` is still read; `upgrade` replaces it with a list.
|
|
112
|
+
The legacy boolean `"modules": true|false` is still read; `upgrade` replaces it with a list (`true` becomes `["all"]` without a TTY).
|
|
113
113
|
|
|
114
114
|
### Live site detection
|
|
115
115
|
|
|
@@ -119,7 +119,7 @@ The legacy boolean `"modules": true|false` is still read; `upgrade` replaces it
|
|
|
119
119
|
|
|
120
120
|
| Option | Behaviour |
|
|
121
121
|
| :--- | :--- |
|
|
122
|
-
| `--version <x>` | Target loader/modules version
|
|
122
|
+
| `--version <x>` | Target loader/modules version, range or dist-tag. It must be published on npm (staged releases are refused until approved). Default: `^<latest published 2.x>` from `npm view`, or `^2` when the registry is not reachable. (Implemented as `--target` internally so it does not collide with `stagepass --version`.) |
|
|
123
123
|
| `--site <host>` | Live site to detect modules from (default: the origin linked in `~/.stagepass/projects.json` for this folder). Stored in `stagepass.json` when given. |
|
|
124
124
|
| `--site-modules <list>` | Set the modules list directly (`none`, `all`, or a comma list). |
|
|
125
125
|
| `--skip-site` | Do not fetch the live site. |
|
package/package.json
CHANGED
package/src/cli.js
CHANGED
|
@@ -267,7 +267,7 @@ export function createProgram(deps = {}) {
|
|
|
267
267
|
.option('-y, --yes', 'Non-interactive; accept detected or existing modules')
|
|
268
268
|
.option(
|
|
269
269
|
'--target <version>',
|
|
270
|
-
'Target
|
|
270
|
+
'Target version, range or dist-tag; must be published on npm (default: ^<latest published 2.x>, ^2 offline)'
|
|
271
271
|
)
|
|
272
272
|
// Public docs use `--version`; rewriteArgvForUpgrade maps it to `--target`
|
|
273
273
|
// so it does not collide with the root `stagepass --version` handler.
|
package/src/commands/new.js
CHANGED
|
@@ -20,16 +20,24 @@ import {
|
|
|
20
20
|
import { STAGEPASS_PACKAGES_VERSION } from '../lib/versions.js';
|
|
21
21
|
import { formatModules, normalizeModulesList } from '../lib/project-modules.js';
|
|
22
22
|
import { describeDetection, detectSite, normalizeSiteUrl } from '../lib/site-detect.js';
|
|
23
|
+
import {
|
|
24
|
+
PACKAGES_MAJOR_RANGE,
|
|
25
|
+
fetchPublished,
|
|
26
|
+
installedHasPerModuleTypes,
|
|
27
|
+
rangeHasPerModuleTypes,
|
|
28
|
+
resolveSnippetLoaderVersion,
|
|
29
|
+
} from '../lib/registry.js';
|
|
30
|
+
import { updateConfigTypes } from '../lib/project-sync.js';
|
|
31
|
+
|
|
32
|
+
const PER_MODULE_TYPES_HINT =
|
|
33
|
+
'@stagepass/modules on npm has no per-module types yet (needs 2.2.0); ' +
|
|
34
|
+
'types use "@stagepass/modules" for now. Run `stagepass upgrade` once it is published to narrow them.';
|
|
23
35
|
|
|
24
36
|
/**
|
|
25
|
-
* Resolve a boolean flag that may appear as --foo / --no-foo.
|
|
26
|
-
* Only argv is trusted: Commander defaults negated pairs to true, which would
|
|
27
|
-
* break defaults like --install (off unless passed).
|
|
28
|
-
*
|
|
29
37
|
* @param {object} _options
|
|
30
38
|
* @param {string} key
|
|
31
39
|
* @param {string[]} argv
|
|
32
|
-
* @param {boolean|undefined} defaultValue
|
|
40
|
+
* @param {boolean|undefined} defaultValue
|
|
33
41
|
*/
|
|
34
42
|
export function resolveBoolFlag(_options, key, argv, defaultValue) {
|
|
35
43
|
const pos = `--${key}`;
|
|
@@ -40,7 +48,7 @@ export function resolveBoolFlag(_options, key, argv, defaultValue) {
|
|
|
40
48
|
}
|
|
41
49
|
|
|
42
50
|
/**
|
|
43
|
-
* @param {object} options
|
|
51
|
+
* @param {object} options
|
|
44
52
|
* @param {object} [deps]
|
|
45
53
|
*/
|
|
46
54
|
async function resolveAuthor(options, deps) {
|
|
@@ -84,7 +92,6 @@ async function askYesNo(rl, question, defaultYes) {
|
|
|
84
92
|
}
|
|
85
93
|
|
|
86
94
|
/**
|
|
87
|
-
* Merge flags + optional interactive prompts into a resolved config.
|
|
88
95
|
* @param {string|undefined} nameArg
|
|
89
96
|
* @param {object} options
|
|
90
97
|
* @param {object} deps
|
|
@@ -104,7 +111,7 @@ export async function resolveNewOptions(nameArg, options = {}, deps = {}) {
|
|
|
104
111
|
let styles =
|
|
105
112
|
options.styles === 'css' || options.styles === 'scss' ? options.styles : '';
|
|
106
113
|
|
|
107
|
-
// Prefer argv for --foo / --no-foo so Commander
|
|
114
|
+
// Prefer argv for --foo / --no-foo so Commander defaults do not flip.
|
|
108
115
|
let modules = resolveBoolFlag(options, 'modules', argv, undefined);
|
|
109
116
|
let git = resolveBoolFlag(options, 'git', argv, undefined);
|
|
110
117
|
let install = resolveBoolFlag(options, 'install', argv, undefined);
|
|
@@ -128,9 +135,9 @@ export async function resolveNewOptions(nameArg, options = {}, deps = {}) {
|
|
|
128
135
|
}
|
|
129
136
|
|
|
130
137
|
/**
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
138
|
+
* Detect modules on the live site (public GET, never executed).
|
|
139
|
+
* @returns {Promise<string[] | null>} detected list, or null when unknown
|
|
140
|
+
*/
|
|
134
141
|
const detectFromSite = async () => {
|
|
135
142
|
if (!origin) return null;
|
|
136
143
|
log(`Checking live site https://${origin}/ for Stagepass script tags...`);
|
|
@@ -220,7 +227,7 @@ export async function resolveNewOptions(nameArg, options = {}, deps = {}) {
|
|
|
220
227
|
error: '--no-modules conflicts with declared site modules. Drop --no-modules or pass --site-modules none.',
|
|
221
228
|
};
|
|
222
229
|
}
|
|
223
|
-
//
|
|
230
|
+
// npm package (types) follows the declared list; legacy default is included.
|
|
224
231
|
if (siteModules.length > 0) modules = true;
|
|
225
232
|
|
|
226
233
|
// Non-interactive / --yes defaults
|
|
@@ -278,15 +285,38 @@ export async function resolveNewOptions(nameArg, options = {}, deps = {}) {
|
|
|
278
285
|
remote: remoteCheck.remote,
|
|
279
286
|
install,
|
|
280
287
|
dryRun: options.dryRun === true,
|
|
281
|
-
|
|
282
|
-
|
|
288
|
+
// Bundled loader until createNew resolves the published one (AC-14).
|
|
289
|
+
loaderVersion: deps.packagesVersion || STAGEPASS_PACKAGES_VERSION,
|
|
283
290
|
},
|
|
284
291
|
};
|
|
285
292
|
}
|
|
286
293
|
|
|
287
294
|
/**
|
|
288
|
-
*
|
|
289
|
-
*
|
|
295
|
+
* @param {string} targetDir
|
|
296
|
+
* @param {{ siteModules: string[], perModuleTypes?: boolean }} config
|
|
297
|
+
* @param {typeof fs} fse
|
|
298
|
+
* @param {(msg?: string) => void} log
|
|
299
|
+
*/
|
|
300
|
+
async function reconcileInstalledTypes(targetDir, config, fse, log) {
|
|
301
|
+
const installed = await installedHasPerModuleTypes(targetDir, fse);
|
|
302
|
+
if (installed === null || installed === (config.perModuleTypes !== false)) return;
|
|
303
|
+
for (const name of ['tsconfig.json', 'jsconfig.json']) {
|
|
304
|
+
const file = path.join(targetDir, name);
|
|
305
|
+
if (!(await fse.pathExists(file))) continue;
|
|
306
|
+
const raw = await fse.readFile(file, 'utf8');
|
|
307
|
+
const result = updateConfigTypes(raw, config.siteModules, {
|
|
308
|
+
modulesDependency: true,
|
|
309
|
+
perModuleTypes: installed,
|
|
310
|
+
});
|
|
311
|
+
if (result.status === 'updated' && result.content) {
|
|
312
|
+
await fse.writeFile(file, result.content, 'utf8');
|
|
313
|
+
log(chalk.dim(`${name}: types ${result.types.join(', ')} (matches installed @stagepass/modules)`));
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
if (!installed) log(chalk.yellow(PER_MODULE_TYPES_HINT));
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
/**
|
|
290
320
|
* @param {string|undefined} nameArg
|
|
291
321
|
* @param {object} [options]
|
|
292
322
|
* @param {object} [deps]
|
|
@@ -336,6 +366,29 @@ export async function createNew(nameArg, options = {}, deps = {}) {
|
|
|
336
366
|
}
|
|
337
367
|
};
|
|
338
368
|
|
|
369
|
+
// Per-module types since modules 2.2.0; else umbrella types so tsc passes.
|
|
370
|
+
const singleModuleTypes =
|
|
371
|
+
config.modules === true &&
|
|
372
|
+
config.siteModules.length > 0 &&
|
|
373
|
+
!config.siteModules.includes('all');
|
|
374
|
+
if (singleModuleTypes && !config.dryRun) {
|
|
375
|
+
const published = await (deps.fetchPublished || fetchPublished)('@stagepass/modules', { exec });
|
|
376
|
+
config.perModuleTypes =
|
|
377
|
+
rangeHasPerModuleTypes(published ? published.versions : null, PACKAGES_MAJOR_RANGE) === true;
|
|
378
|
+
if (!config.perModuleTypes) log(chalk.yellow(PER_MODULE_TYPES_HINT));
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
// Snippets pin the resolved loader, never the CLI version (AC-14).
|
|
382
|
+
if (!config.dryRun) {
|
|
383
|
+
const loader = await (deps.fetchPublished || fetchPublished)('@stagepass/loader', { exec });
|
|
384
|
+
config.loaderVersion = await resolveSnippetLoaderVersion({
|
|
385
|
+
published: loader,
|
|
386
|
+
cwd: targetDir,
|
|
387
|
+
bundled: config.loaderVersion,
|
|
388
|
+
fse,
|
|
389
|
+
});
|
|
390
|
+
}
|
|
391
|
+
|
|
339
392
|
let files;
|
|
340
393
|
try {
|
|
341
394
|
files = await planScaffoldFiles(config, { readTemplate });
|
|
@@ -423,6 +476,9 @@ export async function createNew(nameArg, options = {}, deps = {}) {
|
|
|
423
476
|
error(chalk.red(`npm install exited with code ${result.code}`));
|
|
424
477
|
return typeof result.code === 'number' ? result.code : 1;
|
|
425
478
|
}
|
|
479
|
+
if (singleModuleTypes) {
|
|
480
|
+
await reconcileInstalledTypes(targetDir, config, fse, log);
|
|
481
|
+
}
|
|
426
482
|
} catch (err) {
|
|
427
483
|
error(
|
|
428
484
|
chalk.red(
|
|
@@ -451,5 +507,5 @@ export async function createNew(nameArg, options = {}, deps = {}) {
|
|
|
451
507
|
return 0;
|
|
452
508
|
}
|
|
453
509
|
|
|
454
|
-
/** Commander-facing alias
|
|
510
|
+
/** Commander-facing alias */
|
|
455
511
|
export const newProject = createNew;
|
package/src/commands/upgrade.js
CHANGED
|
@@ -10,7 +10,16 @@ import {
|
|
|
10
10
|
import readline from 'node:readline/promises';
|
|
11
11
|
import { stdin as input, stdout as output } from 'node:process';
|
|
12
12
|
import { STAGEPASS_PACKAGES_VERSION } from '../lib/versions.js';
|
|
13
|
-
import {
|
|
13
|
+
import {
|
|
14
|
+
PACKAGES_MAJOR_RANGE,
|
|
15
|
+
fetchPublished,
|
|
16
|
+
resolveSnippetLoaderVersion,
|
|
17
|
+
installedHasPerModuleTypes,
|
|
18
|
+
maxSatisfying,
|
|
19
|
+
rangeHasPerModuleTypes,
|
|
20
|
+
satisfiesSimple,
|
|
21
|
+
} from '../lib/registry.js';
|
|
22
|
+
import { detectSite, normalizeSiteUrl } from '../lib/site-detect.js';
|
|
14
23
|
import {
|
|
15
24
|
applyProjectSync,
|
|
16
25
|
planProjectSync,
|
|
@@ -20,13 +29,12 @@ import {
|
|
|
20
29
|
|
|
21
30
|
const STAGEPASS_SCOPE = '@stagepass/';
|
|
22
31
|
|
|
32
|
+
const SAFE_SPEC_RE = /^[~^]?[0-9A-Za-z][0-9A-Za-z._+-]*$/;
|
|
33
|
+
|
|
23
34
|
/**
|
|
24
|
-
* True when the version looks like an npm dist-tag that needs resolution.
|
|
25
35
|
* @param {string} version
|
|
26
36
|
*/
|
|
27
|
-
function
|
|
28
|
-
if (!version) return false;
|
|
29
|
-
// Semver-ish or range: treat as literal. Dist-tags are bare words.
|
|
37
|
+
function isDistTag(version) {
|
|
30
38
|
if (/^[0-9]/.test(version) || version.startsWith('^') || version.startsWith('~')) {
|
|
31
39
|
return false;
|
|
32
40
|
}
|
|
@@ -35,51 +43,73 @@ function needsNpmView(version) {
|
|
|
35
43
|
}
|
|
36
44
|
|
|
37
45
|
/**
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
* Contacts the network only when `--version` is an npm dist-tag (e.g. latest).
|
|
41
|
-
*
|
|
46
|
+
* Never writes a version/range no published release satisfies (avoids ETARGET on staged releases).
|
|
47
|
+
* @param {string} name
|
|
42
48
|
* @param {string|undefined} requested
|
|
43
|
-
* @param {
|
|
44
|
-
* @
|
|
45
|
-
* @param {string} [deps.packagesVersion]
|
|
46
|
-
* @param {string} [deps.cliVersion] Legacy alias for packagesVersion (tests)
|
|
47
|
-
* @returns {Promise<string>}
|
|
49
|
+
* @param {{ distTags: Record<string, string>, versions: string[] } | null} published
|
|
50
|
+
* @returns {{ spec: string, resolved: string | null, offline: boolean }}
|
|
48
51
|
*/
|
|
49
|
-
export
|
|
50
|
-
const
|
|
51
|
-
|
|
52
|
-
|
|
52
|
+
export function resolvePackageTarget(name, requested, published) {
|
|
53
|
+
const value = requested == null ? '' : String(requested).trim().replace(/^v(?=\d)/i, '');
|
|
54
|
+
if (value && !SAFE_SPEC_RE.test(value)) {
|
|
55
|
+
throw new Error('Invalid --version value (use a semver, range prefix ^/~, or npm dist-tag).');
|
|
56
|
+
}
|
|
53
57
|
|
|
54
|
-
if (!
|
|
55
|
-
return
|
|
58
|
+
if (!value) {
|
|
59
|
+
if (!published) return { spec: PACKAGES_MAJOR_RANGE, resolved: null, offline: true };
|
|
60
|
+
const latest = published.distTags.latest;
|
|
61
|
+
const best =
|
|
62
|
+
latest && satisfiesSimple(latest, PACKAGES_MAJOR_RANGE)
|
|
63
|
+
? latest
|
|
64
|
+
: maxSatisfying(published.versions, PACKAGES_MAJOR_RANGE);
|
|
65
|
+
if (!best) return { spec: PACKAGES_MAJOR_RANGE, resolved: null, offline: false };
|
|
66
|
+
return { spec: `^${best}`, resolved: best, offline: false };
|
|
56
67
|
}
|
|
57
68
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
if (!/^[~^]?[0-9A-Za-z][0-9A-Za-z._+-]*$/.test(literal)) {
|
|
63
|
-
throw new Error(
|
|
64
|
-
'Invalid --version value (use a semver, range prefix ^/~, or npm dist-tag).'
|
|
65
|
-
);
|
|
66
|
-
}
|
|
67
|
-
return literal;
|
|
69
|
+
if (!published) {
|
|
70
|
+
throw new Error(
|
|
71
|
+
`Could not check ${name}@${value} on npm (offline?). Retry online, or run without --version to use ${PACKAGES_MAJOR_RANGE}.`
|
|
72
|
+
);
|
|
68
73
|
}
|
|
69
74
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
75
|
+
if (isDistTag(value)) {
|
|
76
|
+
const tagged = published.distTags[value];
|
|
77
|
+
if (!tagged) throw new Error(`${name} has no dist-tag "${value}".`);
|
|
78
|
+
return { spec: tagged, resolved: tagged, offline: false };
|
|
74
79
|
}
|
|
75
|
-
|
|
76
|
-
|
|
80
|
+
|
|
81
|
+
const best = maxSatisfying(published.versions, value);
|
|
82
|
+
if (!best) {
|
|
83
|
+
throw new Error(
|
|
84
|
+
`No published ${name} matches ${value} (staged releases cannot be installed until they are approved).`
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
return { spec: value, resolved: best, offline: false };
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* @param {string|undefined} requested `--version`
|
|
92
|
+
* @param {string[]} names package names
|
|
93
|
+
* @param {object} [deps]
|
|
94
|
+
* @param {(name: string) => Promise<{ distTags: Record<string, string>, versions: string[] } | null>} [deps.fetchPublished]
|
|
95
|
+
* @param {import('../lib/exec.js').ExecFn} [deps.exec]
|
|
96
|
+
* @returns {Promise<{ targets: Record<string, { spec: string, resolved: string | null, offline: boolean }>, published: Record<string, { distTags: Record<string, string>, versions: string[] } | null> }>}
|
|
97
|
+
*/
|
|
98
|
+
export async function resolveUpgradeTargets(requested, names, deps = {}) {
|
|
99
|
+
const lookup =
|
|
100
|
+
deps.fetchPublished || ((name) => fetchPublished(name, { exec: deps.exec || defaultExec }));
|
|
101
|
+
/** @type {Record<string, any>} */
|
|
102
|
+
const targets = {};
|
|
103
|
+
/** @type {Record<string, any>} */
|
|
104
|
+
const published = {};
|
|
105
|
+
for (const name of [...new Set(names)]) {
|
|
106
|
+
published[name] = await lookup(name);
|
|
107
|
+
targets[name] = resolvePackageTarget(name, requested, published[name]);
|
|
77
108
|
}
|
|
78
|
-
return
|
|
109
|
+
return { targets, published };
|
|
79
110
|
}
|
|
80
111
|
|
|
81
112
|
/**
|
|
82
|
-
* Collect @stagepass/* entries from dependencies and devDependencies.
|
|
83
113
|
* @param {object} pkg
|
|
84
114
|
* @returns {{ section: 'dependencies'|'devDependencies', name: string, from: string }[]}
|
|
85
115
|
*/
|
|
@@ -99,19 +129,18 @@ export function findStagepassDeps(pkg) {
|
|
|
99
129
|
}
|
|
100
130
|
|
|
101
131
|
/**
|
|
102
|
-
* Rewrite @stagepass/* versions in a package.json object (mutates a copy).
|
|
103
132
|
* @param {object} pkg
|
|
104
|
-
* @param {string}
|
|
133
|
+
* @param {string | Record<string, string>} target one spec for all, or per package name
|
|
105
134
|
* @returns {{ next: object, changes: { name: string, from: string, to: string, section: string }[] }}
|
|
106
135
|
*/
|
|
107
|
-
export function applyStagepassVersions(pkg,
|
|
136
|
+
export function applyStagepassVersions(pkg, target) {
|
|
108
137
|
const next = structuredClone(pkg);
|
|
109
138
|
const changes = [];
|
|
110
|
-
const target = String(targetVersion);
|
|
111
139
|
|
|
112
140
|
for (const { section, name, from } of findStagepassDeps(pkg)) {
|
|
113
141
|
if (!next[section]) continue;
|
|
114
|
-
const to = target;
|
|
142
|
+
const to = typeof target === 'string' ? target : target[name];
|
|
143
|
+
if (!to) continue;
|
|
115
144
|
if (String(from) !== to) {
|
|
116
145
|
changes.push({ name, from: String(from), to, section });
|
|
117
146
|
}
|
|
@@ -122,10 +151,6 @@ export function applyStagepassVersions(pkg, targetVersion) {
|
|
|
122
151
|
}
|
|
123
152
|
|
|
124
153
|
/**
|
|
125
|
-
* Upgrade @stagepass/* deps in the current project's package.json and sync
|
|
126
|
-
* the declared modules into stagepass.json, AGENTS.md, the Cursor rule and
|
|
127
|
-
* tsconfig/jsconfig types.
|
|
128
|
-
*
|
|
129
154
|
* @param {object} [options]
|
|
130
155
|
* @param {boolean} [options.dryRun]
|
|
131
156
|
* @param {string} [options.version]
|
|
@@ -137,7 +162,7 @@ export function applyStagepassVersions(pkg, targetVersion) {
|
|
|
137
162
|
* @param {typeof fs} [deps.fs]
|
|
138
163
|
* @param {string} [deps.cwd]
|
|
139
164
|
* @param {import('../lib/exec.js').ExecFn} [deps.exec]
|
|
140
|
-
* @param {string} [deps.
|
|
165
|
+
* @param {(name: string) => Promise<{ distTags: Record<string, string>, versions: string[] } | null>} [deps.fetchPublished]
|
|
141
166
|
* @param {(msg?: string) => void} [deps.log]
|
|
142
167
|
* @returns {Promise<number>} exit code
|
|
143
168
|
*/
|
|
@@ -152,17 +177,6 @@ export async function upgrade(options = {}, deps = {}) {
|
|
|
152
177
|
return 1;
|
|
153
178
|
}
|
|
154
179
|
|
|
155
|
-
let targetVersion;
|
|
156
|
-
try {
|
|
157
|
-
targetVersion = await resolveUpgradeVersion(options.version, {
|
|
158
|
-
exec: deps.exec || defaultExec,
|
|
159
|
-
packagesVersion: deps.packagesVersion || deps.cliVersion,
|
|
160
|
-
});
|
|
161
|
-
} catch (error) {
|
|
162
|
-
log(chalk.red(error instanceof Error ? error.message : String(error)));
|
|
163
|
-
return 1;
|
|
164
|
-
}
|
|
165
|
-
|
|
166
180
|
const raw = await fse.readFile(pkgPath, 'utf8');
|
|
167
181
|
let pkg;
|
|
168
182
|
try {
|
|
@@ -172,22 +186,61 @@ export async function upgrade(options = {}, deps = {}) {
|
|
|
172
186
|
return 1;
|
|
173
187
|
}
|
|
174
188
|
|
|
175
|
-
|
|
189
|
+
if (options.site) {
|
|
190
|
+
const c = normalizeSiteUrl(options.site);
|
|
191
|
+
if (!c.ok) {
|
|
192
|
+
log(chalk.red(c.error));
|
|
193
|
+
return 1;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
const names = findStagepassDeps(pkg).map((d) => d.name);
|
|
198
|
+
let resolvedTargets;
|
|
199
|
+
try {
|
|
200
|
+
resolvedTargets = await resolveUpgradeTargets(options.version, names, {
|
|
201
|
+
exec: deps.exec || defaultExec,
|
|
202
|
+
fetchPublished: deps.fetchPublished,
|
|
203
|
+
});
|
|
204
|
+
} catch (error) {
|
|
205
|
+
log(chalk.red(error instanceof Error ? error.message : String(error)));
|
|
206
|
+
return 1;
|
|
207
|
+
}
|
|
208
|
+
const { targets, published } = resolvedTargets;
|
|
209
|
+
/** @type {Record<string, string>} */
|
|
210
|
+
const specs = Object.fromEntries(Object.entries(targets).map(([n, t]) => [n, t.spec]));
|
|
211
|
+
if (Object.values(targets).some((t) => t.offline)) {
|
|
212
|
+
log(chalk.yellow(`npm registry not reachable: using ${PACKAGES_MAJOR_RANGE} (resolves to the newest published 2.x).`));
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
const { next, changes } = applyStagepassVersions(pkg, specs);
|
|
176
216
|
|
|
177
217
|
if (changes.length === 0) {
|
|
178
|
-
|
|
179
|
-
if (existing.length === 0) {
|
|
218
|
+
if (names.length === 0) {
|
|
180
219
|
log(chalk.yellow('No @stagepass/* packages found in package.json.'));
|
|
181
220
|
} else {
|
|
182
|
-
log(chalk.green(
|
|
221
|
+
log(chalk.green('@stagepass/* already at the newest published ranges.'));
|
|
183
222
|
}
|
|
184
223
|
} else {
|
|
185
|
-
log(chalk.bold(
|
|
224
|
+
log(chalk.bold('Upgrade @stagepass/* (published versions only)'));
|
|
186
225
|
for (const change of changes) {
|
|
187
226
|
log(` ${change.name}: ${change.from} → ${change.to} (${change.section})`);
|
|
188
227
|
}
|
|
189
228
|
}
|
|
190
229
|
|
|
230
|
+
// Per-module types need @stagepass/modules >= 2.2.0 resolvable from the range.
|
|
231
|
+
/** @type {boolean | undefined} */
|
|
232
|
+
let perModuleTypes;
|
|
233
|
+
const modulesSpec =
|
|
234
|
+
next?.dependencies?.['@stagepass/modules'] || next?.devDependencies?.['@stagepass/modules'];
|
|
235
|
+
if (modulesSpec) {
|
|
236
|
+
const fromRegistry = rangeHasPerModuleTypes(
|
|
237
|
+
published['@stagepass/modules'] ? published['@stagepass/modules'].versions : null,
|
|
238
|
+
String(modulesSpec)
|
|
239
|
+
);
|
|
240
|
+
perModuleTypes =
|
|
241
|
+
fromRegistry != null ? fromRegistry : (await installedHasPerModuleTypes(cwd, fse)) === true;
|
|
242
|
+
}
|
|
243
|
+
|
|
191
244
|
// Declared modules: live site first, then stagepass.json, then ask.
|
|
192
245
|
const sp = await readStagepassJson(cwd, fse);
|
|
193
246
|
if (sp.error) log(chalk.yellow(sp.error));
|
|
@@ -235,6 +288,7 @@ export async function upgrade(options = {}, deps = {}) {
|
|
|
235
288
|
modules: resolved.modules,
|
|
236
289
|
origin: options.site ? resolved.origin : null,
|
|
237
290
|
pkg: next,
|
|
291
|
+
perModuleTypes,
|
|
238
292
|
},
|
|
239
293
|
fse
|
|
240
294
|
);
|
|
@@ -249,7 +303,35 @@ export async function upgrade(options = {}, deps = {}) {
|
|
|
249
303
|
for (const note of plan.notes) {
|
|
250
304
|
log(chalk.yellow(` note: ${note}`));
|
|
251
305
|
}
|
|
306
|
+
if (perModuleTypes === false && resolved.modules.length > 0 && !resolved.modules.includes('all')) {
|
|
307
|
+
log(
|
|
308
|
+
chalk.yellow(
|
|
309
|
+
' 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.'
|
|
310
|
+
)
|
|
311
|
+
);
|
|
312
|
+
}
|
|
252
313
|
|
|
314
|
+
// Snippet: --version pin of loader on 2.x wins; resolve before writing so a failure changes nothing.
|
|
315
|
+
let loaderPub = '@stagepass/loader' in published ? published['@stagepass/loader'] : undefined;
|
|
316
|
+
const getLoaderPub = async () => {
|
|
317
|
+
if (loaderPub !== undefined) return loaderPub;
|
|
318
|
+
const lookup = deps.fetchPublished || ((name) => fetchPublished(name, { exec: deps.exec || defaultExec }));
|
|
319
|
+
loaderPub = await lookup('@stagepass/loader');
|
|
320
|
+
return loaderPub;
|
|
321
|
+
};
|
|
322
|
+
let snippetVersion = null;
|
|
323
|
+
if (options.version != null) {
|
|
324
|
+
let t = targets['@stagepass/loader'];
|
|
325
|
+
if (!t) {
|
|
326
|
+
try {
|
|
327
|
+
t = resolvePackageTarget('@stagepass/loader', options.version, await getLoaderPub());
|
|
328
|
+
} catch (e) {
|
|
329
|
+
log(chalk.red(e instanceof Error ? e.message : String(e)));
|
|
330
|
+
return 1;
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
if (t.resolved && satisfiesSimple(t.resolved, PACKAGES_MAJOR_RANGE)) snippetVersion = t.resolved;
|
|
334
|
+
}
|
|
253
335
|
if (options.dryRun) {
|
|
254
336
|
log(chalk.dim('\nDry run: no files were modified.'));
|
|
255
337
|
} else {
|
|
@@ -263,8 +345,16 @@ export async function upgrade(options = {}, deps = {}) {
|
|
|
263
345
|
if (plan.writes.length > 0) log(chalk.green(`Updated ${plan.writes.length} project file(s).`));
|
|
264
346
|
}
|
|
265
347
|
|
|
348
|
+
if (!snippetVersion) {
|
|
349
|
+
snippetVersion = await resolveSnippetLoaderVersion({
|
|
350
|
+
published: await getLoaderPub(),
|
|
351
|
+
cwd,
|
|
352
|
+
bundled: STAGEPASS_PACKAGES_VERSION,
|
|
353
|
+
fse,
|
|
354
|
+
});
|
|
355
|
+
}
|
|
266
356
|
const snippets = buildWebflowSnippets({
|
|
267
|
-
version:
|
|
357
|
+
version: snippetVersion,
|
|
268
358
|
moduleList: resolved.modules,
|
|
269
359
|
});
|
|
270
360
|
printWebflowSnippets(snippets, log);
|
package/src/lib/doctor-site.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Doctor checks
|
|
3
|
-
*
|
|
4
|
-
* Read-only: one public GET of the live page, parsed and never executed.
|
|
2
|
+
* Doctor checks: declared modules vs Stagepass scripts on the live site.
|
|
3
|
+
* Read-only public GET; HTML parsed, never executed.
|
|
5
4
|
*/
|
|
6
5
|
|
|
7
6
|
import path from 'node:path';
|
|
@@ -41,7 +40,7 @@ export async function runProjectSiteChecks(opts = {}) {
|
|
|
41
40
|
}
|
|
42
41
|
|
|
43
42
|
if (opts.skipSite) return checks;
|
|
44
|
-
const found = await resolveProjectOrigin(cwd, sp.data, { loadProjects: opts.loadProjects });
|
|
43
|
+
const found = await resolveProjectOrigin(cwd, sp.data, { loadProjects: opts.loadProjects, log: opts.log });
|
|
45
44
|
if (!found) {
|
|
46
45
|
checks.push({
|
|
47
46
|
id: 'project.site',
|
|
@@ -71,12 +71,22 @@ export function isStagepassModulesTypeEntry(entry) {
|
|
|
71
71
|
/**
|
|
72
72
|
* `compilerOptions.types` entries for a declared list.
|
|
73
73
|
* @param {string[]} modules normalized list
|
|
74
|
-
*
|
|
74
|
+
* Single modules use `@stagepass/modules/<id>` (modules >= 2.2.0). When the
|
|
75
|
+
* installed or resolvable @stagepass/modules has no per-module types
|
|
76
|
+
* (`perModuleTypes: false`, e.g. only 2.0.0 published), the umbrella
|
|
77
|
+
* `@stagepass/modules` is used so the project still type-checks.
|
|
78
|
+
*
|
|
79
|
+
* @param {string[]} modules normalized list
|
|
80
|
+
* @param {{ modulesDependency?: boolean, perModuleTypes?: boolean }} [opts] module types need the npm dep
|
|
75
81
|
* @returns {string[]}
|
|
76
82
|
*/
|
|
77
83
|
export function typesForModules(modules, opts = {}) {
|
|
78
84
|
const types = ['@stagepass/loader'];
|
|
79
85
|
if (opts.modulesDependency === false) return types;
|
|
86
|
+
if (modules.length > 0 && opts.perModuleTypes === false) {
|
|
87
|
+
types.push('@stagepass/modules');
|
|
88
|
+
return types;
|
|
89
|
+
}
|
|
80
90
|
if (modules.includes('all')) {
|
|
81
91
|
types.push('@stagepass/modules');
|
|
82
92
|
} else {
|
package/src/lib/project-sync.js
CHANGED
|
@@ -1,12 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Sync generated project files with
|
|
3
|
-
* (used by `stagepass upgrade` and `stagepass doctor`).
|
|
4
|
-
*
|
|
5
|
-
* Writes only:
|
|
6
|
-
* - stagepass.json `modules` (and `origin` when passed via --site)
|
|
7
|
-
* - AGENTS.md managed block (created when missing)
|
|
8
|
-
* - .cursor/rules/stagepass.mdc (created when missing, managed block refreshed)
|
|
9
|
-
* - tsconfig.json / jsconfig.json `compilerOptions.types` (plain JSON only)
|
|
2
|
+
* Sync generated project files with declared Stagepass modules
|
|
10
3
|
*/
|
|
11
4
|
|
|
12
5
|
import path from 'node:path';
|
|
@@ -25,10 +18,9 @@ import {
|
|
|
25
18
|
typesForModules,
|
|
26
19
|
} from './project-modules.js';
|
|
27
20
|
import { loadProjects } from './projects.js';
|
|
28
|
-
import { describeDetection, normalizeSiteUrl } from './site-detect.js';
|
|
21
|
+
import { describeDetection, normalizeSiteUrl, SITE_PORT_ERROR } from './site-detect.js';
|
|
29
22
|
|
|
30
23
|
/**
|
|
31
|
-
* Read and parse stagepass.json (null when missing or invalid).
|
|
32
24
|
* @param {string} cwd
|
|
33
25
|
* @param {typeof fs} [fse]
|
|
34
26
|
* @returns {Promise<{ exists: boolean, data: Record<string, unknown> | null, raw: string, error?: string }>}
|
|
@@ -49,7 +41,6 @@ export async function readStagepassJson(cwd, fse = fs) {
|
|
|
49
41
|
}
|
|
50
42
|
|
|
51
43
|
/**
|
|
52
|
-
* Declared modules from stagepass.json (`null` when missing or legacy boolean).
|
|
53
44
|
* @param {Record<string, unknown> | null} data
|
|
54
45
|
* @returns {string[] | null}
|
|
55
46
|
*/
|
|
@@ -59,10 +50,11 @@ export function declaredModules(data) {
|
|
|
59
50
|
return result.ok ? result.modules : null;
|
|
60
51
|
}
|
|
61
52
|
|
|
53
|
+
function warnPortSkip(log, source) {
|
|
54
|
+
if (log) log(`Skipped ${source}: it contains a port. Use the host only, e.g. my-site.webflow.io.`);
|
|
55
|
+
}
|
|
56
|
+
|
|
62
57
|
/**
|
|
63
|
-
* Live origin for a project: stagepass.json `origin`, else the first concrete
|
|
64
|
-
* (non-wildcard) origin stored by `stagepass link` for this project folder.
|
|
65
|
-
*
|
|
66
58
|
* @param {string} cwd
|
|
67
59
|
* @param {Record<string, unknown> | null} data
|
|
68
60
|
* @param {{ loadProjects?: typeof loadProjects, realpath?: (p: string) => Promise<string> }} [deps]
|
|
@@ -72,6 +64,7 @@ export async function resolveProjectOrigin(cwd, data, deps = {}) {
|
|
|
72
64
|
if (data && typeof data.origin === 'string' && data.origin.trim() !== '') {
|
|
73
65
|
const check = normalizeSiteUrl(data.origin);
|
|
74
66
|
if (check.ok) return { origin: check.host, source: 'stagepass.json' };
|
|
67
|
+
if (check.error === SITE_PORT_ERROR) warnPortSkip(deps.log, 'stagepass.json origin');
|
|
75
68
|
}
|
|
76
69
|
const load = deps.loadProjects || loadProjects;
|
|
77
70
|
const realpath = deps.realpath || ((p) => fs.realpath(p));
|
|
@@ -99,21 +92,18 @@ export async function resolveProjectOrigin(cwd, data, deps = {}) {
|
|
|
99
92
|
for (const origin of Array.isArray(entry.origins) ? entry.origins : []) {
|
|
100
93
|
const check = normalizeSiteUrl(String(origin));
|
|
101
94
|
if (check.ok) return { origin: check.host, source: 'stagepass link' };
|
|
95
|
+
if (check.error === SITE_PORT_ERROR) warnPortSkip(deps.log, 'linked origin from stagepass link');
|
|
102
96
|
}
|
|
103
97
|
}
|
|
104
98
|
return null;
|
|
105
99
|
}
|
|
106
100
|
|
|
107
101
|
/**
|
|
108
|
-
* Decide the declared modules list for `upgrade`.
|
|
109
|
-
* Order: --site-modules flag, live site detection, existing stagepass.json,
|
|
110
|
-
* interactive prompt. The npm dependency is only mentioned as a hint.
|
|
111
|
-
*
|
|
112
102
|
* @param {object} input
|
|
113
103
|
* @param {string} input.cwd
|
|
114
|
-
* @param {Record<string, unknown> | null} input.data
|
|
115
|
-
* @param {object} input.pkg
|
|
116
|
-
* @param {object} input.options
|
|
104
|
+
* @param {Record<string, unknown> | null} input.data
|
|
105
|
+
* @param {object} input.pkg
|
|
106
|
+
* @param {object} input.options
|
|
117
107
|
* @param {object} deps
|
|
118
108
|
* @param {(site: string) => Promise<any>} deps.detectSite
|
|
119
109
|
* @param {(question: string, def: string) => Promise<string>} [deps.ask]
|
|
@@ -147,7 +137,7 @@ export async function resolveUpgradeModules(input, deps) {
|
|
|
147
137
|
if (!check.ok) return { ok: false, error: check.error };
|
|
148
138
|
origin = check.host;
|
|
149
139
|
} else {
|
|
150
|
-
const found = await resolveProjectOrigin(cwd, data, { loadProjects: deps.loadProjects });
|
|
140
|
+
const found = await resolveProjectOrigin(cwd, data, { loadProjects: deps.loadProjects, log });
|
|
151
141
|
origin = found ? found.origin : null;
|
|
152
142
|
}
|
|
153
143
|
|
|
@@ -179,9 +169,18 @@ export async function resolveUpgradeModules(input, deps) {
|
|
|
179
169
|
source = 'stagepass.json';
|
|
180
170
|
}
|
|
181
171
|
|
|
172
|
+
// Legacy `"modules": true` meant all modules typed; keep as `["all"]` without a live site.
|
|
173
|
+
if (!candidate && data?.modules === true) {
|
|
174
|
+
candidate = ['all'];
|
|
175
|
+
source = 'legacy "modules": true in stagepass.json';
|
|
176
|
+
}
|
|
177
|
+
|
|
182
178
|
if (!deps.interactive) {
|
|
183
179
|
if (candidate) {
|
|
184
180
|
log(`Modules: ${formatModules(candidate)} (inferred from ${source}). Check it, or pass --site-modules.`);
|
|
181
|
+
if (candidate.includes('all') && source.startsWith('legacy')) {
|
|
182
|
+
log('Hint: narrow it to the module tags your site loads, e.g. stagepass upgrade --site-modules cookies.');
|
|
183
|
+
}
|
|
185
184
|
return { ok: true, modules: candidate, source, origin, detection };
|
|
186
185
|
}
|
|
187
186
|
if (hasModulesDep) {
|
|
@@ -217,11 +216,9 @@ export async function resolveUpgradeModules(input, deps) {
|
|
|
217
216
|
}
|
|
218
217
|
|
|
219
218
|
/**
|
|
220
|
-
*
|
|
221
|
-
*
|
|
222
|
-
* @param {string} raw file contents
|
|
219
|
+
* @param {string} raw
|
|
223
220
|
* @param {string[]} modules
|
|
224
|
-
* @param {{ modulesDependency: boolean }} opts
|
|
221
|
+
* @param {{ modulesDependency: boolean, perModuleTypes?: boolean }} opts
|
|
225
222
|
* @returns {{ status: 'updated' | 'unchanged' | 'skipped', content?: string, reason?: string, types?: string[] }}
|
|
226
223
|
*/
|
|
227
224
|
export function updateConfigTypes(raw, modules, opts) {
|
|
@@ -262,13 +259,12 @@ export function updateConfigTypes(raw, modules, opts) {
|
|
|
262
259
|
}
|
|
263
260
|
|
|
264
261
|
/**
|
|
265
|
-
* Plan file changes for the declared modules (pure, no writes).
|
|
266
|
-
*
|
|
267
262
|
* @param {object} input
|
|
268
263
|
* @param {string} input.cwd
|
|
269
264
|
* @param {string[]} input.modules
|
|
270
|
-
* @param {string | null} [input.origin]
|
|
271
|
-
* @param {object} input.pkg
|
|
265
|
+
* @param {string | null} [input.origin]
|
|
266
|
+
* @param {object} input.pkg
|
|
267
|
+
* @param {boolean} [input.perModuleTypes]
|
|
272
268
|
* @param {typeof fs} [fse]
|
|
273
269
|
* @returns {Promise<{ writes: { file: string, content: string, action: string }[], notes: string[] }>}
|
|
274
270
|
*/
|
|
@@ -281,6 +277,7 @@ export async function planProjectSync(input, fse = fs) {
|
|
|
281
277
|
const modulesDependency = Boolean(
|
|
282
278
|
pkg?.dependencies?.['@stagepass/modules'] || pkg?.devDependencies?.['@stagepass/modules']
|
|
283
279
|
);
|
|
280
|
+
const perModuleTypes = input.perModuleTypes;
|
|
284
281
|
|
|
285
282
|
// stagepass.json
|
|
286
283
|
const sp = await readStagepassJson(cwd, fse);
|
|
@@ -356,9 +353,9 @@ export async function planProjectSync(input, fse = fs) {
|
|
|
356
353
|
if (!(await fse.pathExists(file))) continue;
|
|
357
354
|
sawConfig = true;
|
|
358
355
|
const raw = await fse.readFile(file, 'utf8');
|
|
359
|
-
const result = updateConfigTypes(raw, modules, { modulesDependency });
|
|
356
|
+
const result = updateConfigTypes(raw, modules, { modulesDependency, perModuleTypes });
|
|
360
357
|
if (result.status === 'skipped') {
|
|
361
|
-
const wanted = JSON.stringify(typesForModules(modules, { modulesDependency }));
|
|
358
|
+
const wanted = JSON.stringify(typesForModules(modules, { modulesDependency, perModuleTypes }));
|
|
362
359
|
notes.push(`${name} ${result.reason}: "types": ${wanted}`);
|
|
363
360
|
} else if (result.status === 'updated') {
|
|
364
361
|
writes.push({ file: name, content: result.content, action: `types: ${result.types.join(', ')}` });
|
|
@@ -375,7 +372,6 @@ export async function planProjectSync(input, fse = fs) {
|
|
|
375
372
|
}
|
|
376
373
|
|
|
377
374
|
/**
|
|
378
|
-
* Write planned changes (skips when dryRun).
|
|
379
375
|
* @param {string} cwd
|
|
380
376
|
* @param {{ file: string, content: string }[]} writes
|
|
381
377
|
* @param {typeof fs} [fse]
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
/** Read-only npm registry lookups for @stagepass/* packages. */
|
|
2
|
+
|
|
3
|
+
import fs from 'fs-extra';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import { defaultExec } from './exec.js';
|
|
6
|
+
import { MODULE_IDS } from './project-modules.js';
|
|
7
|
+
import { STAGEPASS_PACKAGES_VERSION } from './versions.js';
|
|
8
|
+
|
|
9
|
+
/** Major range for new projects (matches `@2` CDN embed policy). */
|
|
10
|
+
export const PACKAGES_MAJOR_RANGE = '^2';
|
|
11
|
+
|
|
12
|
+
/** First @stagepass/modules release with per-module type entry points. */
|
|
13
|
+
export const PER_MODULE_TYPES_SINCE = '2.2.0';
|
|
14
|
+
|
|
15
|
+
const SEMVER_RE = /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-([0-9A-Za-z.-]+))?$/;
|
|
16
|
+
const PKG_NAME_RE = /^@stagepass\/[a-z0-9][a-z0-9._-]*$/;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* @param {string} version
|
|
20
|
+
* @returns {{ major: number, minor: number, patch: number, pre: boolean } | null}
|
|
21
|
+
*/
|
|
22
|
+
export function parseSemver(version) {
|
|
23
|
+
const m = SEMVER_RE.exec(String(version));
|
|
24
|
+
if (!m) return null;
|
|
25
|
+
return { major: +m[1], minor: +m[2], patch: +m[3], pre: Boolean(m[4]) };
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* @param {string} a
|
|
30
|
+
* @param {string} b
|
|
31
|
+
* @returns {number}
|
|
32
|
+
*/
|
|
33
|
+
export function compareSemver(a, b) {
|
|
34
|
+
const x = parseSemver(a);
|
|
35
|
+
const y = parseSemver(b);
|
|
36
|
+
if (!x || !y) return 0;
|
|
37
|
+
return x.major - y.major || x.minor - y.minor || x.patch - y.patch;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* @param {string} version
|
|
42
|
+
* @param {string} range
|
|
43
|
+
* @returns {boolean}
|
|
44
|
+
*/
|
|
45
|
+
export function satisfiesSimple(version, range) {
|
|
46
|
+
const v = parseSemver(version);
|
|
47
|
+
if (!v || v.pre) return false;
|
|
48
|
+
const r = String(range).trim();
|
|
49
|
+
const m = /^([~^]?)(\d+)(?:\.(\d+))?(?:\.(\d+))?$/.exec(r);
|
|
50
|
+
if (!m) return false;
|
|
51
|
+
const [, op, majS, minS, patS] = m;
|
|
52
|
+
const maj = +majS;
|
|
53
|
+
const min = minS == null ? 0 : +minS;
|
|
54
|
+
const pat = patS == null ? 0 : +patS;
|
|
55
|
+
const base = `${maj}.${min}.${pat}`;
|
|
56
|
+
if (op === '') {
|
|
57
|
+
if (minS == null || patS == null) {
|
|
58
|
+
return v.major === maj && (minS == null || v.minor === min);
|
|
59
|
+
}
|
|
60
|
+
return compareSemver(version, base) === 0;
|
|
61
|
+
}
|
|
62
|
+
if (compareSemver(version, base) < 0) return false;
|
|
63
|
+
if (op === '~') {
|
|
64
|
+
return v.major === maj && (minS == null ? true : v.minor === min);
|
|
65
|
+
}
|
|
66
|
+
// caret (major >= 1 for all @stagepass releases)
|
|
67
|
+
if (maj > 0) return v.major === maj;
|
|
68
|
+
if (min > 0) return v.major === 0 && v.minor === min;
|
|
69
|
+
return v.major === 0 && v.minor === 0 && v.patch === pat;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* @param {string[]} versions
|
|
74
|
+
* @param {string} range
|
|
75
|
+
* @returns {string | null}
|
|
76
|
+
*/
|
|
77
|
+
export function maxSatisfying(versions, range) {
|
|
78
|
+
let best = null;
|
|
79
|
+
for (const v of versions || []) {
|
|
80
|
+
if (satisfiesSimple(v, range) && (best == null || compareSemver(v, best) > 0)) best = v;
|
|
81
|
+
}
|
|
82
|
+
return best;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* @param {string} name
|
|
87
|
+
* @param {{ exec?: import('./exec.js').ExecFn, timeoutMs?: number }} [deps]
|
|
88
|
+
* @returns {Promise<{ distTags: Record<string, string>, versions: string[] } | null>}
|
|
89
|
+
*/
|
|
90
|
+
export async function fetchPublished(name, deps = {}) {
|
|
91
|
+
if (!PKG_NAME_RE.test(name)) throw new Error(`Invalid package name: ${name}`);
|
|
92
|
+
const exec = deps.exec || defaultExec;
|
|
93
|
+
let stdout;
|
|
94
|
+
try {
|
|
95
|
+
const result = await exec('npm', ['view', name, 'dist-tags', 'versions', '--json'], {
|
|
96
|
+
timeout: deps.timeoutMs ?? 15000,
|
|
97
|
+
});
|
|
98
|
+
if (result && typeof result.code === 'number' && result.code !== 0) return null;
|
|
99
|
+
stdout = String(result?.stdout || '');
|
|
100
|
+
} catch {
|
|
101
|
+
return null;
|
|
102
|
+
}
|
|
103
|
+
let data;
|
|
104
|
+
try {
|
|
105
|
+
data = JSON.parse(stdout);
|
|
106
|
+
} catch {
|
|
107
|
+
return null;
|
|
108
|
+
}
|
|
109
|
+
if (!data || typeof data !== 'object') return null;
|
|
110
|
+
const rawVersions = Array.isArray(data.versions)
|
|
111
|
+
? data.versions
|
|
112
|
+
: typeof data.versions === 'string'
|
|
113
|
+
? [data.versions]
|
|
114
|
+
: [];
|
|
115
|
+
const versions = rawVersions.filter((v) => typeof v === 'string' && parseSemver(v));
|
|
116
|
+
/** @type {Record<string, string>} */
|
|
117
|
+
const distTags = {};
|
|
118
|
+
const tags = data['dist-tags'];
|
|
119
|
+
if (tags && typeof tags === 'object') {
|
|
120
|
+
for (const [tag, v] of Object.entries(tags)) {
|
|
121
|
+
if (/^[a-z][a-z0-9_-]*$/i.test(tag) && typeof v === 'string' && parseSemver(v)) distTags[tag] = v;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
if (versions.length === 0) return null;
|
|
125
|
+
return { distTags, versions };
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* @param {string[] | null | undefined} versions
|
|
130
|
+
* @param {string} range
|
|
131
|
+
* @returns {boolean | null} null when unknown (no registry data)
|
|
132
|
+
*/
|
|
133
|
+
export function rangeHasPerModuleTypes(versions, range) {
|
|
134
|
+
if (!versions) return null;
|
|
135
|
+
const best = maxSatisfying(versions, range);
|
|
136
|
+
if (!best) return false;
|
|
137
|
+
return compareSemver(best, PER_MODULE_TYPES_SINCE) >= 0;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* @param {string} cwd
|
|
142
|
+
* @param {typeof fs} [fse]
|
|
143
|
+
* @returns {Promise<boolean | null>} null when not installed
|
|
144
|
+
*/
|
|
145
|
+
export async function installedHasPerModuleTypes(cwd, fse = fs) {
|
|
146
|
+
const dir = path.join(cwd, 'node_modules', '@stagepass', 'modules');
|
|
147
|
+
if (!(await fse.pathExists(path.join(dir, 'package.json')))) return null;
|
|
148
|
+
for (const id of MODULE_IDS) {
|
|
149
|
+
if (!(await fse.pathExists(path.join(dir, `${id}.d.ts`)))) return false;
|
|
150
|
+
}
|
|
151
|
+
return true;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* @param {string} cwd
|
|
156
|
+
* @param {typeof fs} [fse]
|
|
157
|
+
* @returns {Promise<string | null>}
|
|
158
|
+
*/
|
|
159
|
+
export async function installedLoaderVersion(cwd, fse = fs) {
|
|
160
|
+
const file = path.join(cwd, 'node_modules', '@stagepass', 'loader', 'package.json');
|
|
161
|
+
try {
|
|
162
|
+
if (!(await fse.pathExists(file))) return null;
|
|
163
|
+
const pkg = await fse.readJson(file);
|
|
164
|
+
const version = typeof pkg?.version === 'string' ? pkg.version : '';
|
|
165
|
+
return satisfiesSimple(version, PACKAGES_MAJOR_RANGE) ? version : null;
|
|
166
|
+
} catch {
|
|
167
|
+
return null;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Loader version for Webflow snippets (AC-14); never the CLI version.
|
|
173
|
+
* @param {object} options
|
|
174
|
+
* @param {{ distTags: Record<string, string>, versions: string[] } | null} options.published
|
|
175
|
+
* @param {string} options.cwd
|
|
176
|
+
* @param {string} [options.bundled]
|
|
177
|
+
* @param {typeof fs} [options.fse]
|
|
178
|
+
* @returns {Promise<string>}
|
|
179
|
+
*/
|
|
180
|
+
export async function resolveSnippetLoaderVersion({ published, cwd, bundled, fse = fs }) {
|
|
181
|
+
const best = published ? maxSatisfying(published.versions, PACKAGES_MAJOR_RANGE) : null;
|
|
182
|
+
if (best) return best;
|
|
183
|
+
return (await installedLoaderVersion(cwd, fse)) || bundled || STAGEPASS_PACKAGES_VERSION;
|
|
184
|
+
}
|
package/src/lib/scaffold.js
CHANGED
|
@@ -61,7 +61,8 @@ export function scaffoldTemplateMap(lang, styles) {
|
|
|
61
61
|
* @property {boolean} install
|
|
62
62
|
* @property {boolean} dryRun
|
|
63
63
|
* @property {string} [loaderVersion] @stagepass/loader + modules version (snippets)
|
|
64
|
-
* @property {string} [packagesRange] Override for package.json ranges (default ^<major
|
|
64
|
+
* @property {string} [packagesRange] Override for package.json ranges (default ^<major>, e.g. ^2)
|
|
65
|
+
* @property {boolean} [perModuleTypes] false: use the umbrella `@stagepass/modules` types (modules < 2.2.0)
|
|
65
66
|
*/
|
|
66
67
|
|
|
67
68
|
/**
|
|
@@ -69,9 +70,9 @@ export function scaffoldTemplateMap(lang, styles) {
|
|
|
69
70
|
* @returns {Record<string, string>}
|
|
70
71
|
*/
|
|
71
72
|
export function buildPackageJson(options) {
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
73
|
+
// Never derive the range from the CLI or target version: a release can be
|
|
74
|
+
// staged but unpublished (ETARGET). The major range matches the @2 embeds.
|
|
75
|
+
const range = options.packagesRange || packagesRange(STAGEPASS_PACKAGES_VERSION);
|
|
75
76
|
const isTs = options.lang === 'ts';
|
|
76
77
|
const useSass = options.styles === 'scss';
|
|
77
78
|
|
|
@@ -140,7 +141,10 @@ export async function planScaffoldFiles(options, deps = {}) {
|
|
|
140
141
|
version,
|
|
141
142
|
moduleList: siteModules,
|
|
142
143
|
});
|
|
143
|
-
const types = typesForModules(siteModules, {
|
|
144
|
+
const types = typesForModules(siteModules, {
|
|
145
|
+
modulesDependency: options.modules === true,
|
|
146
|
+
perModuleTypes: options.perModuleTypes,
|
|
147
|
+
});
|
|
144
148
|
|
|
145
149
|
const vars = {
|
|
146
150
|
NAME: options.name,
|
package/src/lib/site-detect.js
CHANGED
|
@@ -1,9 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Detect
|
|
3
|
-
*
|
|
4
|
-
* Public GET only: https, no cookies or auth headers, max 3 redirects,
|
|
5
|
-
* ~5 s timeout, size cap. The fetched HTML is parsed with regexes for
|
|
6
|
-
* <script src> values and is never executed.
|
|
2
|
+
* Detect Stagepass scripts on a live Webflow site.
|
|
3
|
+
* Public GET only: https, no cookies/credentials; HTML scanned, never executed.
|
|
7
4
|
*/
|
|
8
5
|
|
|
9
6
|
import { validateOriginEntry } from './origins.js';
|
|
@@ -12,26 +9,55 @@ import { MODULE_CHOICES, normalizeModulesList } from './project-modules.js';
|
|
|
12
9
|
export const DETECT_TIMEOUT_MS = 5000;
|
|
13
10
|
export const DETECT_MAX_BYTES = 2 * 1024 * 1024;
|
|
14
11
|
export const DETECT_MAX_REDIRECTS = 3;
|
|
12
|
+
export const SITE_PORT_ERROR =
|
|
13
|
+
'Site URL must not contain a port, also not :443. Use the host only, e.g. my-site.webflow.io.';
|
|
15
14
|
|
|
16
|
-
//
|
|
15
|
+
// Bound attribute length so the scan stays linear on hostile input.
|
|
17
16
|
const SCRIPT_TAG_RE = /<script\b[^<>]{0,4096}>/gi;
|
|
18
17
|
const SRC_ATTR_RE = /\ssrc\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s"'>]+))/i;
|
|
19
18
|
const STAGEPASS_SRC_RE =
|
|
20
19
|
/^(?:https?:)?\/\/(cdn\.jsdelivr\.net\/npm|unpkg\.com)\/@stagepass\/(loader|modules)(?:@([0-9A-Za-z.^~_-]+))?\/dist\/([a-z]+)(?:\.min)?\.js(?:[?#].*)?$/i;
|
|
21
20
|
|
|
22
21
|
/**
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
22
|
+
* True when the raw URL names a port (`:443`/`:0443` included).
|
|
23
|
+
* new URL drops default ports; read authority from the raw string before new URL.
|
|
24
|
+
* @param {string} input
|
|
25
|
+
* @returns {boolean}
|
|
26
|
+
*/
|
|
27
|
+
function hasExplicitPort(input) {
|
|
28
|
+
const s = String(input == null ? '' : input)
|
|
29
|
+
.replace(/^[\u0000-\u0020]+|[\u0000-\u0020]+$/g, '')
|
|
30
|
+
.replace(/[\t\n\r]/g, '')
|
|
31
|
+
.replace(/\\/g, '/');
|
|
32
|
+
const scheme = /^[a-z][a-z0-9+.-]*:/i.exec(s);
|
|
33
|
+
const rest = scheme ? s.slice(scheme[0].length) : s;
|
|
34
|
+
// No authority without `//`.
|
|
35
|
+
if (!rest.startsWith('//')) return false;
|
|
36
|
+
const authority = rest.replace(/^\/+/, '').split(/[/?#]/, 1)[0];
|
|
37
|
+
const hostPort = authority.slice(authority.lastIndexOf('@') + 1);
|
|
38
|
+
if (hostPort.startsWith('[')) {
|
|
39
|
+
const end = hostPort.indexOf(']');
|
|
40
|
+
return end === -1 || hostPort.slice(end + 1).includes(':');
|
|
41
|
+
}
|
|
42
|
+
return hostPort.includes(':');
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Normalize host/https URL; rejects http, wildcards, credentials, invalid hosts.
|
|
26
47
|
* @param {string} input
|
|
27
48
|
* @returns {{ ok: true, url: string, host: string } | { ok: false, error: string }}
|
|
28
49
|
*/
|
|
29
50
|
export function normalizeSiteUrl(input) {
|
|
30
51
|
const raw = String(input == null ? '' : input).trim();
|
|
31
52
|
if (!raw) return { ok: false, error: 'No live site given.' };
|
|
53
|
+
const candidate = /^[a-z][a-z0-9+.-]*:\/\//i.test(raw) ? raw : `https://${raw}`;
|
|
54
|
+
// Port check on raw string before new URL (default ports would be dropped).
|
|
55
|
+
if (hasExplicitPort(candidate)) {
|
|
56
|
+
return { ok: false, error: SITE_PORT_ERROR };
|
|
57
|
+
}
|
|
32
58
|
let url;
|
|
33
59
|
try {
|
|
34
|
-
url = new URL(
|
|
60
|
+
url = new URL(candidate);
|
|
35
61
|
} catch {
|
|
36
62
|
return { ok: false, error: `Invalid site URL: ${raw}` };
|
|
37
63
|
}
|
|
@@ -42,7 +68,7 @@ export function normalizeSiteUrl(input) {
|
|
|
42
68
|
return { ok: false, error: 'Site URL must not contain credentials.' };
|
|
43
69
|
}
|
|
44
70
|
if (url.port) {
|
|
45
|
-
return { ok: false, error:
|
|
71
|
+
return { ok: false, error: SITE_PORT_ERROR };
|
|
46
72
|
}
|
|
47
73
|
const host = url.hostname.toLowerCase();
|
|
48
74
|
const check = validateOriginEntry(host);
|
|
@@ -54,9 +80,7 @@ export function normalizeSiteUrl(input) {
|
|
|
54
80
|
}
|
|
55
81
|
|
|
56
82
|
/**
|
|
57
|
-
*
|
|
58
|
-
* many unterminated `<!--`). An unterminated comment drops the rest.
|
|
59
|
-
*
|
|
83
|
+
* Strip HTML comments in linear time (lazy regex is quadratic on unterminated `<!--`).
|
|
60
84
|
* @param {string} html
|
|
61
85
|
* @returns {string}
|
|
62
86
|
*/
|
|
@@ -78,8 +102,6 @@ function stripHtmlComments(html) {
|
|
|
78
102
|
}
|
|
79
103
|
|
|
80
104
|
/**
|
|
81
|
-
* Parse Stagepass loader / module script tags from HTML (no execution).
|
|
82
|
-
*
|
|
83
105
|
* @param {string} html
|
|
84
106
|
* @returns {{ loader: { version: string, cdn: string } | null, modules: { id: string, version: string, cdn: string }[] }}
|
|
85
107
|
*/
|
|
@@ -88,7 +110,6 @@ export function parseStagepassScripts(html) {
|
|
|
88
110
|
let loader = null;
|
|
89
111
|
/** @type {Map<string, { id: string, version: string, cdn: string }>} */
|
|
90
112
|
const modules = new Map();
|
|
91
|
-
// Ignore commented-out tags; the HTML is only scanned, never executed.
|
|
92
113
|
const source = stripHtmlComments(String(html || ''));
|
|
93
114
|
|
|
94
115
|
for (const match of source.matchAll(SCRIPT_TAG_RE)) {
|
|
@@ -115,7 +136,6 @@ export function parseStagepassScripts(html) {
|
|
|
115
136
|
}
|
|
116
137
|
|
|
117
138
|
/**
|
|
118
|
-
* Declared-list form of detected modules (`all` wins).
|
|
119
139
|
* @param {{ modules: { id: string }[] }} detection
|
|
120
140
|
* @returns {string[]}
|
|
121
141
|
*/
|
|
@@ -125,7 +145,6 @@ export function detectedModulesList(detection) {
|
|
|
125
145
|
}
|
|
126
146
|
|
|
127
147
|
/**
|
|
128
|
-
* Read a response body up to `maxBytes` (truncates; never throws on size).
|
|
129
148
|
* @param {Response} res
|
|
130
149
|
* @param {number} maxBytes
|
|
131
150
|
*/
|
|
@@ -153,9 +172,7 @@ async function readCapped(res, maxBytes) {
|
|
|
153
172
|
}
|
|
154
173
|
|
|
155
174
|
/**
|
|
156
|
-
*
|
|
157
|
-
*
|
|
158
|
-
* @param {string} siteInput host or https URL
|
|
175
|
+
* @param {string} siteInput
|
|
159
176
|
* @param {object} [deps]
|
|
160
177
|
* @param {typeof fetch} [deps.fetch]
|
|
161
178
|
* @param {number} [deps.timeoutMs]
|
|
@@ -200,6 +217,10 @@ export async function detectSite(siteInput, deps = {}) {
|
|
|
200
217
|
if (next.username || next.password) {
|
|
201
218
|
return { ok: false, url: current, error: 'Redirect URL contains credentials.' };
|
|
202
219
|
}
|
|
220
|
+
// Port check on raw Location before following (URL drops default ports).
|
|
221
|
+
if (next.port || hasExplicitPort(location)) {
|
|
222
|
+
return { ok: false, url: current, error: 'Redirect URL contains a port and was not followed.' };
|
|
223
|
+
}
|
|
203
224
|
current = next.toString();
|
|
204
225
|
continue;
|
|
205
226
|
}
|
|
@@ -226,7 +247,6 @@ export async function detectSite(siteInput, deps = {}) {
|
|
|
226
247
|
}
|
|
227
248
|
|
|
228
249
|
/**
|
|
229
|
-
* One-line summary of a detection result.
|
|
230
250
|
* @param {Awaited<ReturnType<typeof detectSite>>} det
|
|
231
251
|
*/
|
|
232
252
|
export function describeDetection(det) {
|
package/src/lib/snippets.js
CHANGED
|
@@ -7,23 +7,21 @@ import { STAGEPASS_PACKAGES_VERSION } from './versions.js';
|
|
|
7
7
|
* @returns {string}
|
|
8
8
|
*/
|
|
9
9
|
export function sanitizeSnippetVersion(version) {
|
|
10
|
-
const value = String(version == null ? '' : version)
|
|
11
|
-
//
|
|
12
|
-
if (!/^[
|
|
10
|
+
const value = String(version == null ? '' : version);
|
|
11
|
+
// Exact stable X.Y.Z only (no prerelease, build metadata, or ranges).
|
|
12
|
+
if (!/^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)$/.test(value)) {
|
|
13
13
|
throw new Error(
|
|
14
|
-
'Invalid version for Webflow snippets (use a
|
|
14
|
+
'Invalid version for Webflow snippets (use a stable X.Y.Z semver).'
|
|
15
15
|
);
|
|
16
16
|
}
|
|
17
17
|
return value;
|
|
18
18
|
}
|
|
19
19
|
|
|
20
20
|
/**
|
|
21
|
-
* Build recommended Webflow Custom Code snippets (jsDelivr major pin @2).
|
|
22
|
-
*
|
|
23
21
|
* @param {object} options
|
|
24
|
-
* @param {string} options.version
|
|
25
|
-
* @param {boolean} [options.modules]
|
|
26
|
-
* @param {string[]} [options.moduleList]
|
|
22
|
+
* @param {string} options.version
|
|
23
|
+
* @param {boolean} [options.modules]
|
|
24
|
+
* @param {string[]} [options.moduleList]
|
|
27
25
|
* @returns {{ head: string, footer: string, majorPin: string, exactPin: string }}
|
|
28
26
|
*/
|
|
29
27
|
export function buildWebflowSnippets(options) {
|
|
@@ -79,8 +77,7 @@ export function buildWebflowSnippets(options) {
|
|
|
79
77
|
}
|
|
80
78
|
|
|
81
79
|
/**
|
|
82
|
-
*
|
|
83
|
-
* @param {{ head: string, footer: string }} snippets
|
|
80
|
+
* @param {{ head: string, footer: string, exactPin?: string }} snippets
|
|
84
81
|
* @param {(msg: string) => void} [log]
|
|
85
82
|
*/
|
|
86
83
|
export function printWebflowSnippets(snippets, log = console.log) {
|
|
@@ -89,6 +86,6 @@ export function printWebflowSnippets(snippets, log = console.log) {
|
|
|
89
86
|
log('\nWebflow Custom Code (Footer):');
|
|
90
87
|
log(snippets.footer);
|
|
91
88
|
log(
|
|
92
|
-
`\nOptional exact pin example (instead of @2): replace @2 with the exact version (e.g.
|
|
89
|
+
`\nOptional exact pin example (instead of @2): replace @2 with the exact version (e.g. ${snippets.exactPin || `@${STAGEPASS_PACKAGES_VERSION}`}).`
|
|
93
90
|
);
|
|
94
91
|
}
|
package/src/lib/versions.js
CHANGED
|
@@ -1,27 +1,25 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Version of @stagepass/loader and @stagepass/modules this CLI targets.
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
* packages/loader and packages/modules.
|
|
4
|
+
* Used for docs and snippet examples only. Dependency ranges never come from
|
|
5
|
+
* this or the CLI version: new projects use the major range (`^2`) and
|
|
6
|
+
* `stagepass upgrade` only writes ranges a published version satisfies.
|
|
7
|
+
* A monorepo test keeps this in sync with packages/loader and packages/modules.
|
|
8
8
|
*/
|
|
9
9
|
export const STAGEPASS_PACKAGES_VERSION = '2.2.0';
|
|
10
10
|
|
|
11
11
|
/**
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
* npm resolves it once published, even while the release only carries the
|
|
16
|
-
* `next` dist-tag (latest 2.0.0 does not satisfy it).
|
|
12
|
+
* Major caret range for a version (e.g. 2.2.0 → ^2), matching the `@2` CDN
|
|
13
|
+
* embed policy. Generated projects use it so `npm install` works with
|
|
14
|
+
* whatever 2.x is published; staged but unpublished releases never break it.
|
|
17
15
|
*
|
|
18
16
|
* @param {string} [version]
|
|
19
17
|
* @returns {string}
|
|
20
18
|
*/
|
|
21
19
|
export function packagesRange(version = STAGEPASS_PACKAGES_VERSION) {
|
|
22
|
-
const match = /^[~^]?(\d+)
|
|
20
|
+
const match = /^[~^]?(\d+)(?:\.\d+){0,2}$/.exec(String(version).trim());
|
|
23
21
|
if (!match) {
|
|
24
22
|
throw new Error(`Invalid @stagepass package version: ${version}`);
|
|
25
23
|
}
|
|
26
|
-
return `^${match[1]}
|
|
24
|
+
return `^${match[1]}`;
|
|
27
25
|
}
|