fix(cli): atomic config writes, validate subcommands and reject positionals

This commit is contained in:
ZTatman 2026-04-13 13:51:55 -04:00
parent fb7ffaf248
commit 9b7f5d6194
2 changed files with 37 additions and 15 deletions

View file

@ -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) {

View file

@ -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(