@stagepass/cli 1.0.1 → 2.1.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/LICENSE +21 -0
- package/README.md +216 -0
- package/TESTING-macos.md +58 -0
- package/bin/index.js +2 -76
- package/package.json +10 -4
- package/src/cli.js +320 -0
- package/src/commands/autostart.js +85 -0
- package/src/commands/certs.js +185 -0
- package/src/commands/daemon.js +126 -0
- package/src/commands/doctor.js +56 -0
- package/src/commands/link.js +145 -57
- package/src/commands/new.js +368 -0
- package/src/commands/npm-run.js +84 -0
- package/src/commands/reload.js +14 -11
- package/src/commands/start.js +21 -4
- package/src/commands/stop.js +15 -0
- package/src/commands/unlink.js +17 -30
- package/src/commands/upgrade.js +198 -0
- package/src/lib/autostart.js +360 -0
- package/src/lib/caddyfile.js +338 -0
- package/src/lib/cdn-snippets.js +27 -0
- package/src/lib/certs.js +476 -0
- package/src/lib/doctor.js +503 -0
- package/src/lib/domain.js +125 -0
- package/src/lib/exec.js +71 -0
- package/src/lib/migrate.js +96 -0
- package/src/lib/origins.js +160 -0
- package/src/lib/paths.js +132 -0
- package/src/lib/platform.js +47 -0
- package/src/lib/project-name.js +47 -0
- package/src/lib/projects.js +181 -0
- package/src/lib/remote-url.js +63 -0
- package/src/lib/scaffold.js +186 -0
- package/src/lib/snippets.js +83 -0
- package/src/lib/spawn.js +30 -0
- package/templates/project/AGENTS.md.tmpl +37 -0
- package/templates/project/README.md.tmpl +44 -0
- package/templates/project/jsconfig.json.tmpl +14 -0
- package/templates/project/scripts/build.mjs +110 -0
- package/templates/project/src/main.js.tmpl +29 -0
- package/templates/project/src/main.ts.tmpl +29 -0
- package/templates/project/src/styles.css.tmpl +8 -0
- package/templates/project/src/styles.scss.tmpl +8 -0
- package/templates/project/stagepass.json.tmpl +8 -0
- package/templates/project/tsconfig.json.tmpl +13 -0
- package/src/commands/__tests__/link.test.js +0 -113
- package/src/commands/__tests__/reload.test.js +0 -72
- package/src/commands/__tests__/unlink.test.js +0 -90
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reject git remotes that embed credentials (user:pass@, tokens as userinfo).
|
|
3
|
+
* SSH remotes like git@github.com:org/repo.git are allowed.
|
|
4
|
+
*
|
|
5
|
+
* @param {unknown} remote
|
|
6
|
+
* @returns {{ ok: true, remote: string } | { ok: false, error: string }}
|
|
7
|
+
*/
|
|
8
|
+
export function validateGitRemote(remote) {
|
|
9
|
+
if (remote === undefined || remote === null || remote === '') {
|
|
10
|
+
return { ok: true, remote: '' };
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
if (typeof remote !== 'string') {
|
|
14
|
+
return { ok: false, error: 'Git remote must be a string URL.' };
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const value = remote.trim();
|
|
18
|
+
if (!value) {
|
|
19
|
+
return { ok: true, remote: '' };
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// HTTPS/HTTP/git+http(s) with userinfo
|
|
23
|
+
if (/^[a-z][a-z0-9+.-]*:\/\//i.test(value)) {
|
|
24
|
+
try {
|
|
25
|
+
const parsed = new URL(value);
|
|
26
|
+
if (parsed.username || parsed.password) {
|
|
27
|
+
return {
|
|
28
|
+
ok: false,
|
|
29
|
+
error:
|
|
30
|
+
'Git remote must not contain credentials (user:pass@ or tokens in the URL). Use SSH or a credential helper instead.',
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
} catch {
|
|
34
|
+
return { ok: false, error: 'Git remote URL is invalid.' };
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// Belt-and-suspenders: userinfo before host
|
|
38
|
+
if (/:\/\/[^/@]+@/.test(value)) {
|
|
39
|
+
return {
|
|
40
|
+
ok: false,
|
|
41
|
+
error:
|
|
42
|
+
'Git remote must not contain credentials (user:pass@ or tokens in the URL). Use SSH or a credential helper instead.',
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return { ok: true, remote: value };
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// SCP-like SSH: git@host:path (no password embed)
|
|
50
|
+
if (/^[^/@]+@[^:]+:/.test(value)) {
|
|
51
|
+
// Reject git:token@host style mistaken as SSH
|
|
52
|
+
if (/^[^/@]+:[^/@]+@/.test(value)) {
|
|
53
|
+
return {
|
|
54
|
+
ok: false,
|
|
55
|
+
error:
|
|
56
|
+
'Git remote must not contain credentials (user:pass@ or tokens in the URL). Use SSH or a credential helper instead.',
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
return { ok: true, remote: value };
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return { ok: true, remote: value };
|
|
63
|
+
}
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import { fileURLToPath } from 'node:url';
|
|
3
|
+
import { createRequire } from 'module';
|
|
4
|
+
import { buildWebflowSnippets } from './snippets.js';
|
|
5
|
+
|
|
6
|
+
const require = createRequire(import.meta.url);
|
|
7
|
+
const { version: cliVersion } = require('../../package.json');
|
|
8
|
+
|
|
9
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
10
|
+
const __dirname = path.dirname(__filename);
|
|
11
|
+
|
|
12
|
+
/** Absolute path to packaged templates/project */
|
|
13
|
+
export function defaultTemplatesRoot() {
|
|
14
|
+
return path.join(__dirname, '../../templates/project');
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* @typedef {object} ScaffoldOptions
|
|
19
|
+
* @property {string} name
|
|
20
|
+
* @property {string} dir Absolute or relative target directory
|
|
21
|
+
* @property {string} [author]
|
|
22
|
+
* @property {'ts'|'js'} lang
|
|
23
|
+
* @property {'scss'|'css'} styles
|
|
24
|
+
* @property {boolean} modules
|
|
25
|
+
* @property {boolean} git
|
|
26
|
+
* @property {string} [remote]
|
|
27
|
+
* @property {boolean} install
|
|
28
|
+
* @property {boolean} dryRun
|
|
29
|
+
* @property {string} [loaderVersion]
|
|
30
|
+
*/
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* @param {ScaffoldOptions} options
|
|
34
|
+
* @returns {Record<string, string>}
|
|
35
|
+
*/
|
|
36
|
+
export function buildPackageJson(options) {
|
|
37
|
+
const version = options.loaderVersion || cliVersion;
|
|
38
|
+
const isTs = options.lang === 'ts';
|
|
39
|
+
const useSass = options.styles === 'scss';
|
|
40
|
+
|
|
41
|
+
/** @type {Record<string, string>} */
|
|
42
|
+
const devDependencies = {
|
|
43
|
+
'@stagepass/loader': version,
|
|
44
|
+
esbuild: '^0.25.0',
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
if (options.modules) {
|
|
48
|
+
devDependencies['@stagepass/modules'] = version;
|
|
49
|
+
}
|
|
50
|
+
if (isTs) {
|
|
51
|
+
devDependencies.typescript = '^5.6.0';
|
|
52
|
+
}
|
|
53
|
+
if (useSass) {
|
|
54
|
+
devDependencies.sass = '^1.80.0';
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return {
|
|
58
|
+
name: options.name,
|
|
59
|
+
private: true,
|
|
60
|
+
version: '0.0.0',
|
|
61
|
+
description: `Stagepass project ${options.name}`,
|
|
62
|
+
author: options.author || '',
|
|
63
|
+
type: 'module',
|
|
64
|
+
scripts: {
|
|
65
|
+
build: 'node scripts/build.mjs',
|
|
66
|
+
watch: 'node scripts/build.mjs --watch',
|
|
67
|
+
},
|
|
68
|
+
devDependencies,
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Apply {{TOKEN}} replacements. Unknown tokens left intact.
|
|
74
|
+
* @param {string} source
|
|
75
|
+
* @param {Record<string, string>} vars
|
|
76
|
+
*/
|
|
77
|
+
export function applyTemplate(source, vars) {
|
|
78
|
+
return source.replace(/\{\{([A-Z0-9_]+)\}\}/g, (match, key) => {
|
|
79
|
+
if (Object.prototype.hasOwnProperty.call(vars, key)) {
|
|
80
|
+
return vars[key];
|
|
81
|
+
}
|
|
82
|
+
return match;
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Plan files to write for a scaffold (relative paths → contents).
|
|
88
|
+
* @param {ScaffoldOptions} options
|
|
89
|
+
* @param {object} [deps]
|
|
90
|
+
* @param {(rel: string) => Promise<string>} [deps.readTemplate]
|
|
91
|
+
* @returns {Promise<{ relativePath: string, contents: string }[]>}
|
|
92
|
+
*/
|
|
93
|
+
export async function planScaffoldFiles(options, deps = {}) {
|
|
94
|
+
const version = options.loaderVersion || cliVersion;
|
|
95
|
+
const isTs = options.lang === 'ts';
|
|
96
|
+
const useSass = options.styles === 'scss';
|
|
97
|
+
const entryJs = isTs ? 'src/main.ts' : 'src/main.js';
|
|
98
|
+
const entryCss = useSass ? 'src/styles.scss' : 'src/styles.css';
|
|
99
|
+
const configFile = isTs ? 'tsconfig.json' : 'jsconfig.json';
|
|
100
|
+
|
|
101
|
+
const snippets = buildWebflowSnippets({
|
|
102
|
+
version,
|
|
103
|
+
modules: options.modules,
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
const vars = {
|
|
107
|
+
NAME: options.name,
|
|
108
|
+
AUTHOR: options.author || '',
|
|
109
|
+
LANG: options.lang,
|
|
110
|
+
STYLES: options.styles,
|
|
111
|
+
MODULES_JSON: options.modules ? 'true' : 'false',
|
|
112
|
+
LOADER_VERSION: version,
|
|
113
|
+
HEAD_SNIPPET: snippets.head,
|
|
114
|
+
FOOTER_SNIPPET: snippets.footer,
|
|
115
|
+
ENTRY_JS: entryJs,
|
|
116
|
+
ENTRY_CSS: entryCss,
|
|
117
|
+
USE_SASS: useSass ? 'true' : 'false',
|
|
118
|
+
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
|
+
};
|
|
123
|
+
|
|
124
|
+
const readTemplate =
|
|
125
|
+
deps.readTemplate ||
|
|
126
|
+
(async () => {
|
|
127
|
+
throw new Error('readTemplate dependency required');
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
/** @type {{ relativePath: string, contents: string }[]} */
|
|
131
|
+
const files = [];
|
|
132
|
+
|
|
133
|
+
const pkg = buildPackageJson(options);
|
|
134
|
+
files.push({
|
|
135
|
+
relativePath: 'package.json',
|
|
136
|
+
contents: `${JSON.stringify(pkg, null, 2)}\n`,
|
|
137
|
+
});
|
|
138
|
+
|
|
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
|
+
];
|
|
158
|
+
|
|
159
|
+
for (const item of templateMap) {
|
|
160
|
+
const raw = await readTemplate(item.template);
|
|
161
|
+
files.push({
|
|
162
|
+
relativePath: item.out,
|
|
163
|
+
contents: applyTemplate(raw, vars),
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
return files;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* List relative paths that already exist and would be overwritten.
|
|
172
|
+
* @param {string} targetDir
|
|
173
|
+
* @param {{ relativePath: string }[]} files
|
|
174
|
+
* @param {{ pathExists: (p: string) => Promise<boolean> }} fs
|
|
175
|
+
*/
|
|
176
|
+
export async function findConflicts(targetDir, files, fs) {
|
|
177
|
+
/** @type {string[]} */
|
|
178
|
+
const conflicts = [];
|
|
179
|
+
for (const file of files) {
|
|
180
|
+
const abs = path.join(targetDir, file.relativePath);
|
|
181
|
+
if (await fs.pathExists(abs)) {
|
|
182
|
+
conflicts.push(file.relativePath);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
return conflicts;
|
|
186
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Safe token for CDN pins and data-stagepass-version (blocks HTML/attr injection).
|
|
3
|
+
* @param {unknown} version
|
|
4
|
+
* @returns {string}
|
|
5
|
+
*/
|
|
6
|
+
export function sanitizeSnippetVersion(version) {
|
|
7
|
+
const value = String(version == null ? '' : version).trim();
|
|
8
|
+
// Allow optional ^/~ range prefixes used in package.json; reject quotes and HTML.
|
|
9
|
+
if (!/^[~^]?[0-9A-Za-z][0-9A-Za-z._+-]*$/.test(value)) {
|
|
10
|
+
throw new Error(
|
|
11
|
+
'Invalid version for Webflow snippets (use a semver or npm dist-tag).'
|
|
12
|
+
);
|
|
13
|
+
}
|
|
14
|
+
return value;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Build recommended Webflow Custom Code snippets (jsDelivr major pin @2).
|
|
19
|
+
*
|
|
20
|
+
* @param {object} options
|
|
21
|
+
* @param {string} options.version Exact package version for data-stagepass-version / docs
|
|
22
|
+
* @param {boolean} [options.modules]
|
|
23
|
+
* @returns {{ head: string, footer: string, majorPin: string, exactPin: string }}
|
|
24
|
+
*/
|
|
25
|
+
export function buildWebflowSnippets(options) {
|
|
26
|
+
const version = sanitizeSnippetVersion(options.version || '2.1.0');
|
|
27
|
+
const modules = options.modules === true;
|
|
28
|
+
const majorPin = '@2';
|
|
29
|
+
const exactPin = `@${version}`;
|
|
30
|
+
|
|
31
|
+
const loaderLines = [
|
|
32
|
+
'<!-- Stagepass loader (major pin @2; optional exact pin shown in data-stagepass-version) -->',
|
|
33
|
+
'<script',
|
|
34
|
+
` src="https://cdn.jsdelivr.net/npm/@stagepass/loader${majorPin}/dist/loader.min.js"`,
|
|
35
|
+
` data-stagepass-version="${version}"`,
|
|
36
|
+
'></script>',
|
|
37
|
+
];
|
|
38
|
+
|
|
39
|
+
if (modules) {
|
|
40
|
+
loaderLines.push(
|
|
41
|
+
`<script src="https://cdn.jsdelivr.net/npm/@stagepass/modules${majorPin}/dist/inject.min.js"></script>`,
|
|
42
|
+
`<script src="https://cdn.jsdelivr.net/npm/@stagepass/modules${majorPin}/dist/cookies.min.js"></script>`
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const footerLines = [
|
|
47
|
+
'<!-- Project assets: production URLs in data-src / data-href; local paths for Stagepass -->',
|
|
48
|
+
'<!-- After `npm run build`, check dist/manifest.json for content-hashed filenames. -->',
|
|
49
|
+
'<script',
|
|
50
|
+
' data-src="https://cdn.example.com/js/main.js"',
|
|
51
|
+
' data-stagepass="true"',
|
|
52
|
+
' data-stagepass-path="dist/main.js"',
|
|
53
|
+
'></script>',
|
|
54
|
+
'<link',
|
|
55
|
+
' rel="stylesheet"',
|
|
56
|
+
' data-href="https://cdn.example.com/css/styles.css"',
|
|
57
|
+
' data-stagepass="true"',
|
|
58
|
+
' data-stagepass-path="dist/styles.css"',
|
|
59
|
+
'>',
|
|
60
|
+
];
|
|
61
|
+
|
|
62
|
+
return {
|
|
63
|
+
head: loaderLines.join('\n'),
|
|
64
|
+
footer: footerLines.join('\n'),
|
|
65
|
+
majorPin,
|
|
66
|
+
exactPin,
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Print head/footer snippets with section labels.
|
|
72
|
+
* @param {{ head: string, footer: string }} snippets
|
|
73
|
+
* @param {(msg: string) => void} [log]
|
|
74
|
+
*/
|
|
75
|
+
export function printWebflowSnippets(snippets, log = console.log) {
|
|
76
|
+
log('\nWebflow Custom Code (Head):');
|
|
77
|
+
log(snippets.head);
|
|
78
|
+
log('\nWebflow Custom Code (Footer):');
|
|
79
|
+
log(snippets.footer);
|
|
80
|
+
log(
|
|
81
|
+
'\nOptional exact pin example (instead of @2): replace @2 with the exact version (e.g. @2.1.0).'
|
|
82
|
+
);
|
|
83
|
+
}
|
package/src/lib/spawn.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { spawn as nodeSpawn } from 'node:child_process';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @typedef {object} SpawnResult
|
|
5
|
+
* @property {number|null} code
|
|
6
|
+
* @property {NodeJS.Signals|null} [signal]
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* @typedef {(command: string, args?: string[], options?: object) => Promise<SpawnResult>} SpawnFn
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Spawn without a shell. Forwards stdio by default.
|
|
15
|
+
* Always forces shell:false after spreading options so callers cannot enable a shell.
|
|
16
|
+
* @type {SpawnFn}
|
|
17
|
+
*/
|
|
18
|
+
export function defaultSpawn(command, args = [], options = {}) {
|
|
19
|
+
return new Promise((resolve, reject) => {
|
|
20
|
+
const child = nodeSpawn(command, args, {
|
|
21
|
+
stdio: 'inherit',
|
|
22
|
+
...options,
|
|
23
|
+
shell: false,
|
|
24
|
+
});
|
|
25
|
+
child.on('error', reject);
|
|
26
|
+
child.on('close', (code, signal) => {
|
|
27
|
+
resolve({ code, signal });
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# {{NAME}} (Stagepass project)
|
|
2
|
+
|
|
3
|
+
Profile for AI coding agents and humans working on this Webflow + Stagepass project.
|
|
4
|
+
|
|
5
|
+
## Stagepass feature set
|
|
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):** load after `loader.min.js` as separate tags: `inject`, `cookies`, `maintenance`, or `all` (`modules/dist/{inject,cookies,maintenance,all}.min.js`)
|
|
11
|
+
- **Runtime API:** prefer `sp` (`sp.vars`, `sp.log` / `sp.warn` / `sp.error`, `sp.resolveLocalUrl`)
|
|
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
|
+
|
|
24
|
+
## Local workflow
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
npm run watch
|
|
28
|
+
stagepass start
|
|
29
|
+
stagepass link {{NAME}} --origin my-project.webflow.io
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
Then open `https://my-project.webflow.io/?stagepass={{NAME}}.sp`.
|
|
33
|
+
|
|
34
|
+
## Types
|
|
35
|
+
|
|
36
|
+
This project includes `@stagepass/loader` types via `{{CONFIG_FILE}}` (`types: ["@stagepass/loader"]`).
|
|
37
|
+
{{MODULES_NOTE}}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# {{NAME}}
|
|
2
|
+
|
|
3
|
+
Local Stagepass project for Webflow.
|
|
4
|
+
|
|
5
|
+
## Setup
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install
|
|
9
|
+
npm run build
|
|
10
|
+
stagepass link {{NAME}} --origin my-project.webflow.io
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Activate on your live site: `https://my-project.webflow.io/?stagepass={{NAME}}.sp`
|
|
14
|
+
|
|
15
|
+
## Scripts
|
|
16
|
+
|
|
17
|
+
| Script | Description |
|
|
18
|
+
| :--- | :--- |
|
|
19
|
+
| `npm run build` | Bundle JS/CSS into `dist/` with content hashes + `manifest.json` |
|
|
20
|
+
| `npm run watch` | Rebuild on change |
|
|
21
|
+
|
|
22
|
+
## Webflow Custom Code
|
|
23
|
+
|
|
24
|
+
Prefer the major CDN pin `@2`. An exact pin is optional (see `data-stagepass-version`).
|
|
25
|
+
|
|
26
|
+
### Head
|
|
27
|
+
|
|
28
|
+
```html
|
|
29
|
+
{{HEAD_SNIPPET}}
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
Optional exact pin (replace `@2` in the URL if you need a locked version): `@{{LOADER_VERSION}}`.
|
|
33
|
+
|
|
34
|
+
### Footer
|
|
35
|
+
|
|
36
|
+
```html
|
|
37
|
+
{{FOOTER_SNIPPET}}
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
After each production upload, set `data-src` / `data-href` to your hosted URLs. For content-hashed uploads, read the filenames from `dist/manifest.json`. Local Stagepass swapping can keep using the stable `dist/main.js` and `dist/styles.css` copies.
|
|
41
|
+
|
|
42
|
+
## PHP
|
|
43
|
+
|
|
44
|
+
PHP helpers for this scaffolder are planned for a later Stagepass release. Linked `.sp` sites can still use PHP via the CLI when PHP is installed system-wide.
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2018",
|
|
4
|
+
"module": "ESNext",
|
|
5
|
+
"moduleResolution": "bundler",
|
|
6
|
+
"checkJs": true,
|
|
7
|
+
"strict": true,
|
|
8
|
+
"noEmit": true,
|
|
9
|
+
"skipLibCheck": true,
|
|
10
|
+
"lib": ["ES2018", "DOM"],
|
|
11
|
+
"types": ["@stagepass/loader"]
|
|
12
|
+
},
|
|
13
|
+
"include": ["src/**/*.js"]
|
|
14
|
+
}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* Stagepass project build: esbuild (+ optional sass) → content-hashed dist files + manifest.json.
|
|
4
|
+
* Also writes stable dist/main.js and dist/styles.css copies for local Stagepass paths.
|
|
5
|
+
*/
|
|
6
|
+
import { createHash } from 'node:crypto';
|
|
7
|
+
import { watch as fsWatch, promises as fs } from 'node:fs';
|
|
8
|
+
import path from 'node:path';
|
|
9
|
+
import { fileURLToPath } from 'node:url';
|
|
10
|
+
import * as esbuild from 'esbuild';
|
|
11
|
+
|
|
12
|
+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
13
|
+
const root = path.join(__dirname, '..');
|
|
14
|
+
const distDir = path.join(root, 'dist');
|
|
15
|
+
const watchMode = process.argv.includes('--watch');
|
|
16
|
+
|
|
17
|
+
const ENTRY_JS = process.env.STAGEPASS_ENTRY_JS || '{{ENTRY_JS}}';
|
|
18
|
+
const ENTRY_CSS = process.env.STAGEPASS_ENTRY_CSS || '{{ENTRY_CSS}}';
|
|
19
|
+
const USE_SASS = (process.env.STAGEPASS_USE_SASS || '{{USE_SASS}}') === 'true';
|
|
20
|
+
|
|
21
|
+
async function ensureDist() {
|
|
22
|
+
await fs.mkdir(distDir, { recursive: true });
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function hashContent(buf) {
|
|
26
|
+
return createHash('sha256').update(buf).digest('hex').slice(0, 8);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
async function writeHashed(stableName, contents, encoding = undefined) {
|
|
30
|
+
const buf = Buffer.isBuffer(contents)
|
|
31
|
+
? contents
|
|
32
|
+
: Buffer.from(contents, encoding || 'utf8');
|
|
33
|
+
const hash = hashContent(buf);
|
|
34
|
+
const ext = path.extname(stableName);
|
|
35
|
+
const base = path.basename(stableName, ext);
|
|
36
|
+
const hashedName = `${base}.${hash}${ext}`;
|
|
37
|
+
await fs.writeFile(path.join(distDir, hashedName), buf);
|
|
38
|
+
await fs.writeFile(path.join(distDir, stableName), buf);
|
|
39
|
+
return hashedName;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
async function buildJs() {
|
|
43
|
+
const entry = path.join(root, ENTRY_JS);
|
|
44
|
+
const result = await esbuild.build({
|
|
45
|
+
entryPoints: [entry],
|
|
46
|
+
bundle: true,
|
|
47
|
+
write: false,
|
|
48
|
+
format: 'iife',
|
|
49
|
+
platform: 'browser',
|
|
50
|
+
target: ['es2018'],
|
|
51
|
+
sourcemap: false,
|
|
52
|
+
logLevel: 'warning',
|
|
53
|
+
});
|
|
54
|
+
const out = result.outputFiles[0];
|
|
55
|
+
return writeHashed('main.js', out.contents);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
async function buildCss() {
|
|
59
|
+
const entry = path.join(root, ENTRY_CSS);
|
|
60
|
+
let css;
|
|
61
|
+
if (USE_SASS) {
|
|
62
|
+
const sass = await import('sass');
|
|
63
|
+
const compiled = sass.compile(entry, { style: 'compressed' });
|
|
64
|
+
css = compiled.css;
|
|
65
|
+
} else {
|
|
66
|
+
css = await fs.readFile(entry, 'utf8');
|
|
67
|
+
}
|
|
68
|
+
return writeHashed('styles.css', css, 'utf8');
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
async function buildOnce() {
|
|
72
|
+
await ensureDist();
|
|
73
|
+
const mainFile = await buildJs();
|
|
74
|
+
const stylesFile = await buildCss();
|
|
75
|
+
const manifest = {
|
|
76
|
+
'main.js': mainFile,
|
|
77
|
+
'styles.css': stylesFile,
|
|
78
|
+
};
|
|
79
|
+
await fs.writeFile(
|
|
80
|
+
path.join(distDir, 'manifest.json'),
|
|
81
|
+
`${JSON.stringify(manifest, null, 2)}\n`,
|
|
82
|
+
'utf8'
|
|
83
|
+
);
|
|
84
|
+
return manifest;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const manifest = await buildOnce();
|
|
88
|
+
console.log('[stagepass] build ok', manifest);
|
|
89
|
+
|
|
90
|
+
if (watchMode) {
|
|
91
|
+
let pending = false;
|
|
92
|
+
const rebuild = async () => {
|
|
93
|
+
if (pending) return;
|
|
94
|
+
pending = true;
|
|
95
|
+
try {
|
|
96
|
+
const next = await buildOnce();
|
|
97
|
+
console.log('[stagepass] rebuild ok', next);
|
|
98
|
+
} catch (err) {
|
|
99
|
+
console.error('[stagepass] rebuild failed', err);
|
|
100
|
+
} finally {
|
|
101
|
+
pending = false;
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
for (const rel of [ENTRY_JS, ENTRY_CSS]) {
|
|
105
|
+
fsWatch(path.join(root, rel), () => {
|
|
106
|
+
rebuild().catch(() => {});
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
console.log('[stagepass] watching…');
|
|
110
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Webflow-safe entry: runs once, waits for DOM and window.Webflow when present.
|
|
3
|
+
* No globals leaked; no eval.
|
|
4
|
+
*/
|
|
5
|
+
(() => {
|
|
6
|
+
const run = () => {
|
|
7
|
+
// Your project code here.
|
|
8
|
+
// Prefer `sp` when Stagepass loader is present (session-aware logging, vars, etc.).
|
|
9
|
+
const sp = window.sp;
|
|
10
|
+
if (sp && typeof sp.log === 'function') {
|
|
11
|
+
sp.log('{{NAME}}: ready');
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
const start = () => {
|
|
16
|
+
const w = window;
|
|
17
|
+
if (w.Webflow && typeof w.Webflow.push === 'function') {
|
|
18
|
+
w.Webflow.push(run);
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
run();
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
if (document.readyState === 'loading') {
|
|
25
|
+
document.addEventListener('DOMContentLoaded', start, { once: true });
|
|
26
|
+
} else {
|
|
27
|
+
start();
|
|
28
|
+
}
|
|
29
|
+
})();
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Webflow-safe entry: runs once, waits for DOM and window.Webflow when present.
|
|
3
|
+
* No globals leaked; no eval.
|
|
4
|
+
*/
|
|
5
|
+
(() => {
|
|
6
|
+
const run = (): void => {
|
|
7
|
+
// Your project code here.
|
|
8
|
+
// Prefer `sp` when Stagepass loader is present (session-aware logging, vars, etc.).
|
|
9
|
+
const sp = (window as Window & { sp?: { log?: (...args: unknown[]) => void } }).sp;
|
|
10
|
+
sp?.log?.('{{NAME}}: ready');
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
const start = (): void => {
|
|
14
|
+
const w = window as Window & {
|
|
15
|
+
Webflow?: { push?: (fn: () => void) => void };
|
|
16
|
+
};
|
|
17
|
+
if (w.Webflow && typeof w.Webflow.push === 'function') {
|
|
18
|
+
w.Webflow.push(run);
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
run();
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
if (document.readyState === 'loading') {
|
|
25
|
+
document.addEventListener('DOMContentLoaded', start, { once: true });
|
|
26
|
+
} else {
|
|
27
|
+
start();
|
|
28
|
+
}
|
|
29
|
+
})();
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2018",
|
|
4
|
+
"module": "ESNext",
|
|
5
|
+
"moduleResolution": "bundler",
|
|
6
|
+
"strict": true,
|
|
7
|
+
"noEmit": true,
|
|
8
|
+
"skipLibCheck": true,
|
|
9
|
+
"lib": ["ES2018", "DOM"],
|
|
10
|
+
"types": ["@stagepass/loader"]
|
|
11
|
+
},
|
|
12
|
+
"include": ["src/**/*.ts"]
|
|
13
|
+
}
|