@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,198 @@
|
|
|
1
|
+
import fs from 'fs-extra';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import { createRequire } from 'module';
|
|
4
|
+
import chalk from 'chalk';
|
|
5
|
+
import { defaultExec } from '../lib/exec.js';
|
|
6
|
+
import {
|
|
7
|
+
buildWebflowSnippets,
|
|
8
|
+
printWebflowSnippets,
|
|
9
|
+
} from '../lib/snippets.js';
|
|
10
|
+
|
|
11
|
+
const require = createRequire(import.meta.url);
|
|
12
|
+
const { version: cliVersion } = require('../../package.json');
|
|
13
|
+
|
|
14
|
+
const STAGEPASS_SCOPE = '@stagepass/';
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* True when the version looks like an npm dist-tag that needs resolution.
|
|
18
|
+
* @param {string} version
|
|
19
|
+
*/
|
|
20
|
+
function needsNpmView(version) {
|
|
21
|
+
if (!version) return false;
|
|
22
|
+
// Semver-ish or range: treat as literal. Dist-tags are bare words.
|
|
23
|
+
if (/^[0-9]/.test(version) || version.startsWith('^') || version.startsWith('~')) {
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
if (version.includes('.')) return false;
|
|
27
|
+
return /^[a-z][a-z0-9_-]*$/i.test(version);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Resolve target version. Uses CLI package version by default.
|
|
32
|
+
* Contacts the network only when `--version` is an npm dist-tag (e.g. latest).
|
|
33
|
+
*
|
|
34
|
+
* @param {string|undefined} requested
|
|
35
|
+
* @param {object} deps
|
|
36
|
+
* @param {import('../lib/exec.js').ExecFn} [deps.exec]
|
|
37
|
+
* @param {string} [deps.cliVersion]
|
|
38
|
+
* @returns {Promise<string>}
|
|
39
|
+
*/
|
|
40
|
+
export async function resolveUpgradeVersion(requested, deps = {}) {
|
|
41
|
+
const exec = deps.exec || defaultExec;
|
|
42
|
+
const fallback = deps.cliVersion || cliVersion;
|
|
43
|
+
|
|
44
|
+
if (!requested || String(requested).trim() === '') {
|
|
45
|
+
return fallback;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const value = String(requested).trim();
|
|
49
|
+
if (!needsNpmView(value)) {
|
|
50
|
+
const literal = value.replace(/^v/i, '');
|
|
51
|
+
// Keep package.json / snippet versions free of shell or HTML metacharacters.
|
|
52
|
+
if (!/^[~^]?[0-9A-Za-z][0-9A-Za-z._+-]*$/.test(literal)) {
|
|
53
|
+
throw new Error(
|
|
54
|
+
'Invalid --version value (use a semver, range prefix ^/~, or npm dist-tag).'
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
return literal;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const { stdout } = await exec('npm', ['view', `@stagepass/cli@${value}`, 'version']);
|
|
61
|
+
const resolved = String(stdout || '').trim();
|
|
62
|
+
if (!resolved) {
|
|
63
|
+
throw new Error(`Could not resolve version for dist-tag "${value}".`);
|
|
64
|
+
}
|
|
65
|
+
if (!/^[~^]?[0-9A-Za-z][0-9A-Za-z._+-]*$/.test(resolved)) {
|
|
66
|
+
throw new Error(`Resolved version looks unsafe: ${resolved}`);
|
|
67
|
+
}
|
|
68
|
+
return resolved;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Collect @stagepass/* entries from dependencies and devDependencies.
|
|
73
|
+
* @param {object} pkg
|
|
74
|
+
* @returns {{ section: 'dependencies'|'devDependencies', name: string, from: string }[]}
|
|
75
|
+
*/
|
|
76
|
+
export function findStagepassDeps(pkg) {
|
|
77
|
+
/** @type {{ section: 'dependencies'|'devDependencies', name: string, from: string }[]} */
|
|
78
|
+
const found = [];
|
|
79
|
+
for (const section of /** @type {const} */ (['dependencies', 'devDependencies'])) {
|
|
80
|
+
const block = pkg[section];
|
|
81
|
+
if (!block || typeof block !== 'object') continue;
|
|
82
|
+
for (const [name, from] of Object.entries(block)) {
|
|
83
|
+
if (name.startsWith(STAGEPASS_SCOPE)) {
|
|
84
|
+
found.push({ section, name, from: String(from) });
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return found;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Rewrite @stagepass/* versions in a package.json object (mutates a copy).
|
|
93
|
+
* @param {object} pkg
|
|
94
|
+
* @param {string} targetVersion
|
|
95
|
+
* @returns {{ next: object, changes: { name: string, from: string, to: string, section: string }[] }}
|
|
96
|
+
*/
|
|
97
|
+
export function applyStagepassVersions(pkg, targetVersion) {
|
|
98
|
+
const next = structuredClone(pkg);
|
|
99
|
+
const changes = [];
|
|
100
|
+
const target = String(targetVersion);
|
|
101
|
+
|
|
102
|
+
for (const { section, name, from } of findStagepassDeps(pkg)) {
|
|
103
|
+
if (!next[section]) continue;
|
|
104
|
+
const to = target;
|
|
105
|
+
if (String(from) !== to) {
|
|
106
|
+
changes.push({ name, from: String(from), to, section });
|
|
107
|
+
}
|
|
108
|
+
next[section][name] = to;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
return { next, changes };
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Upgrade @stagepass/* deps in the current project's package.json.
|
|
116
|
+
*
|
|
117
|
+
* @param {object} [options]
|
|
118
|
+
* @param {boolean} [options.dryRun]
|
|
119
|
+
* @param {string} [options.version]
|
|
120
|
+
* @param {object} [deps]
|
|
121
|
+
* @param {typeof fs} [deps.fs]
|
|
122
|
+
* @param {string} [deps.cwd]
|
|
123
|
+
* @param {import('../lib/exec.js').ExecFn} [deps.exec]
|
|
124
|
+
* @param {string} [deps.cliVersion]
|
|
125
|
+
* @param {(msg?: string) => void} [deps.log]
|
|
126
|
+
* @returns {Promise<number>} exit code
|
|
127
|
+
*/
|
|
128
|
+
export async function upgrade(options = {}, deps = {}) {
|
|
129
|
+
const fse = deps.fs || fs;
|
|
130
|
+
const cwd = deps.cwd || process.cwd();
|
|
131
|
+
const log = deps.log || console.log;
|
|
132
|
+
const pkgPath = path.join(cwd, 'package.json');
|
|
133
|
+
|
|
134
|
+
if (!(await fse.pathExists(pkgPath))) {
|
|
135
|
+
log(chalk.red('No package.json found in the current directory.'));
|
|
136
|
+
return 1;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
let targetVersion;
|
|
140
|
+
try {
|
|
141
|
+
targetVersion = await resolveUpgradeVersion(options.version, {
|
|
142
|
+
exec: deps.exec || defaultExec,
|
|
143
|
+
cliVersion: deps.cliVersion || cliVersion,
|
|
144
|
+
});
|
|
145
|
+
} catch (error) {
|
|
146
|
+
log(chalk.red(error instanceof Error ? error.message : String(error)));
|
|
147
|
+
return 1;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
const raw = await fse.readFile(pkgPath, 'utf8');
|
|
151
|
+
let pkg;
|
|
152
|
+
try {
|
|
153
|
+
pkg = JSON.parse(raw);
|
|
154
|
+
} catch {
|
|
155
|
+
log(chalk.red('package.json is not valid JSON.'));
|
|
156
|
+
return 1;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
const { next, changes } = applyStagepassVersions(pkg, targetVersion);
|
|
160
|
+
const hasModules = Boolean(
|
|
161
|
+
next.dependencies?.['@stagepass/modules'] ||
|
|
162
|
+
next.devDependencies?.['@stagepass/modules']
|
|
163
|
+
);
|
|
164
|
+
|
|
165
|
+
if (changes.length === 0) {
|
|
166
|
+
const existing = findStagepassDeps(pkg);
|
|
167
|
+
if (existing.length === 0) {
|
|
168
|
+
log(chalk.yellow('No @stagepass/* packages found in package.json.'));
|
|
169
|
+
} else {
|
|
170
|
+
log(chalk.green(`@stagepass/* already at ${targetVersion}.`));
|
|
171
|
+
}
|
|
172
|
+
} else {
|
|
173
|
+
log(chalk.bold(`Upgrade @stagepass/* → ${targetVersion}`));
|
|
174
|
+
for (const change of changes) {
|
|
175
|
+
log(` ${change.name}: ${change.from} → ${change.to} (${change.section})`);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
if (options.dryRun) {
|
|
179
|
+
log(chalk.dim('\nDry run: package.json was not modified.'));
|
|
180
|
+
} else {
|
|
181
|
+
const trailingNewline = raw.endsWith('\n');
|
|
182
|
+
const body = `${JSON.stringify(next, null, 2)}${trailingNewline ? '\n' : ''}`;
|
|
183
|
+
await fse.writeFile(pkgPath, body, 'utf8');
|
|
184
|
+
log(chalk.green('\nUpdated package.json.'));
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
const snippets = buildWebflowSnippets({
|
|
189
|
+
version: targetVersion,
|
|
190
|
+
modules: hasModules,
|
|
191
|
+
});
|
|
192
|
+
printWebflowSnippets(snippets, log);
|
|
193
|
+
|
|
194
|
+
log(chalk.bold('\nNext step (not run automatically):'));
|
|
195
|
+
log(' npm install');
|
|
196
|
+
|
|
197
|
+
return 0;
|
|
198
|
+
}
|
|
@@ -0,0 +1,360 @@
|
|
|
1
|
+
import fs from 'fs-extra';
|
|
2
|
+
import os from 'node:os';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import {
|
|
5
|
+
LAUNCH_AGENT_LABEL,
|
|
6
|
+
getLaunchAgentPlistPath,
|
|
7
|
+
getLaunchAgentsDir,
|
|
8
|
+
getLogsDir,
|
|
9
|
+
getDaemonStdoutLogPath,
|
|
10
|
+
getDaemonStderrLogPath,
|
|
11
|
+
getCaddyfilePath,
|
|
12
|
+
} from './paths.js';
|
|
13
|
+
import { defaultExec, resolveCommand } from './exec.js';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Escape a string for use inside an XML plist text node.
|
|
17
|
+
* @param {string} value
|
|
18
|
+
*/
|
|
19
|
+
export function escapeXml(value) {
|
|
20
|
+
return String(value)
|
|
21
|
+
.replace(/&/g, '&')
|
|
22
|
+
.replace(/</g, '<')
|
|
23
|
+
.replace(/>/g, '>')
|
|
24
|
+
.replace(/"/g, '"')
|
|
25
|
+
.replace(/'/g, ''');
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Build LaunchAgent ProgramArguments for `stagepass daemon`.
|
|
30
|
+
* @param {{ nodePath: string, cliPath: string }} paths
|
|
31
|
+
* @returns {string[]}
|
|
32
|
+
*/
|
|
33
|
+
export function buildDaemonProgramArguments(paths) {
|
|
34
|
+
return [paths.nodePath, paths.cliPath, 'daemon'];
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* launchctl bootstrap arguments (gui domain).
|
|
39
|
+
* @param {number|string} uid
|
|
40
|
+
* @param {string} plistPath
|
|
41
|
+
* @returns {string[]}
|
|
42
|
+
*/
|
|
43
|
+
export function buildLaunchctlBootstrapArgs(uid, plistPath) {
|
|
44
|
+
return ['bootstrap', `gui/${uid}`, plistPath];
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* launchctl bootout arguments (gui domain).
|
|
49
|
+
* @param {number|string} uid
|
|
50
|
+
* @param {string} [label]
|
|
51
|
+
* @returns {string[]}
|
|
52
|
+
*/
|
|
53
|
+
export function buildLaunchctlBootoutArgs(uid, label = LAUNCH_AGENT_LABEL) {
|
|
54
|
+
return ['bootout', `gui/${uid}/${label}`];
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* launchctl print arguments (gui domain).
|
|
59
|
+
* @param {number|string} uid
|
|
60
|
+
* @param {string} [label]
|
|
61
|
+
* @returns {string[]}
|
|
62
|
+
*/
|
|
63
|
+
export function buildLaunchctlPrintArgs(uid, label = LAUNCH_AGENT_LABEL) {
|
|
64
|
+
return ['print', `gui/${uid}/${label}`];
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Render a LaunchAgent plist that runs `stagepass daemon` at login.
|
|
69
|
+
*
|
|
70
|
+
* Port 443: existing `stagepass start` runs `caddy start` as the user (sudo is
|
|
71
|
+
* only used for conflict detection / kill). A user LaunchAgent therefore has the
|
|
72
|
+
* same bind privileges as interactive `start` on macOS. If bind fails at
|
|
73
|
+
* runtime, `daemon` logs a clear error and exits 0 so KeepAlive does not loop.
|
|
74
|
+
*
|
|
75
|
+
* @param {object} opts
|
|
76
|
+
* @param {string} opts.nodePath
|
|
77
|
+
* @param {string} opts.cliPath
|
|
78
|
+
* @param {string} [opts.label]
|
|
79
|
+
* @param {string} [opts.stdoutPath]
|
|
80
|
+
* @param {string} [opts.stderrPath]
|
|
81
|
+
* @param {string} [opts.workingDirectory]
|
|
82
|
+
*/
|
|
83
|
+
export function renderLaunchAgentPlist(opts) {
|
|
84
|
+
const label = opts.label || LAUNCH_AGENT_LABEL;
|
|
85
|
+
const programArguments = buildDaemonProgramArguments({
|
|
86
|
+
nodePath: opts.nodePath,
|
|
87
|
+
cliPath: opts.cliPath,
|
|
88
|
+
});
|
|
89
|
+
const stdoutPath = opts.stdoutPath || getDaemonStdoutLogPath();
|
|
90
|
+
const stderrPath = opts.stderrPath || getDaemonStderrLogPath();
|
|
91
|
+
const workingDirectory = opts.workingDirectory || os.homedir();
|
|
92
|
+
|
|
93
|
+
const argXml = programArguments
|
|
94
|
+
.map((a) => `\t\t<string>${escapeXml(a)}</string>`)
|
|
95
|
+
.join('\n');
|
|
96
|
+
|
|
97
|
+
return `<?xml version="1.0" encoding="UTF-8"?>
|
|
98
|
+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
|
99
|
+
<plist version="1.0">
|
|
100
|
+
<dict>
|
|
101
|
+
\t<key>Label</key>
|
|
102
|
+
\t<string>${escapeXml(label)}</string>
|
|
103
|
+
\t<key>ProgramArguments</key>
|
|
104
|
+
\t<array>
|
|
105
|
+
${argXml}
|
|
106
|
+
\t</array>
|
|
107
|
+
\t<key>WorkingDirectory</key>
|
|
108
|
+
\t<string>${escapeXml(workingDirectory)}</string>
|
|
109
|
+
\t<key>RunAtLoad</key>
|
|
110
|
+
\t<true/>
|
|
111
|
+
\t<key>KeepAlive</key>
|
|
112
|
+
\t<dict>
|
|
113
|
+
\t\t<key>SuccessfulExit</key>
|
|
114
|
+
\t\t<false/>
|
|
115
|
+
\t</dict>
|
|
116
|
+
\t<key>StandardOutPath</key>
|
|
117
|
+
\t<string>${escapeXml(stdoutPath)}</string>
|
|
118
|
+
\t<key>StandardErrorPath</key>
|
|
119
|
+
\t<string>${escapeXml(stderrPath)}</string>
|
|
120
|
+
</dict>
|
|
121
|
+
</plist>
|
|
122
|
+
`;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Require an absolute path that exists on disk (LaunchAgent ProgramArguments).
|
|
127
|
+
* @param {string|null|undefined} candidate
|
|
128
|
+
* @param {string} label
|
|
129
|
+
* @param {(p: string) => Promise<boolean>} pathExists
|
|
130
|
+
* @returns {Promise<{ ok: true, path: string } | { ok: false, error: string }>}
|
|
131
|
+
*/
|
|
132
|
+
async function requireAbsoluteExistingPath(candidate, label, pathExists) {
|
|
133
|
+
if (typeof candidate !== 'string' || candidate.length === 0) {
|
|
134
|
+
return { ok: false, error: `Could not resolve absolute path to ${label}.` };
|
|
135
|
+
}
|
|
136
|
+
if (!path.isAbsolute(candidate)) {
|
|
137
|
+
return {
|
|
138
|
+
ok: false,
|
|
139
|
+
error: `${label} path must be absolute (got: ${candidate}).`,
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
if (!(await pathExists(candidate))) {
|
|
143
|
+
return {
|
|
144
|
+
ok: false,
|
|
145
|
+
error: `${label} path does not exist: ${candidate}`,
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
return { ok: true, path: candidate };
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Resolve absolute node + CLI entry paths for the LaunchAgent.
|
|
153
|
+
* @param {object} [opts]
|
|
154
|
+
* @param {string} [opts.execPath]
|
|
155
|
+
* @param {string} [opts.argv1]
|
|
156
|
+
* @param {import('./exec.js').ExecFn} [opts.exec]
|
|
157
|
+
* @param {(p: string) => Promise<string>} [opts.realpath]
|
|
158
|
+
* @param {(p: string) => Promise<boolean>} [opts.pathExists]
|
|
159
|
+
*/
|
|
160
|
+
export async function resolveAutostartPaths(opts = {}) {
|
|
161
|
+
const execPath = opts.execPath || process.execPath;
|
|
162
|
+
// Nullish coalescing so tests can inject '' / null without falling back to process.argv[1].
|
|
163
|
+
const argv1 = opts.argv1 !== undefined ? opts.argv1 : process.argv[1];
|
|
164
|
+
const realpath =
|
|
165
|
+
opts.realpath || ((p) => fs.realpath(p).then((r) => String(r)));
|
|
166
|
+
const exec = opts.exec || defaultExec;
|
|
167
|
+
const pathExists = opts.pathExists || ((p) => fs.pathExists(p));
|
|
168
|
+
|
|
169
|
+
const nodeResolved = path.resolve(execPath);
|
|
170
|
+
const nodeCheck = await requireAbsoluteExistingPath(
|
|
171
|
+
nodeResolved,
|
|
172
|
+
'node',
|
|
173
|
+
pathExists
|
|
174
|
+
);
|
|
175
|
+
if (!nodeCheck.ok) {
|
|
176
|
+
return {
|
|
177
|
+
ok: false,
|
|
178
|
+
error: nodeCheck.error,
|
|
179
|
+
nodePath: nodeResolved,
|
|
180
|
+
cliPath: null,
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
const nodePath = nodeCheck.path;
|
|
184
|
+
|
|
185
|
+
let cliPath = null;
|
|
186
|
+
|
|
187
|
+
if (argv1) {
|
|
188
|
+
try {
|
|
189
|
+
cliPath = await realpath(argv1);
|
|
190
|
+
} catch {
|
|
191
|
+
cliPath = path.resolve(argv1);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
if (!cliPath) {
|
|
196
|
+
const whichStagepass = await resolveCommand('stagepass', exec);
|
|
197
|
+
if (whichStagepass) {
|
|
198
|
+
try {
|
|
199
|
+
cliPath = await realpath(whichStagepass);
|
|
200
|
+
} catch {
|
|
201
|
+
cliPath = whichStagepass;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
const cliCheck = await requireAbsoluteExistingPath(
|
|
207
|
+
cliPath,
|
|
208
|
+
'stagepass CLI entrypoint',
|
|
209
|
+
pathExists
|
|
210
|
+
);
|
|
211
|
+
if (!cliCheck.ok) {
|
|
212
|
+
return {
|
|
213
|
+
ok: false,
|
|
214
|
+
error: cliCheck.error,
|
|
215
|
+
nodePath,
|
|
216
|
+
cliPath: cliPath || null,
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
return { ok: true, nodePath, cliPath: cliCheck.path, error: null };
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* @param {object} [opts]
|
|
225
|
+
* @param {import('./exec.js').ExecFn} [opts.exec]
|
|
226
|
+
* @param {number|string} [opts.uid]
|
|
227
|
+
* @param {string} [opts.plistPath]
|
|
228
|
+
*/
|
|
229
|
+
export async function getAutostartStatus(opts = {}) {
|
|
230
|
+
const exec = opts.exec || defaultExec;
|
|
231
|
+
const uid = opts.uid ?? process.getuid?.() ?? os.userInfo().uid;
|
|
232
|
+
const plistPath = opts.plistPath || getLaunchAgentPlistPath();
|
|
233
|
+
const pathExists = opts.pathExists || ((p) => fs.pathExists(p));
|
|
234
|
+
const plistExists = await pathExists(plistPath);
|
|
235
|
+
|
|
236
|
+
let loaded = false;
|
|
237
|
+
let printOutput = '';
|
|
238
|
+
let printError = null;
|
|
239
|
+
try {
|
|
240
|
+
const result = await exec('launchctl', buildLaunchctlPrintArgs(uid));
|
|
241
|
+
printOutput = result.stdout || '';
|
|
242
|
+
loaded = true;
|
|
243
|
+
} catch (error) {
|
|
244
|
+
printError = error instanceof Error ? error.message : String(error);
|
|
245
|
+
loaded = false;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
return {
|
|
249
|
+
label: LAUNCH_AGENT_LABEL,
|
|
250
|
+
plistPath,
|
|
251
|
+
plistExists,
|
|
252
|
+
loaded,
|
|
253
|
+
printOutput,
|
|
254
|
+
printError,
|
|
255
|
+
logsDir: getLogsDir(),
|
|
256
|
+
caddyfilePath: getCaddyfilePath(),
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* Write plist (idempotent) and bootstrap the LaunchAgent.
|
|
262
|
+
* @param {object} [opts]
|
|
263
|
+
*/
|
|
264
|
+
export async function enableAutostart(opts = {}) {
|
|
265
|
+
const exec = opts.exec || defaultExec;
|
|
266
|
+
const uid = opts.uid ?? process.getuid?.() ?? os.userInfo().uid;
|
|
267
|
+
const paths =
|
|
268
|
+
opts.paths ||
|
|
269
|
+
(await resolveAutostartPaths({
|
|
270
|
+
execPath: opts.execPath,
|
|
271
|
+
argv1: opts.argv1,
|
|
272
|
+
exec,
|
|
273
|
+
realpath: opts.realpath,
|
|
274
|
+
pathExists: opts.pathExists,
|
|
275
|
+
}));
|
|
276
|
+
|
|
277
|
+
if (!paths.ok) {
|
|
278
|
+
return { ok: false, error: paths.error };
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
// Defense in depth when callers inject `paths` directly.
|
|
282
|
+
if (!path.isAbsolute(paths.nodePath) || !path.isAbsolute(paths.cliPath)) {
|
|
283
|
+
return {
|
|
284
|
+
ok: false,
|
|
285
|
+
error: 'Autostart nodePath and cliPath must be absolute.',
|
|
286
|
+
};
|
|
287
|
+
}
|
|
288
|
+
const pathExists = opts.pathExists || ((p) => fs.pathExists(p));
|
|
289
|
+
if (!(await pathExists(paths.nodePath)) || !(await pathExists(paths.cliPath))) {
|
|
290
|
+
return {
|
|
291
|
+
ok: false,
|
|
292
|
+
error: 'Autostart nodePath or cliPath does not exist on disk.',
|
|
293
|
+
};
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
const plistPath = opts.plistPath || getLaunchAgentPlistPath();
|
|
297
|
+
const agentsDir = path.dirname(plistPath);
|
|
298
|
+
const ensureDir = opts.ensureDir || ((p) => fs.ensureDir(p));
|
|
299
|
+
const writeFile = opts.writeFile || ((p, c) => fs.writeFile(p, c, 'utf8'));
|
|
300
|
+
const logsDir = opts.logsDir || getLogsDir();
|
|
301
|
+
|
|
302
|
+
await ensureDir(agentsDir);
|
|
303
|
+
await ensureDir(logsDir);
|
|
304
|
+
|
|
305
|
+
const plist = renderLaunchAgentPlist({
|
|
306
|
+
nodePath: paths.nodePath,
|
|
307
|
+
cliPath: paths.cliPath,
|
|
308
|
+
stdoutPath: opts.stdoutPath || getDaemonStdoutLogPath(),
|
|
309
|
+
stderrPath: opts.stderrPath || getDaemonStderrLogPath(),
|
|
310
|
+
workingDirectory: opts.workingDirectory || os.homedir(),
|
|
311
|
+
});
|
|
312
|
+
|
|
313
|
+
await writeFile(plistPath, plist);
|
|
314
|
+
|
|
315
|
+
// Idempotent: bootout if already loaded, then bootstrap.
|
|
316
|
+
try {
|
|
317
|
+
await exec('launchctl', buildLaunchctlBootoutArgs(uid));
|
|
318
|
+
} catch {
|
|
319
|
+
// not loaded yet
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
try {
|
|
323
|
+
await exec('launchctl', buildLaunchctlBootstrapArgs(uid, plistPath));
|
|
324
|
+
} catch (error) {
|
|
325
|
+
return {
|
|
326
|
+
ok: false,
|
|
327
|
+
error: error instanceof Error ? error.message : String(error),
|
|
328
|
+
plistPath,
|
|
329
|
+
paths,
|
|
330
|
+
};
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
return { ok: true, plistPath, paths, error: null };
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/**
|
|
337
|
+
* Bootout and remove the LaunchAgent plist.
|
|
338
|
+
* @param {object} [opts]
|
|
339
|
+
*/
|
|
340
|
+
export async function disableAutostart(opts = {}) {
|
|
341
|
+
const exec = opts.exec || defaultExec;
|
|
342
|
+
const uid = opts.uid ?? process.getuid?.() ?? os.userInfo().uid;
|
|
343
|
+
const plistPath = opts.plistPath || getLaunchAgentPlistPath();
|
|
344
|
+
const pathExists = opts.pathExists || ((p) => fs.pathExists(p));
|
|
345
|
+
const remove = opts.remove || ((p) => fs.remove(p));
|
|
346
|
+
|
|
347
|
+
try {
|
|
348
|
+
await exec('launchctl', buildLaunchctlBootoutArgs(uid));
|
|
349
|
+
} catch {
|
|
350
|
+
// ignore if not loaded
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
if (await pathExists(plistPath)) {
|
|
354
|
+
await remove(plistPath);
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
return { ok: true, plistPath };
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
export { LAUNCH_AGENT_LABEL, getLaunchAgentPlistPath, getLaunchAgentsDir };
|