Skip to content

Commit

Permalink
refactor: clearer type names
Browse files Browse the repository at this point in the history
  • Loading branch information
kanadgupta committed Sep 14, 2023
1 parent fd1c619 commit b7dfaba
Show file tree
Hide file tree
Showing 13 changed files with 36 additions and 34 deletions.
4 changes: 2 additions & 2 deletions src/cmds/login.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { CommandOptions } from '../lib/baseCommand';
import type { ZeroAuthCommandOptions } from '../lib/baseCommand';

import prompts from 'prompts';

Expand Down Expand Up @@ -45,7 +45,7 @@ export default class LoginCommand extends Command {
];
}

async run(opts: CommandOptions<Options>) {
async run(opts: ZeroAuthCommandOptions<Options>) {
await super.run(opts);

prompts.override(opts);
Expand Down
4 changes: 2 additions & 2 deletions src/cmds/logout.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { CommandOptions } from '../lib/baseCommand';
import type { ZeroAuthCommandOptions } from '../lib/baseCommand';

import Command, { CommandCategories } from '../lib/baseCommand';
import config from '../lib/config';
Expand All @@ -16,7 +16,7 @@ export default class LogoutCommand extends Command {
this.args = [];
}

async run(opts: CommandOptions<{}>) {
async run(opts: ZeroAuthCommandOptions<{}>) {
await super.run(opts);

if (configStore.has('email') && configStore.has('project')) {
Expand Down
4 changes: 2 additions & 2 deletions src/cmds/oas.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { CommandOptions } from '../lib/baseCommand';
import type { ZeroAuthCommandOptions } from '../lib/baseCommand';

import Command, { CommandCategories } from '../lib/baseCommand';
import isHidden from '../lib/decorators/isHidden';
Expand All @@ -16,7 +16,7 @@ export default class OASCommand extends Command {
this.args = [];
}

async run(opts: CommandOptions<{}>) {
async run(opts: ZeroAuthCommandOptions<{}>) {
await super.run(opts);

const message = [
Expand Down
4 changes: 2 additions & 2 deletions src/cmds/open.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { CommandOptions } from '../lib/baseCommand';
import type { ZeroAuthCommandOptions } from '../lib/baseCommand';

import chalk from 'chalk';
import open from 'open';
Expand Down Expand Up @@ -31,7 +31,7 @@ export default class OpenCommand extends Command {
];
}

async run(opts: CommandOptions<Options>) {
async run(opts: ZeroAuthCommandOptions<Options>) {
await super.run(opts);

const { dash } = opts;
Expand Down
4 changes: 2 additions & 2 deletions src/cmds/openapi/convert.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { CommandOptions } from '../../lib/baseCommand';
import type { ZeroAuthCommandOptions } from '../../lib/baseCommand';
import type { OASDocument } from 'oas/dist/rmoas.types';

import fs from 'fs';
Expand Down Expand Up @@ -43,7 +43,7 @@ export default class OpenAPIConvertCommand extends Command {
];
}

async run(opts: CommandOptions<Options>) {
async run(opts: ZeroAuthCommandOptions<Options>) {
await super.run(opts);

const { spec, workingDirectory } = opts;
Expand Down
4 changes: 2 additions & 2 deletions src/cmds/openapi/inspect.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Analysis, AnalyzedFeature } from '../../lib/analyzeOas';
import type { CommandOptions } from '../../lib/baseCommand';
import type { ZeroAuthCommandOptions } from '../../lib/baseCommand';
import type { OASDocument } from 'oas/dist/rmoas.types';

import chalk from 'chalk';
Expand Down Expand Up @@ -215,7 +215,7 @@ export default class OpenAPIInspectCommand extends Command {
return report.join('\n');
}

async run(opts: CommandOptions<Options>) {
async run(opts: ZeroAuthCommandOptions<Options>) {
await super.run(opts);

const { spec, workingDirectory, feature: features } = opts;
Expand Down
4 changes: 2 additions & 2 deletions src/cmds/openapi/reduce.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { CommandOptions } from '../../lib/baseCommand';
import type { ZeroAuthCommandOptions } from '../../lib/baseCommand';
import type { OASDocument } from 'oas/dist/rmoas.types';

import fs from 'fs';
Expand Down Expand Up @@ -70,7 +70,7 @@ export default class OpenAPIReduceCommand extends Command {
];
}

async run(opts: CommandOptions<Options>) {
async run(opts: ZeroAuthCommandOptions<Options>) {
await super.run(opts);

const { spec, title, workingDirectory } = opts;
Expand Down
6 changes: 3 additions & 3 deletions src/cmds/openapi/validate.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { CommandOptions } from '../../lib/baseCommand';
import type { ZeroAuthCommandOptions } from '../../lib/baseCommand';

import chalk from 'chalk';

Expand Down Expand Up @@ -32,7 +32,7 @@ export default class OpenAPIValidateCommand extends Command {
];
}

async run(opts: CommandOptions<Options>) {
async run(opts: ZeroAuthCommandOptions<Options>) {
await super.run(opts);

const { spec, workingDirectory } = opts;
Expand All @@ -45,7 +45,7 @@ export default class OpenAPIValidateCommand extends Command {

const { specPath, specType } = await prepareOas(spec, 'openapi:validate');
return Promise.resolve(chalk.green(`${specPath} is a valid ${specType} API definition!`)).then(msg =>
createGHA(msg, this.command, this.args, { ...opts, spec: specPath } as CommandOptions<Options>),
createGHA(msg, this.command, this.args, { ...opts, spec: specPath } as ZeroAuthCommandOptions<Options>),
);
}
}
4 changes: 2 additions & 2 deletions src/cmds/validate.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Options } from './openapi/validate';
import type { CommandOptions } from '../lib/baseCommand';
import type { ZeroAuthCommandOptions } from '../lib/baseCommand';

import Command from '../lib/baseCommand';
import isHidden from '../lib/decorators/isHidden';
Expand All @@ -16,7 +16,7 @@ export default class ValidateAliasCommand extends OpenAPIValidateCommand {
this.description = 'Alias for `rdme openapi:validate` [deprecated].';
}

async run(opts: CommandOptions<Options>) {
async run(opts: ZeroAuthCommandOptions<Options>) {
Command.warn('`rdme validate` has been deprecated. Please use `rdme openapi:validate` instead.');
return super.run(opts);
}
Expand Down
4 changes: 2 additions & 2 deletions src/cmds/whoami.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { CommandOptions } from '../lib/baseCommand';
import type { ZeroAuthCommandOptions } from '../lib/baseCommand';

import chalk from 'chalk';

Expand All @@ -18,7 +18,7 @@ export default class WhoAmICommand extends Command {
this.args = [];
}

async run(opts: CommandOptions<{}>) {
async run(opts: ZeroAuthCommandOptions<{}>) {
await super.run(opts);

const { email, project } = getCurrentConfig();
Expand Down
8 changes: 4 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable no-underscore-dangle */
import type Command from './lib/baseCommand';
import type { CommandOptions, AuthenticatedCommandOptions } from './lib/baseCommand';
import type { CommandOptions } from './lib/baseCommand';
import type { CommandLineOptions } from 'command-line-args';

import chalk from 'chalk';
Expand Down Expand Up @@ -83,7 +83,7 @@ export default function rdme(rawProcessArgv: NodeJS.Process['argv']) {
}

try {
let cmdArgv: CommandOptions<{}> | AuthenticatedCommandOptions<{}> | CommandLineOptions;
let cmdArgv: CommandOptions<{}> | CommandLineOptions;
let bin: Command;

// Handling for `rdme help` and `rdme help <command>` cases.
Expand Down Expand Up @@ -135,9 +135,9 @@ export default function rdme(rawProcessArgv: NodeJS.Process['argv']) {

cmdArgv = { key, ...cmdArgv };

return bin.run(cmdArgv as CommandOptions<{}> | AuthenticatedCommandOptions<{}>).then((msg: string) => {
return bin.run(cmdArgv as CommandOptions<{}>).then((msg: string) => {
if (bin.supportsGHA) {
return createGHA(msg, bin.command, bin.args, cmdArgv as CommandOptions<{}> | AuthenticatedCommandOptions<{}>);
return createGHA(msg, bin.command, bin.args, cmdArgv as CommandOptions<{}>);
}
return msg;
});
Expand Down
12 changes: 7 additions & 5 deletions src/lib/baseCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@ import isCI from './isCI';
import { debug, info, warn } from './logger';
import loginFlow from './loginFlow';

export type CommandOptions<T> = T & {
github?: boolean;
} & { key?: never };
export type CommandOptions<T> = ZeroAuthCommandOptions<T> | AuthenticatedCommandOptions<T>;

export type AuthenticatedCommandOptions<T> = Omit<CommandOptions<T>, 'key'> & {
export type AuthenticatedCommandOptions<T> = Omit<ZeroAuthCommandOptions<T>, 'key'> & {
key: string;
version?: string;
};

export type ZeroAuthCommandOptions<T> = T & {
github?: boolean;
} & { key?: never };

export enum CommandCategories {
ADMIN = 'admin',
APIS = 'apis',
Expand Down Expand Up @@ -86,7 +88,7 @@ export default class Command {
*/
args!: OptionDefinition[];

async run(opts: CommandOptions<{}> | AuthenticatedCommandOptions<{}>): Promise<string> {
async run(opts: CommandOptions<{}>): Promise<string> {
Command.debug(`command: ${this.command}`);
Command.debug(`opts: ${JSON.stringify(opts)}`);

Expand Down
8 changes: 4 additions & 4 deletions src/lib/createGHA/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type commands from '../../cmds';
import type { AuthenticatedCommandOptions, CommandOptions } from '../baseCommand';
import type { CommandOptions } from '../baseCommand';
import type { OptionDefinition } from 'command-line-usage';

import fs from 'fs';
Expand Down Expand Up @@ -48,7 +48,7 @@ export const getGHAFileName = (fileName: string) => {
* Returns a redacted `key` if the current command uses authentication.
* Otherwise, returns `false`.
*/
function getKey(args: OptionDefinition[], opts: CommandOptions<{}> | AuthenticatedCommandOptions<{}>): string | false {
function getKey(args: OptionDefinition[], opts: CommandOptions<{}>): string | false {
if (args.some(arg => arg.name === 'key')) {
return `••••••••••••${opts.key?.slice(-5) || ''}`;
}
Expand All @@ -61,7 +61,7 @@ function getKey(args: OptionDefinition[], opts: CommandOptions<{}> | Authenticat
function constructCmdString(
command: keyof typeof commands,
args: OptionDefinition[],
opts: CommandOptions<{}> | AuthenticatedCommandOptions<{}>,
opts: CommandOptions<{}>,
): string {
const optsString = args
.sort(arg => (arg.defaultOption ? -1 : 0))
Expand Down Expand Up @@ -153,7 +153,7 @@ export default async function createGHA(
msg: string,
command: keyof typeof commands,
args: OptionDefinition[],
opts: CommandOptions<{}> | AuthenticatedCommandOptions<{}>,
opts: CommandOptions<{}>,
) {
debug(`running GHA onboarding for ${command} command`);
debug(`opts used in createGHA: ${JSON.stringify(opts)}`);
Expand Down

0 comments on commit b7dfaba

Please sign in to comment.