/
backports.ts
132 lines (117 loc) · 3.73 KB
/
backports.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import { LogContexts, Logger } from 'bs-logger'
import { Deprecateds, Helps, interpolate } from './messages'
const context = { [LogContexts.namespace]: 'backports' }
/**
* @internal
*/
export const backportJestConfig = <T extends jest.InitialOptions | jest.ProjectConfig>(
logger: Logger,
config: T,
): T => {
logger.debug({ ...context, config }, 'backporting config')
const { globals = {} } = (config || {}) as any
const { 'ts-jest': tsJest = {} } = globals as any
const mergeTsJest: any = {}
let hadWarnings = false
const warnConfig = (oldPath: string, newPath: string, note?: string) => {
hadWarnings = true
logger.warn(
context,
interpolate(note ? Deprecateds.ConfigOptionWithNote : Deprecateds.ConfigOption, {
oldPath,
newPath,
note,
}),
)
}
if ('__TS_CONFIG__' in globals) {
warnConfig('globals.__TS_CONFIG__', 'globals.ts-jest.tsConfig')
if (typeof globals.__TS_CONFIG__ === 'object') {
mergeTsJest.tsConfig = globals.__TS_CONFIG__
}
delete globals.__TS_CONFIG__
}
if ('__TRANSFORM_HTML__' in globals) {
warnConfig('globals.__TRANSFORM_HTML__', 'globals.ts-jest.stringifyContentPathRegex')
if (globals.__TRANSFORM_HTML__) {
mergeTsJest.stringifyContentPathRegex = '\\.html?$'
}
delete globals.__TRANSFORM_HTML__
}
if ('typeCheck' in tsJest) {
warnConfig('globals.ts-jest.typeCheck', 'globals.ts-jest.isolatedModules')
mergeTsJest.isolatedModules = !tsJest.typeCheck
delete tsJest.typeCheck
}
if ('tsConfigFile' in tsJest) {
warnConfig('globals.ts-jest.tsConfigFile', 'globals.ts-jest.tsConfig')
if (tsJest.tsConfigFile) {
mergeTsJest.tsConfig = tsJest.tsConfigFile
}
delete tsJest.tsConfigFile
}
if ('enableTsDiagnostics' in tsJest) {
warnConfig('globals.ts-jest.enableTsDiagnostics', 'globals.ts-jest.diagnostics')
if (tsJest.enableTsDiagnostics) {
mergeTsJest.diagnostics = { warnOnly: true }
if (typeof tsJest.enableTsDiagnostics === 'string') mergeTsJest.diagnostics.pathRegex = tsJest.enableTsDiagnostics
} else {
mergeTsJest.diagnostics = false
}
delete tsJest.enableTsDiagnostics
}
if ('useBabelrc' in tsJest) {
warnConfig('globals.ts-jest.useBabelrc', 'globals.ts-jest.babelConfig', Deprecateds.ConfigOptionUseBabelRcNote)
if (tsJest.useBabelrc != null) {
mergeTsJest.babelConfig = tsJest.useBabelrc ? true : {}
}
delete tsJest.useBabelrc
}
// if ('babelConfig' in tsJest) {
// warnConfig('globals.ts-jest.babelConfig', 'globals.ts-jest.babelConfig')
// if (tsJest.babelConfig != null) {
// mergeTsJest.babelConfig = tsJest.babelConfig
// }
// delete tsJest.babelConfig
// }
if ('skipBabel' in tsJest) {
warnConfig('globals.ts-jest.skipBabel', 'globals.ts-jest.babelConfig')
if (tsJest.skipBabel === false && !mergeTsJest.babelConfig) {
mergeTsJest.babelConfig = true
}
delete tsJest.skipBabel
}
// if we had some warnings we can inform the user about the CLI tool
if (hadWarnings) {
logger.warn(context, Helps.MigrateConfigUsingCLI)
}
return {
...(config as any),
globals: {
...globals,
'ts-jest': {
...mergeTsJest,
...tsJest,
},
},
}
}
/**
* @internal
*/
export const backportTsJestDebugEnvVar = (logger: Logger) => {
if ('TS_JEST_DEBUG' in process.env) {
const shouldLog = !/^\s*(?:0|f(?:alse)?|no?|disabled?|off|)\s*$/i.test(process.env.TS_JEST_DEBUG || '')
delete process.env.TS_JEST_DEBUG
if (shouldLog) {
process.env.TS_JEST_LOG = `ts-jest.log,stderr:warn`
}
logger.warn(
context,
interpolate(Deprecateds.EnvVar, {
old: 'TS_JEST_DEBUG',
new: 'TS_JEST_LOG',
}),
)
}
}