@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,126 @@
|
|
|
1
|
+
import fs from 'fs-extra';
|
|
2
|
+
import chalk from 'chalk';
|
|
3
|
+
import { defaultExec } from '../lib/exec.js';
|
|
4
|
+
import { getCaddyfilePath, getLogsDir } from '../lib/paths.js';
|
|
5
|
+
import { runCertsCheck } from '../lib/certs.js';
|
|
6
|
+
import { loadProjects } from '../lib/projects.js';
|
|
7
|
+
import { getProjectsPath } from '../lib/paths.js';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Detect bind / permission failures that will not self-heal on immediate restart.
|
|
11
|
+
* LaunchAgent KeepAlive (SuccessfulExit=false) must not restart-loop on these.
|
|
12
|
+
* @param {string} detail
|
|
13
|
+
* @returns {boolean}
|
|
14
|
+
*/
|
|
15
|
+
export function isPermanentDaemonFailure(detail) {
|
|
16
|
+
return /permission denied|bind|address already in use|EADDRINUSE|EACCES/i.test(
|
|
17
|
+
String(detail || '')
|
|
18
|
+
);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Non-interactive foreground daemon for LaunchAgent use.
|
|
23
|
+
* Never uses sudo. Runs certs check (log only, no TLS handshake), then `caddy run`.
|
|
24
|
+
*
|
|
25
|
+
* Existing `stagepass start` also launches Caddy as the user (sudo is only for
|
|
26
|
+
* port conflict helpers). Binding TCP 443 as a user LaunchAgent is therefore
|
|
27
|
+
* expected to work on macOS the same way. Permanent startup failures (missing
|
|
28
|
+
* Caddyfile, port 443 bind) exit 0 after logging so launchd KeepAlive does not
|
|
29
|
+
* restart-loop; unexpected mid-run failures exit 1 so KeepAlive can retry.
|
|
30
|
+
*
|
|
31
|
+
* @param {object} [options]
|
|
32
|
+
* @param {import('../lib/exec.js').ExecFn} [options.exec]
|
|
33
|
+
* @param {(file: string, args: string[], opts?: object) => Promise<unknown>} [options.runForeground]
|
|
34
|
+
* @param {(msg: string) => void} [options.log]
|
|
35
|
+
* @returns {Promise<number>}
|
|
36
|
+
*/
|
|
37
|
+
export async function daemon(options = {}) {
|
|
38
|
+
const log =
|
|
39
|
+
options.log ||
|
|
40
|
+
((msg) => {
|
|
41
|
+
const line = `[stagepass daemon] ${msg}`;
|
|
42
|
+
console.error(line);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
const exec = options.exec || defaultExec;
|
|
46
|
+
const caddyfilePath = options.caddyfilePath || getCaddyfilePath();
|
|
47
|
+
const pathExists = options.pathExists || ((p) => fs.pathExists(p));
|
|
48
|
+
|
|
49
|
+
await fs.ensureDir(options.logsDir || getLogsDir());
|
|
50
|
+
|
|
51
|
+
if (!(await pathExists(caddyfilePath))) {
|
|
52
|
+
log(`Caddyfile missing: ${caddyfilePath}`);
|
|
53
|
+
log('Refusing to start. Run: stagepass setup && stagepass link');
|
|
54
|
+
// Exit 0: permanent until the user fixes config; avoid KeepAlive restart loop.
|
|
55
|
+
return 0;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// Non-interactive cert check (log only; never prompts; never repairs).
|
|
59
|
+
// Skip TLS handshakes: Caddy is not running yet during cold start.
|
|
60
|
+
try {
|
|
61
|
+
const projectsPath = options.projectsPath || getProjectsPath();
|
|
62
|
+
const doc = await loadProjects(projectsPath);
|
|
63
|
+
const domains = Object.keys(doc.projects || {});
|
|
64
|
+
const report = await runCertsCheck({
|
|
65
|
+
exec,
|
|
66
|
+
domains,
|
|
67
|
+
skipHandshake: true,
|
|
68
|
+
handshakeTimeoutMs: 2000,
|
|
69
|
+
});
|
|
70
|
+
log(`certs root: ${report.root.status} ${report.root.reason}`);
|
|
71
|
+
log(
|
|
72
|
+
`certs trust: ${report.trust?.trusted ? 'trusted' : 'NOT trusted'}${
|
|
73
|
+
report.trust?.error ? ` (${report.trust.error})` : ''
|
|
74
|
+
}`
|
|
75
|
+
);
|
|
76
|
+
if (report.orphans?.length) {
|
|
77
|
+
log(
|
|
78
|
+
`certs orphans: ${report.orphans.length} (report only; SHA-1 ${report.orphans
|
|
79
|
+
.map((o) => o.sha1 || '?')
|
|
80
|
+
.join(', ')})`
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
} catch (error) {
|
|
84
|
+
log(
|
|
85
|
+
`certs check failed (continuing): ${
|
|
86
|
+
error instanceof Error ? error.message : String(error)
|
|
87
|
+
}`
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const args = ['run', '--config', caddyfilePath, '--adapter', 'caddyfile'];
|
|
92
|
+
log(`starting: caddy ${args.join(' ')}`);
|
|
93
|
+
|
|
94
|
+
const runForeground =
|
|
95
|
+
options.runForeground ||
|
|
96
|
+
(async (file, fileArgs) => {
|
|
97
|
+
// Inherit stdio so LaunchAgent log redirects capture Caddy output.
|
|
98
|
+
return exec(file, fileArgs, { stdio: 'inherit' });
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
try {
|
|
102
|
+
await runForeground('caddy', args);
|
|
103
|
+
log('caddy exited cleanly');
|
|
104
|
+
return 0;
|
|
105
|
+
} catch (error) {
|
|
106
|
+
const err = /** @type {{ message?: string, stderr?: string, code?: string|number }} */ (
|
|
107
|
+
error
|
|
108
|
+
);
|
|
109
|
+
const detail = [err.message, err.stderr].filter(Boolean).join(' | ');
|
|
110
|
+
log(`caddy failed: ${detail}`);
|
|
111
|
+
if (isPermanentDaemonFailure(detail)) {
|
|
112
|
+
log(
|
|
113
|
+
'Port 443 bind/listen failed for the user LaunchAgent. Exiting without KeepAlive retry. Use interactive `stagepass start` (which can resolve conflicts with confirmation), or free port 443, then retry.'
|
|
114
|
+
);
|
|
115
|
+
if (process.stderr.isTTY) {
|
|
116
|
+
console.error(chalk.red(detail));
|
|
117
|
+
}
|
|
118
|
+
// Exit 0 so SuccessfulExit=false KeepAlive does not restart-loop.
|
|
119
|
+
return 0;
|
|
120
|
+
}
|
|
121
|
+
if (process.stderr.isTTY) {
|
|
122
|
+
console.error(chalk.red(detail));
|
|
123
|
+
}
|
|
124
|
+
return 1;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import chalk from 'chalk';
|
|
2
|
+
import { runDoctor } from '../lib/doctor.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @param {object} [options]
|
|
6
|
+
* @param {boolean} [options.json]
|
|
7
|
+
* @param {boolean} [options.verbose]
|
|
8
|
+
* @returns {Promise<number>} exit code
|
|
9
|
+
*/
|
|
10
|
+
export async function doctor(options = {}) {
|
|
11
|
+
const result = await runDoctor({
|
|
12
|
+
skipHandshake: options.skipHandshake === true,
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
if (options.json) {
|
|
16
|
+
const payload = {
|
|
17
|
+
ok: result.exitCode === 0,
|
|
18
|
+
summary: result.summary,
|
|
19
|
+
checks: result.checks,
|
|
20
|
+
hints: result.hints,
|
|
21
|
+
};
|
|
22
|
+
console.log(JSON.stringify(payload, null, 2));
|
|
23
|
+
return result.exitCode;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
console.log(chalk.bold('\nStagepass doctor\n'));
|
|
27
|
+
|
|
28
|
+
for (const item of result.checks) {
|
|
29
|
+
const tag =
|
|
30
|
+
item.status === 'ok'
|
|
31
|
+
? chalk.green('ok')
|
|
32
|
+
: item.status === 'warn'
|
|
33
|
+
? chalk.yellow('warn')
|
|
34
|
+
: item.status === 'fail'
|
|
35
|
+
? chalk.red('fail')
|
|
36
|
+
: chalk.cyan(item.status);
|
|
37
|
+
console.log(`[${tag}] ${item.message}`);
|
|
38
|
+
if (item.detail && (options.verbose || item.status !== 'ok')) {
|
|
39
|
+
console.log(chalk.dim(` ${item.detail}`));
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
console.log('');
|
|
44
|
+
for (const hint of result.hints) {
|
|
45
|
+
console.log(chalk.cyan(`hint: ${hint}`));
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const { ok, warn, fail } = result.summary;
|
|
49
|
+
console.log(
|
|
50
|
+
chalk.bold(
|
|
51
|
+
`\nSummary: ${ok} ok, ${warn} warn, ${fail} fail (exit ${result.exitCode})`
|
|
52
|
+
)
|
|
53
|
+
);
|
|
54
|
+
|
|
55
|
+
return result.exitCode;
|
|
56
|
+
}
|
package/src/commands/link.js
CHANGED
|
@@ -1,90 +1,178 @@
|
|
|
1
1
|
import fs from 'fs-extra';
|
|
2
2
|
import path from 'path';
|
|
3
|
-
import os from 'os';
|
|
4
3
|
import chalk from 'chalk';
|
|
5
4
|
import { execa } from 'execa';
|
|
6
5
|
import ora from 'ora';
|
|
6
|
+
import { resolveLinkDomain } from '../lib/domain.js';
|
|
7
|
+
import { renderSiteBlock, upsertBlock, parseBlocks } from '../lib/caddyfile.js';
|
|
8
|
+
import {
|
|
9
|
+
validateRoot,
|
|
10
|
+
getCaddyfilePath,
|
|
11
|
+
getStagepassDir,
|
|
12
|
+
getProjectsPath,
|
|
13
|
+
} from '../lib/paths.js';
|
|
14
|
+
import { upsertProject, getProject } from '../lib/projects.js';
|
|
15
|
+
import {
|
|
16
|
+
validateOriginList,
|
|
17
|
+
isBroadWebflowWildcard,
|
|
18
|
+
} from '../lib/origins.js';
|
|
19
|
+
import { ensureCaddyfileMigrated } from '../lib/migrate.js';
|
|
7
20
|
|
|
21
|
+
/**
|
|
22
|
+
* @param {string|undefined} domain
|
|
23
|
+
* @param {object} options
|
|
24
|
+
* @param {boolean} [options.verbose]
|
|
25
|
+
* @param {string[]} [options.origin]
|
|
26
|
+
* @param {string[]} [options.removeOrigin]
|
|
27
|
+
* @param {boolean} [options.allowAllOrigins]
|
|
28
|
+
*/
|
|
8
29
|
export async function link(domain, options = {}) {
|
|
9
30
|
const currentDir = process.cwd();
|
|
31
|
+
const folderName = path.basename(currentDir);
|
|
10
32
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
33
|
+
const resolved = resolveLinkDomain(domain, folderName);
|
|
34
|
+
if (!resolved.ok) {
|
|
35
|
+
console.log(chalk.red(`❌ ${resolved.error}`));
|
|
36
|
+
return;
|
|
15
37
|
}
|
|
16
|
-
targetDomain = targetDomain.replace(/\.sp$/, '') + '.sp';
|
|
17
38
|
|
|
18
|
-
|
|
19
|
-
const
|
|
20
|
-
|
|
39
|
+
const targetDomain = resolved.domain;
|
|
40
|
+
const rootCheck = validateRoot(currentDir);
|
|
41
|
+
if (!rootCheck.ok) {
|
|
42
|
+
console.log(chalk.red(`❌ ${rootCheck.error}`));
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const originsFromFlags = options.origin || [];
|
|
47
|
+
if (originsFromFlags.length > 0) {
|
|
48
|
+
const check = validateOriginList(originsFromFlags);
|
|
49
|
+
if (!check.ok) {
|
|
50
|
+
console.log(chalk.red(`❌ Invalid --origin: ${check.error}`));
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
for (const entry of check.origins) {
|
|
54
|
+
if (isBroadWebflowWildcard(entry)) {
|
|
55
|
+
console.log(
|
|
56
|
+
chalk.yellow(
|
|
57
|
+
` Warning: ${entry} allows any Webflow user's site to make cross-origin requests to this .sp domain.`
|
|
58
|
+
)
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const originsToRemove = options.removeOrigin || [];
|
|
65
|
+
if (originsToRemove.length > 0) {
|
|
66
|
+
const check = validateOriginList(originsToRemove);
|
|
67
|
+
if (!check.ok) {
|
|
68
|
+
console.log(chalk.red(`❌ Invalid --remove-origin: ${check.error}`));
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
let allowAllOriginsFlag;
|
|
74
|
+
if (options.allowAllOrigins === true) allowAllOriginsFlag = true;
|
|
75
|
+
else if (options.allowAllOrigins === false) allowAllOriginsFlag = false;
|
|
76
|
+
else allowAllOriginsFlag = undefined;
|
|
77
|
+
|
|
78
|
+
const caddyDir = getStagepassDir();
|
|
79
|
+
const caddyFilePath = getCaddyfilePath();
|
|
80
|
+
const projectsPath = getProjectsPath();
|
|
81
|
+
|
|
82
|
+
if (resolved.slugged) {
|
|
83
|
+
console.log(
|
|
84
|
+
chalk.dim(
|
|
85
|
+
`Folder name was not a valid domain; using ${chalk.bold(targetDomain)}`
|
|
86
|
+
)
|
|
87
|
+
);
|
|
88
|
+
}
|
|
21
89
|
|
|
22
90
|
const spinner = ora(`Linking ${chalk.bold(targetDomain)}...`).start();
|
|
23
91
|
|
|
24
|
-
// 1. Write config
|
|
25
92
|
try {
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
let content = '';
|
|
29
|
-
if (await fs.pathExists(caddyFilePath)) {
|
|
30
|
-
content = await fs.readFile(caddyFilePath, 'utf-8');
|
|
31
|
-
}
|
|
93
|
+
await fs.ensureDir(caddyDir);
|
|
32
94
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
handle @options {
|
|
41
|
-
header Access-Control-Allow-Origin *
|
|
42
|
-
header Access-Control-Allow-Methods *
|
|
43
|
-
header Access-Control-Allow-Headers *
|
|
44
|
-
header Access-Control-Max-Age 86400
|
|
45
|
-
respond 204
|
|
95
|
+
spinner.stop();
|
|
96
|
+
await ensureCaddyfileMigrated({ caddyfilePath: caddyFilePath, projectsPath });
|
|
97
|
+
spinner.start(`Linking ${chalk.bold(targetDomain)}...`);
|
|
98
|
+
|
|
99
|
+
let content = '';
|
|
100
|
+
if (await fs.pathExists(caddyFilePath)) {
|
|
101
|
+
content = await fs.readFile(caddyFilePath, 'utf-8');
|
|
46
102
|
}
|
|
47
103
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
104
|
+
const existingBlocks = parseBlocks(content);
|
|
105
|
+
const existingBlock = existingBlocks.find((b) => b.domain === targetDomain);
|
|
106
|
+
const alreadyLinked = Boolean(existingBlock);
|
|
107
|
+
const existingProject = await getProject(targetDomain, projectsPath);
|
|
108
|
+
const isNew = !alreadyLinked && !existingProject;
|
|
109
|
+
|
|
110
|
+
// When projects.json is missing the domain but a v2 block exists, seed from
|
|
111
|
+
// the block marker so re-link does not silently reset CORS settings.
|
|
112
|
+
let seed = null;
|
|
113
|
+
if (!existingProject && existingBlock && !existingBlock.isLegacy) {
|
|
114
|
+
seed = {
|
|
115
|
+
origins: existingBlock.options.origins || [],
|
|
116
|
+
allowAllOrigins: existingBlock.options.allowAllOrigins === true,
|
|
117
|
+
};
|
|
118
|
+
}
|
|
57
119
|
|
|
58
|
-
|
|
120
|
+
const project = await upsertProject(
|
|
121
|
+
targetDomain,
|
|
122
|
+
{
|
|
123
|
+
root: rootCheck.path,
|
|
124
|
+
originsFromFlags,
|
|
125
|
+
originsToRemove,
|
|
126
|
+
allowAllOriginsFlag,
|
|
127
|
+
isNew,
|
|
128
|
+
seed,
|
|
129
|
+
},
|
|
130
|
+
projectsPath
|
|
131
|
+
);
|
|
59
132
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
133
|
+
const newBlock = renderSiteBlock(targetDomain, rootCheck.path, {
|
|
134
|
+
allowAllOrigins: project.allowAllOrigins,
|
|
135
|
+
origins: project.origins,
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
content = upsertBlock(content, targetDomain, newBlock);
|
|
139
|
+
await fs.writeFile(caddyFilePath, `${content.trim()}\n`, 'utf-8');
|
|
65
140
|
|
|
66
|
-
|
|
141
|
+
if (project.allowAllOrigins) {
|
|
142
|
+
spinner.stop();
|
|
143
|
+
console.log(
|
|
144
|
+
chalk.yellow(
|
|
145
|
+
' Warning: allow-all-origins is enabled (Access-Control-Allow-Origin: *). Prefer --origin <live-domain> then --no-allow-all-origins.'
|
|
146
|
+
)
|
|
147
|
+
);
|
|
148
|
+
spinner.start(`Linking ${chalk.bold(targetDomain)}...`);
|
|
149
|
+
} else if (
|
|
150
|
+
!project.origins.some(
|
|
151
|
+
(o) => o.endsWith('.webflow.io') || o.endsWith('.webflow.com') || isBroadWebflowWildcard(o)
|
|
152
|
+
)
|
|
153
|
+
) {
|
|
154
|
+
spinner.stop();
|
|
155
|
+
console.log(
|
|
156
|
+
chalk.dim(
|
|
157
|
+
' Tip: Webflow is not in the default CORS allowlist. Add staging/live with `stagepass link --origin my-project.webflow.io` (or an explicit `*.webflow.io` with caution).'
|
|
158
|
+
)
|
|
159
|
+
);
|
|
160
|
+
spinner.start(`Linking ${chalk.bold(targetDomain)}...`);
|
|
161
|
+
}
|
|
67
162
|
} catch (e) {
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
163
|
+
spinner.fail('Failed to write config.');
|
|
164
|
+
if (options.verbose) console.error(e);
|
|
165
|
+
else if (e && e.message) console.log(chalk.dim(` ${e.message}`));
|
|
166
|
+
return;
|
|
71
167
|
}
|
|
72
168
|
|
|
73
|
-
// 2. Reload Caddy
|
|
74
169
|
try {
|
|
75
|
-
// If verbose is ON, show output. If OFF, ignore it (pipe).
|
|
76
170
|
const stdioMode = options.verbose ? 'inherit' : 'ignore';
|
|
77
|
-
|
|
78
171
|
await execa('caddy', ['reload', '--config', caddyFilePath], { stdio: stdioMode });
|
|
79
|
-
|
|
80
|
-
spinner.succeed(`Linked: https://${targetDomain} -> ${currentDir}`);
|
|
172
|
+
spinner.succeed(`Linked: https://${targetDomain} -> ${rootCheck.path}`);
|
|
81
173
|
} catch (error) {
|
|
82
174
|
spinner.warn('Config written, but Caddy reload failed.');
|
|
83
175
|
console.log(chalk.dim(' Is Stagepass running? Try: stagepass start'));
|
|
84
176
|
if (options.verbose) console.error(error);
|
|
85
177
|
}
|
|
86
178
|
}
|
|
87
|
-
|
|
88
|
-
function escapeRegExp(string) {
|
|
89
|
-
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
90
|
-
}
|