Skip to content

Commit

Permalink
refactor: extract cli args before running cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
tyler-dane committed Dec 20, 2024
1 parent 5c651bc commit e30bfec
Showing 1 changed file with 35 additions and 18 deletions.
53 changes: 35 additions & 18 deletions packages/scripts/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,61 +9,78 @@ import { runBuild } from "./commands/build";
import { ALL_PACKAGES, CATEGORY_VM } from "./common/cli.constants";
import { startDeleteFlow } from "./commands/delete";
import { log } from "./common/cli.utils";
import { Options_Cli } from "./common/cli.types";

const runScript = async () => {
const exitHelpfully = (msg?: string) => {
msg && log.error(msg);
console.log(program.helpInformation());
process.exit(1);
};

const createProgram = () => {
const program = new Command();
program.option(
`-e, --environment [${CATEGORY_VM.STAG}|${CATEGORY_VM.PROD}]`,
`-e, --environment [${CATEGORY_VM.STAG} | ${CATEGORY_VM.PROD}]`,
"specify environment"
);
program.option("-f, --force", "forces operation, no cautionary prompts");
program.option(
"-u, --user [id|email]",
"-u, --user [id | email]",
"specifies which user to run script for"
);

program
.command("build")
.description("build compass package(s)")
.argument(
`[${ALL_PACKAGES.join("|")}]`,
`[${ALL_PACKAGES.join(" | ")}]`,
"package(s) to build, separated by comma"
)
.option("--skip-env", "skips copying env files to build");

program
.command("delete")
.description("deletes users data from compass database");
return program;
};

const exitHelpfully = (program: Command, msg?: string) => {
msg && log.error(msg);
console.log(program.helpInformation());
process.exit(1);
};

const getCliOptions = (program: Command): Options_Cli => {
const _options = program.opts();
const packages = program.args[1]?.split(",");

const options = {
..._options,
packages,
force: _options["force"] === true,
user: _options["user"] as string,
};

return options;
};

const runScript = async () => {
const program = createProgram();
program.parse(process.argv);

const options = program.opts();
const cmd = program.args[0];
const options = getCliOptions(program);
const { user, force } = options;

const cmd = program.args[0];
switch (true) {
case cmd === "build": {
await runBuild(options);
break;
}
case cmd === "delete": {
const force = options["force"] as boolean;
const user = options["user"] as string;

if (!user || typeof user !== "string") {
exitHelpfully("You must supply a user");
exitHelpfully(program, "You must supply a user");
}

await startDeleteFlow(user, force);
await startDeleteFlow(user as string, force);
break;
}
default:
exitHelpfully("Unsupported cmd");
exitHelpfully(program, "Unsupported cmd");
}
};

Expand Down

0 comments on commit e30bfec

Please sign in to comment.