1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.updateToDefaultExport = exports.updateRootFiles = exports.updateExportsJestConfig = void 0;
|
4 | const devkit_1 = require("@nrwl/devkit");
|
5 | const versions_1 = require("../../utils/versions");
|
6 | const executor_options_utils_1 = require("@nrwl/workspace/src/utilities/executor-options-utils");
|
7 | const tsquery_1 = require("@phenomnomnominal/tsquery");
|
8 | function updateExportsJestConfig(tree) {
|
9 | const { didUpdateRootPreset } = updateRootFiles(tree);
|
10 | let shouldInstallTsNode = false;
|
11 | (0, executor_options_utils_1.forEachExecutorOptions)(tree, '@nrwl/jest:jest', (options) => {
|
12 | if (options.jestConfig && tree.exists(options.jestConfig)) {
|
13 | if (options.jestConfig.endsWith('.ts')) {
|
14 | updateToDefaultExport(tree, options.jestConfig);
|
15 | shouldInstallTsNode = true;
|
16 | }
|
17 | const updatedImport = updateNxPresetImport(tree.read(options.jestConfig, 'utf-8'));
|
18 | tree.write(options.jestConfig, updatedImport);
|
19 |
|
20 | if (didUpdateRootPreset) {
|
21 | const projectConfig = tree.read(options.jestConfig, 'utf-8');
|
22 | const updatedConfig = projectConfig.replace(/(preset:\s*['"][.\/]*)(jest\.preset\.ts)(['"])/g, '$1jest.preset.js$3');
|
23 | tree.write(options.jestConfig, updatedConfig);
|
24 | }
|
25 | }
|
26 | });
|
27 | return shouldInstallTsNode
|
28 | ? (0, devkit_1.addDependenciesToPackageJson)(tree, {}, { 'ts-node': versions_1.tsNodeVersion })
|
29 | : () => { };
|
30 | }
|
31 | exports.updateExportsJestConfig = updateExportsJestConfig;
|
32 | function updateRootFiles(tree) {
|
33 | let didUpdateRootPreset = false;
|
34 | if (tree.exists('jest.config.ts')) {
|
35 | updateToDefaultExport(tree, 'jest.config.ts');
|
36 | }
|
37 | if (tree.exists('jest.preset.ts')) {
|
38 |
|
39 | tree.rename('jest.preset.ts', 'jest.preset.js');
|
40 | didUpdateRootPreset = true;
|
41 | }
|
42 | if (tree.exists('jest.preset.js')) {
|
43 | const newContents = updateNxPresetImport(tree.read('jest.preset.js', 'utf-8'));
|
44 | tree.write('jest.preset.js', newContents);
|
45 | }
|
46 | return {
|
47 | didUpdateRootPreset,
|
48 | };
|
49 | }
|
50 | exports.updateRootFiles = updateRootFiles;
|
51 | function updateNxPresetImport(fileContents) {
|
52 | return fileContents.replace(/require\(['"]@nrwl\/jest\/preset['"]\)[;\s]*?[\n\r]/g, `require('@nrwl/jest/preset').default;
|
53 | `);
|
54 | }
|
55 | function updateToDefaultExport(tree, filePath) {
|
56 | const newConfig = tsquery_1.tsquery.replace(tree.read(filePath, 'utf-8'), 'ExpressionStatement BinaryExpression', (node) => {
|
57 | if (node.left.getText() === 'module.exports') {
|
58 | return `export default ${node.right.getText()}`;
|
59 | }
|
60 | return node.getText();
|
61 | });
|
62 | tree.write(filePath, newConfig);
|
63 | }
|
64 | exports.updateToDefaultExport = updateToDefaultExport;
|
65 | exports.default = updateExportsJestConfig;
|
66 |
|
\ | No newline at end of file |