cli: add preflight check and version flag
- Add preflightWriteTargets to check file existence before writing - Remove stub commands (start, stop, status) from help text - Add --version flag support - Sequential file writes with preflight check
This commit is contained in:
parent
577d91fc47
commit
b400ff4e96
2 changed files with 25 additions and 7 deletions
|
|
@ -80,6 +80,22 @@ async function writePrivateFile(
|
|||
}
|
||||
}
|
||||
|
||||
async function preflightWriteTargets(filePaths: string[]): Promise<void> {
|
||||
for (const filePath of filePaths) {
|
||||
try {
|
||||
await fs.access(filePath);
|
||||
throw new Error(
|
||||
`Error: the file '${c.underline(path.basename(filePath))}' already exists in directory ${c.underline(path.dirname(filePath))}`,
|
||||
);
|
||||
} catch (error) {
|
||||
const err = error as NodeJS.ErrnoException;
|
||||
if (err.code !== "ENOENT") {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export async function initCommand(_args: string[]): Promise<void> {
|
||||
p.intro(c.bold("myst init"));
|
||||
p.note(
|
||||
|
|
@ -189,10 +205,9 @@ export async function initCommand(_args: string[]): Promise<void> {
|
|||
const configPath = path.join(outDir, "myst.config.json");
|
||||
|
||||
try {
|
||||
await Promise.all([
|
||||
writePrivateFile(envPath, envContent),
|
||||
writePrivateFile(configPath, configContent),
|
||||
]);
|
||||
await preflightWriteTargets([envPath, configPath]);
|
||||
await writePrivateFile(envPath, envContent);
|
||||
await writePrivateFile(configPath, configContent);
|
||||
} catch (e) {
|
||||
s.stop();
|
||||
p.cancel(
|
||||
|
|
|
|||
|
|
@ -25,9 +25,6 @@ interface CLIConfig {
|
|||
const config: CLIConfig = {
|
||||
commands: [
|
||||
{ name: "init", desc: "Bootstrap Myst and generate config/env files" },
|
||||
{ name: "start", desc: "Start Myst grant access control services" },
|
||||
{ name: "stop", desc: "Stop Myst grant access control services" },
|
||||
{ name: "status", desc: "List the health of running Myst services" },
|
||||
],
|
||||
globalFlags: [
|
||||
{ name: ["-h", "--help"], desc: "Display help information" },
|
||||
|
|
@ -80,6 +77,12 @@ async function main(): Promise<void> {
|
|||
const [command, ...options] = process.argv.slice(2);
|
||||
const flags = options.filter((o) => o.startsWith("-"));
|
||||
|
||||
if (flags.includes("-v") || flags.includes("--version")) {
|
||||
process.stdout.write(`${pkg.version}\n`);
|
||||
process.exitCode = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
if (!command || flags.includes("-h") || flags.includes("--help")) {
|
||||
process.stdout.write(`${HELP_TEXT}`);
|
||||
process.exitCode = 0;
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue