@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/lib/scaffold.js
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
import path from 'node:path';
|
|
2
2
|
import { fileURLToPath } from 'node:url';
|
|
3
|
-
import { createRequire } from 'module';
|
|
4
3
|
import { buildWebflowSnippets } from './snippets.js';
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
4
|
+
import { CURSOR_RULE_PATH, buildCursorRule, buildManagedBlock } from './ai-guide.js';
|
|
5
|
+
import { typesForModules } from './project-modules.js';
|
|
6
|
+
import { STAGEPASS_PACKAGES_VERSION, packagesRange } from './versions.js';
|
|
8
7
|
|
|
9
8
|
const __filename = fileURLToPath(import.meta.url);
|
|
10
9
|
const __dirname = path.dirname(__filename);
|
|
@@ -14,6 +13,39 @@ export function defaultTemplatesRoot() {
|
|
|
14
13
|
return path.join(__dirname, '../../templates/project');
|
|
15
14
|
}
|
|
16
15
|
|
|
16
|
+
/**
|
|
17
|
+
* Template file → output path. Dotfiles are stored without the leading dot
|
|
18
|
+
* because npm pack / publish drops `.gitignore` (and `.npmignore`) files.
|
|
19
|
+
* Never add a template whose name starts with `.` (a test enforces this).
|
|
20
|
+
*
|
|
21
|
+
* @param {'ts'|'js'} lang
|
|
22
|
+
* @param {'scss'|'css'} styles
|
|
23
|
+
* @returns {{ template: string, out: string }[]}
|
|
24
|
+
*/
|
|
25
|
+
export function scaffoldTemplateMap(lang, styles) {
|
|
26
|
+
const isTs = lang === 'ts';
|
|
27
|
+
const useSass = styles === 'scss';
|
|
28
|
+
return [
|
|
29
|
+
{ template: 'scripts/build.mjs', out: 'scripts/build.mjs' },
|
|
30
|
+
{ template: 'gitignore', out: '.gitignore' },
|
|
31
|
+
{ template: 'README.md.tmpl', out: 'README.md' },
|
|
32
|
+
{ template: 'AGENTS.md.tmpl', out: 'AGENTS.md' },
|
|
33
|
+
{ template: 'stagepass.json.tmpl', out: 'stagepass.json' },
|
|
34
|
+
{
|
|
35
|
+
template: isTs ? 'src/main.ts.tmpl' : 'src/main.js.tmpl',
|
|
36
|
+
out: isTs ? 'src/main.ts' : 'src/main.js',
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
template: useSass ? 'src/styles.scss.tmpl' : 'src/styles.css.tmpl',
|
|
40
|
+
out: useSass ? 'src/styles.scss' : 'src/styles.css',
|
|
41
|
+
},
|
|
42
|
+
{
|
|
43
|
+
template: isTs ? 'tsconfig.json.tmpl' : 'jsconfig.json.tmpl',
|
|
44
|
+
out: isTs ? 'tsconfig.json' : 'jsconfig.json',
|
|
45
|
+
},
|
|
46
|
+
];
|
|
47
|
+
}
|
|
48
|
+
|
|
17
49
|
/**
|
|
18
50
|
* @typedef {object} ScaffoldOptions
|
|
19
51
|
* @property {string} name
|
|
@@ -21,12 +53,16 @@ export function defaultTemplatesRoot() {
|
|
|
21
53
|
* @property {string} [author]
|
|
22
54
|
* @property {'ts'|'js'} lang
|
|
23
55
|
* @property {'scss'|'css'} styles
|
|
24
|
-
* @property {boolean} modules
|
|
56
|
+
* @property {boolean} modules Add @stagepass/modules to devDependencies
|
|
57
|
+
* @property {string[]} [siteModules] Declared modules the Webflow site loads (stagepass.json)
|
|
58
|
+
* @property {string} [origin] Live site host (stagepass.json `origin`)
|
|
25
59
|
* @property {boolean} git
|
|
26
60
|
* @property {string} [remote]
|
|
27
61
|
* @property {boolean} install
|
|
28
62
|
* @property {boolean} dryRun
|
|
29
|
-
* @property {string} [loaderVersion]
|
|
63
|
+
* @property {string} [loaderVersion] @stagepass/loader + modules version (snippets)
|
|
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)
|
|
30
66
|
*/
|
|
31
67
|
|
|
32
68
|
/**
|
|
@@ -34,18 +70,20 @@ export function defaultTemplatesRoot() {
|
|
|
34
70
|
* @returns {Record<string, string>}
|
|
35
71
|
*/
|
|
36
72
|
export function buildPackageJson(options) {
|
|
37
|
-
|
|
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);
|
|
38
76
|
const isTs = options.lang === 'ts';
|
|
39
77
|
const useSass = options.styles === 'scss';
|
|
40
78
|
|
|
41
79
|
/** @type {Record<string, string>} */
|
|
42
80
|
const devDependencies = {
|
|
43
|
-
'@stagepass/loader':
|
|
81
|
+
'@stagepass/loader': range,
|
|
44
82
|
esbuild: '^0.25.0',
|
|
45
83
|
};
|
|
46
84
|
|
|
47
85
|
if (options.modules) {
|
|
48
|
-
devDependencies['@stagepass/modules'] =
|
|
86
|
+
devDependencies['@stagepass/modules'] = range;
|
|
49
87
|
}
|
|
50
88
|
if (isTs) {
|
|
51
89
|
devDependencies.typescript = '^5.6.0';
|
|
@@ -91,16 +129,21 @@ export function applyTemplate(source, vars) {
|
|
|
91
129
|
* @returns {Promise<{ relativePath: string, contents: string }[]>}
|
|
92
130
|
*/
|
|
93
131
|
export async function planScaffoldFiles(options, deps = {}) {
|
|
94
|
-
const version = options.loaderVersion ||
|
|
132
|
+
const version = options.loaderVersion || STAGEPASS_PACKAGES_VERSION;
|
|
95
133
|
const isTs = options.lang === 'ts';
|
|
96
134
|
const useSass = options.styles === 'scss';
|
|
97
135
|
const entryJs = isTs ? 'src/main.ts' : 'src/main.js';
|
|
98
136
|
const entryCss = useSass ? 'src/styles.scss' : 'src/styles.css';
|
|
99
137
|
const configFile = isTs ? 'tsconfig.json' : 'jsconfig.json';
|
|
100
138
|
|
|
139
|
+
const siteModules = Array.isArray(options.siteModules) ? options.siteModules : [];
|
|
101
140
|
const snippets = buildWebflowSnippets({
|
|
102
141
|
version,
|
|
103
|
-
|
|
142
|
+
moduleList: siteModules,
|
|
143
|
+
});
|
|
144
|
+
const types = typesForModules(siteModules, {
|
|
145
|
+
modulesDependency: options.modules === true,
|
|
146
|
+
perModuleTypes: options.perModuleTypes,
|
|
104
147
|
});
|
|
105
148
|
|
|
106
149
|
const vars = {
|
|
@@ -108,7 +151,11 @@ export async function planScaffoldFiles(options, deps = {}) {
|
|
|
108
151
|
AUTHOR: options.author || '',
|
|
109
152
|
LANG: options.lang,
|
|
110
153
|
STYLES: options.styles,
|
|
111
|
-
MODULES_JSON:
|
|
154
|
+
MODULES_JSON: JSON.stringify(siteModules),
|
|
155
|
+
ORIGIN_LINE: options.origin ? `\n "origin": ${JSON.stringify(options.origin)},` : '',
|
|
156
|
+
TYPES_JSON: JSON.stringify(types),
|
|
157
|
+
TYPES_INLINE: JSON.stringify(types).replace(/","/g, '", "'),
|
|
158
|
+
STAGEPASS_BLOCK: buildManagedBlock(siteModules),
|
|
112
159
|
LOADER_VERSION: version,
|
|
113
160
|
HEAD_SNIPPET: snippets.head,
|
|
114
161
|
FOOTER_SNIPPET: snippets.footer,
|
|
@@ -116,9 +163,6 @@ export async function planScaffoldFiles(options, deps = {}) {
|
|
|
116
163
|
ENTRY_CSS: entryCss,
|
|
117
164
|
USE_SASS: useSass ? 'true' : 'false',
|
|
118
165
|
CONFIG_FILE: configFile,
|
|
119
|
-
MODULES_NOTE: options.modules
|
|
120
|
-
? '\nModules are enabled: load `inject` / `cookies` (or `all`) after the loader in Webflow Head.\n'
|
|
121
|
-
: '\nModules are not included. Add `@stagepass/modules` later if you need `sp.inject` or `sp.cookies`.\n',
|
|
122
166
|
};
|
|
123
167
|
|
|
124
168
|
const readTemplate =
|
|
@@ -136,25 +180,7 @@ export async function planScaffoldFiles(options, deps = {}) {
|
|
|
136
180
|
contents: `${JSON.stringify(pkg, null, 2)}\n`,
|
|
137
181
|
});
|
|
138
182
|
|
|
139
|
-
const templateMap =
|
|
140
|
-
{ template: 'scripts/build.mjs', out: 'scripts/build.mjs' },
|
|
141
|
-
{ template: '.gitignore', out: '.gitignore' },
|
|
142
|
-
{ template: 'README.md.tmpl', out: 'README.md' },
|
|
143
|
-
{ template: 'AGENTS.md.tmpl', out: 'AGENTS.md' },
|
|
144
|
-
{ template: 'stagepass.json.tmpl', out: 'stagepass.json' },
|
|
145
|
-
{
|
|
146
|
-
template: isTs ? 'src/main.ts.tmpl' : 'src/main.js.tmpl',
|
|
147
|
-
out: entryJs,
|
|
148
|
-
},
|
|
149
|
-
{
|
|
150
|
-
template: useSass ? 'src/styles.scss.tmpl' : 'src/styles.css.tmpl',
|
|
151
|
-
out: entryCss,
|
|
152
|
-
},
|
|
153
|
-
{
|
|
154
|
-
template: isTs ? 'tsconfig.json.tmpl' : 'jsconfig.json.tmpl',
|
|
155
|
-
out: configFile,
|
|
156
|
-
},
|
|
157
|
-
];
|
|
183
|
+
const templateMap = scaffoldTemplateMap(options.lang, options.styles);
|
|
158
184
|
|
|
159
185
|
for (const item of templateMap) {
|
|
160
186
|
const raw = await readTemplate(item.template);
|
|
@@ -164,6 +190,10 @@ export async function planScaffoldFiles(options, deps = {}) {
|
|
|
164
190
|
});
|
|
165
191
|
}
|
|
166
192
|
|
|
193
|
+
// Generated in code (not a template): npm drops dotfiles and dot folders
|
|
194
|
+
// are easy to lose in the published package.
|
|
195
|
+
files.push({ relativePath: CURSOR_RULE_PATH, contents: buildCursorRule(siteModules) });
|
|
196
|
+
|
|
167
197
|
return files;
|
|
168
198
|
}
|
|
169
199
|
|
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Detect which Stagepass scripts a live Webflow site loads.
|
|
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.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { validateOriginEntry } from './origins.js';
|
|
10
|
+
import { MODULE_CHOICES, normalizeModulesList } from './project-modules.js';
|
|
11
|
+
|
|
12
|
+
export const DETECT_TIMEOUT_MS = 5000;
|
|
13
|
+
export const DETECT_MAX_BYTES = 2 * 1024 * 1024;
|
|
14
|
+
export const DETECT_MAX_REDIRECTS = 3;
|
|
15
|
+
|
|
16
|
+
// Bounded attribute run keeps the scan linear on hostile input.
|
|
17
|
+
const SCRIPT_TAG_RE = /<script\b[^<>]{0,4096}>/gi;
|
|
18
|
+
const SRC_ATTR_RE = /\ssrc\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s"'>]+))/i;
|
|
19
|
+
const STAGEPASS_SRC_RE =
|
|
20
|
+
/^(?:https?:)?\/\/(cdn\.jsdelivr\.net\/npm|unpkg\.com)\/@stagepass\/(loader|modules)(?:@([0-9A-Za-z.^~_-]+))?\/dist\/([a-z]+)(?:\.min)?\.js(?:[?#].*)?$/i;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Normalize a live site input (host or https URL) to an https URL.
|
|
24
|
+
* Rejects http, wildcards, credentials, and invalid hosts.
|
|
25
|
+
*
|
|
26
|
+
* @param {string} input
|
|
27
|
+
* @returns {{ ok: true, url: string, host: string } | { ok: false, error: string }}
|
|
28
|
+
*/
|
|
29
|
+
export function normalizeSiteUrl(input) {
|
|
30
|
+
const raw = String(input == null ? '' : input).trim();
|
|
31
|
+
if (!raw) return { ok: false, error: 'No live site given.' };
|
|
32
|
+
let url;
|
|
33
|
+
try {
|
|
34
|
+
url = new URL(/^[a-z][a-z0-9+.-]*:\/\//i.test(raw) ? raw : `https://${raw}`);
|
|
35
|
+
} catch {
|
|
36
|
+
return { ok: false, error: `Invalid site URL: ${raw}` };
|
|
37
|
+
}
|
|
38
|
+
if (url.protocol !== 'https:') {
|
|
39
|
+
return { ok: false, error: 'Live site must use https.' };
|
|
40
|
+
}
|
|
41
|
+
if (url.username || url.password) {
|
|
42
|
+
return { ok: false, error: 'Site URL must not contain credentials.' };
|
|
43
|
+
}
|
|
44
|
+
if (url.port) {
|
|
45
|
+
return { ok: false, error: 'Site URL must not contain a port.' };
|
|
46
|
+
}
|
|
47
|
+
const host = url.hostname.toLowerCase();
|
|
48
|
+
const check = validateOriginEntry(host);
|
|
49
|
+
if (!check.ok || check.value.startsWith('*.')) {
|
|
50
|
+
return { ok: false, error: `Not a usable site host: ${host}` };
|
|
51
|
+
}
|
|
52
|
+
const path = url.pathname && url.pathname !== '' ? url.pathname : '/';
|
|
53
|
+
return { ok: true, url: `https://${host}${path}`, host };
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Remove `<!-- … -->` comments in linear time (a lazy regex is quadratic on
|
|
58
|
+
* many unterminated `<!--`). An unterminated comment drops the rest.
|
|
59
|
+
*
|
|
60
|
+
* @param {string} html
|
|
61
|
+
* @returns {string}
|
|
62
|
+
*/
|
|
63
|
+
function stripHtmlComments(html) {
|
|
64
|
+
let out = '';
|
|
65
|
+
let pos = 0;
|
|
66
|
+
while (pos < html.length) {
|
|
67
|
+
const start = html.indexOf('<!--', pos);
|
|
68
|
+
if (start === -1) {
|
|
69
|
+
out += html.slice(pos);
|
|
70
|
+
break;
|
|
71
|
+
}
|
|
72
|
+
out += html.slice(pos, start);
|
|
73
|
+
const end = html.indexOf('-->', start + 4);
|
|
74
|
+
if (end === -1) break;
|
|
75
|
+
pos = end + 3;
|
|
76
|
+
}
|
|
77
|
+
return out;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Parse Stagepass loader / module script tags from HTML (no execution).
|
|
82
|
+
*
|
|
83
|
+
* @param {string} html
|
|
84
|
+
* @returns {{ loader: { version: string, cdn: string } | null, modules: { id: string, version: string, cdn: string }[] }}
|
|
85
|
+
*/
|
|
86
|
+
export function parseStagepassScripts(html) {
|
|
87
|
+
/** @type {{ version: string, cdn: string } | null} */
|
|
88
|
+
let loader = null;
|
|
89
|
+
/** @type {Map<string, { id: string, version: string, cdn: string }>} */
|
|
90
|
+
const modules = new Map();
|
|
91
|
+
// Ignore commented-out tags; the HTML is only scanned, never executed.
|
|
92
|
+
const source = stripHtmlComments(String(html || ''));
|
|
93
|
+
|
|
94
|
+
for (const match of source.matchAll(SCRIPT_TAG_RE)) {
|
|
95
|
+
const tag = match[0];
|
|
96
|
+
const attr = SRC_ATTR_RE.exec(tag);
|
|
97
|
+
if (!attr) continue;
|
|
98
|
+
const src = (attr[1] ?? attr[2] ?? attr[3] ?? '').trim();
|
|
99
|
+
const m = STAGEPASS_SRC_RE.exec(src);
|
|
100
|
+
if (!m) continue;
|
|
101
|
+
const cdn = m[1].toLowerCase().startsWith('unpkg') ? 'unpkg' : 'jsdelivr';
|
|
102
|
+
const pkg = m[2].toLowerCase();
|
|
103
|
+
const version = m[3] || 'latest';
|
|
104
|
+
const file = m[4].toLowerCase();
|
|
105
|
+
if (pkg === 'loader') {
|
|
106
|
+
if (file === 'loader' && !loader) loader = { version, cdn };
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
if (MODULE_CHOICES.includes(file) && !modules.has(file)) {
|
|
110
|
+
modules.set(file, { id: file, version, cdn });
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
return { loader, modules: [...modules.values()] };
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Declared-list form of detected modules (`all` wins).
|
|
119
|
+
* @param {{ modules: { id: string }[] }} detection
|
|
120
|
+
* @returns {string[]}
|
|
121
|
+
*/
|
|
122
|
+
export function detectedModulesList(detection) {
|
|
123
|
+
const result = normalizeModulesList(detection.modules.map((m) => m.id));
|
|
124
|
+
return result.ok ? result.modules : [];
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Read a response body up to `maxBytes` (truncates; never throws on size).
|
|
129
|
+
* @param {Response} res
|
|
130
|
+
* @param {number} maxBytes
|
|
131
|
+
*/
|
|
132
|
+
async function readCapped(res, maxBytes) {
|
|
133
|
+
if (!res.body || typeof res.body.getReader !== 'function') {
|
|
134
|
+
const text = await res.text();
|
|
135
|
+
return text.slice(0, maxBytes);
|
|
136
|
+
}
|
|
137
|
+
const reader = res.body.getReader();
|
|
138
|
+
const chunks = [];
|
|
139
|
+
let total = 0;
|
|
140
|
+
while (total < maxBytes) {
|
|
141
|
+
const { done, value } = await reader.read();
|
|
142
|
+
if (done) break;
|
|
143
|
+
chunks.push(value);
|
|
144
|
+
total += value.byteLength;
|
|
145
|
+
}
|
|
146
|
+
try {
|
|
147
|
+
await reader.cancel();
|
|
148
|
+
} catch {
|
|
149
|
+
// ignore
|
|
150
|
+
}
|
|
151
|
+
const buf = Buffer.concat(chunks.map((c) => Buffer.from(c)));
|
|
152
|
+
return buf.subarray(0, maxBytes).toString('utf8');
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Fetch a live page and detect Stagepass scripts.
|
|
157
|
+
*
|
|
158
|
+
* @param {string} siteInput host or https URL
|
|
159
|
+
* @param {object} [deps]
|
|
160
|
+
* @param {typeof fetch} [deps.fetch]
|
|
161
|
+
* @param {number} [deps.timeoutMs]
|
|
162
|
+
* @param {number} [deps.maxBytes]
|
|
163
|
+
* @returns {Promise<{ ok: true, url: string, loader: { version: string, cdn: string } | null, modules: { id: string, version: string, cdn: string }[], list: string[] } | { ok: false, url?: string, error: string }>}
|
|
164
|
+
*/
|
|
165
|
+
export async function detectSite(siteInput, deps = {}) {
|
|
166
|
+
const normalized = normalizeSiteUrl(siteInput);
|
|
167
|
+
if (!normalized.ok) return { ok: false, error: normalized.error };
|
|
168
|
+
|
|
169
|
+
const fetchImpl = deps.fetch || globalThis.fetch;
|
|
170
|
+
if (typeof fetchImpl !== 'function') {
|
|
171
|
+
return { ok: false, url: normalized.url, error: 'fetch is not available in this Node.js version.' };
|
|
172
|
+
}
|
|
173
|
+
const timeoutMs = deps.timeoutMs ?? DETECT_TIMEOUT_MS;
|
|
174
|
+
const maxBytes = deps.maxBytes ?? DETECT_MAX_BYTES;
|
|
175
|
+
const controller = new AbortController();
|
|
176
|
+
const timer = setTimeout(() => controller.abort(), timeoutMs);
|
|
177
|
+
|
|
178
|
+
let current = normalized.url;
|
|
179
|
+
try {
|
|
180
|
+
for (let hop = 0; hop <= DETECT_MAX_REDIRECTS; hop++) {
|
|
181
|
+
const res = await fetchImpl(current, {
|
|
182
|
+
method: 'GET',
|
|
183
|
+
redirect: 'manual',
|
|
184
|
+
credentials: 'omit',
|
|
185
|
+
signal: controller.signal,
|
|
186
|
+
headers: {
|
|
187
|
+
accept: 'text/html,application/xhtml+xml',
|
|
188
|
+
'user-agent': 'stagepass-cli (site check)',
|
|
189
|
+
},
|
|
190
|
+
});
|
|
191
|
+
if (res.status >= 300 && res.status < 400) {
|
|
192
|
+
const location = res.headers.get('location');
|
|
193
|
+
if (!location) {
|
|
194
|
+
return { ok: false, url: current, error: `Redirect without Location (${res.status}).` };
|
|
195
|
+
}
|
|
196
|
+
const next = new URL(location, current);
|
|
197
|
+
if (next.protocol !== 'https:') {
|
|
198
|
+
return { ok: false, url: current, error: 'Redirect to a non-https URL was not followed.' };
|
|
199
|
+
}
|
|
200
|
+
if (next.username || next.password) {
|
|
201
|
+
return { ok: false, url: current, error: 'Redirect URL contains credentials.' };
|
|
202
|
+
}
|
|
203
|
+
current = next.toString();
|
|
204
|
+
continue;
|
|
205
|
+
}
|
|
206
|
+
if (!res.ok) {
|
|
207
|
+
return { ok: false, url: current, error: `Site answered HTTP ${res.status}.` };
|
|
208
|
+
}
|
|
209
|
+
const html = await readCapped(res, maxBytes);
|
|
210
|
+
const parsed = parseStagepassScripts(html);
|
|
211
|
+
return { ok: true, url: current, ...parsed, list: detectedModulesList(parsed) };
|
|
212
|
+
}
|
|
213
|
+
return { ok: false, url: current, error: `More than ${DETECT_MAX_REDIRECTS} redirects.` };
|
|
214
|
+
} catch (err) {
|
|
215
|
+
const aborted = controller.signal.aborted || (err && err.name === 'AbortError');
|
|
216
|
+
return {
|
|
217
|
+
ok: false,
|
|
218
|
+
url: current,
|
|
219
|
+
error: aborted
|
|
220
|
+
? `Site did not answer within ${Math.round(timeoutMs / 1000)} s.`
|
|
221
|
+
: `Site not reachable: ${err instanceof Error ? err.message : String(err)}`,
|
|
222
|
+
};
|
|
223
|
+
} finally {
|
|
224
|
+
clearTimeout(timer);
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* One-line summary of a detection result.
|
|
230
|
+
* @param {Awaited<ReturnType<typeof detectSite>>} det
|
|
231
|
+
*/
|
|
232
|
+
export function describeDetection(det) {
|
|
233
|
+
if (!det.ok) return det.error;
|
|
234
|
+
const loader = det.loader
|
|
235
|
+
? `loader @${det.loader.version} (${det.loader.cdn})`
|
|
236
|
+
: 'no Stagepass loader';
|
|
237
|
+
const mods =
|
|
238
|
+
det.modules.length === 0
|
|
239
|
+
? 'no modules'
|
|
240
|
+
: `modules ${det.modules.map((m) => `${m.id}@${m.version}`).join(', ')}`;
|
|
241
|
+
return `${loader}, ${mods}`;
|
|
242
|
+
}
|
package/src/lib/snippets.js
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import { normalizeModulesList } from './project-modules.js';
|
|
2
|
+
import { STAGEPASS_PACKAGES_VERSION } from './versions.js';
|
|
3
|
+
|
|
1
4
|
/**
|
|
2
5
|
* Safe token for CDN pins and data-stagepass-version (blocks HTML/attr injection).
|
|
3
6
|
* @param {unknown} version
|
|
@@ -19,12 +22,21 @@ export function sanitizeSnippetVersion(version) {
|
|
|
19
22
|
*
|
|
20
23
|
* @param {object} options
|
|
21
24
|
* @param {string} options.version Exact package version for data-stagepass-version / docs
|
|
22
|
-
* @param {boolean} [options.modules]
|
|
25
|
+
* @param {boolean} [options.modules] Legacy: true = inject + cookies tags
|
|
26
|
+
* @param {string[]} [options.moduleList] Declared modules (wins over `modules`)
|
|
23
27
|
* @returns {{ head: string, footer: string, majorPin: string, exactPin: string }}
|
|
24
28
|
*/
|
|
25
29
|
export function buildWebflowSnippets(options) {
|
|
26
|
-
const version = sanitizeSnippetVersion(options.version ||
|
|
27
|
-
|
|
30
|
+
const version = sanitizeSnippetVersion(options.version || STAGEPASS_PACKAGES_VERSION);
|
|
31
|
+
/** @type {string[]} */
|
|
32
|
+
let moduleFiles = [];
|
|
33
|
+
if (Array.isArray(options.moduleList)) {
|
|
34
|
+
const parsed = normalizeModulesList(options.moduleList);
|
|
35
|
+
if (!parsed.ok) throw new Error(parsed.error);
|
|
36
|
+
moduleFiles = parsed.modules;
|
|
37
|
+
} else if (options.modules === true) {
|
|
38
|
+
moduleFiles = ['inject', 'cookies'];
|
|
39
|
+
}
|
|
28
40
|
const majorPin = '@2';
|
|
29
41
|
const exactPin = `@${version}`;
|
|
30
42
|
|
|
@@ -36,10 +48,9 @@ export function buildWebflowSnippets(options) {
|
|
|
36
48
|
'></script>',
|
|
37
49
|
];
|
|
38
50
|
|
|
39
|
-
|
|
51
|
+
for (const id of moduleFiles) {
|
|
40
52
|
loaderLines.push(
|
|
41
|
-
`<script src="https://cdn.jsdelivr.net/npm/@stagepass/modules${majorPin}/dist
|
|
42
|
-
`<script src="https://cdn.jsdelivr.net/npm/@stagepass/modules${majorPin}/dist/cookies.min.js"></script>`
|
|
53
|
+
`<script src="https://cdn.jsdelivr.net/npm/@stagepass/modules${majorPin}/dist/${id}.min.js"></script>`
|
|
43
54
|
);
|
|
44
55
|
}
|
|
45
56
|
|
|
@@ -78,6 +89,6 @@ export function printWebflowSnippets(snippets, log = console.log) {
|
|
|
78
89
|
log('\nWebflow Custom Code (Footer):');
|
|
79
90
|
log(snippets.footer);
|
|
80
91
|
log(
|
|
81
|
-
|
|
92
|
+
`\nOptional exact pin example (instead of @2): replace @2 with the exact version (e.g. @${STAGEPASS_PACKAGES_VERSION}).`
|
|
82
93
|
);
|
|
83
94
|
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Version of @stagepass/loader and @stagepass/modules this CLI targets.
|
|
3
|
+
*
|
|
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
|
+
*/
|
|
9
|
+
export const STAGEPASS_PACKAGES_VERSION = '2.2.0';
|
|
10
|
+
|
|
11
|
+
/**
|
|
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.
|
|
15
|
+
*
|
|
16
|
+
* @param {string} [version]
|
|
17
|
+
* @returns {string}
|
|
18
|
+
*/
|
|
19
|
+
export function packagesRange(version = STAGEPASS_PACKAGES_VERSION) {
|
|
20
|
+
const match = /^[~^]?(\d+)(?:\.\d+){0,2}$/.exec(String(version).trim());
|
|
21
|
+
if (!match) {
|
|
22
|
+
throw new Error(`Invalid @stagepass package version: ${version}`);
|
|
23
|
+
}
|
|
24
|
+
return `^${match[1]}`;
|
|
25
|
+
}
|
|
@@ -7,8 +7,8 @@ Profile for AI coding agents and humans working on this Webflow + Stagepass proj
|
|
|
7
7
|
- **CLI:** local `.sp` HTTPS domains (Caddy), DNS, optional PHP, `link` / `unlink` / `start` / `stop` / `reload`, plus `doctor` / `certs` / `autostart` / `daemon` and project helpers `new` / `build` / `watch` / `upgrade`
|
|
8
8
|
- **Loader:** swaps tagged production assets for local URLs when a session is active (`?stagepass={{NAME}}.sp`)
|
|
9
9
|
- **Session:** `localStorage` JSON `{ domain, expires }` (default 12 h)
|
|
10
|
-
- **Modules (optional):**
|
|
11
|
-
- **Runtime API:**
|
|
10
|
+
- **Modules (optional):** separate script tags after `loader.min.js`: `inject`, `cookies`, `maintenance`, or `all`. This project declares the modules its Webflow site loads in `stagepass.json` (`modules`); the API reference below only covers those.
|
|
11
|
+
- **Runtime API:** use `sp` (see "Stagepass runtime API" below)
|
|
12
12
|
- **CDN:** Prefer jsDelivr with major pin `@2` (exact pin optional), e.g. `https://cdn.jsdelivr.net/npm/@stagepass/loader@2/dist/loader.min.js`. unpkg serves the same paths if you need an alternative CDN.
|
|
13
13
|
|
|
14
14
|
## Safe development rules (beginners)
|
|
@@ -33,5 +33,7 @@ Then open `https://my-project.webflow.io/?stagepass={{NAME}}.sp`.
|
|
|
33
33
|
|
|
34
34
|
## Types
|
|
35
35
|
|
|
36
|
-
|
|
37
|
-
|
|
36
|
+
`{{CONFIG_FILE}}` lists `types: {{TYPES_INLINE}}`, generated from `stagepass.json`. Modules that are not declared have no types, so the editor flags `sp.<module>` calls that would fail on the live site. After enabling a module (Webflow script tag + `stagepass.json`), run `stagepass upgrade`.
|
|
37
|
+
|
|
38
|
+
{{STAGEPASS_BLOCK}}
|
|
39
|
+
|