refactor(cli): improve init command error handling; change "service

account" verbaige to "dedicated forgejo user" to match Forgejo user
model;
This commit is contained in:
ZTatman 2026-04-07 13:12:19 -04:00
parent 1eb5368c9e
commit 342252efbb
4 changed files with 65 additions and 71 deletions

View file

@ -174,23 +174,24 @@ export async function initCommand(_args: string[]): Promise<void> {
}), }),
forgejoBotUsername: () => forgejoBotUsername: () =>
p.text({ p.text({
message: "What dedicated Forgejo username should Myst use for API access?", message:
"What dedicated Forgejo username should Myst use for API access?",
placeholder: "myst-bot", placeholder: "myst-bot",
validate: (v) => validate: (v) =>
v?.trim() v?.trim() ? undefined : "Dedicated Forgejo username is required",
? undefined
: "Dedicated Forgejo username is required",
}), }),
forgejoPat: () => forgejoPat: () =>
p.password({ p.password({
message: message:
"Paste the Forgejo personal access token for that dedicated Forgejo user", "Paste the Forgejo personal access token for that dedicated Forgejo user",
mask: "*",
validate: (v) => (v?.trim() ? undefined : "Forgejo PAT is required"), validate: (v) => (v?.trim() ? undefined : "Forgejo PAT is required"),
}), }),
grantTokenSecret: () => grantTokenSecret: () =>
p.password({ p.password({
message: message:
"Paste a grant token secret, or press enter to generate one automatically", "Paste a grant token secret, or press enter to generate one automatically",
mask: "*",
validate: () => undefined, validate: () => undefined,
}), }),
}, },
@ -203,11 +204,16 @@ export async function initCommand(_args: string[]): Promise<void> {
); );
const s = p.spinner(); const s = p.spinner();
try {
s.start("Generating config files"); s.start("Generating config files");
const publicUrl = normalizeHttpUrl(answers.publicUrl); const publicUrl = normalizeHttpUrl(answers.publicUrl);
const adminUrl = normalizeHttpUrl(answers.adminUrl); const adminUrl = normalizeHttpUrl(answers.adminUrl);
const forgejoBaseUrl = normalizeHttpUrl(answers.forgejoBaseUrl); const forgejoBaseUrl = normalizeHttpUrl(answers.forgejoBaseUrl);
const grantTokenSecret = answers.grantTokenSecret?.trim() || generateSecret(); const grantTokenSecret =
answers.grantTokenSecret?.trim() || generateSecret();
const envContent = generateEnvFile({ const envContent = generateEnvFile({
adminUrl, adminUrl,
databaseUrl: answers.databaseUrl, databaseUrl: answers.databaseUrl,
@ -217,6 +223,7 @@ export async function initCommand(_args: string[]): Promise<void> {
grantTokenSecret, grantTokenSecret,
publicUrl, publicUrl,
}); });
const configContent = JSON.stringify( const configContent = JSON.stringify(
{ {
adminUrl, adminUrl,
@ -232,27 +239,15 @@ export async function initCommand(_args: string[]): Promise<void> {
const envPath = path.join(outDir, ".env"); const envPath = path.join(outDir, ".env");
const configPath = path.join(outDir, "myst.config.json"); const configPath = path.join(outDir, "myst.config.json");
try {
await preflightWriteTargets([envPath, configPath]); await preflightWriteTargets([envPath, configPath]);
await writePrivateFile(envPath, envContent); await writePrivateFile(envPath, envContent);
await writePrivateFile(configPath, configContent); await writePrivateFile(configPath, configContent);
} catch (e) {
s.stop();
p.cancel(
e instanceof Error
? e.message
: `Failed to write config files to ${outDir}`,
);
return;
}
s.stop("Config files generated"); s.stop("Config files generated");
p.note( p.note(
[c.green(".env"), c.green("myst.config.json")].join("\n"), [c.green(".env"), c.green("myst.config.json")].join("\n"),
c.yellow("Files written to current directory"), c.yellow("Files written to current directory"),
); );
p.note( p.note(
[ [
`${c.bold("Public viewer URL:")} ${c.cyan(publicUrl)}`, `${c.bold("Public viewer URL:")} ${c.cyan(publicUrl)}`,
@ -269,4 +264,11 @@ export async function initCommand(_args: string[]): Promise<void> {
"Done. Deploy Myst, then run forgejo bootstrap and doctor commands to verify setup.", "Done. Deploy Myst, then run forgejo bootstrap and doctor commands to verify setup.",
), ),
); );
} catch (error) {
s.stop();
p.cancel(
error instanceof Error ? error.message : "Failed to initialize Myst",
);
process.exitCode = 1;
}
} }

View file

@ -12,7 +12,8 @@ function escapeEnvValue(value: string): string {
return `"${value return `"${value
.replace(/\\/g, "\\\\") // Escape backslashes .replace(/\\/g, "\\\\") // Escape backslashes
.replace(/"/g, '\\"') // Escape double quotes .replace(/"/g, '\\"') // Escape double quotes
.replace(/\r?\n/g, "\\n")}"`; // Escape newlines .replace(/\r?\n/g, "\\n") // Escape newlines
.replace(/\$/g, "\\$")}"`; // Escape dollar signs
} }
/** /**

View file

@ -1,7 +1 @@
export const MYST_DEFAULT_FORGEJO_PORT = 3000;
export const MYST_DEFAULT_SERVICE_PORT = 4000; export const MYST_DEFAULT_SERVICE_PORT = 4000;
export const MYST_VISIBILITY_MODES = {
TEAM: "team",
ALLOWLIST: "allowlist"
} as const;

View file

@ -1,5 +1,3 @@
export type VisibilityMode = "team" | "allowlist";
export interface RepoRef { export interface RepoRef {
owner: string; owner: string;
name: string; name: string;
@ -10,4 +8,3 @@ export interface GrantSnapshot {
sourceBranch: string; sourceBranch: string;
commitSha: string; commitSha: string;
} }