Skip to content

Commit

Permalink
refactor(cli): move validation from util to Cli class
Browse files Browse the repository at this point in the history
this improves readability, because the class has access to the 'options', which means we don't have to keep passing that arg to the util functions
  • Loading branch information
tyler-dane committed Jan 3, 2025
1 parent c4d387f commit 1259eb7
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 44 deletions.
64 changes: 53 additions & 11 deletions packages/scripts/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,8 @@ import { Command } from "commander";
import { runBuild } from "./commands/build";
import { ALL_PACKAGES, CATEGORY_VM } from "./common/cli.constants";
import { startDeleteFlow } from "./commands/delete";
import { Options_Cli } from "./common/cli.types";
import {
log,
mergeOptions,
validateOptions,
validatePackages,
} from "./common/cli.utils";
import { Options_Cli, Schema_Options_Cli } from "./common/cli.types";
import { getPckgsTo, log, mergeOptions } from "./common/cli.utils";

class CompassCli {
private program: Command;
Expand Down Expand Up @@ -58,7 +53,7 @@ class CompassCli {

private getCliOptions(): Options_Cli {
const options = mergeOptions(this.program);
const validOptions = validateOptions(options);
const validOptions = this._validateOptions(options);

return validOptions;
}
Expand All @@ -69,30 +64,77 @@ class CompassCli {

switch (true) {
case cmd === "build": {
await this._validateBuild();
await runBuild(this.options);
break;
}
case cmd === "delete": {
if (!user || typeof user !== "string") {
this.exitHelpfully("You must supply a user");
this._exitHelpfully("You must supply a user");
}
await startDeleteFlow(user as string, force);
break;
}
default:
this.exitHelpfully("Unsupported cmd");
this._exitHelpfully("Unsupported cmd");
}
}

private exitHelpfully(msg?: string) {
private _exitHelpfully(msg?: string) {
msg && log.error(msg);
console.log(this.program.helpInformation());
process.exit(1);
}

private async _validateBuild() {
const packages = this.options.packages
? this.options.packages
: await getPckgsTo("build");

if (!packages) {
this._exitHelpfully("Package must be defined");
}

const unsupportedPackages = packages.filter(
(pkg) => !ALL_PACKAGES.includes(pkg)
);
if (unsupportedPackages.length > 0) {
this._exitHelpfully(
`One or more of these packages isn't supported: ${unsupportedPackages.toString()}`
);
}
}

private _validateOptions(options: Options_Cli) {
const { data, error } = Schema_Options_Cli.safeParse(options);
if (error) {
this._exitHelpfully(
`Invalid CLI options: ${JSON.stringify(error.format())}`
);
}

return data as Options_Cli;
}
}

const cli = new CompassCli(process.argv);
cli.run().catch((err) => {
console.log(err);
process.exit(1);
});

export const validatePackages = (packages: string[] | undefined) => {
if (!packages) {
log.error("Package must be defined");
process.exit(1);
}
const unsupportedPackages = packages.filter(
(pkg) => !ALL_PACKAGES.includes(pkg)
);
if (unsupportedPackages.length > 0) {
log.error(
`One or more of these packages isn't supported: ${unsupportedPackages.toString()}`
);
process.exit(1);
}
};
9 changes: 3 additions & 6 deletions packages/scripts/src/commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,21 @@ import {
PCKG,
} from "@scripts/common/cli.constants";
import {
getPckgsTo,
_confirm,
log,
fileExists,
getClientId,
getApiBaseUrl,
getEnvironmentAnswer,
validatePackages,
} from "@scripts/common/cli.utils";

export const runBuild = async (options: Options_Cli) => {
const pckgs = options.packages ? options.packages : await getPckgsTo("build");
validatePackages(pckgs);
const packages = options.packages as string[];

if (pckgs.includes(PCKG.NODE)) {
if (packages.includes(PCKG.NODE)) {
await buildNodePckgs(options);
}
if (pckgs.includes(PCKG.WEB)) {
if (packages.includes(PCKG.WEB)) {
await buildWeb(options);
}
};
Expand Down
28 changes: 1 addition & 27 deletions packages/scripts/src/common/cli.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import shell from "shelljs";
import { Command } from "commander";

import { ALL_PACKAGES, CLI_ENV } from "./cli.constants";
import { Environment_Cli, Options_Cli, Schema_Options_Cli } from "./cli.types";
import { Environment_Cli, Options_Cli } from "./cli.types";

export const fileExists = (file: string) => {
return shell.test("-e", file);
Expand Down Expand Up @@ -163,29 +163,3 @@ export const _confirm = async (question: string, _default = true) => {
process.exit(1);
});
};

export const validateOptions = (options: Options_Cli): Options_Cli => {
const { data, error } = Schema_Options_Cli.safeParse(options);
if (error) {
log.error(`Invalid CLI options: ${JSON.stringify(error.format())}`);
process.exit(1);
}

return data;
};

export const validatePackages = (packages: string[] | undefined) => {
if (!packages) {
log.error("Package must be defined");
process.exit(1);
}
const unsupportedPackages = packages.filter(
(pkg) => !ALL_PACKAGES.includes(pkg)
);
if (unsupportedPackages.length > 0) {
log.error(
`One or more of these packages isn't supported: ${unsupportedPackages.toString()}`
);
process.exit(1);
}
};

0 comments on commit 1259eb7

Please sign in to comment.