Skip to content

Commit

Permalink
extract registry
Browse files Browse the repository at this point in the history
  • Loading branch information
ComradeVanti committed Sep 17, 2024
1 parent b4ba2db commit 53e09fe
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 56 deletions.
4 changes: 3 additions & 1 deletion src/cli/cmd-add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -61,6 +62,7 @@ export function makeAddCmd(
.addArgument(otherPkgsArg)
.addOption(addTestableOpt)
.addOption(forceOpt)
.addOption(primaryRegistryUrlOpt)
.description(
`add package to manifest json
openupm add <pkg> [otherPkgs...]
Expand Down Expand Up @@ -105,7 +107,7 @@ openupm add <pkg>@<version> [otherPkgs...]`
readTextFile,
debugLog,
upmConfigPath,
env.primaryRegistryUrl
addOptions.registry
);

const addResults = await addDependenciesUsing(
Expand Down
4 changes: 3 additions & 1 deletion src/cli/cmd-deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -56,6 +57,7 @@ export function makeDepsCmd(
.alias("dep")
.addArgument(pkgArg)
.addOption(deepOpt)
.addOption(primaryRegistryUrlOpt)
.description(
`view package dependencies
openupm deps <pkg>
Expand Down Expand Up @@ -83,7 +85,7 @@ openupm deps <pkg>@<version>`
readTextFile,
debugLog,
upmConfigPath,
env.primaryRegistryUrl
depsOptions.registry
);
const sources = [primaryRegistry, unityRegistry];

Expand Down
15 changes: 10 additions & 5 deletions src/cli/cmd-login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -17,6 +16,7 @@ import {
promptRegistryUrl,
promptUsername,
} from "./prompts";
import { mustBeRegistryUrl } from "./validators";

const usernameOpt = new Option("-u, --username <username>", "username").default(
null
Expand All @@ -30,6 +30,13 @@ const emailOpt = new Option("-e, --email <email>", "email address").default(
null
);

const registryOpt = new Option(
"-r, --registry <url>",
"url of registry into which to login"
)
.argParser(mustBeRegistryUrl)
.default(null);

const basicAuthOpt = new Option(
"--basic-auth",
"use basic authentication instead of token"
Expand Down Expand Up @@ -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) {
Expand All @@ -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,
Expand Down
6 changes: 4 additions & 2 deletions src/cli/cmd-search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -41,10 +42,11 @@ export function makeSearchCmd(
);
return new Command("search")
.argument("<keyword>", "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<GlobalOptions>();

// parse env
Expand All @@ -65,7 +67,7 @@ export function makeSearchCmd(
readTextFile,
debugLog,
upmConfigPath,
env.primaryRegistryUrl
searchOptions.registry
);

let usedEndpoint = "npmsearch";
Expand Down
6 changes: 4 additions & 2 deletions src/cli/cmd-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -34,10 +35,11 @@ export function makeViewCmd(
) {
return new Command("view")
.argument("<pkg>", "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<GlobalOptions>();

// parse env
Expand All @@ -58,7 +60,7 @@ export function makeViewCmd(
readTextFile,
debugLog,
upmConfigPath,
env.primaryRegistryUrl
viewOptions.registry
);

// parse name
Expand Down
14 changes: 14 additions & 0 deletions src/cli/opt-registry.ts
Original file line number Diff line number Diff line change
@@ -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 <url>",
"specify registry url"
)
.argParser(mustBeRegistryUrl)
.default(openupmRegistryUrl);
4 changes: 0 additions & 4 deletions src/cli/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
22 changes: 0 additions & 22 deletions src/cli/parse-env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

/**
Expand All @@ -32,10 +27,6 @@ export type Env = Readonly<{
* Whether to fall back to the Unity registry.
*/
upstream: boolean;
/**
* The primary registry url.
*/
primaryRegistryUrl: RegistryUrl;
}>;

/**
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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,
};
Expand Down
19 changes: 0 additions & 19 deletions test/unit/cli/parse-env.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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, {});
Expand Down

0 comments on commit 53e09fe

Please sign in to comment.