import { program } from 'commander'; import { resolve } from 'path'; import { builtinModules } from 'module'; import { error } from './feedback'; const dedupe = (item: string, pos: number, list: string[]): boolean => list.indexOf(item) === pos; export interface Config { src: string; cwd: string; ignoredDependencies: string[], runtimeDependencies: string[], debug: boolean; testMatch: string[], } export const defaultConfig: Config = { src: 'src', cwd: process.cwd(), ignoredDependencies: [...builtinModules], runtimeDependencies: [], debug: true, testMatch: [ '**/__tests__/**/*.?(m|c)[jt]s?(x)', '**/__stories__/**/*.?(m|c)[jt]s?(x)', '**/?(*.)+(spec|specs|test|tests|story|stories).?(m|c)[jt]s?(x)'], }; export function getCommand(argv: string[] = process.argv): Config { const defaultCwd = process.cwd(); program.option('--cwd ', 'Execution folder', process.cwd()); program.option('--src ', 'Source folder', defaultConfig.src); program.option('--ignoredDependencies ', 'Packages that are used in the `src` folder (e.g. `import fs from "fs"`) but do not need to be added to the `dependencies` section of the `package.json`.', defaultConfig.ignoredDependencies.join(' ')); program.option('--runtimeDependencies ', 'Packages that are not used in an import statement in the `src` folder but still need to be specified in the `dependencies` section of the `package.json`.', defaultConfig.runtimeDependencies.join(' ')); program.option('--testMatch ', 'The glob patterns uses to detect test files.', defaultConfig.testMatch.join(' ')); program.parse(argv); const options = program.opts(); let { cwd } = options; if (cwd !== defaultCwd) { cwd = resolve('.', cwd); } const src = resolve(cwd, options.src); let ignoredDependencies = defaultConfig.ignoredDependencies || []; if (Array.isArray(options.ignoredDependencies)) { ignoredDependencies = options.ignoredDependencies as string[]; } let runtimeDependencies = defaultConfig.runtimeDependencies || []; if (Array.isArray(options.runtimeDependencies)) { runtimeDependencies = options.runtimeDependencies as string[]; } let testMatch = defaultConfig.testMatch || []; if (Array.isArray(options.testMatch)) { testMatch = options.testMatch as string[]; } const debug: boolean = options.debug === true; if (!src) { error('Missing --src parameter'); } return { src, debug, cwd, ignoredDependencies: ignoredDependencies.filter(dedupe), runtimeDependencies: runtimeDependencies.filter(dedupe), testMatch: testMatch.filter(dedupe), }; }