UNPKG

4.89 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, '__esModule', {
4 value: true
5});
6exports.isJSONString =
7 exports._replaceRootDirTags =
8 exports.replaceRootDirInPath =
9 exports.escapeGlobCharacters =
10 exports.resolve =
11 exports.DOCUMENTATION_NOTE =
12 exports.BULLET =
13 void 0;
14
15function path() {
16 const data = _interopRequireWildcard(require('path'));
17
18 path = function () {
19 return data;
20 };
21
22 return data;
23}
24
25function _chalk() {
26 const data = _interopRequireDefault(require('chalk'));
27
28 _chalk = function () {
29 return data;
30 };
31
32 return data;
33}
34
35function _jestResolve() {
36 const data = _interopRequireDefault(require('jest-resolve'));
37
38 _jestResolve = function () {
39 return data;
40 };
41
42 return data;
43}
44
45function _jestValidate() {
46 const data = require('jest-validate');
47
48 _jestValidate = function () {
49 return data;
50 };
51
52 return data;
53}
54
55function _interopRequireDefault(obj) {
56 return obj && obj.__esModule ? obj : {default: obj};
57}
58
59function _getRequireWildcardCache(nodeInterop) {
60 if (typeof WeakMap !== 'function') return null;
61 var cacheBabelInterop = new WeakMap();
62 var cacheNodeInterop = new WeakMap();
63 return (_getRequireWildcardCache = function (nodeInterop) {
64 return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
65 })(nodeInterop);
66}
67
68function _interopRequireWildcard(obj, nodeInterop) {
69 if (!nodeInterop && obj && obj.__esModule) {
70 return obj;
71 }
72 if (obj === null || (typeof obj !== 'object' && typeof obj !== 'function')) {
73 return {default: obj};
74 }
75 var cache = _getRequireWildcardCache(nodeInterop);
76 if (cache && cache.has(obj)) {
77 return cache.get(obj);
78 }
79 var newObj = {};
80 var hasPropertyDescriptor =
81 Object.defineProperty && Object.getOwnPropertyDescriptor;
82 for (var key in obj) {
83 if (key !== 'default' && Object.prototype.hasOwnProperty.call(obj, key)) {
84 var desc = hasPropertyDescriptor
85 ? Object.getOwnPropertyDescriptor(obj, key)
86 : null;
87 if (desc && (desc.get || desc.set)) {
88 Object.defineProperty(newObj, key, desc);
89 } else {
90 newObj[key] = obj[key];
91 }
92 }
93 }
94 newObj.default = obj;
95 if (cache) {
96 cache.set(obj, newObj);
97 }
98 return newObj;
99}
100
101/**
102 * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
103 *
104 * This source code is licensed under the MIT license found in the
105 * LICENSE file in the root directory of this source tree.
106 */
107const BULLET = _chalk().default.bold('\u25cf ');
108
109exports.BULLET = BULLET;
110const DOCUMENTATION_NOTE = ` ${_chalk().default.bold(
111 'Configuration Documentation:'
112)}
113 https://jestjs.io/docs/configuration
114`;
115exports.DOCUMENTATION_NOTE = DOCUMENTATION_NOTE;
116
117const createValidationError = message =>
118 new (_jestValidate().ValidationError)(
119 `${BULLET}Validation Error`,
120 message,
121 DOCUMENTATION_NOTE
122 );
123
124const resolve = (resolver, {key, filePath, rootDir, optional}) => {
125 const module = _jestResolve().default.findNodeModule(
126 replaceRootDirInPath(rootDir, filePath),
127 {
128 basedir: rootDir,
129 resolver: resolver || undefined
130 }
131 );
132
133 if (!module && !optional) {
134 throw createValidationError(` Module ${_chalk().default.bold(
135 filePath
136 )} in the ${_chalk().default.bold(key)} option was not found.
137 ${_chalk().default.bold('<rootDir>')} is: ${rootDir}`);
138 } /// can cast as string since nulls will be thrown
139
140 return module;
141};
142
143exports.resolve = resolve;
144
145const escapeGlobCharacters = path => path.replace(/([()*{}\[\]!?\\])/g, '\\$1');
146
147exports.escapeGlobCharacters = escapeGlobCharacters;
148
149const replaceRootDirInPath = (rootDir, filePath) => {
150 if (!/^<rootDir>/.test(filePath)) {
151 return filePath;
152 }
153
154 return path().resolve(
155 rootDir,
156 path().normalize('./' + filePath.substr('<rootDir>'.length))
157 );
158};
159
160exports.replaceRootDirInPath = replaceRootDirInPath;
161
162const _replaceRootDirInObject = (rootDir, config) => {
163 const newConfig = {};
164
165 for (const configKey in config) {
166 newConfig[configKey] =
167 configKey === 'rootDir'
168 ? config[configKey]
169 : _replaceRootDirTags(rootDir, config[configKey]);
170 }
171
172 return newConfig;
173};
174
175const _replaceRootDirTags = (rootDir, config) => {
176 if (config == null) {
177 return config;
178 }
179
180 switch (typeof config) {
181 case 'object':
182 if (Array.isArray(config)) {
183 /// can be string[] or {}[]
184 return config.map(item => _replaceRootDirTags(rootDir, item));
185 }
186
187 if (config instanceof RegExp) {
188 return config;
189 }
190
191 return _replaceRootDirInObject(rootDir, config);
192
193 case 'string':
194 return replaceRootDirInPath(rootDir, config);
195 }
196
197 return config;
198};
199
200exports._replaceRootDirTags = _replaceRootDirTags;
201
202// newtype
203const isJSONString = text =>
204 text != null &&
205 typeof text === 'string' &&
206 text.startsWith('{') &&
207 text.endsWith('}');
208
209exports.isJSONString = isJSONString;