@stagepass/cli 2.1.0 → 2.2.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.
- 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 +96 -9
- package/src/commands/upgrade.js +96 -18
- 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 +98 -0
- package/src/lib/project-sync.js +394 -0
- package/src/lib/scaffold.js +60 -34
- package/src/lib/site-detect.js +242 -0
- package/src/lib/snippets.js +18 -7
- package/src/lib/versions.js +27 -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/stagepass.json.tmpl +1 -1
- package/templates/project/tsconfig.json.tmpl +1 -1
package/README.md
CHANGED
|
@@ -54,7 +54,7 @@ Linked: https://my-project.sp -> /absolute/path/to/project
|
|
|
54
54
|
| `stagepass unlink [domain]` | Remove the site block and `projects.json` entry, reload Caddy. |
|
|
55
55
|
| `stagepass reload` | Reload Caddy configuration. Migrates legacy Caddyfile blocks if needed. |
|
|
56
56
|
| `stagepass stop` | Stop background services. Prints a hint when login autostart is enabled. |
|
|
57
|
-
| `stagepass doctor` | Read-only diagnostics (Homebrew, Caddy, PHP/php-fpm, dnsmasq, resolver, port 443, Caddyfile, projects, certs, LaunchAgent). Never uses sudo. `--json
|
|
57
|
+
| `stagepass doctor` | Read-only diagnostics (Homebrew, Caddy, PHP/php-fpm, dnsmasq, resolver, port 443, Caddyfile, projects, certs, LaunchAgent, plus the current project's declared modules vs. its live site). Never uses sudo. `--json`, `--skip-site`. Exit `1` if any fail. |
|
|
58
58
|
| `stagepass certs check` | Read-only Caddy local CA check (validity, System keychain trust, orphans, optional TLS handshake per linked domain). `--json`, `--skip-handshake`. |
|
|
59
59
|
| `stagepass certs trust` | Run `caddy trust` after interactive confirmation (or `--yes`). |
|
|
60
60
|
| `stagepass autostart on\|off\|status` | Manage a user LaunchAgent (`io.github.arobertherz.stagepass`) that runs `stagepass daemon` at login. |
|
|
@@ -62,7 +62,7 @@ Linked: https://my-project.sp -> /absolute/path/to/project
|
|
|
62
62
|
| `stagepass new [name]` | Scaffold a lean Webflow project (TypeScript + SCSS by default). |
|
|
63
63
|
| `stagepass build` | Run `npm run build` in the current project (forwards exit code). |
|
|
64
64
|
| `stagepass watch` | Run `npm run watch` in the current project (forwards exit code). |
|
|
65
|
-
| `stagepass upgrade` | Bump `@stagepass
|
|
65
|
+
| `stagepass upgrade` | Bump `@stagepass/*`, sync declared modules, `AGENTS.md`, the Cursor rule and `types`, print Webflow snippets. |
|
|
66
66
|
|
|
67
67
|
All commands support `-v, --verbose` where applicable.
|
|
68
68
|
|
|
@@ -84,23 +84,56 @@ Scaffold a project with flags for every choice (interactive prompts only on a TT
|
|
|
84
84
|
| `--author <name>` | `package.json` author (default: `git config user.name`). |
|
|
85
85
|
| `--lang ts\|js` | Language (default: `ts`). |
|
|
86
86
|
| `--styles scss\|css` | Styles (default: `scss`). |
|
|
87
|
-
| `--modules` / `--no-modules` | Include `@stagepass/modules` (default: yes). |
|
|
87
|
+
| `--modules` / `--no-modules` | Include `@stagepass/modules` as npm dependency (default: yes; forced on when `--site-modules` lists a module). |
|
|
88
|
+
| `--site <host>` | Live Webflow site (https) to detect the loaded modules from. Stored as the project origin. |
|
|
89
|
+
| `--site-modules <list>` | Modules the site loads: `none`, `all`, or a comma list of `inject`, `cookies`, `maintenance`. Takes precedence over `--site`. |
|
|
88
90
|
| `--git` / `--no-git` | `git init` (default: yes). |
|
|
89
91
|
| `--remote <url>` | Set `origin` after git init (rejects URLs with embedded credentials). |
|
|
90
92
|
| `--install` / `--no-install` | Run `npm install` (default: no). |
|
|
91
93
|
| `-y, --yes` | Non-interactive; use defaults for omitted flags. |
|
|
92
94
|
| `--dry-run` | Print the file plan without writing. |
|
|
93
95
|
|
|
94
|
-
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`
|
|
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.2.0` (same major as the CDN pin `@2`, minor that ships the per-module types).
|
|
97
|
+
|
|
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
|
+
|
|
100
|
+
### Declared modules (`stagepass.json`)
|
|
101
|
+
|
|
102
|
+
```json
|
|
103
|
+
{ "name": "my-project", "modules": ["cookies"] }
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
`modules` is `[]` (loader only), a list of `inject`, `cookies`, `maintenance`, or `["all"]`. It must match what your Webflow site loads. From it the CLI generates:
|
|
107
|
+
|
|
108
|
+
- the managed block in `AGENTS.md` (between `<!-- stagepass:begin -->` and `<!-- stagepass:end -->`): loader API always, full sections for declared modules, a "not available; enable by …" line for the others, and the rule never to use undeclared `sp.<module>`;
|
|
109
|
+
- `.cursor/rules/stagepass.mdc` (globs `src/**`) with the same content;
|
|
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
|
+
|
|
112
|
+
The legacy boolean `"modules": true|false` is still read; `upgrade` replaces it with a list.
|
|
113
|
+
|
|
114
|
+
### Live site detection
|
|
115
|
+
|
|
116
|
+
`new --site`, `upgrade` and `doctor` read the modules from the live site: one public `GET` of the origin, https only, no cookies or auth headers, about 5 s timeout, a size cap, at most 3 redirects (all https). Only `<script src>` attributes are parsed with a regex (HTML comments ignored); nothing is executed. Recognised: `@stagepass/loader` and `@stagepass/modules` `dist/{inject,cookies,maintenance,all}` files on `cdn.jsdelivr.net/npm/` or `unpkg.com/`, any version (`@2`, `@2.0.0`, none). The npm dependency in `package.json` is only a hint.
|
|
95
117
|
|
|
96
118
|
### `upgrade` options
|
|
97
119
|
|
|
98
120
|
| Option | Behaviour |
|
|
99
121
|
| :--- | :--- |
|
|
100
|
-
| `--version <x>` | Target version (default: this CLI
|
|
101
|
-
| `--
|
|
122
|
+
| `--version <x>` | Target loader/modules version (default: the version this CLI targets, currently `2.2.0`). Dist-tags such as `latest` resolve via `npm view`. (Implemented as `--target` internally so it does not collide with `stagepass --version`.) |
|
|
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
|
+
| `--site-modules <list>` | Set the modules list directly (`none`, `all`, or a comma list). |
|
|
125
|
+
| `--skip-site` | Do not fetch the live site. |
|
|
126
|
+
| `-y, --yes` | Accept the detected or existing list without asking. |
|
|
127
|
+
| `--dry-run` | Print every planned change; write no file. |
|
|
128
|
+
|
|
129
|
+
`upgrade` bumps `@stagepass/*` in `package.json`, then resolves the modules list (`--site-modules`, else live detection, confirmed interactively; if the site is unreachable or no origin is known it asks; without a TTY it keeps the existing list or `[]` and prints a hint). It then syncs the project:
|
|
130
|
+
|
|
131
|
+
- `stagepass.json`: writes the `modules` list.
|
|
132
|
+
- `AGENTS.md`: created if missing, otherwise only the managed block is replaced (or appended when there is none). Your own text stays. Broken markers are left alone with a hint.
|
|
133
|
+
- `.cursor/rules/stagepass.mdc`: created if missing, otherwise only its managed block is refreshed.
|
|
134
|
+
- `tsconfig.json` / `jsconfig.json`: only `compilerOptions.types` changes, the rest of the JSON is preserved. Files with comments, or without a `types` array, are skipped with a hint.
|
|
102
135
|
|
|
103
|
-
|
|
136
|
+
Every change is printed as `file: action` (unchanged files are not listed; skipped files appear as hints). `npm install` is printed, not run. Snippets use the major pin `@2` plus an optional exact pin note.
|
|
104
137
|
|
|
105
138
|
### Domain argument (`link` / `unlink`)
|
|
106
139
|
|
package/package.json
CHANGED
package/src/cli.js
CHANGED
|
@@ -197,6 +197,7 @@ export function createProgram(deps = {}) {
|
|
|
197
197
|
.description('Diagnose the local Stagepass stack (read-only; never uses sudo)')
|
|
198
198
|
.option('-v, --verbose', 'Show extra detail for ok checks')
|
|
199
199
|
.option('--json', 'Machine-readable JSON output')
|
|
200
|
+
.option('--skip-site', 'Do not fetch the live site of the current project')
|
|
200
201
|
.action(async (options) => {
|
|
201
202
|
const code = await doctor(options);
|
|
202
203
|
process.exitCode = code;
|
|
@@ -257,12 +258,16 @@ export function createProgram(deps = {}) {
|
|
|
257
258
|
program
|
|
258
259
|
.command('upgrade')
|
|
259
260
|
.description(
|
|
260
|
-
'Bump @stagepass
|
|
261
|
+
'Bump @stagepass/*, sync modules, AGENTS.md, Cursor rule and types, print Webflow snippets'
|
|
261
262
|
)
|
|
262
|
-
.option('--dry-run', 'Show changes without writing
|
|
263
|
+
.option('--dry-run', 'Show changes without writing any file')
|
|
264
|
+
.option('--site <host>', 'Live Webflow site to detect loaded modules from (https)')
|
|
265
|
+
.option('--site-modules <list>', 'Modules the site loads: none, all, or inject,cookies,maintenance')
|
|
266
|
+
.option('--skip-site', 'Do not fetch the live site')
|
|
267
|
+
.option('-y, --yes', 'Non-interactive; accept detected or existing modules')
|
|
263
268
|
.option(
|
|
264
269
|
'--target <version>',
|
|
265
|
-
'Target version (default: this CLI
|
|
270
|
+
'Target loader/modules version (default: the version this CLI targets; dist-tags use npm view)'
|
|
266
271
|
)
|
|
267
272
|
// Public docs use `--version`; rewriteArgvForUpgrade maps it to `--target`
|
|
268
273
|
// so it does not collide with the root `stagepass --version` handler.
|
|
@@ -301,8 +306,10 @@ export function createProgram(deps = {}) {
|
|
|
301
306
|
.option('--author <author>', 'Author for package.json (default: git user.name)')
|
|
302
307
|
.option('--lang <lang>', 'ts or js (default: ts)')
|
|
303
308
|
.option('--styles <styles>', 'scss or css (default: scss)')
|
|
304
|
-
.option('--modules', 'Include @stagepass/modules')
|
|
309
|
+
.option('--modules', 'Include @stagepass/modules (npm, types)')
|
|
305
310
|
.option('--no-modules', 'Skip @stagepass/modules')
|
|
311
|
+
.option('--site <host>', 'Live Webflow site to detect loaded modules from (https)')
|
|
312
|
+
.option('--site-modules <list>', 'Modules the site loads: none, all, or inject,cookies,maintenance')
|
|
306
313
|
.option('--git', 'Initialize a git repository')
|
|
307
314
|
.option('--no-git', 'Do not initialize git')
|
|
308
315
|
.option('--remote <url>', 'Git remote URL (requires git)')
|
package/src/commands/doctor.js
CHANGED
package/src/commands/new.js
CHANGED
|
@@ -3,7 +3,6 @@ import path from 'node:path';
|
|
|
3
3
|
import readline from 'node:readline/promises';
|
|
4
4
|
import { stdin as input, stdout as output } from 'node:process';
|
|
5
5
|
import chalk from 'chalk';
|
|
6
|
-
import { createRequire } from 'module';
|
|
7
6
|
import { defaultExec } from '../lib/exec.js';
|
|
8
7
|
import { defaultSpawn } from '../lib/spawn.js';
|
|
9
8
|
import { validateProjectName } from '../lib/project-name.js';
|
|
@@ -18,8 +17,9 @@ import {
|
|
|
18
17
|
printWebflowSnippets,
|
|
19
18
|
} from '../lib/snippets.js';
|
|
20
19
|
|
|
21
|
-
|
|
22
|
-
|
|
20
|
+
import { STAGEPASS_PACKAGES_VERSION } from '../lib/versions.js';
|
|
21
|
+
import { formatModules, normalizeModulesList } from '../lib/project-modules.js';
|
|
22
|
+
import { describeDetection, detectSite, normalizeSiteUrl } from '../lib/site-detect.js';
|
|
23
23
|
|
|
24
24
|
/**
|
|
25
25
|
* Resolve a boolean flag that may appear as --foo / --no-foo.
|
|
@@ -109,6 +109,37 @@ export async function resolveNewOptions(nameArg, options = {}, deps = {}) {
|
|
|
109
109
|
let git = resolveBoolFlag(options, 'git', argv, undefined);
|
|
110
110
|
let install = resolveBoolFlag(options, 'install', argv, undefined);
|
|
111
111
|
let remote = options.remote != null ? String(options.remote).trim() : '';
|
|
112
|
+
const log = deps.log || console.log;
|
|
113
|
+
const detect = deps.detectSite || detectSite;
|
|
114
|
+
|
|
115
|
+
/** @type {string[] | undefined} */
|
|
116
|
+
let siteModules;
|
|
117
|
+
if (options.siteModules != null) {
|
|
118
|
+
const parsed = normalizeModulesList(String(options.siteModules));
|
|
119
|
+
if (!parsed.ok) return { ok: false, error: parsed.error };
|
|
120
|
+
siteModules = parsed.modules;
|
|
121
|
+
}
|
|
122
|
+
/** @type {string} */
|
|
123
|
+
let origin = '';
|
|
124
|
+
if (options.site != null && String(options.site).trim() !== '') {
|
|
125
|
+
const check = normalizeSiteUrl(String(options.site));
|
|
126
|
+
if (!check.ok) return { ok: false, error: check.error };
|
|
127
|
+
origin = check.host;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Detect modules on the live site (public GET, never executed).
|
|
132
|
+
* @returns {Promise<string[] | null>} detected list, or null when unknown
|
|
133
|
+
*/
|
|
134
|
+
const detectFromSite = async () => {
|
|
135
|
+
if (!origin) return null;
|
|
136
|
+
log(`Checking live site https://${origin}/ for Stagepass script tags...`);
|
|
137
|
+
const det = await detect(origin);
|
|
138
|
+
log(` ${describeDetection(det)}`);
|
|
139
|
+
if (det.ok && det.loader) return det.list;
|
|
140
|
+
if (det.ok) log(' The page does not load @stagepass/loader yet.');
|
|
141
|
+
return null;
|
|
142
|
+
};
|
|
112
143
|
|
|
113
144
|
if (interactive) {
|
|
114
145
|
const rl =
|
|
@@ -133,8 +164,29 @@ export async function resolveNewOptions(nameArg, options = {}, deps = {}) {
|
|
|
133
164
|
const raw = await ask(rl, 'Styles (scss|css)', 'scss');
|
|
134
165
|
styles = raw === 'css' ? 'css' : 'scss';
|
|
135
166
|
}
|
|
136
|
-
if (
|
|
137
|
-
|
|
167
|
+
if (siteModules === undefined && modules !== false) {
|
|
168
|
+
if (!origin) {
|
|
169
|
+
const raw = await ask(rl, 'Live Webflow site to check (optional, e.g. my-project.webflow.io)', '');
|
|
170
|
+
if (raw) {
|
|
171
|
+
const check = normalizeSiteUrl(raw);
|
|
172
|
+
if (check.ok) origin = check.host;
|
|
173
|
+
else log(chalk.yellow(` ${check.error} Skipping the live check.`));
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
const detected = await detectFromSite();
|
|
177
|
+
if (detected && (await askYesNo(rl, `Use modules detected on the site: ${formatModules(detected)}?`, true))) {
|
|
178
|
+
siteModules = detected;
|
|
179
|
+
}
|
|
180
|
+
while (siteModules === undefined) {
|
|
181
|
+
const raw = await ask(
|
|
182
|
+
rl,
|
|
183
|
+
'Modules your Webflow site loads (none | all | inject,cookies,maintenance)',
|
|
184
|
+
detected ? formatModules(detected) : 'none'
|
|
185
|
+
);
|
|
186
|
+
const parsed = normalizeModulesList(raw);
|
|
187
|
+
if (parsed.ok) siteModules = parsed.modules;
|
|
188
|
+
else log(chalk.yellow(` ${parsed.error}`));
|
|
189
|
+
}
|
|
138
190
|
}
|
|
139
191
|
if (git === undefined) {
|
|
140
192
|
git = await askYesNo(rl, 'Initialize git repository?', true);
|
|
@@ -148,8 +200,29 @@ export async function resolveNewOptions(nameArg, options = {}, deps = {}) {
|
|
|
148
200
|
} finally {
|
|
149
201
|
rl.close();
|
|
150
202
|
}
|
|
203
|
+
} else if (siteModules === undefined && modules !== false) {
|
|
204
|
+
const detected = await detectFromSite();
|
|
205
|
+
if (detected) {
|
|
206
|
+
siteModules = detected;
|
|
207
|
+
log(`Modules: ${formatModules(detected)} (detected on ${origin}; change "modules" in stagepass.json if needed).`);
|
|
208
|
+
}
|
|
151
209
|
}
|
|
152
210
|
|
|
211
|
+
if (siteModules === undefined) {
|
|
212
|
+
siteModules = [];
|
|
213
|
+
if (!interactive && modules !== false) {
|
|
214
|
+
log(chalk.dim('Modules: none declared. Pass --site <host> or --site-modules <list>, or edit stagepass.json and run stagepass upgrade.'));
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
if (modules === false && siteModules.length > 0) {
|
|
218
|
+
return {
|
|
219
|
+
ok: false,
|
|
220
|
+
error: '--no-modules conflicts with declared site modules. Drop --no-modules or pass --site-modules none.',
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
// The npm package (types) follows the declared list; keep the legacy default (included).
|
|
224
|
+
if (siteModules.length > 0) modules = true;
|
|
225
|
+
|
|
153
226
|
// Non-interactive / --yes defaults
|
|
154
227
|
if (!name) {
|
|
155
228
|
return { ok: false, error: 'Project name is required (pass [name] or use a TTY).' };
|
|
@@ -199,11 +272,14 @@ export async function resolveNewOptions(nameArg, options = {}, deps = {}) {
|
|
|
199
272
|
lang,
|
|
200
273
|
styles,
|
|
201
274
|
modules,
|
|
275
|
+
siteModules,
|
|
276
|
+
origin: origin || undefined,
|
|
202
277
|
git,
|
|
203
278
|
remote: remoteCheck.remote,
|
|
204
279
|
install,
|
|
205
280
|
dryRun: options.dryRun === true,
|
|
206
|
-
loaderVersion:
|
|
281
|
+
loaderVersion:
|
|
282
|
+
deps.packagesVersion || deps.cliVersion || STAGEPASS_PACKAGES_VERSION,
|
|
207
283
|
},
|
|
208
284
|
};
|
|
209
285
|
}
|
|
@@ -247,7 +323,17 @@ export async function createNew(nameArg, options = {}, deps = {}) {
|
|
|
247
323
|
) {
|
|
248
324
|
throw new Error(`Template path escapes templates root: ${rel}`);
|
|
249
325
|
}
|
|
250
|
-
|
|
326
|
+
try {
|
|
327
|
+
return await fse.readFile(abs, 'utf8');
|
|
328
|
+
} catch (err) {
|
|
329
|
+
if (err && err.code === 'ENOENT') {
|
|
330
|
+
throw new Error(
|
|
331
|
+
`Template "${rel}" is missing from the @stagepass/cli install (${templatesRootResolved}). ` +
|
|
332
|
+
'Reinstall the CLI (npm install -g @stagepass/cli) and try again. Nothing was written.'
|
|
333
|
+
);
|
|
334
|
+
}
|
|
335
|
+
throw err;
|
|
336
|
+
}
|
|
251
337
|
};
|
|
252
338
|
|
|
253
339
|
let files;
|
|
@@ -353,11 +439,12 @@ export async function createNew(nameArg, options = {}, deps = {}) {
|
|
|
353
439
|
log(` cd ${path.relative(cwd, targetDir) || '.'} && npm install`);
|
|
354
440
|
}
|
|
355
441
|
log(' npm run build');
|
|
356
|
-
log(` stagepass link ${config.name} --origin my-project.webflow.io`);
|
|
442
|
+
log(` stagepass link ${config.name} --origin ${config.origin || 'my-project.webflow.io'}`);
|
|
443
|
+
log(chalk.dim(`Declared modules (stagepass.json): ${formatModules(config.siteModules)}`));
|
|
357
444
|
|
|
358
445
|
const snippets = buildWebflowSnippets({
|
|
359
446
|
version: config.loaderVersion,
|
|
360
|
-
|
|
447
|
+
moduleList: config.siteModules,
|
|
361
448
|
});
|
|
362
449
|
printWebflowSnippets(snippets, log);
|
|
363
450
|
|
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,8 +7,16 @@ 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 { detectSite } from '../lib/site-detect.js';
|
|
14
|
+
import {
|
|
15
|
+
applyProjectSync,
|
|
16
|
+
planProjectSync,
|
|
17
|
+
readStagepassJson,
|
|
18
|
+
resolveUpgradeModules,
|
|
19
|
+
} from '../lib/project-sync.js';
|
|
13
20
|
|
|
14
21
|
const STAGEPASS_SCOPE = '@stagepass/';
|
|
15
22
|
|
|
@@ -28,18 +35,21 @@ function needsNpmView(version) {
|
|
|
28
35
|
}
|
|
29
36
|
|
|
30
37
|
/**
|
|
31
|
-
* Resolve target version.
|
|
38
|
+
* Resolve target version. Defaults to the @stagepass/loader + modules version
|
|
39
|
+
* this CLI targets (not the CLI's own version, which can differ on CLI patches).
|
|
32
40
|
* Contacts the network only when `--version` is an npm dist-tag (e.g. latest).
|
|
33
41
|
*
|
|
34
42
|
* @param {string|undefined} requested
|
|
35
43
|
* @param {object} deps
|
|
36
44
|
* @param {import('../lib/exec.js').ExecFn} [deps.exec]
|
|
37
|
-
* @param {string} [deps.
|
|
45
|
+
* @param {string} [deps.packagesVersion]
|
|
46
|
+
* @param {string} [deps.cliVersion] Legacy alias for packagesVersion (tests)
|
|
38
47
|
* @returns {Promise<string>}
|
|
39
48
|
*/
|
|
40
49
|
export async function resolveUpgradeVersion(requested, deps = {}) {
|
|
41
50
|
const exec = deps.exec || defaultExec;
|
|
42
|
-
const fallback =
|
|
51
|
+
const fallback =
|
|
52
|
+
deps.packagesVersion || deps.cliVersion || STAGEPASS_PACKAGES_VERSION;
|
|
43
53
|
|
|
44
54
|
if (!requested || String(requested).trim() === '') {
|
|
45
55
|
return fallback;
|
|
@@ -112,11 +122,17 @@ export function applyStagepassVersions(pkg, targetVersion) {
|
|
|
112
122
|
}
|
|
113
123
|
|
|
114
124
|
/**
|
|
115
|
-
* Upgrade @stagepass/* deps in the current project's package.json
|
|
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.
|
|
116
128
|
*
|
|
117
129
|
* @param {object} [options]
|
|
118
130
|
* @param {boolean} [options.dryRun]
|
|
119
131
|
* @param {string} [options.version]
|
|
132
|
+
* @param {string} [options.site] live Webflow host to detect modules from
|
|
133
|
+
* @param {string} [options.siteModules] none | all | comma list
|
|
134
|
+
* @param {boolean} [options.skipSite]
|
|
135
|
+
* @param {boolean} [options.yes]
|
|
120
136
|
* @param {object} [deps]
|
|
121
137
|
* @param {typeof fs} [deps.fs]
|
|
122
138
|
* @param {string} [deps.cwd]
|
|
@@ -140,7 +156,7 @@ export async function upgrade(options = {}, deps = {}) {
|
|
|
140
156
|
try {
|
|
141
157
|
targetVersion = await resolveUpgradeVersion(options.version, {
|
|
142
158
|
exec: deps.exec || defaultExec,
|
|
143
|
-
|
|
159
|
+
packagesVersion: deps.packagesVersion || deps.cliVersion,
|
|
144
160
|
});
|
|
145
161
|
} catch (error) {
|
|
146
162
|
log(chalk.red(error instanceof Error ? error.message : String(error)));
|
|
@@ -157,10 +173,6 @@ export async function upgrade(options = {}, deps = {}) {
|
|
|
157
173
|
}
|
|
158
174
|
|
|
159
175
|
const { next, changes } = applyStagepassVersions(pkg, targetVersion);
|
|
160
|
-
const hasModules = Boolean(
|
|
161
|
-
next.dependencies?.['@stagepass/modules'] ||
|
|
162
|
-
next.devDependencies?.['@stagepass/modules']
|
|
163
|
-
);
|
|
164
176
|
|
|
165
177
|
if (changes.length === 0) {
|
|
166
178
|
const existing = findStagepassDeps(pkg);
|
|
@@ -174,20 +186,86 @@ export async function upgrade(options = {}, deps = {}) {
|
|
|
174
186
|
for (const change of changes) {
|
|
175
187
|
log(` ${change.name}: ${change.from} → ${change.to} (${change.section})`);
|
|
176
188
|
}
|
|
189
|
+
}
|
|
177
190
|
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
191
|
+
// Declared modules: live site first, then stagepass.json, then ask.
|
|
192
|
+
const sp = await readStagepassJson(cwd, fse);
|
|
193
|
+
if (sp.error) log(chalk.yellow(sp.error));
|
|
194
|
+
const isTty = deps.isTTY != null ? Boolean(deps.isTTY) : Boolean(input.isTTY && output.isTTY);
|
|
195
|
+
const interactive = isTty && options.yes !== true;
|
|
196
|
+
/** @type {import('node:readline/promises').Interface | null} */
|
|
197
|
+
let rl = null;
|
|
198
|
+
const getRl = () => {
|
|
199
|
+
if (!rl) rl = deps.createReadline?.() || readline.createInterface({ input, output });
|
|
200
|
+
return rl;
|
|
201
|
+
};
|
|
202
|
+
let resolved;
|
|
203
|
+
try {
|
|
204
|
+
resolved = await resolveUpgradeModules(
|
|
205
|
+
{ cwd, data: sp.data, pkg: next, options },
|
|
206
|
+
{
|
|
207
|
+
detectSite: deps.detectSite || detectSite,
|
|
208
|
+
loadProjects: deps.loadProjects,
|
|
209
|
+
interactive,
|
|
210
|
+
log,
|
|
211
|
+
ask: async (question, def) => {
|
|
212
|
+
const answer = await getRl().question(`${question} (${def}): `);
|
|
213
|
+
const trimmed = String(answer || '').trim();
|
|
214
|
+
return trimmed === '' ? def : trimmed;
|
|
215
|
+
},
|
|
216
|
+
confirm: async (question, defYes) => {
|
|
217
|
+
const answer = await getRl().question(`${question} (${defYes ? 'Y/n' : 'y/N'}): `);
|
|
218
|
+
const t = String(answer || '').trim().toLowerCase();
|
|
219
|
+
if (t === '') return defYes;
|
|
220
|
+
return ['y', 'yes'].includes(t);
|
|
221
|
+
},
|
|
222
|
+
}
|
|
223
|
+
);
|
|
224
|
+
} finally {
|
|
225
|
+
if (rl) rl.close();
|
|
226
|
+
}
|
|
227
|
+
if (!resolved.ok) {
|
|
228
|
+
log(chalk.red(resolved.error));
|
|
229
|
+
return 1;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
const plan = await planProjectSync(
|
|
233
|
+
{
|
|
234
|
+
cwd,
|
|
235
|
+
modules: resolved.modules,
|
|
236
|
+
origin: options.site ? resolved.origin : null,
|
|
237
|
+
pkg: next,
|
|
238
|
+
},
|
|
239
|
+
fse
|
|
240
|
+
);
|
|
241
|
+
|
|
242
|
+
log(chalk.bold(`\nProject files for modules: ${resolved.modules.length ? resolved.modules.join(', ') : 'none'}`));
|
|
243
|
+
if (plan.writes.length === 0) {
|
|
244
|
+
log(' Everything is up to date.');
|
|
245
|
+
}
|
|
246
|
+
for (const w of plan.writes) {
|
|
247
|
+
log(` ${w.file}: ${w.action}`);
|
|
248
|
+
}
|
|
249
|
+
for (const note of plan.notes) {
|
|
250
|
+
log(chalk.yellow(` note: ${note}`));
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
if (options.dryRun) {
|
|
254
|
+
log(chalk.dim('\nDry run: no files were modified.'));
|
|
255
|
+
} else {
|
|
256
|
+
if (changes.length > 0) {
|
|
181
257
|
const trailingNewline = raw.endsWith('\n');
|
|
182
|
-
const
|
|
183
|
-
await fse.writeFile(pkgPath,
|
|
258
|
+
const pkgBody = `${JSON.stringify(next, null, 2)}${trailingNewline ? '\n' : ''}`;
|
|
259
|
+
await fse.writeFile(pkgPath, pkgBody, 'utf8');
|
|
184
260
|
log(chalk.green('\nUpdated package.json.'));
|
|
185
261
|
}
|
|
262
|
+
await applyProjectSync(cwd, plan.writes, fse);
|
|
263
|
+
if (plan.writes.length > 0) log(chalk.green(`Updated ${plan.writes.length} project file(s).`));
|
|
186
264
|
}
|
|
187
265
|
|
|
188
266
|
const snippets = buildWebflowSnippets({
|
|
189
267
|
version: targetVersion,
|
|
190
|
-
|
|
268
|
+
moduleList: resolved.modules,
|
|
191
269
|
});
|
|
192
270
|
printWebflowSnippets(snippets, log);
|
|
193
271
|
|
|
@@ -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
|
+
}
|