@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,96 @@
|
|
|
1
|
+
import fs from 'fs-extra';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import chalk from 'chalk';
|
|
4
|
+
import {
|
|
5
|
+
formatBackupStamp,
|
|
6
|
+
migrateLegacyContent,
|
|
7
|
+
parseBlocks,
|
|
8
|
+
} from './caddyfile.js';
|
|
9
|
+
import { ensureMigratedProject } from './projects.js';
|
|
10
|
+
import { getCaddyfilePath, getProjectsPath } from './paths.js';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Detect legacy blocks, backup once, rewrite, sync projects.json.
|
|
14
|
+
* Idempotent: second run changes nothing and creates no backup.
|
|
15
|
+
*
|
|
16
|
+
* @param {object} [options]
|
|
17
|
+
* @param {string} [options.caddyfilePath]
|
|
18
|
+
* @param {string} [options.projectsPath]
|
|
19
|
+
* @param {boolean} [options.silent]
|
|
20
|
+
* @returns {Promise<{ changed: boolean, migrated: Array, skipped: Array, backupPath: string|null }>}
|
|
21
|
+
*/
|
|
22
|
+
export async function ensureCaddyfileMigrated(options = {}) {
|
|
23
|
+
const caddyfilePath = options.caddyfilePath || getCaddyfilePath();
|
|
24
|
+
const projectsPath = options.projectsPath || getProjectsPath();
|
|
25
|
+
const silent = options.silent === true;
|
|
26
|
+
|
|
27
|
+
if (!(await fs.pathExists(caddyfilePath))) {
|
|
28
|
+
return { changed: false, migrated: [], skipped: [], backupPath: null };
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const original = await fs.readFile(caddyfilePath, 'utf-8');
|
|
32
|
+
const result = migrateLegacyContent(original, {
|
|
33
|
+
warn: silent
|
|
34
|
+
? () => {}
|
|
35
|
+
: (msg) => console.log(chalk.yellow(` ${msg}`)),
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
if (!result.changed) {
|
|
39
|
+
return {
|
|
40
|
+
changed: false,
|
|
41
|
+
migrated: [],
|
|
42
|
+
skipped: result.skipped,
|
|
43
|
+
backupPath: null,
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const backupPath = `${caddyfilePath}.bak-${formatBackupStamp()}`;
|
|
48
|
+
await fs.copy(caddyfilePath, backupPath);
|
|
49
|
+
await fs.writeFile(caddyfilePath, result.content, 'utf-8');
|
|
50
|
+
|
|
51
|
+
for (const item of result.migrated) {
|
|
52
|
+
await ensureMigratedProject(item.domain, item.root, projectsPath);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if (!silent) {
|
|
56
|
+
console.log(
|
|
57
|
+
chalk.yellow(
|
|
58
|
+
` Migrated ${result.migrated.length} legacy Caddy block(s). Backup: ${path.basename(backupPath)}`
|
|
59
|
+
)
|
|
60
|
+
);
|
|
61
|
+
for (const item of result.migrated) {
|
|
62
|
+
console.log(chalk.dim(` → ${item.domain} (allow-all-origins enabled for compatibility)`));
|
|
63
|
+
}
|
|
64
|
+
console.log(
|
|
65
|
+
chalk.yellow(
|
|
66
|
+
' Tip: add your live domain with `stagepass link --origin example.com`, then `stagepass link --no-allow-all-origins`.'
|
|
67
|
+
)
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return {
|
|
72
|
+
changed: true,
|
|
73
|
+
migrated: result.migrated,
|
|
74
|
+
skipped: result.skipped,
|
|
75
|
+
backupPath,
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Print a one-line security warning when any linked project uses allowAllOrigins.
|
|
81
|
+
*/
|
|
82
|
+
export async function warnIfAllowAllOriginsActive(options = {}) {
|
|
83
|
+
const caddyfilePath = options.caddyfilePath || getCaddyfilePath();
|
|
84
|
+
if (!(await fs.pathExists(caddyfilePath))) return;
|
|
85
|
+
|
|
86
|
+
const content = await fs.readFile(caddyfilePath, 'utf-8');
|
|
87
|
+
const blocks = parseBlocks(content);
|
|
88
|
+
const open = blocks.filter((b) => !b.isLegacy && b.options.allowAllOrigins);
|
|
89
|
+
if (open.length === 0) return;
|
|
90
|
+
|
|
91
|
+
console.log(
|
|
92
|
+
chalk.yellow(
|
|
93
|
+
` Warning: allow-all-origins is active for: ${open.map((b) => b.domain).join(', ')}`
|
|
94
|
+
)
|
|
95
|
+
);
|
|
96
|
+
}
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CORS origin allowlist helpers.
|
|
3
|
+
*
|
|
4
|
+
* Default patterns (anchored, case-insensitive):
|
|
5
|
+
* - https://*.sp
|
|
6
|
+
* - http(s)://localhost[:port]
|
|
7
|
+
* - http(s)://127.0.0.1[:port]
|
|
8
|
+
* - per-project live hostnames / *.wildcards / https:// origins
|
|
9
|
+
* (including explicit `*.webflow.io` when passed via --origin)
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
const UNSAFE_ORIGIN_RE = /[\s#"'{}$\\;()[\],<>|/]/;
|
|
13
|
+
const HOSTNAME_LABEL_RE = /^[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?$/;
|
|
14
|
+
|
|
15
|
+
/** Built-in Origin regex alternatives (without leading ^ / trailing $ / (?i)). */
|
|
16
|
+
export const DEFAULT_ORIGIN_ALTERNATIVES = Object.freeze([
|
|
17
|
+
'https://([a-z0-9-]+\\.)*[a-z0-9-]+\\.sp',
|
|
18
|
+
'https?://localhost(:[0-9]{1,5})?',
|
|
19
|
+
'https?://127\\.0\\.0\\.1(:[0-9]{1,5})?',
|
|
20
|
+
]);
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Broad Webflow wildcards that allow any Webflow user's site.
|
|
24
|
+
* Allowed when passed explicitly via --origin, but deserve a warning.
|
|
25
|
+
*
|
|
26
|
+
* @param {string} entry - validated origin entry
|
|
27
|
+
* @returns {boolean}
|
|
28
|
+
*/
|
|
29
|
+
export function isBroadWebflowWildcard(entry) {
|
|
30
|
+
return entry === '*.webflow.io' || entry === '*.webflow.com';
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Validate a single live-domain / origin entry.
|
|
35
|
+
* Accepts: example.com | www.example.com | *.example.com | https://example.com
|
|
36
|
+
* Also accepts full Webflow staging hosts (my-project.webflow.io) and explicit
|
|
37
|
+
* wildcards (*.webflow.io) when passed by the user.
|
|
38
|
+
*
|
|
39
|
+
* @param {string} input
|
|
40
|
+
* @returns {{ ok: true, value: string } | { ok: false, error: string }}
|
|
41
|
+
*/
|
|
42
|
+
export function validateOriginEntry(input) {
|
|
43
|
+
if (typeof input !== 'string' || input.trim() === '') {
|
|
44
|
+
return { ok: false, error: 'Origin entry is empty.' };
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
let raw = input.trim().toLowerCase();
|
|
48
|
+
|
|
49
|
+
if (/[\x00-\x1f\x7f]/.test(raw)) {
|
|
50
|
+
return { ok: false, error: 'Origin contains control characters.' };
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// Optional https:// only (http reserved for localhost defaults)
|
|
54
|
+
if (raw.startsWith('http://')) {
|
|
55
|
+
return { ok: false, error: 'Live origins must use https:// (or a bare hostname).' };
|
|
56
|
+
}
|
|
57
|
+
if (raw.startsWith('https://')) {
|
|
58
|
+
raw = raw.slice('https://'.length);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// Reject paths / query / credentials leftovers
|
|
62
|
+
if (raw.includes('/') || raw.includes('?') || raw.includes('@') || raw.includes(':')) {
|
|
63
|
+
return { ok: false, error: 'Origin must be a hostname (optional leading *.), no path or port.' };
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
let wildcard = false;
|
|
67
|
+
if (raw.startsWith('*.')) {
|
|
68
|
+
wildcard = true;
|
|
69
|
+
raw = raw.slice(2);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if (!raw || raw.startsWith('.') || raw.endsWith('.') || raw.includes('..')) {
|
|
73
|
+
return { ok: false, error: 'Invalid hostname in origin entry.' };
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// After stripping protocol / wildcard, remaining must be safe hostname chars only
|
|
77
|
+
if (UNSAFE_ORIGIN_RE.test(raw) || raw.includes('*')) {
|
|
78
|
+
return { ok: false, error: 'Origin contains invalid characters.' };
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const labels = raw.split('.');
|
|
82
|
+
if (!labels.every((l) => HOSTNAME_LABEL_RE.test(l))) {
|
|
83
|
+
return { ok: false, error: 'Origin hostname labels are not DNS-safe.' };
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const value = wildcard ? `*.${raw}` : raw;
|
|
87
|
+
return { ok: true, value };
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* @param {string[]} entries
|
|
92
|
+
* @returns {{ ok: true, origins: string[] } | { ok: false, error: string }}
|
|
93
|
+
*/
|
|
94
|
+
export function validateOriginList(entries) {
|
|
95
|
+
const out = [];
|
|
96
|
+
const seen = new Set();
|
|
97
|
+
for (const entry of entries || []) {
|
|
98
|
+
const result = validateOriginEntry(entry);
|
|
99
|
+
if (!result.ok) return result;
|
|
100
|
+
if (!seen.has(result.value)) {
|
|
101
|
+
seen.add(result.value);
|
|
102
|
+
out.push(result.value);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
return { ok: true, origins: out };
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Escape a hostname for use inside a RE2 / Caddy regex (dots only; labels already validated).
|
|
110
|
+
* @param {string} hostname
|
|
111
|
+
*/
|
|
112
|
+
export function escapeHostnameForRegex(hostname) {
|
|
113
|
+
return hostname.replace(/\./g, '\\.');
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Convert a stored origin entry into a regex alternative matching https://...
|
|
118
|
+
* @param {string} entry - validated hostname or *.hostname
|
|
119
|
+
*/
|
|
120
|
+
export function originEntryToRegexAlternative(entry) {
|
|
121
|
+
if (entry.startsWith('*.')) {
|
|
122
|
+
const host = escapeHostnameForRegex(entry.slice(2));
|
|
123
|
+
return `https://([a-z0-9-]+\\.)*${host}`;
|
|
124
|
+
}
|
|
125
|
+
return `https://${escapeHostnameForRegex(entry)}`;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Full Origin header regex for Caddy header_regexp (includes (?i) and anchors).
|
|
130
|
+
* @param {string[]} [customOrigins]
|
|
131
|
+
*/
|
|
132
|
+
export function buildOriginAllowlistRegex(customOrigins = []) {
|
|
133
|
+
const validated = validateOriginList(customOrigins);
|
|
134
|
+
const customs = validated.ok ? validated.origins : [];
|
|
135
|
+
const parts = [
|
|
136
|
+
...DEFAULT_ORIGIN_ALTERNATIVES,
|
|
137
|
+
...customs.map(originEntryToRegexAlternative),
|
|
138
|
+
];
|
|
139
|
+
return `(?i)^(?:${parts.join('|')})$`;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Whether a browser Origin would match the allowlist (for unit tests).
|
|
144
|
+
* Note: Caddy/RE2 accepts (?i) inline; JS RegExp needs the `i` flag instead.
|
|
145
|
+
* @param {string} origin
|
|
146
|
+
* @param {string[]} [customOrigins]
|
|
147
|
+
*/
|
|
148
|
+
export function originMatchesAllowlist(origin, customOrigins = []) {
|
|
149
|
+
try {
|
|
150
|
+
let pattern = buildOriginAllowlistRegex(customOrigins);
|
|
151
|
+
let flags = '';
|
|
152
|
+
if (pattern.startsWith('(?i)')) {
|
|
153
|
+
pattern = pattern.slice(4);
|
|
154
|
+
flags = 'i';
|
|
155
|
+
}
|
|
156
|
+
return new RegExp(pattern, flags).test(origin);
|
|
157
|
+
} catch {
|
|
158
|
+
return false;
|
|
159
|
+
}
|
|
160
|
+
}
|
package/src/lib/paths.js
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import os from 'os';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
|
|
4
|
+
/** Stagepass config directory (~/.stagepass). */
|
|
5
|
+
export function getStagepassDir() {
|
|
6
|
+
return path.join(os.homedir(), '.stagepass');
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function getCaddyfilePath() {
|
|
10
|
+
return path.join(getStagepassDir(), 'Caddyfile');
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function getProjectsPath() {
|
|
14
|
+
return path.join(getStagepassDir(), 'projects.json');
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function getServerLogPath() {
|
|
18
|
+
return path.join(getStagepassDir(), 'server.log');
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** Daemon / autostart log directory (~/.stagepass/logs). */
|
|
22
|
+
export function getLogsDir() {
|
|
23
|
+
return path.join(getStagepassDir(), 'logs');
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function getDaemonStdoutLogPath() {
|
|
27
|
+
return path.join(getLogsDir(), 'daemon.stdout.log');
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function getDaemonStderrLogPath() {
|
|
31
|
+
return path.join(getLogsDir(), 'daemon.stderr.log');
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** LaunchAgent label (owned GitHub-io reverse DNS; not a third-party domain). */
|
|
35
|
+
export const LAUNCH_AGENT_LABEL = 'io.github.arobertherz.stagepass';
|
|
36
|
+
|
|
37
|
+
export function getLaunchAgentsDir() {
|
|
38
|
+
return path.join(os.homedir(), 'Library', 'LaunchAgents');
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function getLaunchAgentPlistPath() {
|
|
42
|
+
return path.join(getLaunchAgentsDir(), `${LAUNCH_AGENT_LABEL}.plist`);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Caddy default data directory on macOS (user instance).
|
|
47
|
+
* Honours XDG_DATA_HOME when set (same rules as Caddy itself).
|
|
48
|
+
* @param {{ home?: string, env?: NodeJS.ProcessEnv }} [opts]
|
|
49
|
+
*/
|
|
50
|
+
export function getCaddyDataDir(opts = {}) {
|
|
51
|
+
const env = opts.env || process.env;
|
|
52
|
+
const home = opts.home || os.homedir();
|
|
53
|
+
if (env.XDG_DATA_HOME) {
|
|
54
|
+
return path.join(env.XDG_DATA_HOME, 'caddy');
|
|
55
|
+
}
|
|
56
|
+
return path.join(home, 'Library', 'Application Support', 'Caddy');
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Path to Caddy's local root CA certificate.
|
|
61
|
+
* @param {{ home?: string, env?: NodeJS.ProcessEnv, dataDir?: string }} [opts]
|
|
62
|
+
*/
|
|
63
|
+
export function getCaddyRootCaPath(opts = {}) {
|
|
64
|
+
const dataDir = opts.dataDir || getCaddyDataDir(opts);
|
|
65
|
+
return path.join(dataDir, 'pki', 'authorities', 'local', 'root.crt');
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Common secondary data dir when Homebrew's Caddy service sets XDG_DATA_HOME.
|
|
70
|
+
* Report-only helper for doctor/certs (never delete).
|
|
71
|
+
* @param {{ brewPrefix?: string|null }} [opts]
|
|
72
|
+
*/
|
|
73
|
+
export function getBrewCaddyDataDir(opts = {}) {
|
|
74
|
+
const prefix = opts.brewPrefix;
|
|
75
|
+
if (!prefix) return null;
|
|
76
|
+
return path.join(prefix, 'var', 'lib', 'caddy');
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** System / dangerous roots that must never be linked as site roots. */
|
|
80
|
+
export const FORBIDDEN_ROOTS = Object.freeze([
|
|
81
|
+
'/',
|
|
82
|
+
'/Users',
|
|
83
|
+
'/System',
|
|
84
|
+
'/Library',
|
|
85
|
+
'/Applications',
|
|
86
|
+
'/private',
|
|
87
|
+
'/etc',
|
|
88
|
+
'/bin',
|
|
89
|
+
'/usr',
|
|
90
|
+
'/sbin',
|
|
91
|
+
'/var',
|
|
92
|
+
'/dev',
|
|
93
|
+
]);
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Escape a path for use inside a Caddyfile double-quoted token.
|
|
97
|
+
* @param {string} value
|
|
98
|
+
* @returns {string}
|
|
99
|
+
*/
|
|
100
|
+
export function escapeCaddyQuotedString(value) {
|
|
101
|
+
return value.replace(/\\/g, '\\\\').replace(/"/g, '\\"');
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Validate a project root path before writing it into the Caddyfile.
|
|
106
|
+
* @param {string} rootPath
|
|
107
|
+
* @returns {{ ok: true, path: string } | { ok: false, error: string }}
|
|
108
|
+
*/
|
|
109
|
+
export function validateRoot(rootPath) {
|
|
110
|
+
if (typeof rootPath !== 'string' || rootPath.length === 0) {
|
|
111
|
+
return { ok: false, error: 'Project path is empty.' };
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
if (/[\x00-\x1f\x7f]/.test(rootPath)) {
|
|
115
|
+
return { ok: false, error: 'Project path contains control characters or newlines.' };
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const resolved = path.resolve(rootPath);
|
|
119
|
+
const home = path.resolve(os.homedir());
|
|
120
|
+
|
|
121
|
+
if (resolved === home) {
|
|
122
|
+
return { ok: false, error: 'Refusing to link your home directory as a site root.' };
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
for (const forbidden of FORBIDDEN_ROOTS) {
|
|
126
|
+
if (resolved === path.resolve(forbidden)) {
|
|
127
|
+
return { ok: false, error: `Refusing to link system path "${forbidden}" as a site root.` };
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
return { ok: true, path: resolved };
|
|
132
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/** Env var that skips the macOS-only guard (tests/CI only). */
|
|
2
|
+
export const SKIP_PLATFORM_CHECK_ENV = 'STAGEPASS_SKIP_PLATFORM_CHECK';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @param {string} [platform=process.platform]
|
|
6
|
+
* @returns {boolean}
|
|
7
|
+
*/
|
|
8
|
+
export function isMacOS(platform = process.platform) {
|
|
9
|
+
return platform === 'darwin';
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Build the user-facing unsupported-platform message.
|
|
14
|
+
* @param {string} platform
|
|
15
|
+
* @returns {string}
|
|
16
|
+
*/
|
|
17
|
+
export function unsupportedPlatformMessage(platform) {
|
|
18
|
+
return `Stagepass CLI supports macOS only (detected: ${platform}).`;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Exit with a clear error when running system commands off macOS.
|
|
23
|
+
* No-op when `STAGEPASS_SKIP_PLATFORM_CHECK=1` (tests/CI only) or on darwin.
|
|
24
|
+
*
|
|
25
|
+
* @param {object} [options]
|
|
26
|
+
* @param {string} [options.platform]
|
|
27
|
+
* @param {NodeJS.ProcessEnv} [options.env]
|
|
28
|
+
* @param {(code: number) => void} [options.exit]
|
|
29
|
+
* @param {(msg: string) => void} [options.write]
|
|
30
|
+
*/
|
|
31
|
+
export function assertSupportedPlatform(options = {}) {
|
|
32
|
+
const platform = options.platform ?? process.platform;
|
|
33
|
+
const env = options.env ?? process.env;
|
|
34
|
+
const exit = options.exit ?? ((code) => process.exit(code));
|
|
35
|
+
const write = options.write ?? ((msg) => console.error(msg));
|
|
36
|
+
|
|
37
|
+
if (env[SKIP_PLATFORM_CHECK_ENV] === '1') {
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if (isMacOS(platform)) {
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
write(unsupportedPlatformMessage(platform));
|
|
46
|
+
exit(1);
|
|
47
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DNS-safe project name: lowercase a-z0-9 and hyphen, 1 to 63 chars,
|
|
3
|
+
* no leading or trailing hyphen.
|
|
4
|
+
*/
|
|
5
|
+
const NAME_RE = /^[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?$/;
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @param {unknown} name
|
|
9
|
+
* @returns {{ ok: true, name: string } | { ok: false, error: string }}
|
|
10
|
+
*/
|
|
11
|
+
export function validateProjectName(name) {
|
|
12
|
+
if (typeof name !== 'string' || name.trim() === '') {
|
|
13
|
+
return { ok: false, error: 'Project name is required.' };
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const trimmed = name.trim();
|
|
17
|
+
if (trimmed !== trimmed.toLowerCase()) {
|
|
18
|
+
return {
|
|
19
|
+
ok: false,
|
|
20
|
+
error: 'Project name must be lowercase (a-z, 0-9, hyphen).',
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
if (trimmed.length < 1 || trimmed.length > 63) {
|
|
25
|
+
return {
|
|
26
|
+
ok: false,
|
|
27
|
+
error: 'Project name must be 1 to 63 characters.',
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
if (trimmed.startsWith('-') || trimmed.endsWith('-')) {
|
|
32
|
+
return {
|
|
33
|
+
ok: false,
|
|
34
|
+
error: 'Project name must not start or end with a hyphen.',
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
if (!NAME_RE.test(trimmed)) {
|
|
39
|
+
return {
|
|
40
|
+
ok: false,
|
|
41
|
+
error:
|
|
42
|
+
'Project name must be DNS-safe: lowercase a-z, 0-9, and hyphens only.',
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return { ok: true, name: trimmed };
|
|
47
|
+
}
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
import fs from 'fs-extra';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import { getProjectsPath } from './paths.js';
|
|
4
|
+
import { validateOriginList } from './origins.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Per-project CORS settings stored in ~/.stagepass/projects.json
|
|
8
|
+
*
|
|
9
|
+
* Shape:
|
|
10
|
+
* {
|
|
11
|
+
* "version": 1,
|
|
12
|
+
* "projects": {
|
|
13
|
+
* "my-app.sp": {
|
|
14
|
+
* "root": "/absolute/path",
|
|
15
|
+
* "origins": ["example.com", "*.example.org"],
|
|
16
|
+
* "allowAllOrigins": false
|
|
17
|
+
* }
|
|
18
|
+
* }
|
|
19
|
+
* }
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
const DEFAULT_DOC = { version: 1, projects: {} };
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* @returns {Promise<{ version: number, projects: Record<string, object> }>}
|
|
26
|
+
*/
|
|
27
|
+
export async function loadProjects(filePath = getProjectsPath()) {
|
|
28
|
+
if (!(await fs.pathExists(filePath))) {
|
|
29
|
+
return structuredClone(DEFAULT_DOC);
|
|
30
|
+
}
|
|
31
|
+
try {
|
|
32
|
+
const raw = await fs.readFile(filePath, 'utf-8');
|
|
33
|
+
const data = JSON.parse(raw);
|
|
34
|
+
if (!data || typeof data !== 'object' || typeof data.projects !== 'object') {
|
|
35
|
+
return structuredClone(DEFAULT_DOC);
|
|
36
|
+
}
|
|
37
|
+
return {
|
|
38
|
+
version: 1,
|
|
39
|
+
projects: data.projects || {},
|
|
40
|
+
};
|
|
41
|
+
} catch {
|
|
42
|
+
return structuredClone(DEFAULT_DOC);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* @param {{ version: number, projects: Record<string, object> }} doc
|
|
48
|
+
*/
|
|
49
|
+
export async function saveProjects(doc, filePath = getProjectsPath()) {
|
|
50
|
+
await fs.ensureDir(path.dirname(filePath));
|
|
51
|
+
const payload = {
|
|
52
|
+
version: 1,
|
|
53
|
+
projects: doc.projects || {},
|
|
54
|
+
};
|
|
55
|
+
await fs.writeFile(filePath, `${JSON.stringify(payload, null, 2)}\n`, 'utf-8');
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* @param {string} domain
|
|
60
|
+
* @returns {Promise<{ root?: string, origins: string[], allowAllOrigins: boolean }|null>}
|
|
61
|
+
*/
|
|
62
|
+
export async function getProject(domain, filePath = getProjectsPath()) {
|
|
63
|
+
const doc = await loadProjects(filePath);
|
|
64
|
+
const entry = doc.projects[domain];
|
|
65
|
+
if (!entry) return null;
|
|
66
|
+
return normalizeProjectEntry(entry);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Merge and persist project settings.
|
|
71
|
+
*
|
|
72
|
+
* Flag semantics for link:
|
|
73
|
+
* - originsFromFlags: when non-empty, merge into existing (deduped)
|
|
74
|
+
* - originsToRemove: when non-empty, remove matching stored origins (after merge)
|
|
75
|
+
* - allowAllOriginsFlag: true | false | undefined (undefined = keep existing / seed / default)
|
|
76
|
+
* - seed: when no projects.json entry exists, start from these values (e.g. Caddyfile marker)
|
|
77
|
+
* - isNew: when true and allowAllOriginsFlag is undefined, default allowAllOrigins to false
|
|
78
|
+
* - forceAllowAllOrigins: migration helper; force true once
|
|
79
|
+
*
|
|
80
|
+
* @param {string} domain
|
|
81
|
+
* @param {object} update
|
|
82
|
+
* @param {string} update.root
|
|
83
|
+
* @param {string[]} [update.originsFromFlags]
|
|
84
|
+
* @param {string[]} [update.originsToRemove]
|
|
85
|
+
* @param {boolean|undefined} [update.allowAllOriginsFlag]
|
|
86
|
+
* @param {{ origins?: string[], allowAllOrigins?: boolean }|null} [update.seed]
|
|
87
|
+
* @param {boolean} [update.isNew]
|
|
88
|
+
* @param {boolean} [update.forceAllowAllOrigins]
|
|
89
|
+
*/
|
|
90
|
+
export async function upsertProject(domain, update, filePath = getProjectsPath()) {
|
|
91
|
+
const doc = await loadProjects(filePath);
|
|
92
|
+
const existing = doc.projects[domain]
|
|
93
|
+
? normalizeProjectEntry(doc.projects[domain])
|
|
94
|
+
: update.seed
|
|
95
|
+
? normalizeProjectEntry(update.seed)
|
|
96
|
+
: { origins: [], allowAllOrigins: false };
|
|
97
|
+
|
|
98
|
+
let origins = existing.origins;
|
|
99
|
+
if (Array.isArray(update.originsFromFlags) && update.originsFromFlags.length > 0) {
|
|
100
|
+
const merged = validateOriginList([...existing.origins, ...update.originsFromFlags]);
|
|
101
|
+
if (!merged.ok) {
|
|
102
|
+
throw new Error(merged.error);
|
|
103
|
+
}
|
|
104
|
+
origins = merged.origins;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
if (Array.isArray(update.originsToRemove) && update.originsToRemove.length > 0) {
|
|
108
|
+
const toRemove = validateOriginList(update.originsToRemove);
|
|
109
|
+
if (!toRemove.ok) {
|
|
110
|
+
throw new Error(toRemove.error);
|
|
111
|
+
}
|
|
112
|
+
const removeSet = new Set(toRemove.origins);
|
|
113
|
+
origins = origins.filter((o) => !removeSet.has(o));
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
let allowAllOrigins = existing.allowAllOrigins;
|
|
117
|
+
if (update.isNew && update.allowAllOriginsFlag === undefined) {
|
|
118
|
+
allowAllOrigins = false;
|
|
119
|
+
}
|
|
120
|
+
if (update.allowAllOriginsFlag === true) allowAllOrigins = true;
|
|
121
|
+
if (update.allowAllOriginsFlag === false) allowAllOrigins = false;
|
|
122
|
+
|
|
123
|
+
if (update.forceAllowAllOrigins === true) {
|
|
124
|
+
allowAllOrigins = true;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
doc.projects[domain] = {
|
|
128
|
+
root: update.root,
|
|
129
|
+
origins,
|
|
130
|
+
allowAllOrigins,
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
await saveProjects(doc, filePath);
|
|
134
|
+
return normalizeProjectEntry(doc.projects[domain]);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Ensure a projects.json entry exists after migrating a legacy Caddy block.
|
|
139
|
+
*
|
|
140
|
+
* Semantics:
|
|
141
|
+
* - Existing entry: leave unchanged (never drop stored origins; never force
|
|
142
|
+
* allowAllOrigins — the user may already have tightened CORS).
|
|
143
|
+
* - New entry from legacy migration: allowAllOrigins=true for compatibility,
|
|
144
|
+
* origins=[] until the user adds --origin.
|
|
145
|
+
*
|
|
146
|
+
* A migrated legacy project therefore keeps allowAllOrigins=true until the user
|
|
147
|
+
* turns it off; existing stored origins are never dropped by migration.
|
|
148
|
+
*/
|
|
149
|
+
export async function ensureMigratedProject(domain, root, filePath = getProjectsPath()) {
|
|
150
|
+
const doc = await loadProjects(filePath);
|
|
151
|
+
const existing = doc.projects[domain];
|
|
152
|
+
if (existing) {
|
|
153
|
+
return normalizeProjectEntry(existing);
|
|
154
|
+
}
|
|
155
|
+
doc.projects[domain] = {
|
|
156
|
+
root,
|
|
157
|
+
origins: [],
|
|
158
|
+
allowAllOrigins: true,
|
|
159
|
+
};
|
|
160
|
+
await saveProjects(doc, filePath);
|
|
161
|
+
return normalizeProjectEntry(doc.projects[domain]);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
export async function removeProject(domain, filePath = getProjectsPath()) {
|
|
165
|
+
const doc = await loadProjects(filePath);
|
|
166
|
+
if (doc.projects[domain]) {
|
|
167
|
+
delete doc.projects[domain];
|
|
168
|
+
await saveProjects(doc, filePath);
|
|
169
|
+
return true;
|
|
170
|
+
}
|
|
171
|
+
return false;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
function normalizeProjectEntry(entry) {
|
|
175
|
+
const originsResult = validateOriginList(entry.origins || []);
|
|
176
|
+
return {
|
|
177
|
+
root: entry.root,
|
|
178
|
+
origins: originsResult.ok ? originsResult.origins : [],
|
|
179
|
+
allowAllOrigins: entry.allowAllOrigins === true,
|
|
180
|
+
};
|
|
181
|
+
}
|