Skip to content

Commit

Permalink
refactor: result-code types for cmd-functions (#70)
Browse files Browse the repository at this point in the history
The cmd functions currently always only return 0 or 1. Add types to express this in their signature. Every function gets their own type in preparation for potentially more codes that are not shared between functions.

Co-authored-by: Favo Yang <favoyang@gmail.com>
  • Loading branch information
ComradeVanti and favoyang authored Dec 20, 2023
1 parent d1fffaa commit 675eadb
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 10 deletions.
6 changes: 3 additions & 3 deletions src/cmd-add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ export type AddOptions = CmdOptions<{
force?: boolean;
}>;

type ResultCode = 0 | 1;
type AddResultCode = 0 | 1;

type AddResult = {
dirty: boolean;
code: ResultCode;
code: AddResultCode;
};

/**
Expand All @@ -43,7 +43,7 @@ type AddResult = {
export const add = async function (
pkgs: PackageReference | PackageReference[],
options: AddOptions
): Promise<ResultCode> {
): Promise<AddResultCode> {
if (!Array.isArray(pkgs)) pkgs = [pkgs];
// parse env
const env = await parseEnv(options, true);
Expand Down
4 changes: 3 additions & 1 deletion src/cmd-deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import {
} from "./types/package-reference";
import { CmdOptions } from "./types/options";

type DepsResultCode = 0 | 1;

export type DepsOptions = CmdOptions<{
deep?: boolean;
}>;
Expand All @@ -19,7 +21,7 @@ export type DepsOptions = CmdOptions<{
export const deps = async function (
pkg: PackageReference,
options: DepsOptions
) {
): Promise<DepsResultCode> {
// parse env
const env = await parseEnv(options, false);
if (env === null) return 1;
Expand Down
8 changes: 7 additions & 1 deletion src/cmd-login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,14 @@ export type LoginOptions = CmdOptions<{
alwaysAuth?: boolean;
}>;

type LoginResultCode = 0 | 1;

/**
* @throws Error An unhandled error occurred
*/
export const login = async function (options: LoginOptions) {
export const login = async function (
options: LoginOptions
): Promise<LoginResultCode> {
// parse env
const env = await parseEnv(options, false);
if (env === null) return 1;
Expand Down Expand Up @@ -75,6 +79,8 @@ export const login = async function (options: LoginOptions) {
options._global.registry as RegistryUrl,
token
);

return 0;
};

/**
Expand Down
15 changes: 12 additions & 3 deletions src/cmd-remove.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,25 @@ import { CmdOptions } from "./types/options";

export type RemoveOptions = CmdOptions;

type RemoveResultCode = 0 | 1;

type RemoveResult = {
code: RemoveResultCode;
dirty: boolean;
};

export const remove = async function (
pkgs: PackageReference[] | PackageReference,
options: RemoveOptions
) {
): Promise<RemoveResultCode> {
if (!Array.isArray(pkgs)) pkgs = [pkgs];
// parse env
const env = await parseEnv(options, true);
if (env === null) return 1;

const removeSingle = async function (pkg: PackageReference) {
const removeSingle = async function (
pkg: PackageReference
): Promise<RemoveResult> {
// dirty flag
let dirty = false;
// parse name
Expand Down Expand Up @@ -73,7 +82,7 @@ export const remove = async function (
// remove
const results = [];
for (const pkg of pkgs) results.push(await removeSingle(pkg));
const result = {
const result: RemoveResult = {
code: results.filter((x) => x.code != 0).length > 0 ? 1 : 0,
dirty: results.filter((x) => x.dirty).length > 0,
};
Expand Down
7 changes: 6 additions & 1 deletion src/cmd-search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ type DateString = string;

type TableRow = [DomainName, SemanticVersion, DateString, ""];

type SearchResultCode = 0 | 1;

export type SearchOptions = CmdOptions;

export type SearchedPkgInfo = Omit<PkgInfo, "versions"> & {
Expand Down Expand Up @@ -116,7 +118,10 @@ const getTableRow = function (pkg: SearchedPkgInfo): TableRow {
return [name, version, date, ""];
};

export async function search(keyword: string, options: SearchOptions) {
export async function search(
keyword: string,
options: SearchOptions
): Promise<SearchResultCode> {
// parse env
const env = await parseEnv(options, false);
if (env === null) return 1;
Expand Down
4 changes: 3 additions & 1 deletion src/cmd-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ import { CmdOptions } from "./types/options";

export type ViewOptions = CmdOptions;

type ViewResultCode = 0 | 1;

export const view = async function (
pkg: PackageReference,
options: ViewOptions
) {
): Promise<ViewResultCode> {
// parse env
const env = await parseEnv(options, false);
if (env === null) return 1;
Expand Down

0 comments on commit 675eadb

Please sign in to comment.