Skip to content

Commit

Permalink
style(cli): more readable code when setting defaults for init
Browse files Browse the repository at this point in the history
  • Loading branch information
huafu committed Oct 6, 2018
1 parent b1eed98 commit 89ad06a
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions src/cli/config/init.ts
Expand Up @@ -30,17 +30,21 @@ export const run: CliCommand = async (args: Arguments /* , logger: Logger */) =>
const pkgJson = hasPackage ? JSON.parse(readFileSync(pkgFile, 'utf8')) : {}

// auto js/babel
let { js, babel } = args
if (js != null || babel != null) {
if (js == null) js = babel ? 'babel' : undefined
else if (babel == null) babel = js === 'babel'
let { js: jsFilesProcessor, babel: shouldPostProcessWithBabel } = args
// set defaults for missing options
if (jsFilesProcessor == null) {
// set default js files processor depending on whether the user wants to post-process with babel
jsFilesProcessor = shouldPostProcessWithBabel ? 'babel' : undefined
} else if (shouldPostProcessWithBabel == null) {
// auto enables babel post-processing if the user wants babel to process js files
shouldPostProcessWithBabel = jsFilesProcessor === 'babel'
}

// preset
let preset: TsJestPresetDescriptor | undefined
if (js === 'babel') {
if (jsFilesProcessor === 'babel') {
preset = jsWIthBabel
} else if (js === 'ts') {
} else if (jsFilesProcessor === 'ts') {
preset = jsWithTs
} else {
preset = defaults
Expand Down Expand Up @@ -70,11 +74,11 @@ export const run: CliCommand = async (args: Arguments /* , logger: Logger */) =>
// package.json config
const base: any = jestPreset ? { preset: preset.name } : { ...preset.value }
if (!jsdom) base.testEnvironment = 'node'
if (tsconfig || babel) {
if (tsconfig || shouldPostProcessWithBabel) {
const tsJestConf: any = {}
base.globals = { 'ts-jest': tsJestConf }
if (tsconfig) tsJestConf.tsconfig = tsconfig
if (babel) tsJestConf.babelConfig = true
if (shouldPostProcessWithBabel) tsJestConf.babelConfig = true
}
body = JSON.stringify({ ...pkgJson, jest: base }, undefined, ' ')
} else {
Expand All @@ -91,11 +95,11 @@ export const run: CliCommand = async (args: Arguments /* , logger: Logger */) =>
}
if (!jsdom) content.push(` testEnvironment: 'node',`)

if (tsconfig || babel) {
if (tsconfig || shouldPostProcessWithBabel) {
content.push(` globals: {`)
content.push(` 'ts-jest': {`)
if (tsconfig) content.push(` tsconfig: ${stringifyJson5(tsconfig)},`)
if (babel) content.push(` babelConfig: true,`)
if (shouldPostProcessWithBabel) content.push(` babelConfig: true,`)
content.push(` },`)
content.push(` },`)
}
Expand Down

0 comments on commit 89ad06a

Please sign in to comment.