mirror of
https://github.com/ZTatman/myst.git
synced 2026-11-22 12:57:58 +00:00
fix(cli): atomic config writes, validate subcommands and reject positionals
This commit is contained in:
parent
fb7ffaf248
commit
9b7f5d6194
2 changed files with 37 additions and 15 deletions
|
|
@ -191,7 +191,11 @@ export async function runCli(
|
||||||
|
|
||||||
// If help is requested for a specific command, show command-specific help
|
// If help is requested for a specific command, show command-specific help
|
||||||
if (wantsCommandHelp) {
|
if (wantsCommandHelp) {
|
||||||
const actualSubcommand = isHelpFlag(subcommand) ? undefined : subcommand;
|
const subcommands = cmdConf.subcommands;
|
||||||
|
const actualSubcommand =
|
||||||
|
subcommand && subcommands && subcommand in subcommands
|
||||||
|
? subcommand
|
||||||
|
: undefined;
|
||||||
showCommandHelp(command, cmdConf, actualSubcommand);
|
showCommandHelp(command, cmdConf, actualSubcommand);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
@ -233,14 +237,22 @@ export async function runCli(
|
||||||
? [subcommand, ...remainingArgs]
|
? [subcommand, ...remainingArgs]
|
||||||
: remainingArgs;
|
: remainingArgs;
|
||||||
|
|
||||||
|
try {
|
||||||
// Parse target command options
|
// Parse target command options
|
||||||
const { values: cmdOpts } = parseArgs({
|
const { values: cmdOpts, positionals } = parseArgs({
|
||||||
args: argsForOptions,
|
args: argsForOptions,
|
||||||
options: targetCmdConf.options || {},
|
options: targetCmdConf.options || {},
|
||||||
allowPositionals: true,
|
allowPositionals: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
try {
|
if (positionals.length > 0) {
|
||||||
|
process.stderr.write(
|
||||||
|
`${c.red("Error:")} Unexpected argument: ${positionals[0]}\n`,
|
||||||
|
);
|
||||||
|
process.exitCode = 1;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// Execute the target command handler
|
// Execute the target command handler
|
||||||
await targetCmdConf.handler(cmdOpts);
|
await targetCmdConf.handler(cmdOpts);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|
|
||||||
|
|
@ -115,7 +115,6 @@ async function init(opts: { output?: string }) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (choice === "overwrite") {
|
if (choice === "overwrite") {
|
||||||
await fs.unlink(envPath);
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -174,7 +173,6 @@ async function init(opts: { output?: string }) {
|
||||||
onCancel: () => {
|
onCancel: () => {
|
||||||
p.cancel("\nOperation cancelled.");
|
p.cancel("\nOperation cancelled.");
|
||||||
process.exitCode = 0;
|
process.exitCode = 0;
|
||||||
throw new Error("Operation cancelled");
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
@ -200,11 +198,23 @@ async function init(opts: { output?: string }) {
|
||||||
publicUrl,
|
publicUrl,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (envExists) {
|
||||||
|
const tempPath = envPath + ".tmp";
|
||||||
|
await fs.writeFile(tempPath, envContent, {
|
||||||
|
encoding: "utf8",
|
||||||
|
flag: "wx",
|
||||||
|
mode: 0o600,
|
||||||
|
});
|
||||||
|
|
||||||
|
await fs.unlink(envPath).catch(() => {});
|
||||||
|
await fs.rename(tempPath, envPath);
|
||||||
|
} else {
|
||||||
await fs.writeFile(envPath, envContent, {
|
await fs.writeFile(envPath, envContent, {
|
||||||
encoding: "utf8",
|
encoding: "utf8",
|
||||||
flag: "wx",
|
flag: "wx",
|
||||||
mode: 0o600,
|
mode: 0o600,
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
s.stop(c.yellow(`.env created at ${envPath}`));
|
s.stop(c.yellow(`.env created at ${envPath}`));
|
||||||
p.note(
|
p.note(
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue