Skip to content

Commit

Permalink
refactor: make options type readonly (#121)
Browse files Browse the repository at this point in the history
* refactor: make type readonly

* refactor: make type readonly
  • Loading branch information
ComradeVanti authored Jan 16, 2024
1 parent 1e7d075 commit 46d848a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 21 deletions.
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 { addAuth, encodeBasicAuth, UPMConfig } 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;
}
>;

0 comments on commit 46d848a

Please sign in to comment.