diff --git a/src/cli/cmd-add.ts b/src/cli/cmd-add.ts index 7ba574ec..80c48c4f 100644 --- a/src/cli/cmd-add.ts +++ b/src/cli/cmd-add.ts @@ -13,6 +13,7 @@ import type { GetRegistryPackument } from "../io/registry"; import type { CheckUrlExists } from "../io/www"; import { eachValue } from "./cli-parsing"; import { withErrorLogger } from "./error-logging"; +import { primaryRegistryUrlOpt } from "./opt-registry"; import type { GlobalOptions } from "./options"; import { parseEnvUsing } from "./parse-env"; import { mustBePackageReference } from "./validators"; @@ -61,6 +62,7 @@ export function makeAddCmd( .addArgument(otherPkgsArg) .addOption(addTestableOpt) .addOption(forceOpt) + .addOption(primaryRegistryUrlOpt) .description( `add package to manifest json openupm add [otherPkgs...] @@ -105,7 +107,7 @@ openupm add @ [otherPkgs...]` readTextFile, debugLog, upmConfigPath, - env.primaryRegistryUrl + addOptions.registry ); const addResults = await addDependenciesUsing( diff --git a/src/cli/cmd-deps.ts b/src/cli/cmd-deps.ts index 1b27e7a8..105d9431 100644 --- a/src/cli/cmd-deps.ts +++ b/src/cli/cmd-deps.ts @@ -23,6 +23,7 @@ import type { GetRegistryPackument } from "../io/registry"; import type { CheckUrlExists } from "../io/www"; import { stringifyDependencyGraph } from "./dependency-logging"; import { withErrorLogger } from "./error-logging"; +import { primaryRegistryUrlOpt } from "./opt-registry"; import { GlobalOptions } from "./options"; import { parseEnvUsing } from "./parse-env"; import { ResultCodes } from "./result-codes"; @@ -56,6 +57,7 @@ export function makeDepsCmd( .alias("dep") .addArgument(pkgArg) .addOption(deepOpt) + .addOption(primaryRegistryUrlOpt) .description( `view package dependencies openupm deps @@ -83,7 +85,7 @@ openupm deps @` readTextFile, debugLog, upmConfigPath, - env.primaryRegistryUrl + depsOptions.registry ); const sources = [primaryRegistry, unityRegistry]; diff --git a/src/cli/cmd-login.ts b/src/cli/cmd-login.ts index 2a00f607..96860758 100644 --- a/src/cli/cmd-login.ts +++ b/src/cli/cmd-login.ts @@ -3,7 +3,6 @@ import { Logger } from "npmlog"; import { loginUsing } from "../app/login"; import { partialApply } from "../domain/fp-utils"; import type { DebugLog } from "../domain/logging"; -import { coerceRegistryUrl } from "../domain/registry-url"; import { getHomePathFromEnv } from "../domain/special-paths"; import { getUserUpmConfigPathFor } from "../domain/upm-config"; import type { ReadTextFile, WriteTextFile } from "../io/fs"; @@ -17,6 +16,7 @@ import { promptRegistryUrl, promptUsername, } from "./prompts"; +import { mustBeRegistryUrl } from "./validators"; const usernameOpt = new Option("-u, --username ", "username").default( null @@ -30,6 +30,13 @@ const emailOpt = new Option("-e, --email ", "email address").default( null ); +const registryOpt = new Option( + "-r, --registry ", + "url of registry into which to login" +) + .argParser(mustBeRegistryUrl) + .default(null); + const basicAuthOpt = new Option( "--basic-auth", "use basic authentication instead of token" @@ -67,6 +74,7 @@ export function makeLoginCmd( .addOption(emailOpt) .addOption(basicAuthOpt) .addOption(alwaysAuthOpt) + .addOption(registryOpt) .description("authenticate with a scoped registry") .action( withErrorLogger(log, async function (loginOptions, cmd) { @@ -91,11 +99,8 @@ export function makeLoginCmd( const username = loginOptions.username ?? (await promptUsername()); const password = loginOptions.password ?? (await promptPassword()); const email = loginOptions.email ?? (await promptEmail()); - const loginRegistry = - globalOptions.registry !== undefined - ? coerceRegistryUrl(globalOptions.registry) - : await promptRegistryUrl(); + loginOptions.registry ?? (await promptRegistryUrl()); await login( username, diff --git a/src/cli/cmd-search.ts b/src/cli/cmd-search.ts index d5bade8e..8f5539a6 100644 --- a/src/cli/cmd-search.ts +++ b/src/cli/cmd-search.ts @@ -10,6 +10,7 @@ import { getUserUpmConfigPathFor } from "../domain/upm-config"; import type { ReadTextFile } from "../io/fs"; import type { GetAllRegistryPackuments, SearchRegistry } from "../io/registry"; import { withErrorLogger } from "./error-logging"; +import { primaryRegistryUrlOpt } from "./opt-registry"; import { GlobalOptions } from "./options"; import { formatAsTable } from "./output-formatting"; import { parseEnvUsing } from "./parse-env"; @@ -41,10 +42,11 @@ export function makeSearchCmd( ); return new Command("search") .argument("", "The keyword to search") + .addOption(primaryRegistryUrlOpt) .aliases(["s", "se", "find"]) .description("Search package by keyword") .action( - withErrorLogger(log, async function (keyword, _, cmd) { + withErrorLogger(log, async function (keyword, searchOptions, cmd) { const globalOptions = cmd.optsWithGlobals(); // parse env @@ -65,7 +67,7 @@ export function makeSearchCmd( readTextFile, debugLog, upmConfigPath, - env.primaryRegistryUrl + searchOptions.registry ); let usedEndpoint = "npmsearch"; diff --git a/src/cli/cmd-view.ts b/src/cli/cmd-view.ts index 2c20f21e..129291ef 100644 --- a/src/cli/cmd-view.ts +++ b/src/cli/cmd-view.ts @@ -12,6 +12,7 @@ import { getUserUpmConfigPathFor } from "../domain/upm-config"; import type { ReadTextFile } from "../io/fs"; import type { GetRegistryPackument } from "../io/registry"; import { withErrorLogger } from "./error-logging"; +import { primaryRegistryUrlOpt } from "./opt-registry"; import { GlobalOptions } from "./options"; import { formatPackumentInfo } from "./output-formatting"; import { parseEnvUsing } from "./parse-env"; @@ -34,10 +35,11 @@ export function makeViewCmd( ) { return new Command("view") .argument("", "Reference to a package", mustBePackageReference) + .addOption(primaryRegistryUrlOpt) .aliases(["v", "info", "show"]) .description("view package information") .action( - withErrorLogger(log, async function (pkg, _, cmd) { + withErrorLogger(log, async function (pkg, viewOptions, cmd) { const globalOptions = cmd.optsWithGlobals(); // parse env @@ -58,7 +60,7 @@ export function makeViewCmd( readTextFile, debugLog, upmConfigPath, - env.primaryRegistryUrl + viewOptions.registry ); // parse name diff --git a/src/cli/opt-registry.ts b/src/cli/opt-registry.ts new file mode 100644 index 00000000..f65d1d94 --- /dev/null +++ b/src/cli/opt-registry.ts @@ -0,0 +1,14 @@ +import { Option } from "@commander-js/extra-typings"; +import { openupmRegistryUrl } from "../domain/registry-url"; +import { mustBeRegistryUrl } from "./validators"; + +/** + * CLI option for the primary registry from which to resolve packages. + * Defaults to {@link openupmRegistryUrl}. + */ +export const primaryRegistryUrlOpt = new Option( + "-r, --registry ", + "specify registry url" +) + .argParser(mustBeRegistryUrl) + .default(openupmRegistryUrl); diff --git a/src/cli/options.ts b/src/cli/options.ts index 3bb477df..f5971c50 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<{ - /** - * Override package registry to use. - */ - registry?: string; /** * Whether to print logs. */ diff --git a/src/cli/parse-env.ts b/src/cli/parse-env.ts index 6eb82a79..177708d0 100644 --- a/src/cli/parse-env.ts +++ b/src/cli/parse-env.ts @@ -2,11 +2,6 @@ import chalk from "chalk"; import { Logger } from "npmlog"; import path from "path"; import { CustomError } from "ts-custom-error"; -import { - coerceRegistryUrl, - openupmRegistryUrl, - RegistryUrl, -} from "../domain/registry-url"; import { CmdOptions } from "./options"; /** @@ -32,10 +27,6 @@ export type Env = Readonly<{ * Whether to fall back to the Unity registry. */ upstream: boolean; - /** - * The primary registry url. - */ - primaryRegistryUrl: RegistryUrl; }>; /** @@ -83,15 +74,6 @@ export function determineIsSystemUser(options: CmdOptions): boolean { return options.systemUser === true; } -/** - * Determines the primary registry url. - * @param options Cmd options. - */ -export function determinePrimaryRegistryUrl(options: CmdOptions): RegistryUrl { - if (options.registry === undefined) return openupmRegistryUrl; - return coerceRegistryUrl(options.registry); -} - /** * Function for parsing environment information and global * command-options for further usage. @@ -123,15 +105,11 @@ export async function parseEnvUsing( // auth const systemUser = determineIsSystemUser(options); - // registries - const primaryRegistryUrl = determinePrimaryRegistryUrl(options); - // cwd const cwd = determineCwd(processCwd, options); return { cwd, - primaryRegistryUrl, systemUser, upstream, }; diff --git a/test/unit/cli/parse-env.test.ts b/test/unit/cli/parse-env.test.ts index e7db725a..9c802c9b 100644 --- a/test/unit/cli/parse-env.test.ts +++ b/test/unit/cli/parse-env.test.ts @@ -3,12 +3,9 @@ import { determineCwd, determineIsSystemUser, determineLogLevel, - determinePrimaryRegistryUrl, determineUseColor, determineUseUpstream, } from "../../../src/cli/parse-env"; -import { openupmRegistryUrl } from "../../../src/domain/registry-url"; -import { someRegistryUrl } from "../../common/data-registry"; const testRootPath = "/users/some-user/projects/MyUnityProject"; @@ -115,22 +112,6 @@ describe("parse env", () => { }); }); - describe("registry", () => { - it("should be openupm by default", () => { - const actual = determinePrimaryRegistryUrl({}); - - expect(actual).toEqual(openupmRegistryUrl); - }); - - it("should be custom registry url if overridden", () => { - const actual = determinePrimaryRegistryUrl({ - registry: someRegistryUrl, - }); - - expect(actual).toEqual(someRegistryUrl); - }); - }); - describe("cwd", () => { it("should be process directory by default", () => { const actual = determineCwd(testRootPath, {});