diff --git a/src/cli/index.ts b/src/cli/index.ts index c00d9c25..098c920a 100644 --- a/src/cli/index.ts +++ b/src/cli/index.ts @@ -1,4 +1,4 @@ -import { createCommand } from "@commander-js/extra-typings"; +import { createCommand, Option } from "@commander-js/extra-typings"; import type { Logger } from "npmlog"; import pkginfo from "pkginfo"; import type { DebugLog } from "../domain/logging"; @@ -18,6 +18,11 @@ import { makeRemoveCmd } from "./cmd-remove"; import { makeSearchCmd } from "./cmd-search"; import { makeViewCmd } from "./cmd-view"; +const verboseOpt = new Option( + "-v, --verbose", + "output extra debugging" +).default(false); + /** * Makes the openupm cli app with the given dependencies. * @param fetchPackument IO function for fetching registry packuments. @@ -43,11 +48,16 @@ export function makeOpenupmCli( pkginfo(module); const program = createCommand() .version(module.exports.version) - .option("-v, --verbose", "output extra debugging") + .addOption(verboseOpt) .option("--system-user", "auth for Windows system user") .option("--no-upstream", "don't use upstream unity registry") .option("--no-color", "disable color"); + program.on("option:verbose", function () { + const verbose = program.opts().verbose; + log.level = verbose ? "verbose" : "notice"; + }); + program.addCommand( makeAddCmd( fetchCheckUrlExists, diff --git a/src/cli/options.ts b/src/cli/options.ts index 7d4c83bb..8e2e39eb 100644 --- a/src/cli/options.ts +++ b/src/cli/options.ts @@ -2,10 +2,6 @@ * Options which are shared between commands. */ export type GlobalOptions = Readonly<{ - /** - * Whether to print logs. - */ - verbose?: boolean; /** * Whether to use color in the console. */ diff --git a/src/cli/parse-env.ts b/src/cli/parse-env.ts index 7a3662db..442f848a 100644 --- a/src/cli/parse-env.ts +++ b/src/cli/parse-env.ts @@ -24,14 +24,6 @@ export type Env = Readonly<{ upstream: boolean; }>; -/** - * Determines which log level to use for output. - * @param options Cmd options. - */ -export function determineLogLevel(options: CmdOptions): "verbose" | "notice" { - return options.verbose ? "verbose" : "notice"; -} - /** * Determines whether to use color for output. * @param envVars Environment variables. @@ -73,9 +65,6 @@ export async function parseEnvUsing( envVars: Record, options: CmdOptions ): Promise { - // log level - log.level = determineLogLevel(options); - // color const useColor = determineUseColor(envVars, options); if (!useColor) { diff --git a/test/unit/cli/parse-env.test.ts b/test/unit/cli/parse-env.test.ts index 342e3804..203ef3fd 100644 --- a/test/unit/cli/parse-env.test.ts +++ b/test/unit/cli/parse-env.test.ts @@ -1,35 +1,10 @@ import { determineIsSystemUser, - determineLogLevel, determineUseColor, determineUseUpstream, } from "../../../src/cli/parse-env"; describe("parse env", () => { - describe("log-level", () => { - it("should be verbose if verbose option is true", () => { - const actual = determineLogLevel({ - verbose: true, - }); - - expect(actual).toEqual("verbose"); - }); - - it("should be notice if verbose option is false", () => { - const actual = determineLogLevel({ - verbose: false, - }); - - expect(actual).toEqual("notice"); - }); - - it("should be notice if verbose option is missing", () => { - const actual = determineLogLevel({}); - - expect(actual).toEqual("notice"); - }); - }); - describe("color", () => { it("should use color if color option is true", () => { const actual = determineUseColor(