From f71b4120913f720be63705ea29ed80e8b8bf3f1d Mon Sep 17 00:00:00 2001 From: Diego Ferreiro Val Date: Sat, 15 Jun 2019 07:39:41 -0700 Subject: [PATCH] feat: first config types --- packages/@best/cli/src/cli/index.ts | 12 +++++++----- packages/@best/config/src/index.ts | 10 +++++----- packages/@best/config/src/types.ts | 8 +++++++- packages/@best/config/src/utils/normalize.ts | 4 ++-- 4 files changed, 21 insertions(+), 13 deletions(-) diff --git a/packages/@best/cli/src/cli/index.ts b/packages/@best/cli/src/cli/index.ts index 2e053fcd..9d64c068 100644 --- a/packages/@best/cli/src/cli/index.ts +++ b/packages/@best/cli/src/cli/index.ts @@ -6,6 +6,8 @@ import { getConfigs, BestCliOptions } from '@best/config'; import { preRunMessager, errorMessager } from '@best/messager'; import { runBest } from '../run_best'; import { runCompare } from '../run_compare'; +import { Stream } from 'stream'; +import { ProjectConfigs, ProjectConfig } from '@best/config/build/types'; export function buildArgs(maybeArgv?: string[]): BestCliOptions { const parsedArgs = yargs(maybeArgv || process.argv.slice(2)) @@ -48,21 +50,21 @@ export async function run(maybeArgv?: string[], project?: string) { } export async function runCLI(argsCLI: BestCliOptions, projects: string[]) { - const outputStream: any = process.stdout; - let rawConfigs; + const outputStream: Stream = process.stdout; + let projectConfigs: ProjectConfigs; let results; try { preRunMessager.print('Looking for Best configurations...', outputStream); - rawConfigs = await getConfigs(projects, argsCLI); + projectConfigs = await getConfigs(projects, argsCLI); } finally { preRunMessager.clear(outputStream); } - const { globalConfig, configs } = rawConfigs; + const { globalConfig, configs } = projectConfigs; if (argsCLI.clearCache) { - configs.forEach((config :any) => { + configs.forEach((config : ProjectConfig) => { rimraf.sync(config.cacheDirectory); process.stdout.write(`Cleared ${config.cacheDirectory}\n`); }); diff --git a/packages/@best/config/src/index.ts b/packages/@best/config/src/index.ts index 4be4f8c8..097b968a 100644 --- a/packages/@best/config/src/index.ts +++ b/packages/@best/config/src/index.ts @@ -2,7 +2,7 @@ import { resolveConfigPath, readConfigAndSetRootDir, ensureNoDuplicateConfigs } from './utils/resolve-config'; import { getGitInfo, GitInfo } from './utils/git'; import { normalizeConfig, normalizeRegexPattern, normalizeRootDirPattern } from './utils/normalize'; -import { BestCliOptions, DefaultProjectOptions, GlobalConfig, ProjectConfig, FrozenProjectConfig, FrozenGlobalConfig } from './types'; +import { BestCliOptions, DefaultProjectOptions, FrozenProjectConfig, FrozenGlobalConfig, ProjectConfigs } from './types'; export { BestCliOptions }; function generateProjectConfigs(options: DefaultProjectOptions, isRoot: boolean, gitInfo?: GitInfo): { projectConfig: FrozenProjectConfig, globalConfig: FrozenGlobalConfig | undefined } { @@ -20,7 +20,7 @@ function generateProjectConfigs(options: DefaultProjectOptions, isRoot: boolean, rootProjectName: options.projectName, nonFlagArgs: options.nonFlagArgs, gitInfo: gitInfo, - // outputMetricPattern: normalizeRegexPattern(options.outputMetricNames), + outputMetricPattern: normalizeRegexPattern(options.outputMetricNames), // outputTotals: options.outputTotals, // outputHistograms: options.outputHistograms, // outputHistogramPattern: normalizeRegexPattern(options.outputHistogramNames), @@ -31,7 +31,7 @@ function generateProjectConfigs(options: DefaultProjectOptions, isRoot: boolean, }); } - const projectConfig: ProjectConfig = Object.freeze({ + const projectConfig: FrozenProjectConfig = Object.freeze({ cache: options.cache, cacheDirectory: options.cacheDirectory, useHttp: options.useHttp, @@ -60,7 +60,7 @@ function generateProjectConfigs(options: DefaultProjectOptions, isRoot: boolean, return { globalConfig, projectConfig }; } -export async function readConfig(cliOptions: BestCliOptions, packageRoot: string, parentConfigPath?: string): Promise<{ configPath: string, globalConfig?: GlobalConfig, projectConfig: ProjectConfig }> { +export async function readConfig(cliOptions: BestCliOptions, packageRoot: string, parentConfigPath?: string): Promise<{ configPath: string, globalConfig?: FrozenGlobalConfig, projectConfig: FrozenProjectConfig }> { const configPath = resolveConfigPath(cliOptions.config ? cliOptions.config : packageRoot, process.cwd()); const rawOptions = readConfigAndSetRootDir(configPath); const options = normalizeConfig(rawOptions, cliOptions); @@ -80,7 +80,7 @@ export async function readConfig(cliOptions: BestCliOptions, packageRoot: string return { configPath, globalConfig, projectConfig }; } -export async function getConfigs(projectsFromCLIArgs: string[], cliOptions: BestCliOptions): Promise<{ globalConfig: FrozenGlobalConfig, configs: FrozenProjectConfig[] }> { +export async function getConfigs(projectsFromCLIArgs: string[], cliOptions: BestCliOptions): Promise { let globalConfig: FrozenGlobalConfig | undefined; let configs: FrozenProjectConfig[] = []; let projects: string[] = []; diff --git a/packages/@best/config/src/types.ts b/packages/@best/config/src/types.ts index f4bfabdf..75947c06 100644 --- a/packages/@best/config/src/types.ts +++ b/packages/@best/config/src/types.ts @@ -62,8 +62,14 @@ export interface GlobalConfig { } export interface ProjectConfig { - + cacheDirectory: string; } export type FrozenGlobalConfig = Readonly; export type FrozenProjectConfig = Readonly; + + +export interface ProjectConfigs { + globalConfig: FrozenGlobalConfig, + configs: FrozenProjectConfig[] +} diff --git a/packages/@best/config/src/utils/normalize.ts b/packages/@best/config/src/utils/normalize.ts index 2a9a2db7..0452ecd9 100644 --- a/packages/@best/config/src/utils/normalize.ts +++ b/packages/@best/config/src/utils/normalize.ts @@ -107,11 +107,11 @@ export function normalizeRootDirPattern(str: string, rootDir: string) { return str.replace(//g, rootDir); } -export function normalizeRegexPattern(names: any) { +export function normalizeRegexPattern(names: string | string[] | RegExp) { if (typeof names === 'string') { names = names.split(','); } - if (names instanceof Array) { + if (Array.isArray(names)) { names = names.map(name => name.replace(/\*/g, '.*')); names = new RegExp(`^(${names.join('|')})$`); }