From 0eaa11245dc3aea850633a07dfcbcdd2a14b6f9c Mon Sep 17 00:00:00 2001 From: Tyler Dane Date: Mon, 30 Dec 2024 07:17:07 -0600 Subject: [PATCH] feat(cli): add clientId as build option --- packages/scripts/src/cli.ts | 55 ++++++++---------------- packages/scripts/src/commands/build.ts | 4 +- packages/scripts/src/common/cli.utils.ts | 47 +++++++++++++++++++- 3 files changed, 67 insertions(+), 39 deletions(-) diff --git a/packages/scripts/src/cli.ts b/packages/scripts/src/cli.ts index 1ced114c..48490293 100644 --- a/packages/scripts/src/cli.ts +++ b/packages/scripts/src/cli.ts @@ -8,8 +8,13 @@ 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, Schema_Options_Cli } from "./common/cli.types"; -import { log } from "./common/cli.utils"; +import { Options_Cli } from "./common/cli.types"; +import { + log, + mergeOptions, + validateOptions, + validatePackages, +} from "./common/cli.utils"; class CompassCli { private program: Command; @@ -29,18 +34,21 @@ class CompassCli { ); program.option("-f, --force", "force operation, no cautionary prompts"); program.option( - "-u, --user [id | email]", + "--user [id | email]", "specify which user to run script for" ); program .command("build") - .description("build compass package(s)") + .description("build compass package") .argument( `[${ALL_PACKAGES.join(" | ")}]`, - "package(s) to build, separated by comma" + "package to build (only provde 1 at a time)" ) - .option("--skip-env", "skip copying env files to build"); + .option( + "-c, --clientId ", + "google client id to inject into build" + ); program .command("delete") @@ -49,37 +57,10 @@ class CompassCli { } private getCliOptions(): Options_Cli { - const _options = this.program.opts(); - const packages = this.program.args[1]?.split(","); - const options: Options_Cli = { - ..._options, - force: _options["force"] === true, - packages, - }; - - const { data, error } = Schema_Options_Cli.safeParse(options); - if (error) { - log.error(`Invalid CLI options: ${JSON.stringify(error.format())}`); - process.exit(1); - } - - return data; - } + const options = mergeOptions(this.program); + const validOptions = validateOptions(options); - private validatePackages(packages: string[] | undefined) { - if (!packages) { - log.error("Packages 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); - } + return validOptions; } public async run() { @@ -88,7 +69,7 @@ class CompassCli { switch (true) { case cmd === "build": { - this.validatePackages(packages); + validatePackages(packages); await runBuild(this.options); break; } diff --git a/packages/scripts/src/commands/build.ts b/packages/scripts/src/commands/build.ts index 3c441294..fb2fdf00 100644 --- a/packages/scripts/src/commands/build.ts +++ b/packages/scripts/src/commands/build.ts @@ -62,7 +62,9 @@ const buildWeb = async (options: Options_Cli) => { const envFile = environment === "staging" ? ".env" : ".env.prod"; const baseUrl = await getApiBaseUrl(environment); - const gClientId = await getClientId(environment); + const gClientId = options.clientId + ? options.clientId + : await getClientId(environment); const envPath = path.join(__dirname, "..", "..", "..", "backend", envFile); dotenv.config({ path: envPath }); diff --git a/packages/scripts/src/common/cli.utils.ts b/packages/scripts/src/common/cli.utils.ts index 18ed41d4..bf9e8aca 100644 --- a/packages/scripts/src/common/cli.utils.ts +++ b/packages/scripts/src/common/cli.utils.ts @@ -2,9 +2,10 @@ import pkg from "inquirer"; import chalk from "chalk"; const { prompt } = pkg; import shell from "shelljs"; +import { Command } from "commander"; import { ALL_PACKAGES, CLI_ENV } from "./cli.constants"; -import { Environment_Cli } from "./cli.types"; +import { Environment_Cli, Options_Cli, Schema_Options_Cli } from "./cli.types"; export const fileExists = (file: string) => { return shell.test("-e", file); @@ -128,6 +129,24 @@ export const log = { tip: (msg: string) => console.log(chalk.hex("#f5c150")(msg)), }; +export const mergeOptions = (program: Command): Options_Cli => { + const _options = program.opts(); + const packages = program.args[1]?.split(","); + const options: Options_Cli = { + ..._options, + force: _options["force"] === true, + packages, + }; + + const build = program.commands.find((cmd) => cmd.name() === "build"); + const clientId = build?.opts()["clientId"] as string; + if (build && clientId) { + options.clientId = clientId; + } + + return options; +}; + export const _confirm = async (question: string, _default = true) => { const q = [ { @@ -144,3 +163,29 @@ 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("Packages 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); + } +};