-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
230611a
commit 8b2909d
Showing
2 changed files
with
73 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters