diff --git a/packages/cli/changelog/@unreleased/pr-42.v2.yml b/packages/cli/changelog/@unreleased/pr-42.v2.yml new file mode 100644 index 000000000..9110ce8bc --- /dev/null +++ b/packages/cli/changelog/@unreleased/pr-42.v2.yml @@ -0,0 +1,5 @@ +type: improvement +improvement: + description: Move argv override logging to middleware + links: + - https://github.com/palantir/osdk-ts/pull/42 diff --git a/packages/cli/src/commands/site/deploy/index.ts b/packages/cli/src/commands/site/deploy/index.ts index 532e156ee..79e983b3a 100644 --- a/packages/cli/src/commands/site/deploy/index.ts +++ b/packages/cli/src/commands/site/deploy/index.ts @@ -14,11 +14,11 @@ * limitations under the License. */ -import { consola } from "consola"; import type { CommandModule } from "yargs"; import type { LoadedFoundryConfig, SiteConfig } from "../../../util/config.js"; import configLoader from "../../../util/configLoader.js"; import type { CommonSiteArgs } from "../CommonSiteArgs.js"; +import { logDeployCommandConfigFileOverride } from "./logDeployCommandConfigFileOverride.js"; import type { SiteDeployArgs } from "./SiteDeployArgs.js"; const command: CommandModule< @@ -91,40 +91,28 @@ const command: CommandModule< ); } - if ( - (autoVersion?.type !== "git-describe" - || argv.autoVersion !== "git-describe") - && argv.gitTagPrefix != null - ) { + const autoVersionType = argv.autoVersion ?? autoVersion; + if (autoVersionType !== "git-describe") { throw new Error( - `--gitTagPrefix is only supported when --autoVersion=git-describe`, - ); - } - - if (autoVersion != null && argv.autoVersion !== autoVersion.type) { - consola.debug( - `Overriding "autoVersion" from config file with ${argv.autoVersion}`, + `Only 'git-describe' is supported for autoVersion`, ); - if (argv.autoVersion !== "git-describe") { - throw new Error( - `Only 'git-describe' is supported for autoVersion`, - ); - } } - if (directory != null && argv.directory !== directory) { - consola.debug( - `Overriding "directory" from config file with ${argv.directory}`, + const gitTagPrefixValue = argv.gitTagPrefix ?? gitTagPrefix; + // Future proofing for when we support other autoVersion types + if (gitTagPrefixValue != null && autoVersionType !== "git-describe") { + throw new Error( + `--gitTagPrefix is only supported when --autoVersion=git-describe`, ); } - if (gitTagPrefix != null && argv.gitTagPrefix !== gitTagPrefix) { - consola.debug( - `Overriding "gitTagPrefix" from config file with ${argv.gitTagPrefix}`, - ); - } return true; - }); + }).middleware((argv) => + logDeployCommandConfigFileOverride( + argv, + siteConfig, + ) + ); }, handler: async (args) => { const command = await import("./siteDeployCommand.mjs"); diff --git a/packages/cli/src/commands/site/deploy/logDeployCommandConfigFileOverride.ts b/packages/cli/src/commands/site/deploy/logDeployCommandConfigFileOverride.ts new file mode 100644 index 000000000..10360ea68 --- /dev/null +++ b/packages/cli/src/commands/site/deploy/logDeployCommandConfigFileOverride.ts @@ -0,0 +1,51 @@ +/* + * Copyright 2023 Palantir Technologies, Inc. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import type { Arguments } from "yargs"; +import type { SiteConfig } from "../../../util/config.js"; +import type { SiteDeployArgs } from "./SiteDeployArgs.js"; + +export async function logDeployCommandConfigFileOverride( + argv: Arguments, + config: SiteConfig | undefined, +) { + const Consola = await import("consola"); + const consola = Consola.consola; + + if ( + config?.autoVersion != null && argv.autoVersion !== config?.autoVersion.type + ) { + consola.debug( + `Overriding "autoVersion" from config file with ${argv.autoVersion}`, + ); + } + + if (config?.directory != null && argv.directory !== config?.directory) { + consola.debug( + `Overriding "directory" from config file with ${argv.directory}`, + ); + } + + if ( + config?.autoVersion?.tagPrefix != null + && argv.gitTagPrefix != null + && argv.gitTagPrefix !== config?.autoVersion.tagPrefix + ) { + consola.debug( + `Overriding "gitTagPrefix" from config file with ${argv.gitTagPrefix}`, + ); + } +} diff --git a/packages/cli/src/commands/site/index.ts b/packages/cli/src/commands/site/index.ts index 67279a675..56200e8cf 100644 --- a/packages/cli/src/commands/site/index.ts +++ b/packages/cli/src/commands/site/index.ts @@ -14,7 +14,6 @@ * limitations under the License. */ -import { consola } from "consola"; import type { CommandModule } from "yargs"; import type { CliCommonArgs } from "../../CliCommonArgs.js"; import { ExitProcessError } from "../../ExitProcessError.js"; @@ -23,6 +22,7 @@ import type { LoadedFoundryConfig } from "../../util/config.js"; import configLoader from "../../util/configLoader.js"; import type { CommonSiteArgs } from "./CommonSiteArgs.js"; import deploy from "./deploy/index.js"; +import { logSiteCommandConfigFileOverride } from "./logSiteCommandConfigFileOverride.js"; import version from "./version/index.js"; const command: CommandModule = { @@ -36,13 +36,14 @@ const command: CommandModule = { .options({ application: { type: "string", - coerce: (a) => a as ThirdPartyAppRid, + coerce: (application) => application as ThirdPartyAppRid, ...application ? { default: application } : { demandOption: true }, description: "Application RID", }, foundryUrl: { + coerce: (foundryUrl) => foundryUrl.replace(/\/$/, ""), type: "string", ...foundryUrl ? { default: foundryUrl } @@ -68,25 +69,14 @@ const command: CommandModule = { .command(version) .command(deploy) .check((argv) => { - if (application != null && argv.application !== application) { - consola.debug( - `Overriding "application" from config file with ${argv.application}`, - ); - } - - if (foundryUrl != null && argv.foundryUrl !== foundryUrl) { - consola.debug( - `Overriding "foundryUrl" from config file with ${argv.foundryUrl}`, - ); - } - if (!argv.foundryUrl.startsWith("https://")) { throw new ExitProcessError(1, "foundryUrl must start with https://"); } - - argv.foundryUrl = argv.foundryUrl.replace(/\/$/, ""); return true; }) + .middleware((argv) => + logSiteCommandConfigFileOverride(argv, config?.foundryConfig) + ) .demandCommand(); }, handler: async (args) => {}, diff --git a/packages/cli/src/commands/site/logSiteCommandConfigFileOverride.ts b/packages/cli/src/commands/site/logSiteCommandConfigFileOverride.ts new file mode 100644 index 000000000..664326fd2 --- /dev/null +++ b/packages/cli/src/commands/site/logSiteCommandConfigFileOverride.ts @@ -0,0 +1,42 @@ +/* + * Copyright 2023 Palantir Technologies, Inc. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import type { Arguments } from "yargs"; +import type { FoundryConfig } from "../../util/config.js"; +import type { CommonSiteArgs } from "./CommonSiteArgs.js"; + +export async function logSiteCommandConfigFileOverride( + argv: Arguments, + config: FoundryConfig | undefined, +) { + const Consola = await import("consola"); + const consola = Consola.consola; + + if ( + config?.site.application != null + && argv.application !== config.site.application + ) { + consola.debug( + `Overriding "application" from config file with ${argv.application}`, + ); + } + + if (config?.foundryUrl != null && argv.foundryUrl !== config.foundryUrl) { + consola.debug( + `Overriding "foundryUrl" from config file with ${argv.foundryUrl}`, + ); + } +} diff --git a/packages/cli/src/util/config.ts b/packages/cli/src/util/config.ts index 672c62e28..3c92dd942 100644 --- a/packages/cli/src/util/config.ts +++ b/packages/cli/src/util/config.ts @@ -17,7 +17,7 @@ import type { JSONSchemaType } from "ajv"; import { promises as fsPromises } from "node:fs"; -interface GitDescribeAutoVersionConfig { +export interface GitDescribeAutoVersionConfig { type: "git-describe"; tagPrefix?: string; }