From 8b2909d71e74b264d221c03edf8880994cd32e27 Mon Sep 17 00:00:00 2001 From: Ramon Brullo Date: Tue, 17 Sep 2024 16:35:15 +0200 Subject: [PATCH] extract view --- src/cli/cmd-view.ts | 125 +++++++++++++++++++++++--------------------- src/cli/index.ts | 42 ++++----------- 2 files changed, 73 insertions(+), 94 deletions(-) diff --git a/src/cli/cmd-view.ts b/src/cli/cmd-view.ts index 4455fc60..2c20f21e 100644 --- a/src/cli/cmd-view.ts +++ b/src/cli/cmd-view.ts @@ -1,88 +1,91 @@ +import { Command } from "@commander-js/extra-typings"; import { Logger } from "npmlog"; import { EOL } from "os"; import { loadRegistryAuthUsing } from "../app/get-registry-auth"; import { queryAllRegistriesLazy } from "../app/query-registries"; import { PackumentNotFoundError } from "../domain/common-errors"; import type { DebugLog } from "../domain/logging"; -import { - hasVersion, - PackageReference, - splitPackageReference, -} from "../domain/package-reference"; +import { hasVersion, splitPackageReference } from "../domain/package-reference"; import { unityRegistry } from "../domain/registry"; import { getHomePathFromEnv } from "../domain/special-paths"; import { getUserUpmConfigPathFor } from "../domain/upm-config"; import type { ReadTextFile } from "../io/fs"; import type { GetRegistryPackument } from "../io/registry"; -import { CmdOptions } from "./options"; +import { withErrorLogger } from "./error-logging"; +import { GlobalOptions } from "./options"; import { formatPackumentInfo } from "./output-formatting"; import { parseEnvUsing } from "./parse-env"; import { ResultCodes } from "./result-codes"; +import { mustBePackageReference } from "./validators"; /** - * Options passed to the view command. - */ -export type ViewOptions = CmdOptions; - -/** - * The possible result codes with which the view command can exit. - */ -export type ViewResultCode = ResultCodes.Ok | ResultCodes.Error; - -/** - * Cmd-handler for viewing package information. - * @param pkg Reference to the package to view. - * @param options Command options. - */ -export type ViewCmd = ( - pkg: PackageReference, - options: ViewOptions -) => Promise; - -/** - * Makes a {@link ViewCmd} function. + * Makes the `openupm view` cli command with the given dependencies. + * @param getRegistryPackument IO function for fetching registry packages. + * @param readTextFile IO function for reading a text file. + * @param debugLog IO function for debug-logs. + * @param log Logger for cli output. + * @returns The command. */ export function makeViewCmd( getRegistryPackument: GetRegistryPackument, readTextFile: ReadTextFile, debugLog: DebugLog, log: Logger -): ViewCmd { - return async (pkg, options) => { - // parse env - const env = await parseEnvUsing(log, process.env, process.cwd(), options); - const homePath = getHomePathFromEnv(process.env); - const upmConfigPath = getUserUpmConfigPathFor( - process.env, - homePath, - env.systemUser - ); +) { + return new Command("view") + .argument("", "Reference to a package", mustBePackageReference) + .aliases(["v", "info", "show"]) + .description("view package information") + .action( + withErrorLogger(log, async function (pkg, _, cmd) { + const globalOptions = cmd.optsWithGlobals(); - const primaryRegistry = await loadRegistryAuthUsing( - readTextFile, - debugLog, - upmConfigPath, - env.primaryRegistryUrl - ); + // parse env + const env = await parseEnvUsing( + log, + process.env, + process.cwd(), + globalOptions + ); + const homePath = getHomePathFromEnv(process.env); + const upmConfigPath = getUserUpmConfigPathFor( + process.env, + homePath, + env.systemUser + ); - // parse name - if (hasVersion(pkg)) { - const [name] = splitPackageReference(pkg); - log.warn("", `please do not specify a version (Write only '${name}').`); - return ResultCodes.Error; - } + const primaryRegistry = await loadRegistryAuthUsing( + readTextFile, + debugLog, + upmConfigPath, + env.primaryRegistryUrl + ); - // verify name - const sources = [primaryRegistry, ...(env.upstream ? [unityRegistry] : [])]; - const packumentFromRegistry = await queryAllRegistriesLazy( - sources, - (source) => getRegistryPackument(source, pkg) - ); - const packument = packumentFromRegistry?.value ?? null; - if (packument === null) throw new PackumentNotFoundError(pkg); + // parse name + if (hasVersion(pkg)) { + const [name] = splitPackageReference(pkg); + log.warn( + "", + `please do not specify a version (Write only '${name}').` + ); + return process.exit(ResultCodes.Error); + } + + // verify name + const sources = [ + primaryRegistry, + ...(env.upstream ? [unityRegistry] : []), + ]; + const packumentFromRegistry = await queryAllRegistriesLazy( + sources, + (source) => getRegistryPackument(source, pkg) + ); + const packument = packumentFromRegistry?.value ?? null; + if (packument === null) throw new PackumentNotFoundError(pkg); - const output = formatPackumentInfo(packument, EOL); - log.notice("", output); - return ResultCodes.Ok; - }; + const output = formatPackumentInfo(packument, EOL); + log.notice("", output); + return process.exit(ResultCodes.Ok); + }) + ); } diff --git a/src/cli/index.ts b/src/cli/index.ts index 146a7e96..1cc7d34b 100644 --- a/src/cli/index.ts +++ b/src/cli/index.ts @@ -20,9 +20,7 @@ import { makeLoginCmd } from "./cmd-login"; import { makeRemoveCmd } from "./cmd-remove"; import { makeSearchCmd } from "./cmd-search"; import { makeViewCmd } from "./cmd-view"; -import { withErrorLogger } from "./error-logging"; -import { CmdOptions } from "./options"; -import { mustBePackageReference, mustBeRegistryUrl } from "./validators"; +import { mustBeRegistryUrl } from "./validators"; // Composition root @@ -53,15 +51,7 @@ const fetchPackument = getRegistryPackumentUsing( const searchRegistry = searchRegistryUsing(debugLogToConsole); const fetchAllPackuments = getAllRegistryPackumentsUsing(debugLogToConsole); -const viewCmd = makeViewCmd( - getRegistryPackumentUsing(registryClient, debugLogToConsole), - readTextFile, - debugLogToConsole, - log -); - // update-notifier - pkginfo(module); const notifier = updateNotifier({ pkg }); notifier.notify(); @@ -75,17 +65,6 @@ const program = createCommand() .option("--no-upstream", "don't use upstream unity registry") .option("--no-color", "disable color"); -/** - * Creates a CmdOptions object by adding global options to the given - * specific options. - * @param specificOptions The specific options. - */ -function makeCmdOptions>( - specificOptions: T -): CmdOptions { - return { ...specificOptions, ...program.opts() }; -} - program.addCommand( makeAddCmd( fetchCheckUrlExists, @@ -111,17 +90,14 @@ program.addCommand( ) ); -program - .command("view") - .argument("", "Reference to a package", mustBePackageReference) - .aliases(["v", "info", "show"]) - .description("view package information") - .action( - withErrorLogger(log, async function (pkg, options) { - const resultCode = await viewCmd(pkg, makeCmdOptions(options)); - process.exit(resultCode); - }) - ); +program.addCommand( + makeViewCmd( + getRegistryPackumentUsing(registryClient, debugLogToConsole), + readTextFile, + debugLogToConsole, + log + ) +); program.addCommand( makeDepsCmd(