Skip to content

Commit

Permalink
feat(cli): add clientId as build option
Browse files Browse the repository at this point in the history
  • Loading branch information
tyler-dane committed Jan 3, 2025
1 parent 52689a1 commit 0eaa112
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 39 deletions.
55 changes: 18 additions & 37 deletions packages/scripts/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 <clientId>",
"google client id to inject into build"
);

program
.command("delete")
Expand All @@ -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() {
Expand All @@ -88,7 +69,7 @@ class CompassCli {

switch (true) {
case cmd === "build": {
this.validatePackages(packages);
validatePackages(packages);
await runBuild(this.options);
break;
}
Expand Down
4 changes: 3 additions & 1 deletion packages/scripts/src/commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down
47 changes: 46 additions & 1 deletion packages/scripts/src/common/cli.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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 = [
{
Expand All @@ -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);
}
};

0 comments on commit 0eaa112

Please sign in to comment.