Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move argv override logging to middleware #42

Merged
merged 4 commits into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/cli/changelog/@unreleased/pr-42.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: improvement
improvement:
description: Move argv override logging to middleware
links:
- https://github.com/palantir/osdk-ts/pull/42
42 changes: 15 additions & 27 deletions packages/cli/src/commands/site/deploy/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<
Expand Down Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
@@ -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<SiteDeployArgs>,
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}`,
);
}
}
22 changes: 6 additions & 16 deletions packages/cli/src/commands/site/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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<CliCommonArgs, CommonSiteArgs> = {
Expand All @@ -36,13 +36,14 @@ const command: CommandModule<CliCommonArgs, CommonSiteArgs> = {
.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 }
Expand All @@ -68,25 +69,14 @@ const command: CommandModule<CliCommonArgs, CommonSiteArgs> = {
.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) => {},
Expand Down
42 changes: 42 additions & 0 deletions packages/cli/src/commands/site/logSiteCommandConfigFileOverride.ts
Original file line number Diff line number Diff line change
@@ -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<CommonSiteArgs>,
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}`,
);
}
}
2 changes: 1 addition & 1 deletion packages/cli/src/util/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Loading