diff --git a/packages/scripts/src/cli.ts b/packages/scripts/src/cli.ts index c342ba88..1050ff01 100644 --- a/packages/scripts/src/cli.ts +++ b/packages/scripts/src/cli.ts @@ -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; @@ -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; } @@ -69,26 +64,57 @@ 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); @@ -96,3 +122,19 @@ 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); + } +}; diff --git a/packages/scripts/src/commands/build.ts b/packages/scripts/src/commands/build.ts index d3a3aaca..0693a420 100644 --- a/packages/scripts/src/commands/build.ts +++ b/packages/scripts/src/commands/build.ts @@ -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); } }; diff --git a/packages/scripts/src/common/cli.utils.ts b/packages/scripts/src/common/cli.utils.ts index 297690db..7c8d26e2 100644 --- a/packages/scripts/src/common/cli.utils.ts +++ b/packages/scripts/src/common/cli.utils.ts @@ -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); @@ -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); - } -};