1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, '__esModule', {
|
4 | value: true
|
5 | });
|
6 | exports.default = void 0;
|
7 |
|
8 | function path() {
|
9 | const data = _interopRequireWildcard(require('path'));
|
10 |
|
11 | path = function () {
|
12 | return data;
|
13 | };
|
14 |
|
15 | return data;
|
16 | }
|
17 |
|
18 | function fs() {
|
19 | const data = _interopRequireWildcard(require('graceful-fs'));
|
20 |
|
21 | fs = function () {
|
22 | return data;
|
23 | };
|
24 |
|
25 | return data;
|
26 | }
|
27 |
|
28 | function _chalk() {
|
29 | const data = _interopRequireDefault(require('chalk'));
|
30 |
|
31 | _chalk = function () {
|
32 | return data;
|
33 | };
|
34 |
|
35 | return data;
|
36 | }
|
37 |
|
38 | function _prompts() {
|
39 | const data = _interopRequireDefault(require('prompts'));
|
40 |
|
41 | _prompts = function () {
|
42 | return data;
|
43 | };
|
44 |
|
45 | return data;
|
46 | }
|
47 |
|
48 | function _realpathNative() {
|
49 | const data = require('realpath-native');
|
50 |
|
51 | _realpathNative = function () {
|
52 | return data;
|
53 | };
|
54 |
|
55 | return data;
|
56 | }
|
57 |
|
58 | function _jestConfig() {
|
59 | const data = require('jest-config');
|
60 |
|
61 | _jestConfig = function () {
|
62 | return data;
|
63 | };
|
64 |
|
65 | return data;
|
66 | }
|
67 |
|
68 | var _questions = _interopRequireWildcard(require('./questions'));
|
69 |
|
70 | var _errors = require('./errors');
|
71 |
|
72 | var _generate_config_file = _interopRequireDefault(
|
73 | require('./generate_config_file')
|
74 | );
|
75 |
|
76 | var _modify_package_json = _interopRequireDefault(
|
77 | require('./modify_package_json')
|
78 | );
|
79 |
|
80 | function _interopRequireDefault(obj) {
|
81 | return obj && obj.__esModule ? obj : {default: obj};
|
82 | }
|
83 |
|
84 | function _getRequireWildcardCache() {
|
85 | if (typeof WeakMap !== 'function') return null;
|
86 | var cache = new WeakMap();
|
87 | _getRequireWildcardCache = function () {
|
88 | return cache;
|
89 | };
|
90 | return cache;
|
91 | }
|
92 |
|
93 | function _interopRequireWildcard(obj) {
|
94 | if (obj && obj.__esModule) {
|
95 | return obj;
|
96 | }
|
97 | if (obj === null || (typeof obj !== 'object' && typeof obj !== 'function')) {
|
98 | return {default: obj};
|
99 | }
|
100 | var cache = _getRequireWildcardCache();
|
101 | if (cache && cache.has(obj)) {
|
102 | return cache.get(obj);
|
103 | }
|
104 | var newObj = {};
|
105 | var hasPropertyDescriptor =
|
106 | Object.defineProperty && Object.getOwnPropertyDescriptor;
|
107 | for (var key in obj) {
|
108 | if (Object.prototype.hasOwnProperty.call(obj, key)) {
|
109 | var desc = hasPropertyDescriptor
|
110 | ? Object.getOwnPropertyDescriptor(obj, key)
|
111 | : null;
|
112 | if (desc && (desc.get || desc.set)) {
|
113 | Object.defineProperty(newObj, key, desc);
|
114 | } else {
|
115 | newObj[key] = obj[key];
|
116 | }
|
117 | }
|
118 | }
|
119 | newObj.default = obj;
|
120 | if (cache) {
|
121 | cache.set(obj, newObj);
|
122 | }
|
123 | return newObj;
|
124 | }
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 | const {
|
133 | JEST_CONFIG_BASE_NAME,
|
134 | JEST_CONFIG_EXT_MJS,
|
135 | JEST_CONFIG_EXT_JS,
|
136 | JEST_CONFIG_EXT_ORDER,
|
137 | PACKAGE_JSON
|
138 | } = _jestConfig().constants;
|
139 |
|
140 | const getConfigFilename = ext => JEST_CONFIG_BASE_NAME + ext;
|
141 |
|
142 | var _default = async (rootDir = (0, _realpathNative().sync)(process.cwd())) => {
|
143 |
|
144 | const projectPackageJsonPath = path().join(rootDir, PACKAGE_JSON);
|
145 |
|
146 | if (!fs().existsSync(projectPackageJsonPath)) {
|
147 | throw new _errors.NotFoundPackageJsonError(rootDir);
|
148 | }
|
149 |
|
150 | const questions = _questions.default.slice(0);
|
151 |
|
152 | let hasJestProperty = false;
|
153 | let projectPackageJson;
|
154 |
|
155 | try {
|
156 | projectPackageJson = JSON.parse(
|
157 | fs().readFileSync(projectPackageJsonPath, 'utf-8')
|
158 | );
|
159 | } catch (error) {
|
160 | throw new _errors.MalformedPackageJsonError(projectPackageJsonPath);
|
161 | }
|
162 |
|
163 | if (projectPackageJson.jest) {
|
164 | hasJestProperty = true;
|
165 | }
|
166 |
|
167 | const existingJestConfigPath = JEST_CONFIG_EXT_ORDER.find(ext =>
|
168 | fs().existsSync(path().join(rootDir, getConfigFilename(ext)))
|
169 | );
|
170 | const jestConfigPath =
|
171 | existingJestConfigPath ||
|
172 | path().join(
|
173 | rootDir,
|
174 | getConfigFilename(
|
175 | projectPackageJson.type === 'module'
|
176 | ? JEST_CONFIG_EXT_MJS
|
177 | : JEST_CONFIG_EXT_JS
|
178 | )
|
179 | );
|
180 |
|
181 | if (hasJestProperty || existingJestConfigPath) {
|
182 | const result = await (0, _prompts().default)({
|
183 | initial: true,
|
184 | message:
|
185 | 'It seems that you already have a jest configuration, do you want to override it?',
|
186 | name: 'continue',
|
187 | type: 'confirm'
|
188 | });
|
189 |
|
190 | if (!result.continue) {
|
191 | console.log();
|
192 | console.log('Aborting...');
|
193 | return;
|
194 | }
|
195 | }
|
196 |
|
197 | if (
|
198 | !projectPackageJson.scripts ||
|
199 | projectPackageJson.scripts.test !== 'jest'
|
200 | ) {
|
201 | questions.unshift(_questions.testScriptQuestion);
|
202 | }
|
203 |
|
204 | console.log();
|
205 | console.log(
|
206 | _chalk().default.underline(
|
207 | `The following questions will help Jest to create a suitable configuration for your project\n`
|
208 | )
|
209 | );
|
210 | let promptAborted = false;
|
211 |
|
212 | const results = await (0, _prompts().default)(questions, {
|
213 | onCancel: () => {
|
214 | promptAborted = true;
|
215 | }
|
216 | });
|
217 |
|
218 | if (promptAborted) {
|
219 | console.log();
|
220 | console.log('Aborting...');
|
221 | return;
|
222 | }
|
223 |
|
224 | const shouldModifyScripts = results.scripts;
|
225 |
|
226 | if (shouldModifyScripts || hasJestProperty) {
|
227 | const modifiedPackageJson = (0, _modify_package_json.default)({
|
228 | projectPackageJson,
|
229 | shouldModifyScripts
|
230 | });
|
231 | fs().writeFileSync(projectPackageJsonPath, modifiedPackageJson);
|
232 | console.log('');
|
233 | console.log(
|
234 | `✏️ Modified ${_chalk().default.cyan(projectPackageJsonPath)}`
|
235 | );
|
236 | }
|
237 |
|
238 | const generatedConfig = (0, _generate_config_file.default)(
|
239 | results,
|
240 | projectPackageJson.type === 'module' ||
|
241 | jestConfigPath.endsWith(JEST_CONFIG_EXT_MJS)
|
242 | );
|
243 | fs().writeFileSync(jestConfigPath, generatedConfig);
|
244 | console.log('');
|
245 | console.log(
|
246 | `📝 Configuration file created at ${_chalk().default.cyan(jestConfigPath)}`
|
247 | );
|
248 | };
|
249 |
|
250 | exports.default = _default;
|