Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: make options type readonly #121

Merged
merged 2 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 14 additions & 16 deletions src/cmd-login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
import { parseEnv } from "./utils/env";
import { encodeBasicAuth } from "./types/upm-config";
import { Base64 } from "./types/base64";
import { RegistryUrl } from "./types/registry-url";
import { coerceRegistryUrl, RegistryUrl } from "./types/registry-url";
import {
promptEmail,
promptPassword,
Expand Down Expand Up @@ -39,32 +39,30 @@ export const login = async function (
const env = await parseEnv(options, false);
if (env === null) return 1;
// query parameters
if (!options.username) options.username = await promptUsername();
if (!options.password) options.password = await promptPassword();
if (!options.email) options.email = await promptEmail();
if (!options._global.registry)
options._global.registry = await promptRegistryUrl();
const username = options.username ?? (await promptUsername());
const password = options.password ?? (await promptPassword());
const email = options.email ?? (await promptEmail());

const loginRegistry =
options._global.registry !== undefined
? coerceRegistryUrl(options._global.registry)
: await promptRegistryUrl();
let token: string | null = null;
let _auth: Base64 | null = null;
if (options.basicAuth) {
// basic auth
_auth = encodeBasicAuth(options.username, options.password);
_auth = encodeBasicAuth(username, password);
} else {
// npm login
const result = await npmLogin(
options.username,
options.password,
options.email,
options._global.registry as RegistryUrl
);
const result = await npmLogin(username, password, email, loginRegistry);
if (result.code == 1) return result.code;
if (!result.token) {
log.error("auth", "can not find token from server response");
return 1;
}
token = result.token;
// write npm token
await writeNpmToken(options._global.registry as RegistryUrl, result.token);
await writeNpmToken(loginRegistry, result.token);
}

// write unity token
Expand All @@ -74,8 +72,8 @@ export const login = async function (
_auth,
options.alwaysAuth || false,
options.basicAuth || false,
options.email,
options._global.registry as RegistryUrl,
email,
loginRegistry,
token
);

Expand Down
12 changes: 7 additions & 5 deletions src/types/options.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Options which are shared between commands.
*/
type GlobalOptions = {
type GlobalOptions = Readonly<{
/**
* Override package registry to use.
*/
Expand Down Expand Up @@ -34,14 +34,16 @@ type GlobalOptions = {
* Override working directory.
*/
chdir?: string;
};
}>;

/**
* Command-options. Extends the given record with a _global property
* containing {@link GlobalOptions}.
*/
export type CmdOptions<
TOptions extends Record<string, unknown> = Record<string, unknown>
> = TOptions & {
_global: GlobalOptions;
};
> = Readonly<
TOptions & {
_global: GlobalOptions;
}
>;