@stagepass/cli 2.4.1 → 2.6.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 +37 -21
- package/package.json +1 -1
- package/src/cli.js +4 -1
- package/src/commands/dev.js +11 -2
- package/src/commands/new.js +53 -3
- package/src/commands/start.js +23 -1
- package/src/commands/upgrade.js +172 -54
- package/src/lib/ai-guide.js +526 -23
- package/src/lib/build-script-upgrade.js +24 -10
- package/src/lib/cdn-base.js +72 -0
- package/src/lib/cdn-snippets.js +12 -8
- package/src/lib/doctor-site.js +17 -0
- package/src/lib/doctor.js +3 -3
- package/src/lib/live-server.js +141 -34
- package/src/lib/package-versions.js +1 -1
- package/src/lib/project-sync.js +543 -32
- package/src/lib/registry.js +9 -7
- package/src/lib/scaffold.js +45 -5
- package/src/lib/site-detect.js +13 -1
- package/src/lib/snippets.js +267 -35
- package/src/lib/versions.js +9 -8
- package/templates/project/AGENTS.md.tmpl +2 -35
- package/templates/project/README.md.tmpl +4 -2
- package/templates/project/api/README.md.tmpl +16 -195
- package/templates/project/api/action.php +11 -2
- package/templates/project/api/env.example +11 -1
- package/templates/project/api/lib/config.php +5 -1
- package/templates/project/api/lib/request.php +366 -3
- package/templates/project/api/lib/security.php +116 -34
- package/templates/project/php-private/config.php +2 -0
package/src/lib/scaffold.js
CHANGED
|
@@ -1,8 +1,16 @@
|
|
|
1
1
|
import path from 'node:path';
|
|
2
2
|
import { fileURLToPath } from 'node:url';
|
|
3
3
|
import { buildWebflowSnippets } from './snippets.js';
|
|
4
|
-
import {
|
|
4
|
+
import {
|
|
5
|
+
CURSOR_RULE_PATH,
|
|
6
|
+
STAGEPASS_AGENTS_REL,
|
|
7
|
+
buildAgentsPointerBlock,
|
|
8
|
+
buildCursorRule,
|
|
9
|
+
buildStagepassGuide,
|
|
10
|
+
renderStagepassAgentsFile,
|
|
11
|
+
} from './ai-guide.js';
|
|
5
12
|
import { typesForModules } from './project-modules.js';
|
|
13
|
+
import { SNIPPET_LOADER_VERSION, normalizeCdnChoice } from './cdn-base.js';
|
|
6
14
|
import { LOADER_VERSION, packagesRange } from './versions.js';
|
|
7
15
|
|
|
8
16
|
const __filename = fileURLToPath(import.meta.url);
|
|
@@ -92,6 +100,7 @@ export function scaffoldPhpTemplateMap(options = {}) {
|
|
|
92
100
|
* @property {boolean} install
|
|
93
101
|
* @property {boolean} dryRun
|
|
94
102
|
* @property {string} [loaderVersion] @stagepass/loader + modules version (snippets)
|
|
103
|
+
* @property {'stagepass' | 'jsdelivr'} [cdn] Webflow snippet CDN, stored in stagepass.json
|
|
95
104
|
* @property {string} [packagesRange] Override for package.json ranges (default ^<major>, e.g. ^2)
|
|
96
105
|
* @property {boolean} [perModuleTypes] false: use the umbrella `@stagepass/modules` types (modules < 2.2.0)
|
|
97
106
|
*/
|
|
@@ -168,6 +177,9 @@ export function buildStagepassExtraLines(options) {
|
|
|
168
177
|
}
|
|
169
178
|
// Always record php for new projects so link/doctor can tell intentional off from legacy.
|
|
170
179
|
lines.push(`\n "php": ${options.php === true ? 'true' : 'false'},`);
|
|
180
|
+
// Same default as snippet generation: missing or unknown values are the Stagepass CDN.
|
|
181
|
+
const cdn = normalizeCdnChoice(options.cdn) || 'stagepass';
|
|
182
|
+
lines.push(`\n "cdn": ${JSON.stringify(cdn)},`);
|
|
171
183
|
return lines.join('');
|
|
172
184
|
}
|
|
173
185
|
|
|
@@ -179,7 +191,7 @@ export function buildStagepassExtraLines(options) {
|
|
|
179
191
|
* @returns {Promise<{ relativePath: string, contents: string }[]>}
|
|
180
192
|
*/
|
|
181
193
|
export async function planScaffoldFiles(options, deps = {}) {
|
|
182
|
-
const version = options.loaderVersion ||
|
|
194
|
+
const version = options.loaderVersion || SNIPPET_LOADER_VERSION;
|
|
183
195
|
const isTs = options.lang === 'ts';
|
|
184
196
|
const useSass = options.styles === 'scss';
|
|
185
197
|
const entryJs = isTs ? 'src/main.ts' : 'src/main.js';
|
|
@@ -192,6 +204,10 @@ export async function planScaffoldFiles(options, deps = {}) {
|
|
|
192
204
|
const snippets = buildWebflowSnippets({
|
|
193
205
|
version,
|
|
194
206
|
moduleList: siteModules,
|
|
207
|
+
gitRemote: options.git ? options.remote || '' : '',
|
|
208
|
+
gitBranch: options.git ? 'main' : '',
|
|
209
|
+
projectName: options.name,
|
|
210
|
+
cdn: options.cdn,
|
|
195
211
|
});
|
|
196
212
|
const types = typesForModules(siteModules, {
|
|
197
213
|
modulesDependency: options.modules === true,
|
|
@@ -207,10 +223,12 @@ export async function planScaffoldFiles(options, deps = {}) {
|
|
|
207
223
|
ORIGIN_LINE: buildStagepassExtraLines(options),
|
|
208
224
|
TYPES_JSON: JSON.stringify(types),
|
|
209
225
|
TYPES_INLINE: JSON.stringify(types).replace(/","/g, '", "'),
|
|
210
|
-
STAGEPASS_BLOCK:
|
|
226
|
+
STAGEPASS_BLOCK: buildAgentsPointerBlock(),
|
|
211
227
|
LOADER_VERSION: version,
|
|
212
228
|
HEAD_SNIPPET: snippets.head,
|
|
213
229
|
FOOTER_SNIPPET: snippets.footer,
|
|
230
|
+
ASSET_EXAMPLE_URL: snippets.assetExampleUrl,
|
|
231
|
+
ASSET_URL_HINT: snippets.assetHint,
|
|
214
232
|
ENTRY_JS: entryJs,
|
|
215
233
|
ENTRY_CSS: entryCss,
|
|
216
234
|
USE_SASS: useSass ? 'true' : 'false',
|
|
@@ -223,7 +241,7 @@ export async function planScaffoldFiles(options, deps = {}) {
|
|
|
223
241
|
'## PHP',
|
|
224
242
|
'',
|
|
225
243
|
`PHP is on. Caddy serves \`https://${options.name}.sp/api/action.php\` with \`php_fastcgi\`.`,
|
|
226
|
-
'Read `api/README.md`. Copy `api/.env.example` to `.env` in the project root (do not commit it) and set `ALLOWED_ORIGINS` to exact origins, including that `.sp` URL. From Webflow, call the PHP host with its absolute URL, not `window.location.origin`.',
|
|
244
|
+
'Read `api/README.md` and the PHP section of `AGENTS.stagepass.md`. Copy `api/.env.example` to `.env` in the project root (do not commit it) and set `ALLOWED_ORIGINS` to exact origins, including that `.sp` URL. From Webflow, call the PHP host with its absolute URL, not `window.location.origin`.',
|
|
227
245
|
'Apache uses the project `.htaccess`. nginx needs the snippet in `api/README.md` before the PHP location.',
|
|
228
246
|
'',
|
|
229
247
|
].join('\n')
|
|
@@ -265,7 +283,29 @@ export async function planScaffoldFiles(options, deps = {}) {
|
|
|
265
283
|
|
|
266
284
|
// Generated in code (not a template): npm drops dotfiles and dot folders
|
|
267
285
|
// are easy to lose in the published package.
|
|
268
|
-
files.push({
|
|
286
|
+
files.push({
|
|
287
|
+
relativePath: STAGEPASS_AGENTS_REL,
|
|
288
|
+
contents: renderStagepassAgentsFile(
|
|
289
|
+
buildStagepassGuide({
|
|
290
|
+
name: options.name,
|
|
291
|
+
domain: `${options.name}.sp`,
|
|
292
|
+
modules: siteModules,
|
|
293
|
+
php,
|
|
294
|
+
phpBoilerplate: php,
|
|
295
|
+
lang: options.lang,
|
|
296
|
+
origin: options.origin || null,
|
|
297
|
+
production: options.production || null,
|
|
298
|
+
configFile,
|
|
299
|
+
types,
|
|
300
|
+
modulesDependency: options.modules === true,
|
|
301
|
+
cdn: options.cdn,
|
|
302
|
+
loaderVersion: version,
|
|
303
|
+
})
|
|
304
|
+
),
|
|
305
|
+
});
|
|
306
|
+
if (options.cursorRule === true) {
|
|
307
|
+
files.push({ relativePath: CURSOR_RULE_PATH, contents: buildCursorRule() });
|
|
308
|
+
}
|
|
269
309
|
|
|
270
310
|
return files;
|
|
271
311
|
}
|
package/src/lib/site-detect.js
CHANGED
|
@@ -19,9 +19,12 @@ export { HOST_WHITESPACE_ERROR };
|
|
|
19
19
|
// Bounded attribute run keeps the scan linear on hostile input.
|
|
20
20
|
const SCRIPT_TAG_RE = /<script\b[^<>]{0,4096}>/gi;
|
|
21
21
|
const SRC_ATTR_RE = /\ssrc\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s"'>]+))/i;
|
|
22
|
-
//
|
|
22
|
+
// jsDelivr / unpkg: /dist/<file>.js or package-root URLs (optional slash/query); root via package `main`.
|
|
23
23
|
const STAGEPASS_CDN_RE =
|
|
24
24
|
/^(?:https?:)?\/\/(cdn\.jsdelivr\.net\/npm|unpkg\.com)\/@stagepass\/(loader|modules)(?:@([0-9A-Za-z.^~_-]+))?(?:\/dist\/([a-z]+)(?:\.min)?\.js)?\/?(?:[?#].*)?$/i;
|
|
25
|
+
// Stagepass CDN: /loader@2.5.0/loader.min.js (no /dist/, no @scope). Short /loader@2.5.0 also matches.
|
|
26
|
+
const STAGEPASS_OWN_CDN_RE =
|
|
27
|
+
/^(?:https?:)?\/\/cdn\.stagepass\.one\/(loader|modules)(?:@([0-9A-Za-z.^~_-]+))?(?:\/([a-z]+)(?:\.min)?\.js)?\/?(?:[?#].*)?$/i;
|
|
25
28
|
// Self-hosted build filenames (need Stagepass path or data-* hint).
|
|
26
29
|
const STAGEPASS_FILE_RE = /(?:^|\/)(loader|inject|cookies|maintenance|all)(?:\.min)?\.js$/i;
|
|
27
30
|
// `…/stagepass/…` always; `…/@stagepass/…` only for relative src (blocks foreign-host spoofs).
|
|
@@ -148,6 +151,15 @@ export function parseStagepassScripts(html) {
|
|
|
148
151
|
if (!attr) continue;
|
|
149
152
|
const src = q(attr);
|
|
150
153
|
|
|
154
|
+
const ownMatch = STAGEPASS_OWN_CDN_RE.exec(src);
|
|
155
|
+
if (ownMatch) {
|
|
156
|
+
const pkg = ownMatch[1].toLowerCase();
|
|
157
|
+
const file = (ownMatch[3] || (pkg === 'modules' ? 'all' : 'loader')).toLowerCase();
|
|
158
|
+
if (pkg === 'loader' ? file === 'loader' : file !== 'loader') {
|
|
159
|
+
take(file, ownMatch[2] || 'latest', 'stagepass');
|
|
160
|
+
}
|
|
161
|
+
continue;
|
|
162
|
+
}
|
|
151
163
|
const cdnMatch = STAGEPASS_CDN_RE.exec(src);
|
|
152
164
|
if (cdnMatch) {
|
|
153
165
|
const pkg = cdnMatch[2].toLowerCase();
|
package/src/lib/snippets.js
CHANGED
|
@@ -1,33 +1,192 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { execFile as nodeExecFile } from 'node:child_process';
|
|
2
|
+
import { promisify } from 'node:util';
|
|
3
|
+
import {
|
|
4
|
+
SNIPPET_LOADER_VERSION,
|
|
5
|
+
SNIPPET_MODULES_VERSION,
|
|
6
|
+
defaultSnippetPins,
|
|
7
|
+
stagepassPackageUrl,
|
|
8
|
+
} from './cdn-base.js';
|
|
9
|
+
import { validateProjectName } from './project-name.js';
|
|
10
|
+
import { expandModules, normalizeModulesList } from './project-modules.js';
|
|
11
|
+
|
|
12
|
+
const execFileAsync = promisify(nodeExecFile);
|
|
13
|
+
|
|
14
|
+
/** Owner/repo and similar plain tokens. Version pins use {@link SNIPPET_VERSION_RE}. */
|
|
15
|
+
const SNIPPET_TOKEN_BODY = '[0-9A-Za-z][0-9A-Za-z._+-]*';
|
|
16
|
+
const SNIPPET_TOKEN_RE = new RegExp(`^${SNIPPET_TOKEN_BODY}$`);
|
|
17
|
+
/** Exact stable semver. Ranges, dist-tags, prerelease, and major aliases are rejected. */
|
|
18
|
+
const SNIPPET_VERSION_RE = /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)$/;
|
|
19
|
+
|
|
20
|
+
const GITHUB_HTTPS_RE =
|
|
21
|
+
/^https?:\/\/(?:www\.)?github\.com(?::\d+)?\/([^/?#\s]+)\/([^/?#\s]+?)(?:\.git)?\/?(?:[?#].*)?$/i;
|
|
22
|
+
const GITHUB_SSH_URL_RE =
|
|
23
|
+
/^ssh:\/\/(?:git@)?github\.com(?::\d+)?\/([^/?#\s]+)\/([^/?#\s]+?)(?:\.git)?\/?$/i;
|
|
24
|
+
const GITHUB_SCP_RE = /^git@github\.com:([^/\s]+)\/([^/\s]+?)(?:\.git)?$/i;
|
|
3
25
|
|
|
4
26
|
/**
|
|
5
|
-
*
|
|
27
|
+
* Exact semver for CDN pins and data-stagepass-version (blocks HTML/attr injection).
|
|
28
|
+
* Accepts only `x.y.z`. Callers pass a registry-resolved release or `SNIPPET_LOADER_VERSION`.
|
|
6
29
|
* @param {unknown} version
|
|
7
30
|
* @returns {string}
|
|
8
31
|
*/
|
|
9
32
|
export function sanitizeSnippetVersion(version) {
|
|
10
33
|
const value = String(version == null ? '' : version).trim();
|
|
11
|
-
|
|
12
|
-
|
|
34
|
+
if (!SNIPPET_VERSION_RE.test(value)) {
|
|
35
|
+
throw new Error(
|
|
36
|
+
'Invalid version for Webflow snippets (use an exact semver x.y.z).'
|
|
37
|
+
);
|
|
38
|
+
}
|
|
39
|
+
return value;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Owner/repo token. Same character rules as {@link sanitizeSnippetVersion}.
|
|
44
|
+
* @param {unknown} value
|
|
45
|
+
* @returns {string}
|
|
46
|
+
*/
|
|
47
|
+
export function sanitizeSnippetToken(value) {
|
|
48
|
+
const token = String(value == null ? '' : value).trim();
|
|
49
|
+
if (!SNIPPET_TOKEN_RE.test(token)) {
|
|
50
|
+
throw new Error(
|
|
51
|
+
'Invalid name for Webflow snippets (owner and repo must be plain tokens).'
|
|
52
|
+
);
|
|
53
|
+
}
|
|
54
|
+
return token;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* @param {unknown} remote
|
|
59
|
+
* @returns {{ owner: string, repo: string } | null}
|
|
60
|
+
*/
|
|
61
|
+
function parseGitHubRemote(remote) {
|
|
62
|
+
const value = String(remote == null ? '' : remote).trim();
|
|
63
|
+
if (!value) return null;
|
|
64
|
+
|
|
65
|
+
const match =
|
|
66
|
+
GITHUB_HTTPS_RE.exec(value) ||
|
|
67
|
+
GITHUB_SSH_URL_RE.exec(value) ||
|
|
68
|
+
GITHUB_SCP_RE.exec(value);
|
|
69
|
+
const githubShaped =
|
|
70
|
+
/^(?:https?:\/\/(?:www\.)?github\.com(?::\d+)?(?:\/|$)|ssh:\/\/(?:git@)?github\.com(?::\d+)?(?:\/|$)|git@github\.com:)/i.test(
|
|
71
|
+
value
|
|
72
|
+
);
|
|
73
|
+
if (!match) {
|
|
74
|
+
if (githubShaped && /[<>"'`\\\s]/.test(value)) {
|
|
75
|
+
throw new Error(
|
|
76
|
+
'Invalid name for Webflow snippets (owner and repo must be plain tokens).'
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
return null;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
let owner = match[1];
|
|
83
|
+
let repo = match[2];
|
|
84
|
+
try {
|
|
85
|
+
owner = decodeURIComponent(owner);
|
|
86
|
+
repo = decodeURIComponent(repo);
|
|
87
|
+
} catch {
|
|
13
88
|
throw new Error(
|
|
14
|
-
'Invalid
|
|
89
|
+
'Invalid name for Webflow snippets (owner and repo must be plain tokens).'
|
|
15
90
|
);
|
|
16
91
|
}
|
|
92
|
+
return {
|
|
93
|
+
owner: sanitizeSnippetToken(owner),
|
|
94
|
+
repo: sanitizeSnippetToken(repo),
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// Missing or unsafe branch names fall back to main (slashes are allowed).
|
|
99
|
+
function safeBranch(branch) {
|
|
100
|
+
const value = String(branch == null ? '' : branch).trim();
|
|
101
|
+
if (!value || value === 'HEAD') return 'main';
|
|
102
|
+
if (!/^[0-9A-Za-z][0-9A-Za-z._+/-]*$/.test(value)) return 'main';
|
|
103
|
+
if (value.includes('..') || value.includes('//') || value.endsWith('/')) return 'main';
|
|
17
104
|
return value;
|
|
18
105
|
}
|
|
19
106
|
|
|
20
107
|
/**
|
|
21
|
-
*
|
|
108
|
+
* @param {object} [options]
|
|
109
|
+
* @param {unknown} [options.gitRemote]
|
|
110
|
+
* @param {unknown} [options.gitBranch]
|
|
111
|
+
* @param {unknown} [options.projectName]
|
|
112
|
+
*/
|
|
113
|
+
function describeProjectAssets(options = {}) {
|
|
114
|
+
const parsed = parseGitHubRemote(options.gitRemote);
|
|
115
|
+
if (!parsed) {
|
|
116
|
+
const name = validateProjectName(options.projectName);
|
|
117
|
+
const segment = name.ok ? name.name : 'project';
|
|
118
|
+
return {
|
|
119
|
+
kind: 'placeholder',
|
|
120
|
+
js: 'https://YOUR-PRODUCTION-URL/js/main.js',
|
|
121
|
+
css: 'https://YOUR-PRODUCTION-URL/css/styles.css',
|
|
122
|
+
exampleJs: `https://YOUR-PRODUCTION-URL/${segment}/main.a1b2c3d4.js`,
|
|
123
|
+
hint: 'Replace YOUR-PRODUCTION-URL with the host that serves your production assets.',
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
const branch = safeBranch(options.gitBranch);
|
|
127
|
+
const base = `https://cdn.jsdelivr.net/gh/${parsed.owner}/${parsed.repo}@${branch}`;
|
|
128
|
+
return {
|
|
129
|
+
kind: 'github',
|
|
130
|
+
js: `${base}/dist/main.js`,
|
|
131
|
+
css: `${base}/dist/styles.css`,
|
|
132
|
+
exampleJs: `${base}/dist/main.a1b2c3d4.js`,
|
|
133
|
+
hint: 'jsDelivr only serves public repos. A tag is a better production pin than a branch. After npm run build, commit the dist folder, or check dist/manifest.json for hashed file names.',
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* @param {{ kind: string, js: string, css: string }} assets
|
|
139
|
+
* @returns {string}
|
|
140
|
+
*/
|
|
141
|
+
function footerFor(assets) {
|
|
142
|
+
const notes =
|
|
143
|
+
assets.kind === 'github'
|
|
144
|
+
? [
|
|
145
|
+
'<!-- jsDelivr only serves public repos. A tag is a better production pin than a branch. -->',
|
|
146
|
+
'<!-- After `npm run build`, commit the dist folder, or check dist/manifest.json for hashed file names. -->',
|
|
147
|
+
]
|
|
148
|
+
: [
|
|
149
|
+
'<!-- Replace YOUR-PRODUCTION-URL with the host that serves your production assets. -->',
|
|
150
|
+
'<!-- After `npm run build`, check dist/manifest.json for hashed file names. -->',
|
|
151
|
+
];
|
|
152
|
+
return [
|
|
153
|
+
...notes,
|
|
154
|
+
'<script',
|
|
155
|
+
` data-src="${assets.js}"`,
|
|
156
|
+
' data-stagepass="true"',
|
|
157
|
+
' data-stagepass-path="dist/main.js"',
|
|
158
|
+
'></script>',
|
|
159
|
+
'<link',
|
|
160
|
+
' rel="stylesheet"',
|
|
161
|
+
` data-href="${assets.css}"`,
|
|
162
|
+
' data-stagepass="true"',
|
|
163
|
+
' data-stagepass-path="dist/styles.css"',
|
|
164
|
+
'>',
|
|
165
|
+
].join('\n');
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Build recommended Webflow Custom Code snippets.
|
|
170
|
+
* Default host is the Stagepass CDN with exact pins. Pass `cdn: 'jsdelivr'`
|
|
171
|
+
* for the jsDelivr major pin `@2` alternative.
|
|
22
172
|
*
|
|
23
173
|
* @param {object} options
|
|
24
|
-
* @param {string} options.version Exact package version for data-stagepass-version
|
|
174
|
+
* @param {string} options.version Exact package version for the Stagepass CDN URL pin and data-stagepass-version
|
|
25
175
|
* @param {boolean} [options.modules] Legacy: true = inject + cookies tags
|
|
26
176
|
* @param {string[]} [options.moduleList] Declared modules (wins over `modules`)
|
|
27
|
-
* @
|
|
177
|
+
* @param {string} [options.gitRemote] `origin` URL. GitHub https/ssh becomes a jsDelivr `/gh/` URL.
|
|
178
|
+
* @param {string} [options.gitBranch] Current branch. Missing or unsafe values use `main`.
|
|
179
|
+
* @param {string} [options.projectName] Used in the README placeholder example path.
|
|
180
|
+
* @param {string} [options.cdn] `stagepass` (default) or `jsdelivr`
|
|
181
|
+
* @returns {{ head: string, footer: string, majorPin: string, exactPin: string, assetExampleUrl: string, assetHint: string, cdn: string }}
|
|
28
182
|
*/
|
|
29
183
|
export function buildWebflowSnippets(options) {
|
|
30
|
-
|
|
184
|
+
// Fail closed on an explicit unresolvable version (empty or invalid).
|
|
185
|
+
// Omitted version falls back to the bundled exact pin, used for both the
|
|
186
|
+
// Stagepass CDN URL and data-stagepass-version (single source of truth).
|
|
187
|
+
const version = sanitizeSnippetVersion(
|
|
188
|
+
options.version == null ? SNIPPET_LOADER_VERSION : options.version
|
|
189
|
+
);
|
|
31
190
|
/** @type {string[]} */
|
|
32
191
|
let moduleFiles = [];
|
|
33
192
|
if (Array.isArray(options.moduleList)) {
|
|
@@ -37,47 +196,122 @@ export function buildWebflowSnippets(options) {
|
|
|
37
196
|
} else if (options.modules === true) {
|
|
38
197
|
moduleFiles = ['inject', 'cookies'];
|
|
39
198
|
}
|
|
40
|
-
const
|
|
199
|
+
const pins = defaultSnippetPins(options.cdn, { loaderVersion: version });
|
|
200
|
+
const loaderPin = pins.loaderPin;
|
|
201
|
+
const modulesPin = pins.modulesPin;
|
|
41
202
|
const exactPin = `@${version}`;
|
|
203
|
+
const assets = describeProjectAssets(options);
|
|
204
|
+
const loaderComment =
|
|
205
|
+
pins.cdn === 'jsdelivr'
|
|
206
|
+
? '<!-- Stagepass loader (jsDelivr major pin @2; optional exact pin shown in data-stagepass-version) -->'
|
|
207
|
+
: '<!-- Stagepass loader (Stagepass CDN, exact pin) -->';
|
|
42
208
|
|
|
43
209
|
const loaderLines = [
|
|
44
|
-
|
|
210
|
+
loaderComment,
|
|
45
211
|
'<script',
|
|
46
|
-
` src="
|
|
212
|
+
` src="${stagepassPackageUrl('loader', loaderPin, 'loader.min.js', { cdn: pins.cdn })}"`,
|
|
47
213
|
` data-stagepass-version="${version}"`,
|
|
48
214
|
'></script>',
|
|
49
215
|
];
|
|
50
216
|
|
|
51
217
|
for (const id of moduleFiles) {
|
|
52
218
|
loaderLines.push(
|
|
53
|
-
`<script src="
|
|
219
|
+
`<script src="${stagepassPackageUrl('modules', modulesPin, `${id}.min.js`, { cdn: pins.cdn })}"></script>`
|
|
54
220
|
);
|
|
55
221
|
}
|
|
56
222
|
|
|
57
|
-
const footerLines = [
|
|
58
|
-
'<!-- Project assets: production URLs in data-src / data-href; local paths for Stagepass -->',
|
|
59
|
-
'<!-- After `npm run build`, check dist/manifest.json for content-hashed filenames. -->',
|
|
60
|
-
'<script',
|
|
61
|
-
' data-src="https://cdn.example.com/js/main.js"',
|
|
62
|
-
' data-stagepass="true"',
|
|
63
|
-
' data-stagepass-path="dist/main.js"',
|
|
64
|
-
'></script>',
|
|
65
|
-
'<link',
|
|
66
|
-
' rel="stylesheet"',
|
|
67
|
-
' data-href="https://cdn.example.com/css/styles.css"',
|
|
68
|
-
' data-stagepass="true"',
|
|
69
|
-
' data-stagepass-path="dist/styles.css"',
|
|
70
|
-
'>',
|
|
71
|
-
];
|
|
72
|
-
|
|
73
223
|
return {
|
|
74
224
|
head: loaderLines.join('\n'),
|
|
75
|
-
footer:
|
|
76
|
-
majorPin,
|
|
225
|
+
footer: footerFor(assets),
|
|
226
|
+
majorPin: loaderPin,
|
|
77
227
|
exactPin,
|
|
228
|
+
assetExampleUrl: assets.exampleJs,
|
|
229
|
+
assetHint: assets.hint,
|
|
230
|
+
cdn: pins.cdn,
|
|
78
231
|
};
|
|
79
232
|
}
|
|
80
233
|
|
|
234
|
+
/**
|
|
235
|
+
* Head snippet for the loader and the declared modules. Used by `stagepass start`.
|
|
236
|
+
* `all` expands to inject, cookies, maintenance, in that order. An empty list is the loader only.
|
|
237
|
+
* @param {object} [options]
|
|
238
|
+
* @param {string} [options.version]
|
|
239
|
+
* @param {string[]} [options.moduleList] Declared modules (`[]` when none)
|
|
240
|
+
*/
|
|
241
|
+
export function buildOfficialHeadSnippets(options = {}) {
|
|
242
|
+
const raw = Array.isArray(options.moduleList) ? options.moduleList : [];
|
|
243
|
+
const parsed = normalizeModulesList(raw);
|
|
244
|
+
if (!parsed.ok) throw new Error(parsed.error);
|
|
245
|
+
return buildWebflowSnippets({
|
|
246
|
+
version: options.version,
|
|
247
|
+
moduleList: expandModules(parsed.modules),
|
|
248
|
+
cdn: options.cdn,
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* `origin` URL and current branch for footer asset URLs. Empty remote if git is missing.
|
|
254
|
+
* @param {string} cwd
|
|
255
|
+
* @param {import('./exec.js').ExecFn} [exec]
|
|
256
|
+
* @returns {Promise<{ gitRemote: string, gitBranch: string }>}
|
|
257
|
+
*/
|
|
258
|
+
export async function readSnippetGitRef(cwd, exec) {
|
|
259
|
+
const gitRemote = await gitText(cwd, ['remote', 'get-url', 'origin'], exec);
|
|
260
|
+
// symbolic-ref and branch --show-current still work on an unborn branch
|
|
261
|
+
// (no commits yet). rev-parse --abbrev-ref HEAD does not.
|
|
262
|
+
let gitBranch = await gitText(cwd, ['symbolic-ref', '--short', 'HEAD'], exec);
|
|
263
|
+
if (!gitBranch) gitBranch = await gitText(cwd, ['branch', '--show-current'], exec);
|
|
264
|
+
if (!gitBranch || gitBranch === 'HEAD') gitBranch = 'main';
|
|
265
|
+
return { gitRemote, gitBranch };
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* @param {string} cwd
|
|
270
|
+
* @param {string[]} args
|
|
271
|
+
* @param {import('./exec.js').ExecFn} [exec]
|
|
272
|
+
* @returns {Promise<string>}
|
|
273
|
+
*/
|
|
274
|
+
async function gitText(cwd, args, exec) {
|
|
275
|
+
try {
|
|
276
|
+
const result = exec
|
|
277
|
+
? await exec('git', args, { cwd })
|
|
278
|
+
: await execFileAsync('git', args, {
|
|
279
|
+
cwd,
|
|
280
|
+
encoding: 'utf8',
|
|
281
|
+
timeout: 5000,
|
|
282
|
+
maxBuffer: 64 * 1024,
|
|
283
|
+
});
|
|
284
|
+
return String(result?.stdout || '').trim();
|
|
285
|
+
} catch {
|
|
286
|
+
return '';
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
function logExactPin(log, snippets) {
|
|
291
|
+
// exactPin is `@` plus the sanitized version already used in the head snippet.
|
|
292
|
+
const loader = String(snippets?.exactPin || '').replace(/^@/, '') || SNIPPET_LOADER_VERSION;
|
|
293
|
+
if (snippets?.cdn === 'jsdelivr') {
|
|
294
|
+
log(
|
|
295
|
+
`\nOptional exact pin example (instead of @2): loader @${loader}, modules @${SNIPPET_MODULES_VERSION}.`
|
|
296
|
+
);
|
|
297
|
+
return;
|
|
298
|
+
}
|
|
299
|
+
log(
|
|
300
|
+
`\njsDelivr alternative (major pin @2): <script src="https://cdn.jsdelivr.net/npm/@stagepass/loader@2/dist/loader.min.js" data-stagepass-version="${loader}"></script>`
|
|
301
|
+
);
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/**
|
|
305
|
+
* Print the loader/modules head snippet.
|
|
306
|
+
* @param {{ head: string }} snippets
|
|
307
|
+
* @param {(msg: string) => void} [log]
|
|
308
|
+
*/
|
|
309
|
+
export function printWebflowHeadSnippets(snippets, log = console.log) {
|
|
310
|
+
log('\nWebflow Custom Code (Head):');
|
|
311
|
+
log(snippets.head);
|
|
312
|
+
logExactPin(log, snippets);
|
|
313
|
+
}
|
|
314
|
+
|
|
81
315
|
/**
|
|
82
316
|
* Print head/footer snippets with section labels.
|
|
83
317
|
* @param {{ head: string, footer: string }} snippets
|
|
@@ -88,7 +322,5 @@ export function printWebflowSnippets(snippets, log = console.log) {
|
|
|
88
322
|
log(snippets.head);
|
|
89
323
|
log('\nWebflow Custom Code (Footer):');
|
|
90
324
|
log(snippets.footer);
|
|
91
|
-
log
|
|
92
|
-
`\nOptional exact pin example (instead of @2): loader @${LOADER_VERSION}, modules @${MODULES_VERSION}.`
|
|
93
|
-
);
|
|
325
|
+
logExactPin(log, snippets);
|
|
94
326
|
}
|
package/src/lib/versions.js
CHANGED
|
@@ -2,11 +2,13 @@
|
|
|
2
2
|
* Versions of @stagepass/loader and @stagepass/modules this CLI targets.
|
|
3
3
|
*
|
|
4
4
|
* Values come from the generated `package-versions.js` (run
|
|
5
|
-
* `node scripts/sync-cli-versions.mjs`).
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
5
|
+
* `node scripts/sync-cli-versions.mjs`). They track the monorepo packages.
|
|
6
|
+
* Snippet exact pins do not use them: those live in `cdn-base.js`
|
|
7
|
+
* (`SNIPPET_LOADER_VERSION`, `SNIPPET_MODULES_VERSION`). Dependency ranges
|
|
8
|
+
* never come from the CLI version: new projects use the major range (`^2`)
|
|
9
|
+
* and `stagepass upgrade` only writes ranges a published version satisfies.
|
|
10
|
+
* A drift test keeps the generated file in sync with packages/loader and
|
|
11
|
+
* packages/modules.
|
|
10
12
|
*/
|
|
11
13
|
|
|
12
14
|
import { LOADER_VERSION, MODULES_VERSION } from './package-versions.js';
|
|
@@ -14,9 +16,8 @@ import { LOADER_VERSION, MODULES_VERSION } from './package-versions.js';
|
|
|
14
16
|
export { LOADER_VERSION, MODULES_VERSION };
|
|
15
17
|
|
|
16
18
|
/**
|
|
17
|
-
* @deprecated
|
|
18
|
-
*
|
|
19
|
-
* Kept as an alias of `LOADER_VERSION` for older call sites and tests.
|
|
19
|
+
* @deprecated Alias of `LOADER_VERSION` for older call sites and tests.
|
|
20
|
+
* Snippet pins use `SNIPPET_LOADER_VERSION` in `cdn-base.js`.
|
|
20
21
|
*/
|
|
21
22
|
export const STAGEPASS_PACKAGES_VERSION = LOADER_VERSION;
|
|
22
23
|
|
|
@@ -1,40 +1,7 @@
|
|
|
1
1
|
# {{NAME}} (Stagepass project)
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Local Webflow project. Stagepass rules for `src/`, `api/`, and Webflow code are in `AGENTS.stagepass.md`.
|
|
4
4
|
|
|
5
|
-
##
|
|
6
|
-
|
|
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
|
-
- **Loader:** swaps tagged production assets for local URLs when a session is active (`?stagepass={{NAME}}.sp`)
|
|
9
|
-
- **Session:** `localStorage` JSON `{ domain, expires }` (default 12 h)
|
|
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
|
-
- **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
|
-
|
|
14
|
-
## Safe development rules (beginners)
|
|
15
|
-
|
|
16
|
-
1. **No secrets in frontend code.** API keys, tokens, and passwords must never ship in JS/CSS served to the browser.
|
|
17
|
-
2. **No `innerHTML` with untrusted data.** Prefer `textContent`, `createElement`, or trusted, sanitized markup only.
|
|
18
|
-
3. **No `eval` or string-built scripts** with user or CMS input.
|
|
19
|
-
4. **Pin versions** for production embeds (`@2` major pin, or an exact pin when you need a lockstep deploy).
|
|
20
|
-
5. **Review before publishing.** Read the diff for asset tags, CORS origins, and anything that touches live Webflow Custom Code.
|
|
21
|
-
6. **Keep Stagepass tags intact.** Use `data-src` / `data-href` + `data-stagepass` (+ optional `data-stagepass-path`) so production users keep production URLs.
|
|
22
|
-
7. **Do not commit secrets.** Keep `.env*` out of git (see `.gitignore`).
|
|
23
|
-
8. **PHP (if enabled):** never hard-code secrets; read them from the environment. Keep proxy allowlists exact-host and https-only.
|
|
24
|
-
|
|
25
|
-
## Local workflow
|
|
26
|
-
|
|
27
|
-
```bash
|
|
28
|
-
npm run watch
|
|
29
|
-
# or: npm run dev (alias of watch)
|
|
30
|
-
stagepass start
|
|
31
|
-
stagepass link {{NAME}}
|
|
32
|
-
```
|
|
33
|
-
|
|
34
|
-
`stagepass.json` stores staging as `origin` ({{STAGING_HOST}}) and optional `production`. `link` without `--origin` uses those hosts. Then open `https://{{STAGING_HOST}}/?stagepass={{NAME}}.sp`.
|
|
35
|
-
|
|
36
|
-
## Types
|
|
37
|
-
|
|
38
|
-
`{{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`.
|
|
5
|
+
## Notes
|
|
39
6
|
|
|
40
7
|
{{STAGEPASS_BLOCK}}
|
|
@@ -61,13 +61,15 @@ After each production upload, set `data-src` / `data-href` to your hosted URLs.
|
|
|
61
61
|
|
|
62
62
|
Use those values on production tags (and keep `crossorigin="anonymous"` when the asset host sends CORS headers):
|
|
63
63
|
|
|
64
|
+
{{ASSET_URL_HINT}}
|
|
65
|
+
|
|
64
66
|
```html
|
|
65
67
|
<script
|
|
66
|
-
src="
|
|
68
|
+
src="{{ASSET_EXAMPLE_URL}}"
|
|
67
69
|
integrity="sha384-..."
|
|
68
70
|
crossorigin="anonymous"
|
|
69
71
|
data-stagepass
|
|
70
|
-
data-src="
|
|
72
|
+
data-src="{{ASSET_EXAMPLE_URL}}"
|
|
71
73
|
data-stagepass-path="/dist/main.js"
|
|
72
74
|
></script>
|
|
73
75
|
```
|