Skip to content

Commit

Permalink
extract view
Browse files Browse the repository at this point in the history
  • Loading branch information
ComradeVanti committed Sep 17, 2024
1 parent 230611a commit 8b2909d
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 94 deletions.
125 changes: 64 additions & 61 deletions src/cli/cmd-view.ts
Original file line number Diff line number Diff line change
@@ -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<ViewResultCode>;

/**
* 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("<pkg>", "Reference to a package", mustBePackageReference)
.aliases(["v", "info", "show"])
.description("view package information")
.action(
withErrorLogger(log, async function (pkg, _, cmd) {
const globalOptions = cmd.optsWithGlobals<GlobalOptions>();

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);
})
);
}
42 changes: 9 additions & 33 deletions src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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();
Expand All @@ -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<T extends Record<string, unknown>>(
specificOptions: T
): CmdOptions<T> {
return { ...specificOptions, ...program.opts() };
}

program.addCommand(
makeAddCmd(
fetchCheckUrlExists,
Expand All @@ -111,17 +90,14 @@ program.addCommand(
)
);

program
.command("view")
.argument("<pkg>", "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(
Expand Down

0 comments on commit 8b2909d

Please sign in to comment.