/
index.ts
63 lines (50 loc) · 1.79 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import { LogContexts, Logger } from 'bs-logger'
import type { Arguments } from 'yargs'
import * as yargsParser from 'yargs-parser'
import { rootLogger } from '../utils/logger'
const VALID_COMMANDS = ['help', 'config:migrate', 'config:init']
const logger = rootLogger.child({ [LogContexts.namespace]: 'cli', [LogContexts.application]: 'ts-jest' })
/**
* @internal
*/
export type CliCommand = (argv: Arguments, logger: Logger) => Promise<void>
async function cli(args: string[]): Promise<void> {
const parsedArgv = yargsParser(args, {
boolean: ['dry-run', 'jest-preset', 'allow-js', 'diff', 'babel', 'force', 'jsdom'],
string: ['tsconfig', 'js'],
count: ['verbose'],
alias: { verbose: ['v'] },
default: { jestPreset: true, verbose: 0 },
coerce: {
js(val: string) {
const res = val.trim().toLowerCase()
if (!['babel', 'ts'].includes(res)) throw new Error(`The 'js' option must be 'babel' or 'ts', given: '${val}'.`)
return res
},
},
})
// deprecated
if (parsedArgv.allowJs != null) {
if (parsedArgv.js) throw new Error("The 'allowJs' and 'js' options cannot be set together.")
parsedArgv.js = parsedArgv.allowJs ? 'ts' : undefined
}
let command = parsedArgv._.shift() as string
const isHelp = command === 'help'
if (isHelp) command = parsedArgv._.shift() as string
if (!VALID_COMMANDS.includes(command)) command = 'help'
const { run, help }: { run: CliCommand; help: CliCommand } = require(`./${command.replace(/:/g, '/')}`)
const cmd = isHelp && command !== 'help' ? help : run
return cmd(parsedArgv, logger)
}
/**
* @internal
*/
export async function processArgv(): Promise<void> {
try {
await cli(process.argv.slice(2))
process.exit(0)
} catch (err) {
logger.fatal(err.message)
process.exit(1)
}
}