@stagepass/cli 2.1.0 → 2.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +40 -7
- package/package.json +1 -1
- package/src/cli.js +11 -4
- package/src/commands/doctor.js +1 -0
- package/src/commands/new.js +152 -9
- package/src/commands/upgrade.js +218 -71
- package/src/lib/ai-guide.js +161 -0
- package/src/lib/doctor-site.js +89 -0
- package/src/lib/doctor.js +18 -0
- package/src/lib/project-modules.js +108 -0
- package/src/lib/project-sync.js +407 -0
- package/src/lib/registry.js +169 -0
- package/src/lib/scaffold.js +64 -34
- package/src/lib/site-detect.js +242 -0
- package/src/lib/snippets.js +18 -7
- package/src/lib/versions.js +25 -0
- package/templates/project/AGENTS.md.tmpl +6 -4
- package/templates/project/gitignore +7 -0
- package/templates/project/jsconfig.json.tmpl +1 -1
- package/templates/project/src/main.js.tmpl +1 -1
- package/templates/project/stagepass.json.tmpl +1 -1
- package/templates/project/tsconfig.json.tmpl +1 -1
package/src/commands/upgrade.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import fs from 'fs-extra';
|
|
2
2
|
import path from 'path';
|
|
3
|
-
import { createRequire } from 'module';
|
|
4
3
|
import chalk from 'chalk';
|
|
5
4
|
import { defaultExec } from '../lib/exec.js';
|
|
6
5
|
import {
|
|
@@ -8,18 +7,34 @@ import {
|
|
|
8
7
|
printWebflowSnippets,
|
|
9
8
|
} from '../lib/snippets.js';
|
|
10
9
|
|
|
11
|
-
|
|
12
|
-
|
|
10
|
+
import readline from 'node:readline/promises';
|
|
11
|
+
import { stdin as input, stdout as output } from 'node:process';
|
|
12
|
+
import { STAGEPASS_PACKAGES_VERSION } from '../lib/versions.js';
|
|
13
|
+
import {
|
|
14
|
+
PACKAGES_MAJOR_RANGE,
|
|
15
|
+
fetchPublished,
|
|
16
|
+
installedHasPerModuleTypes,
|
|
17
|
+
maxSatisfying,
|
|
18
|
+
rangeHasPerModuleTypes,
|
|
19
|
+
satisfiesSimple,
|
|
20
|
+
} from '../lib/registry.js';
|
|
21
|
+
import { detectSite } from '../lib/site-detect.js';
|
|
22
|
+
import {
|
|
23
|
+
applyProjectSync,
|
|
24
|
+
planProjectSync,
|
|
25
|
+
readStagepassJson,
|
|
26
|
+
resolveUpgradeModules,
|
|
27
|
+
} from '../lib/project-sync.js';
|
|
13
28
|
|
|
14
29
|
const STAGEPASS_SCOPE = '@stagepass/';
|
|
15
30
|
|
|
31
|
+
const SAFE_SPEC_RE = /^[~^]?[0-9A-Za-z][0-9A-Za-z._+-]*$/;
|
|
32
|
+
|
|
16
33
|
/**
|
|
17
|
-
* True when the version looks like an npm dist-tag
|
|
34
|
+
* True when the version looks like an npm dist-tag (bare word, e.g. latest).
|
|
18
35
|
* @param {string} version
|
|
19
36
|
*/
|
|
20
|
-
function
|
|
21
|
-
if (!version) return false;
|
|
22
|
-
// Semver-ish or range: treat as literal. Dist-tags are bare words.
|
|
37
|
+
function isDistTag(version) {
|
|
23
38
|
if (/^[0-9]/.test(version) || version.startsWith('^') || version.startsWith('~')) {
|
|
24
39
|
return false;
|
|
25
40
|
}
|
|
@@ -28,44 +43,80 @@ function needsNpmView(version) {
|
|
|
28
43
|
}
|
|
29
44
|
|
|
30
45
|
/**
|
|
31
|
-
* Resolve
|
|
32
|
-
*
|
|
46
|
+
* Resolve the range to write for one @stagepass/* package.
|
|
47
|
+
*
|
|
48
|
+
* Never writes a version or range that no published release satisfies
|
|
49
|
+
* (releases can be staged but not yet approved, which gives ETARGET):
|
|
50
|
+
* - default: `^<latest>` when latest is in the current major, else
|
|
51
|
+
* `^<highest published 2.x>`; `^2` when npm is unreachable
|
|
52
|
+
* - `--version <semver|range>`: must match a published version
|
|
53
|
+
* - `--version <dist-tag>`: the published version behind that tag
|
|
33
54
|
*
|
|
55
|
+
* @param {string} name
|
|
34
56
|
* @param {string|undefined} requested
|
|
35
|
-
* @param {
|
|
36
|
-
* @
|
|
37
|
-
* @param {string} [deps.cliVersion]
|
|
38
|
-
* @returns {Promise<string>}
|
|
57
|
+
* @param {{ distTags: Record<string, string>, versions: string[] } | null} published
|
|
58
|
+
* @returns {{ spec: string, resolved: string | null, offline: boolean }}
|
|
39
59
|
*/
|
|
40
|
-
export
|
|
41
|
-
const
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
60
|
+
export function resolvePackageTarget(name, requested, published) {
|
|
61
|
+
const value = requested == null ? '' : String(requested).trim().replace(/^v(?=\d)/i, '');
|
|
62
|
+
if (value && !SAFE_SPEC_RE.test(value)) {
|
|
63
|
+
throw new Error('Invalid --version value (use a semver, range prefix ^/~, or npm dist-tag).');
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
if (!value) {
|
|
67
|
+
if (!published) return { spec: PACKAGES_MAJOR_RANGE, resolved: null, offline: true };
|
|
68
|
+
const latest = published.distTags.latest;
|
|
69
|
+
const best =
|
|
70
|
+
latest && satisfiesSimple(latest, PACKAGES_MAJOR_RANGE)
|
|
71
|
+
? latest
|
|
72
|
+
: maxSatisfying(published.versions, PACKAGES_MAJOR_RANGE);
|
|
73
|
+
if (!best) return { spec: PACKAGES_MAJOR_RANGE, resolved: null, offline: false };
|
|
74
|
+
return { spec: `^${best}`, resolved: best, offline: false };
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (!published) {
|
|
78
|
+
throw new Error(
|
|
79
|
+
`Could not check ${name}@${value} on npm (offline?). Retry online, or run without --version to use ${PACKAGES_MAJOR_RANGE}.`
|
|
80
|
+
);
|
|
58
81
|
}
|
|
59
82
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
83
|
+
if (isDistTag(value)) {
|
|
84
|
+
const tagged = published.distTags[value];
|
|
85
|
+
if (!tagged) throw new Error(`${name} has no dist-tag "${value}".`);
|
|
86
|
+
return { spec: tagged, resolved: tagged, offline: false };
|
|
64
87
|
}
|
|
65
|
-
|
|
66
|
-
|
|
88
|
+
|
|
89
|
+
const best = maxSatisfying(published.versions, value);
|
|
90
|
+
if (!best) {
|
|
91
|
+
throw new Error(
|
|
92
|
+
`No published ${name} matches ${value} (staged releases cannot be installed until they are approved).`
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
return { spec: value, resolved: best, offline: false };
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Resolve target ranges for every @stagepass/* dependency in package.json.
|
|
100
|
+
*
|
|
101
|
+
* @param {string|undefined} requested `--version`
|
|
102
|
+
* @param {string[]} names package names
|
|
103
|
+
* @param {object} [deps]
|
|
104
|
+
* @param {(name: string) => Promise<{ distTags: Record<string, string>, versions: string[] } | null>} [deps.fetchPublished]
|
|
105
|
+
* @param {import('../lib/exec.js').ExecFn} [deps.exec]
|
|
106
|
+
* @returns {Promise<{ targets: Record<string, { spec: string, resolved: string | null, offline: boolean }>, published: Record<string, { distTags: Record<string, string>, versions: string[] } | null> }>}
|
|
107
|
+
*/
|
|
108
|
+
export async function resolveUpgradeTargets(requested, names, deps = {}) {
|
|
109
|
+
const lookup =
|
|
110
|
+
deps.fetchPublished || ((name) => fetchPublished(name, { exec: deps.exec || defaultExec }));
|
|
111
|
+
/** @type {Record<string, any>} */
|
|
112
|
+
const targets = {};
|
|
113
|
+
/** @type {Record<string, any>} */
|
|
114
|
+
const published = {};
|
|
115
|
+
for (const name of [...new Set(names)]) {
|
|
116
|
+
published[name] = await lookup(name);
|
|
117
|
+
targets[name] = resolvePackageTarget(name, requested, published[name]);
|
|
67
118
|
}
|
|
68
|
-
return
|
|
119
|
+
return { targets, published };
|
|
69
120
|
}
|
|
70
121
|
|
|
71
122
|
/**
|
|
@@ -91,17 +142,17 @@ export function findStagepassDeps(pkg) {
|
|
|
91
142
|
/**
|
|
92
143
|
* Rewrite @stagepass/* versions in a package.json object (mutates a copy).
|
|
93
144
|
* @param {object} pkg
|
|
94
|
-
* @param {string}
|
|
145
|
+
* @param {string | Record<string, string>} target one spec for all, or per package name
|
|
95
146
|
* @returns {{ next: object, changes: { name: string, from: string, to: string, section: string }[] }}
|
|
96
147
|
*/
|
|
97
|
-
export function applyStagepassVersions(pkg,
|
|
148
|
+
export function applyStagepassVersions(pkg, target) {
|
|
98
149
|
const next = structuredClone(pkg);
|
|
99
150
|
const changes = [];
|
|
100
|
-
const target = String(targetVersion);
|
|
101
151
|
|
|
102
152
|
for (const { section, name, from } of findStagepassDeps(pkg)) {
|
|
103
153
|
if (!next[section]) continue;
|
|
104
|
-
const to = target;
|
|
154
|
+
const to = typeof target === 'string' ? target : target[name];
|
|
155
|
+
if (!to) continue;
|
|
105
156
|
if (String(from) !== to) {
|
|
106
157
|
changes.push({ name, from: String(from), to, section });
|
|
107
158
|
}
|
|
@@ -112,16 +163,22 @@ export function applyStagepassVersions(pkg, targetVersion) {
|
|
|
112
163
|
}
|
|
113
164
|
|
|
114
165
|
/**
|
|
115
|
-
* Upgrade @stagepass/* deps in the current project's package.json
|
|
166
|
+
* Upgrade @stagepass/* deps in the current project's package.json and sync
|
|
167
|
+
* the declared modules into stagepass.json, AGENTS.md, the Cursor rule and
|
|
168
|
+
* tsconfig/jsconfig types.
|
|
116
169
|
*
|
|
117
170
|
* @param {object} [options]
|
|
118
171
|
* @param {boolean} [options.dryRun]
|
|
119
172
|
* @param {string} [options.version]
|
|
173
|
+
* @param {string} [options.site] live Webflow host to detect modules from
|
|
174
|
+
* @param {string} [options.siteModules] none | all | comma list
|
|
175
|
+
* @param {boolean} [options.skipSite]
|
|
176
|
+
* @param {boolean} [options.yes]
|
|
120
177
|
* @param {object} [deps]
|
|
121
178
|
* @param {typeof fs} [deps.fs]
|
|
122
179
|
* @param {string} [deps.cwd]
|
|
123
180
|
* @param {import('../lib/exec.js').ExecFn} [deps.exec]
|
|
124
|
-
* @param {string} [deps.
|
|
181
|
+
* @param {(name: string) => Promise<{ distTags: Record<string, string>, versions: string[] } | null>} [deps.fetchPublished]
|
|
125
182
|
* @param {(msg?: string) => void} [deps.log]
|
|
126
183
|
* @returns {Promise<number>} exit code
|
|
127
184
|
*/
|
|
@@ -136,17 +193,6 @@ export async function upgrade(options = {}, deps = {}) {
|
|
|
136
193
|
return 1;
|
|
137
194
|
}
|
|
138
195
|
|
|
139
|
-
let targetVersion;
|
|
140
|
-
try {
|
|
141
|
-
targetVersion = await resolveUpgradeVersion(options.version, {
|
|
142
|
-
exec: deps.exec || defaultExec,
|
|
143
|
-
cliVersion: deps.cliVersion || cliVersion,
|
|
144
|
-
});
|
|
145
|
-
} catch (error) {
|
|
146
|
-
log(chalk.red(error instanceof Error ? error.message : String(error)));
|
|
147
|
-
return 1;
|
|
148
|
-
}
|
|
149
|
-
|
|
150
196
|
const raw = await fse.readFile(pkgPath, 'utf8');
|
|
151
197
|
let pkg;
|
|
152
198
|
try {
|
|
@@ -156,38 +202,139 @@ export async function upgrade(options = {}, deps = {}) {
|
|
|
156
202
|
return 1;
|
|
157
203
|
}
|
|
158
204
|
|
|
159
|
-
const
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
205
|
+
const names = findStagepassDeps(pkg).map((d) => d.name);
|
|
206
|
+
let resolvedTargets;
|
|
207
|
+
try {
|
|
208
|
+
resolvedTargets = await resolveUpgradeTargets(options.version, names, {
|
|
209
|
+
exec: deps.exec || defaultExec,
|
|
210
|
+
fetchPublished: deps.fetchPublished,
|
|
211
|
+
});
|
|
212
|
+
} catch (error) {
|
|
213
|
+
log(chalk.red(error instanceof Error ? error.message : String(error)));
|
|
214
|
+
return 1;
|
|
215
|
+
}
|
|
216
|
+
const { targets, published } = resolvedTargets;
|
|
217
|
+
/** @type {Record<string, string>} */
|
|
218
|
+
const specs = Object.fromEntries(Object.entries(targets).map(([n, t]) => [n, t.spec]));
|
|
219
|
+
if (Object.values(targets).some((t) => t.offline)) {
|
|
220
|
+
log(chalk.yellow(`npm registry not reachable: using ${PACKAGES_MAJOR_RANGE} (resolves to the newest published 2.x).`));
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
const { next, changes } = applyStagepassVersions(pkg, specs);
|
|
164
224
|
|
|
165
225
|
if (changes.length === 0) {
|
|
166
|
-
|
|
167
|
-
if (existing.length === 0) {
|
|
226
|
+
if (names.length === 0) {
|
|
168
227
|
log(chalk.yellow('No @stagepass/* packages found in package.json.'));
|
|
169
228
|
} else {
|
|
170
|
-
log(chalk.green(
|
|
229
|
+
log(chalk.green('@stagepass/* already at the newest published ranges.'));
|
|
171
230
|
}
|
|
172
231
|
} else {
|
|
173
|
-
log(chalk.bold(
|
|
232
|
+
log(chalk.bold('Upgrade @stagepass/* (published versions only)'));
|
|
174
233
|
for (const change of changes) {
|
|
175
234
|
log(` ${change.name}: ${change.from} → ${change.to} (${change.section})`);
|
|
176
235
|
}
|
|
236
|
+
}
|
|
177
237
|
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
238
|
+
// Per-module types need @stagepass/modules >= 2.2.0 resolvable from the range.
|
|
239
|
+
/** @type {boolean | undefined} */
|
|
240
|
+
let perModuleTypes;
|
|
241
|
+
const modulesSpec =
|
|
242
|
+
next?.dependencies?.['@stagepass/modules'] || next?.devDependencies?.['@stagepass/modules'];
|
|
243
|
+
if (modulesSpec) {
|
|
244
|
+
const fromRegistry = rangeHasPerModuleTypes(
|
|
245
|
+
published['@stagepass/modules'] ? published['@stagepass/modules'].versions : null,
|
|
246
|
+
String(modulesSpec)
|
|
247
|
+
);
|
|
248
|
+
perModuleTypes =
|
|
249
|
+
fromRegistry != null ? fromRegistry : (await installedHasPerModuleTypes(cwd, fse)) === true;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
// Declared modules: live site first, then stagepass.json, then ask.
|
|
253
|
+
const sp = await readStagepassJson(cwd, fse);
|
|
254
|
+
if (sp.error) log(chalk.yellow(sp.error));
|
|
255
|
+
const isTty = deps.isTTY != null ? Boolean(deps.isTTY) : Boolean(input.isTTY && output.isTTY);
|
|
256
|
+
const interactive = isTty && options.yes !== true;
|
|
257
|
+
/** @type {import('node:readline/promises').Interface | null} */
|
|
258
|
+
let rl = null;
|
|
259
|
+
const getRl = () => {
|
|
260
|
+
if (!rl) rl = deps.createReadline?.() || readline.createInterface({ input, output });
|
|
261
|
+
return rl;
|
|
262
|
+
};
|
|
263
|
+
let resolved;
|
|
264
|
+
try {
|
|
265
|
+
resolved = await resolveUpgradeModules(
|
|
266
|
+
{ cwd, data: sp.data, pkg: next, options },
|
|
267
|
+
{
|
|
268
|
+
detectSite: deps.detectSite || detectSite,
|
|
269
|
+
loadProjects: deps.loadProjects,
|
|
270
|
+
interactive,
|
|
271
|
+
log,
|
|
272
|
+
ask: async (question, def) => {
|
|
273
|
+
const answer = await getRl().question(`${question} (${def}): `);
|
|
274
|
+
const trimmed = String(answer || '').trim();
|
|
275
|
+
return trimmed === '' ? def : trimmed;
|
|
276
|
+
},
|
|
277
|
+
confirm: async (question, defYes) => {
|
|
278
|
+
const answer = await getRl().question(`${question} (${defYes ? 'Y/n' : 'y/N'}): `);
|
|
279
|
+
const t = String(answer || '').trim().toLowerCase();
|
|
280
|
+
if (t === '') return defYes;
|
|
281
|
+
return ['y', 'yes'].includes(t);
|
|
282
|
+
},
|
|
283
|
+
}
|
|
284
|
+
);
|
|
285
|
+
} finally {
|
|
286
|
+
if (rl) rl.close();
|
|
287
|
+
}
|
|
288
|
+
if (!resolved.ok) {
|
|
289
|
+
log(chalk.red(resolved.error));
|
|
290
|
+
return 1;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
const plan = await planProjectSync(
|
|
294
|
+
{
|
|
295
|
+
cwd,
|
|
296
|
+
modules: resolved.modules,
|
|
297
|
+
origin: options.site ? resolved.origin : null,
|
|
298
|
+
pkg: next,
|
|
299
|
+
perModuleTypes,
|
|
300
|
+
},
|
|
301
|
+
fse
|
|
302
|
+
);
|
|
303
|
+
|
|
304
|
+
log(chalk.bold(`\nProject files for modules: ${resolved.modules.length ? resolved.modules.join(', ') : 'none'}`));
|
|
305
|
+
if (plan.writes.length === 0) {
|
|
306
|
+
log(' Everything is up to date.');
|
|
307
|
+
}
|
|
308
|
+
for (const w of plan.writes) {
|
|
309
|
+
log(` ${w.file}: ${w.action}`);
|
|
310
|
+
}
|
|
311
|
+
for (const note of plan.notes) {
|
|
312
|
+
log(chalk.yellow(` note: ${note}`));
|
|
313
|
+
}
|
|
314
|
+
if (perModuleTypes === false && resolved.modules.length > 0 && !resolved.modules.includes('all')) {
|
|
315
|
+
log(
|
|
316
|
+
chalk.yellow(
|
|
317
|
+
' 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.'
|
|
318
|
+
)
|
|
319
|
+
);
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
if (options.dryRun) {
|
|
323
|
+
log(chalk.dim('\nDry run: no files were modified.'));
|
|
324
|
+
} else {
|
|
325
|
+
if (changes.length > 0) {
|
|
181
326
|
const trailingNewline = raw.endsWith('\n');
|
|
182
|
-
const
|
|
183
|
-
await fse.writeFile(pkgPath,
|
|
327
|
+
const pkgBody = `${JSON.stringify(next, null, 2)}${trailingNewline ? '\n' : ''}`;
|
|
328
|
+
await fse.writeFile(pkgPath, pkgBody, 'utf8');
|
|
184
329
|
log(chalk.green('\nUpdated package.json.'));
|
|
185
330
|
}
|
|
331
|
+
await applyProjectSync(cwd, plan.writes, fse);
|
|
332
|
+
if (plan.writes.length > 0) log(chalk.green(`Updated ${plan.writes.length} project file(s).`));
|
|
186
333
|
}
|
|
187
334
|
|
|
188
335
|
const snippets = buildWebflowSnippets({
|
|
189
|
-
version:
|
|
190
|
-
|
|
336
|
+
version: targets['@stagepass/loader']?.resolved || STAGEPASS_PACKAGES_VERSION,
|
|
337
|
+
moduleList: resolved.modules,
|
|
191
338
|
});
|
|
192
339
|
printWebflowSnippets(snippets, log);
|
|
193
340
|
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generated AI guidance (AGENTS.md managed block + Cursor rule).
|
|
3
|
+
*
|
|
4
|
+
* Content depends on the declared modules so agents only see APIs that
|
|
5
|
+
* actually run on the Webflow site.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { MODULE_IDS, expandModules, formatModules } from './project-modules.js';
|
|
9
|
+
|
|
10
|
+
export const BLOCK_BEGIN = '<!-- stagepass:begin -->';
|
|
11
|
+
export const BLOCK_END = '<!-- stagepass:end -->';
|
|
12
|
+
export const CURSOR_RULE_PATH = '.cursor/rules/stagepass.mdc';
|
|
13
|
+
|
|
14
|
+
const LOADER_SECTION = `### Loader API (always available)
|
|
15
|
+
|
|
16
|
+
\`sp\` (same object as \`window.stagepass\`) is created by \`loader.min.js\` in the Webflow head before your bundle runs.
|
|
17
|
+
|
|
18
|
+
| API | Returns | Notes |
|
|
19
|
+
| --- | --- | --- |
|
|
20
|
+
| \`sp.vars\` | frozen object | \`isLocal\`, \`env\`, \`domain\`, \`timestamp\`, \`version\`, \`client\` |
|
|
21
|
+
| \`sp.env()\` | \`'local' \\| 'staging' \\| 'production'\` | \`local\` = active session to a \`.sp\` domain; \`staging\` = \`*.webflow.io\` or \`data-stagepass-staging-hosts\` |
|
|
22
|
+
| \`sp.is(name)\` | \`boolean\` | \`true\` when \`sp.env() === name\` |
|
|
23
|
+
| \`sp.can(cap)\` | \`boolean\` | \`'localStorage' \\| 'sessionStorage' \\| 'cookies' \\| 'serviceWorker'\`; anything else is \`false\` |
|
|
24
|
+
| \`sp.client(key?)\` | \`unknown\` | key of \`sp.vars.client\` (\`theme\`, \`browser\`, \`os\`, \`device\`, \`viewport\`, \`locale\`, \`capabilities\`), or the whole object |
|
|
25
|
+
| \`sp.log(...)\`, \`sp.warn(...)\`, \`sp.error(...)\` | \`void\` | prefixed console output, only while a Stagepass session is active |
|
|
26
|
+
| \`sp.resolveLocalUrl(path)\` | \`string\` | local HTTPS URL during a session, \`''\` otherwise |
|
|
27
|
+
| \`sp.on('theme', fn)\` | unsubscribe \`() => void\` | payload \`{ preferred, current }\` on color scheme changes |
|
|
28
|
+
| \`sp.on('session', fn)\` | unsubscribe \`() => void\` | payload \`{ active, domain, swap, debug }\` |
|
|
29
|
+
| \`sp.use(name, version, factory)\` | \`boolean\` | module registration (used by @stagepass/modules, not needed in app code) |
|
|
30
|
+
|
|
31
|
+
\`\`\`ts
|
|
32
|
+
if (sp.is('local')) sp.log('running against the local dev server');
|
|
33
|
+
const off = sp.on('theme', (payload) => {
|
|
34
|
+
const { current } = payload as { current: string };
|
|
35
|
+
document.documentElement.dataset.theme = current;
|
|
36
|
+
});
|
|
37
|
+
if (sp.can('localStorage')) localStorage.setItem('seen', '1');
|
|
38
|
+
\`\`\``;
|
|
39
|
+
|
|
40
|
+
const MODULE_SECTIONS = {
|
|
41
|
+
inject: `### Module: inject (\`sp.inject\`)
|
|
42
|
+
|
|
43
|
+
Loads extra scripts and stylesheets. Webflow script tag: \`@stagepass/modules@2/dist/inject.min.js\` after \`loader.min.js\`.
|
|
44
|
+
|
|
45
|
+
\`\`\`ts
|
|
46
|
+
if (sp.inject) {
|
|
47
|
+
await sp.inject({ src: 'https://cdn.example.com/lib.js', defer: true, stagepass: false });
|
|
48
|
+
await sp.inject({ src: 'https://cdn.example.com/site/widgets.js', localPath: 'dist/widgets.js' });
|
|
49
|
+
await sp.inject({ scripts: [{ src: 'https://cdn.example.com/a.js' }], styles: [{ src: 'https://cdn.example.com/a.css' }] });
|
|
50
|
+
}
|
|
51
|
+
\`\`\`
|
|
52
|
+
|
|
53
|
+
Argument: one options object, an array of them, or \`{ scripts?, styles? }\`. Returns \`Promise<void>\`.
|
|
54
|
+
Options: \`src\` (required), \`type\` (\`'script' \\| 'style'\`), \`stagepass\` (default true: during a session the file is served from the local project via \`localPath\`/\`path\` or the file name; \`false\` always uses \`src\`), \`localPath\`, \`path\`, \`id\`, \`async\`, \`defer\`, \`version\` (adds \`?v=\` in production), \`attributes\`, \`position\` (\`'head' \\| 'body-start' \\| 'body-end' \\| { target, action: 'before' \\| 'after' }\`), \`integrity\`, \`crossorigin\`, \`nonce\`, \`allowedHosts\`.`,
|
|
55
|
+
cookies: `### Module: cookies (\`sp.cookies\`, alias \`sp.cookie\`, plus \`sp.consent\`)
|
|
56
|
+
|
|
57
|
+
Webflow script tag: \`@stagepass/modules@2/dist/cookies.min.js\` after \`loader.min.js\`.
|
|
58
|
+
|
|
59
|
+
\`\`\`ts
|
|
60
|
+
if (sp.cookies) {
|
|
61
|
+
sp.cookies.bake('visited', '1', { days: 30, sameSite: 'Lax' });
|
|
62
|
+
const prefs = sp.cookies.eat('prefs', { json: true }); // value or null
|
|
63
|
+
if (sp.cookies.has('visited')) sp.cookies.throw('visited');
|
|
64
|
+
sp.cookies.bake('ga', 'x', { category: 'analytics' }); // queued until consent allows it
|
|
65
|
+
}
|
|
66
|
+
if (sp.consent) {
|
|
67
|
+
const accepted = ['necessary', 'analytics']; // from your consent banner
|
|
68
|
+
sp.consent.set((category) => accepted.includes(category));
|
|
69
|
+
sp.consent.update(); // flush queued cookies against the consent function
|
|
70
|
+
}
|
|
71
|
+
\`\`\`
|
|
72
|
+
|
|
73
|
+
Cookies: \`bake(name, value, options?)\`, \`eat(name, { decode?, json? })\` (value or \`null\`), \`throw(name, { path?, domain?, secure?, sameSite? })\`, \`has(name)\`, \`list({ decode?, json? })\`, \`throwAll({ category? })\`, \`jar()\`, \`crumbs(prefix)\`.
|
|
74
|
+
Write options: \`days\`, \`maxAge\`, \`path\`, \`domain\`, \`secure\`, \`sameSite\`, \`encode\`, \`json\`, \`category\`.
|
|
75
|
+
Consent: \`allows(category)\`, \`set(fn)\`, \`update()\`.`,
|
|
76
|
+
maintenance: `### Module: maintenance (\`sp.maintenance\`)
|
|
77
|
+
|
|
78
|
+
Webflow script tag: \`@stagepass/modules@2/dist/maintenance.min.js\` after \`loader.min.js\` (optional attributes \`data-stagepass-maintenance="true"\`, \`data-stagepass-maintenance-title\`, \`data-stagepass-maintenance-text\`).
|
|
79
|
+
|
|
80
|
+
\`\`\`ts
|
|
81
|
+
if (sp.maintenance && !sp.maintenance.isActive()) {
|
|
82
|
+
sp.maintenance.enable({ title: 'Back soon', text: 'We are updating the site.' });
|
|
83
|
+
}
|
|
84
|
+
\`\`\`
|
|
85
|
+
|
|
86
|
+
API: \`enable({ title?, text? })\`, \`disable()\`, \`isActive()\`. Developer sessions see a badge instead of the overlay. This is a visual overlay, not access protection.`,
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
function unavailableLine(id) {
|
|
90
|
+
return `- \`sp.${id}\`: not available; enable by adding the \`${id}.min.js\` script tag after \`loader.min.js\` in Webflow, adding \`"${id}"\` to \`modules\` in stagepass.json, and running \`stagepass upgrade\`.`;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Inner Markdown for the managed block (without markers).
|
|
95
|
+
* @param {string[]} modules normalized declared list
|
|
96
|
+
*/
|
|
97
|
+
export function buildGuideBody(modules) {
|
|
98
|
+
const enabled = expandModules(modules);
|
|
99
|
+
const disabled = MODULE_IDS.filter((id) => !enabled.includes(id));
|
|
100
|
+
const parts = [
|
|
101
|
+
'## Stagepass runtime API',
|
|
102
|
+
'',
|
|
103
|
+
`Generated by \`stagepass upgrade\` from \`stagepass.json\` (modules: ${formatModules(modules)}). Do not edit inside this block; it is replaced on upgrade.`,
|
|
104
|
+
'',
|
|
105
|
+
'**Rule for agents:** only use the `sp.*` APIs listed below. Never call `sp.<module>` APIs that are not listed as available. If a task needs one, tell the user to enable that module first (Webflow script tag, stagepass.json, `stagepass upgrade`). Guard optional modules (`if (sp.cookies) { ... }`).',
|
|
106
|
+
'',
|
|
107
|
+
LOADER_SECTION,
|
|
108
|
+
];
|
|
109
|
+
for (const id of enabled) {
|
|
110
|
+
parts.push('', MODULE_SECTIONS[id]);
|
|
111
|
+
}
|
|
112
|
+
if (disabled.length > 0) {
|
|
113
|
+
parts.push('', '### Modules not enabled in this project', '');
|
|
114
|
+
for (const id of disabled) parts.push(unavailableLine(id));
|
|
115
|
+
}
|
|
116
|
+
return parts.join('\n');
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** Full managed block including markers. */
|
|
120
|
+
export function buildManagedBlock(modules) {
|
|
121
|
+
return `${BLOCK_BEGIN}\n${buildGuideBody(modules)}\n${BLOCK_END}`;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Insert or replace the managed block in existing content.
|
|
126
|
+
* Content outside the markers is preserved byte for byte.
|
|
127
|
+
*
|
|
128
|
+
* @param {string} content
|
|
129
|
+
* @param {string} block full block incl. markers
|
|
130
|
+
* @returns {{ content: string, action: 'inserted' | 'updated' | 'unchanged' | 'invalid' }}
|
|
131
|
+
*/
|
|
132
|
+
export function upsertManagedBlock(content, block) {
|
|
133
|
+
const begin = content.indexOf(BLOCK_BEGIN);
|
|
134
|
+
const end = content.indexOf(BLOCK_END);
|
|
135
|
+
if (begin === -1 && end === -1) {
|
|
136
|
+
const sep = content.length === 0 ? '' : content.endsWith('\n\n') ? '' : content.endsWith('\n') ? '\n' : '\n\n';
|
|
137
|
+
return { content: `${content}${sep}${block}\n`, action: 'inserted' };
|
|
138
|
+
}
|
|
139
|
+
if (begin === -1 || end === -1 || end < begin) {
|
|
140
|
+
return { content, action: 'invalid' };
|
|
141
|
+
}
|
|
142
|
+
const next = content.slice(0, begin) + block + content.slice(end + BLOCK_END.length);
|
|
143
|
+
return { content: next, action: next === content ? 'unchanged' : 'updated' };
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Cursor project rule (.cursor/rules/stagepass.mdc).
|
|
148
|
+
* @param {string[]} modules
|
|
149
|
+
*/
|
|
150
|
+
export function buildCursorRule(modules) {
|
|
151
|
+
return [
|
|
152
|
+
'---',
|
|
153
|
+
'description: Stagepass runtime API (sp) for this Webflow project',
|
|
154
|
+
'globs: src/**',
|
|
155
|
+
'alwaysApply: false',
|
|
156
|
+
'---',
|
|
157
|
+
'',
|
|
158
|
+
buildManagedBlock(modules),
|
|
159
|
+
'',
|
|
160
|
+
].join('\n');
|
|
161
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Doctor checks for the current project folder: declared modules in
|
|
3
|
+
* stagepass.json vs. the Stagepass scripts the live Webflow site loads.
|
|
4
|
+
* Read-only: one public GET of the live page, parsed and never executed.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import path from 'node:path';
|
|
8
|
+
import fs from 'fs-extra';
|
|
9
|
+
import { detectSite, describeDetection } from './site-detect.js';
|
|
10
|
+
import { formatModules, sameModules } from './project-modules.js';
|
|
11
|
+
import { declaredModules, readStagepassJson, resolveProjectOrigin } from './project-sync.js';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* @param {object} [opts]
|
|
15
|
+
* @param {string} [opts.cwd]
|
|
16
|
+
* @param {boolean} [opts.skipSite]
|
|
17
|
+
* @param {(site: string) => Promise<any>} [opts.detectSite]
|
|
18
|
+
* @param {Function} [opts.loadProjects]
|
|
19
|
+
* @returns {Promise<{ id: string, status: 'ok'|'warn'|'fail'|'info', message: string, detail?: string }[]>}
|
|
20
|
+
*/
|
|
21
|
+
export async function runProjectSiteChecks(opts = {}) {
|
|
22
|
+
const cwd = opts.cwd || process.cwd();
|
|
23
|
+
if (!(await fs.pathExists(path.join(cwd, 'stagepass.json')))) return [];
|
|
24
|
+
|
|
25
|
+
const checks = [];
|
|
26
|
+
const sp = await readStagepassJson(cwd);
|
|
27
|
+
if (!sp.data) {
|
|
28
|
+
checks.push({ id: 'project.config', status: 'warn', message: sp.error || 'stagepass.json unreadable' });
|
|
29
|
+
return checks;
|
|
30
|
+
}
|
|
31
|
+
const declared = declaredModules(sp.data);
|
|
32
|
+
if (!declared) {
|
|
33
|
+
checks.push({
|
|
34
|
+
id: 'project.modules',
|
|
35
|
+
status: 'warn',
|
|
36
|
+
message: 'stagepass.json has no modules list (["cookies"], ["all"] or [])',
|
|
37
|
+
detail: 'Run: stagepass upgrade',
|
|
38
|
+
});
|
|
39
|
+
} else {
|
|
40
|
+
checks.push({ id: 'project.modules', status: 'ok', message: `Declared modules: ${formatModules(declared)}` });
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
if (opts.skipSite) return checks;
|
|
44
|
+
const found = await resolveProjectOrigin(cwd, sp.data, { loadProjects: opts.loadProjects });
|
|
45
|
+
if (!found) {
|
|
46
|
+
checks.push({
|
|
47
|
+
id: 'project.site',
|
|
48
|
+
status: 'info',
|
|
49
|
+
message: 'No live site known for this project',
|
|
50
|
+
detail: 'Add "origin" to stagepass.json or run stagepass link <name> --origin <host>',
|
|
51
|
+
});
|
|
52
|
+
return checks;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const det = await (opts.detectSite || detectSite)(found.origin);
|
|
56
|
+
if (!det.ok) {
|
|
57
|
+
checks.push({
|
|
58
|
+
id: 'project.site',
|
|
59
|
+
status: 'warn',
|
|
60
|
+
message: `Live site ${found.origin} could not be checked`,
|
|
61
|
+
detail: det.error,
|
|
62
|
+
});
|
|
63
|
+
return checks;
|
|
64
|
+
}
|
|
65
|
+
if (!det.loader) {
|
|
66
|
+
checks.push({
|
|
67
|
+
id: 'project.site',
|
|
68
|
+
status: 'warn',
|
|
69
|
+
message: `Live site ${found.origin} does not load @stagepass/loader`,
|
|
70
|
+
detail: describeDetection(det),
|
|
71
|
+
});
|
|
72
|
+
return checks;
|
|
73
|
+
}
|
|
74
|
+
checks.push({
|
|
75
|
+
id: 'project.site',
|
|
76
|
+
status: 'ok',
|
|
77
|
+
message: `Live site ${found.origin} loads @stagepass/loader@${det.loader.version}`,
|
|
78
|
+
detail: describeDetection(det),
|
|
79
|
+
});
|
|
80
|
+
if (declared && !sameModules(declared, det.list)) {
|
|
81
|
+
checks.push({
|
|
82
|
+
id: 'project.site.modules',
|
|
83
|
+
status: 'warn',
|
|
84
|
+
message: `Modules differ: stagepass.json ${formatModules(declared)}, live site ${formatModules(det.list)}`,
|
|
85
|
+
detail: 'Run stagepass upgrade to sync, or change the Webflow script tags.',
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
return checks;
|
|
89
|
+
}
|
package/src/lib/doctor.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import fs from 'fs-extra';
|
|
2
|
+
import { runProjectSiteChecks } from './doctor-site.js';
|
|
2
3
|
import path from 'node:path';
|
|
3
4
|
import { defaultExec, resolveCommand } from './exec.js';
|
|
4
5
|
import {
|
|
@@ -484,6 +485,23 @@ export async function runDoctor(opts = {}) {
|
|
|
484
485
|
|
|
485
486
|
hints.push(CHROME_LNA_HINT);
|
|
486
487
|
|
|
488
|
+
// Current project folder: declared modules vs. live site (read-only GET)
|
|
489
|
+
if (opts.projectChecks !== false) {
|
|
490
|
+
try {
|
|
491
|
+
const projectChecks = await runProjectSiteChecks({
|
|
492
|
+
cwd: opts.cwd,
|
|
493
|
+
skipSite: opts.skipSite === true,
|
|
494
|
+
detectSite: opts.detectSite,
|
|
495
|
+
loadProjects: opts.loadProjects,
|
|
496
|
+
});
|
|
497
|
+
for (const c of projectChecks) checks.push(check(c.id, c.status, c.message, c.detail));
|
|
498
|
+
} catch (error) {
|
|
499
|
+
checks.push(
|
|
500
|
+
check('project', 'warn', 'Project check failed', error instanceof Error ? error.message : String(error))
|
|
501
|
+
);
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
|
|
487
505
|
const summary = { ok: 0, warn: 0, fail: 0, info: 0 };
|
|
488
506
|
for (const c of checks) {
|
|
489
507
|
if (c.status === 'ok') summary.ok += 1;
|