Skip to content

Commit

Permalink
feat: add types for Cli
Browse files Browse the repository at this point in the history
  • Loading branch information
Diego Ferreiro Val committed Jun 13, 2019
1 parent 7f3cf58 commit b3f0b42
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 17 deletions.
20 changes: 20 additions & 0 deletions packages/@best/cli/src/cli/args.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Options } from "yargs";
import { BestCliOptions } from "@best/config";

export const check = () => {
// TODO: Implement checks
Expand Down Expand Up @@ -80,3 +81,22 @@ export const options: { [key: string]: Options } = {
type: 'boolean',
}
};

export function normalize(args: { [x: string]: any; _: string[]; $0: string }): BestCliOptions {
const { _, help, clearCache, clearResults, gitIntegration, useHttp, externalStorage, runner, runnerConfig, config, projects, iterations, compareStats } = args;
return {
_,
help: Boolean(help),
clearCache: Boolean(clearCache),
clearResults: Boolean(clearResults),
useHttp: Boolean(useHttp),
gitIntegration,
externalStorage,
runner,
runnerConfig: runnerConfig ? JSON.parse(runnerConfig): {},
config,
projects: projects || [],
iterations: parseInt(iterations, 10),
compareStats
};
}
29 changes: 16 additions & 13 deletions packages/@best/cli/src/cli/index.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
import * as args from './args';
import { normalize, usage, options, docs, check } from './args';
import Output from './output';
import yargs from 'yargs';
import rimraf from 'rimraf';
import { getConfigs } from '@best/config';
import { getConfigs, BestCliOptions } from '@best/config';
import { preRunMessager, errorMessager } from '@best/messager';
import { runBest } from '../run_best';
import { runCompare } from '../run_compare';

function buildArgs(maybeArgv: string[]) {
const argsv = yargs(maybeArgv || process.argv.slice(2))
.usage(args.usage)
export function buildArgs(maybeArgv?: string[]): BestCliOptions {
const parsedArgs = yargs(maybeArgv || process.argv.slice(2))
.usage(usage)
.alias('help', 'h')
.options(args.options)
.epilogue(args.docs)
.check(args.check)
.options(options)
.epilogue(docs)
.check(check)
.version(false).argv;
return argsv;

return normalize(parsedArgs);
}

function getProjectListFromCLIArgs(argsCLI: any, project: string): string[] {
const projects = argsCLI.projects ? argsCLI.projects : [];
function getProjectListFromCLIArgs(argsCLI: BestCliOptions, project?: string): string[] {
const projects = argsCLI.projects;

if (project) {
projects.push(project);
Expand All @@ -32,7 +33,7 @@ function getProjectListFromCLIArgs(argsCLI: any, project: string): string[] {
return projects;
}

export async function run(maybeArgv: string[], project: string) {
export async function run(maybeArgv?: string[], project?: string) {
try {
const argsCLI = buildArgs(maybeArgv);
const projects = getProjectListFromCLIArgs(argsCLI, project);
Expand All @@ -46,7 +47,7 @@ export async function run(maybeArgv: string[], project: string) {
}
}

export async function runCLI(argsCLI: any, projects: string[]) {
export async function runCLI(argsCLI: BestCliOptions, projects: string[]) {
const outputStream: any = process.stdout;
let rawConfigs;
let results;
Expand Down Expand Up @@ -94,3 +95,5 @@ export async function runCLI(argsCLI: any, projects: string[]) {

return true;
}

export { BestCliOptions };
4 changes: 2 additions & 2 deletions packages/@best/config/src/defaults.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { cacheDirectory } from '@best/utils';
import { BestCliOptions } from "./types";
import { BestBuildOptions } from "./types";

const defaultOptions: BestCliOptions = {
const defaultOptions: BestBuildOptions = {
cache: true,
cacheDirectory: cacheDirectory(),
useHttp: true,
Expand Down
5 changes: 4 additions & 1 deletion packages/@best/config/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import { replacePathSepForRegex } from '@best/regex-util';
import DEFAULT_CONFIG from './defaults';
import { addGitInformation } from './git';
import { PACKAGE_JSON, BEST_CONFIG } from './constants';
import { BestCliOptions } from './types';

export { BestCliOptions };

const TARGET_COMMIT = process.env.TARGET_COMMIT;
const BASE_COMMIT = process.env.BASE_COMMIT;
Expand Down Expand Up @@ -335,7 +338,7 @@ export async function readConfig(argsCLI: any, packageRoot: string, parentConfig
return { configPath, globalConfig, projectConfig };
}

export async function getConfigs(projectsFromCLIArgs: string[], argv: string[]) {
export async function getConfigs(projectsFromCLIArgs: string[], argv: BestCliOptions) {
let globalConfig;
let configs: any = [];
let projects = projectsFromCLIArgs;
Expand Down
18 changes: 17 additions & 1 deletion packages/@best/config/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export interface BestCliOptions {
export interface BestBuildOptions {
[key: string]: any,
cache: boolean,
cacheDirectory: string,
Expand Down Expand Up @@ -28,3 +28,19 @@ export interface BestCliOptions {
histogramMaxWidth: number,
rootDir?: string
}

export interface BestCliOptions {
_: string[],
help: boolean,
clearCache: boolean,
clearResults: boolean,
gitIntegration: string | undefined,
useHttp: boolean,
externalStorage: string | undefined,
runner: string,
runnerConfig: { [x:string]: any },
config: string | undefined,
projects: string[],
iterations: number,
compareStats: string[] | undefined
};

0 comments on commit b3f0b42

Please sign in to comment.