/
normalizeOptions.js
165 lines (133 loc) · 3.99 KB
/
normalizeOptions.js
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import fs from 'fs';
import path from 'path';
import { createSelector } from 'reselect';
import findBabelConfig from 'find-babel-config';
import glob from 'glob';
import pkgUp from 'pkg-up';
import { escapeRegExp } from './utils';
const defaultExtensions = ['.js', '.jsx', '.es', '.es6', '.mjs'];
const defaultTransformedFunctions = [
'require',
'require.resolve',
'System.import',
// Jest methods
'jest.genMockFromModule',
'jest.mock',
'jest.unmock',
'jest.doMock',
'jest.dontMock',
'jest.setMock',
'jest.requireActual',
'jest.requireMock',
// Older Jest methods
'require.requireActual',
'require.requireMock',
];
function isRegExp(string) {
return string.startsWith('^') || string.endsWith('$');
}
const specialCwd = {
babelrc: startPath => findBabelConfig.sync(startPath).file,
packagejson: startPath => pkgUp.sync({ cwd: startPath }),
};
function normalizeCwd(optsCwd, currentFile) {
let cwd;
if (optsCwd in specialCwd) {
const startPath = currentFile === 'unknown' ? './' : currentFile;
const computedCwd = specialCwd[optsCwd](startPath);
cwd = computedCwd ? path.dirname(computedCwd) : null;
} else {
cwd = optsCwd;
}
return cwd || process.cwd();
}
function normalizeRoot(optsRoot, cwd) {
if (!optsRoot) {
return [];
}
const rootArray = Array.isArray(optsRoot) ? optsRoot : [optsRoot];
return rootArray
.map(dirPath => path.resolve(cwd, dirPath))
.reduce((resolvedDirs, absDirPath) => {
if (glob.hasMagic(absDirPath)) {
const roots = glob
.sync(absDirPath)
.filter(resolvedPath => fs.lstatSync(resolvedPath).isDirectory());
return [...resolvedDirs, ...roots];
}
return [...resolvedDirs, absDirPath];
}, []);
}
function getAliasTarget(key, isKeyRegExp) {
const regExpPattern = isKeyRegExp ? key : `^${escapeRegExp(key)}(/.*|)$`;
return new RegExp(regExpPattern);
}
function getAliasSubstitute(value, isKeyRegExp) {
if (typeof value === 'function') {
return value;
}
if (!isKeyRegExp) {
return ([, match]) => {
// Alias with array of paths
if (Array.isArray(value)) {
return value.map(v => `${v}${match}`);
}
return `${value}${match}`;
};
}
const parts = value.split('\\\\');
return execResult =>
parts
.map(part => part.replace(/\\\d+/g, number => execResult[number.slice(1)] || ''))
.join('\\');
}
function normalizeAlias(optsAlias) {
if (!optsAlias) {
return [];
}
const aliasArray = Array.isArray(optsAlias) ? optsAlias : [optsAlias];
return aliasArray.reduce((aliasPairs, alias) => {
const aliasKeys = Object.keys(alias);
aliasKeys.forEach(key => {
const isKeyRegExp = isRegExp(key);
aliasPairs.push([
getAliasTarget(key, isKeyRegExp),
getAliasSubstitute(alias[key], isKeyRegExp),
]);
});
return aliasPairs;
}, []);
}
function normalizeTransformedFunctions(optsTransformFunctions) {
if (!optsTransformFunctions) {
return defaultTransformedFunctions;
}
return [...defaultTransformedFunctions, ...optsTransformFunctions];
}
function normalizeLoglevel(optsLoglevel) {
return optsLoglevel || 'warn';
}
export default createSelector(
// The currentFile should have an extension; otherwise it's considered a special value
currentFile => (currentFile.includes('.') ? path.dirname(currentFile) : currentFile),
(_, opts) => opts,
(currentFile, opts) => {
const cwd = normalizeCwd(opts.cwd, currentFile);
const root = normalizeRoot(opts.root, cwd);
const alias = normalizeAlias(opts.alias);
const loglevel = normalizeLoglevel(opts.loglevel);
const transformFunctions = normalizeTransformedFunctions(opts.transformFunctions);
const extensions = opts.extensions || defaultExtensions;
const stripExtensions = opts.stripExtensions || extensions;
return {
cwd,
root,
alias,
loglevel,
transformFunctions,
extensions,
stripExtensions,
customResolvePath: opts.resolvePath,
};
}
);