1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.addImportStatementToJestConfig = exports.removePropertyFromJestConfig = exports.addPropertyToJestConfig = void 0;
|
4 | const devkit_1 = require("@nrwl/devkit");
|
5 | const functions_1 = require("./functions");
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | function addPropertyToJestConfig(host, path, propertyName, value, options = { valueAsString: false }) {
|
15 | if (!host.exists(path)) {
|
16 | throw new Error(`Cannot find '${path}' in your workspace.`);
|
17 | }
|
18 | try {
|
19 | const configObject = (0, functions_1.jestConfigObjectAst)(host.read(path, 'utf-8'));
|
20 | const properties = propertyName.split('.');
|
21 | (0, functions_1.addOrUpdateProperty)(host, configObject, properties, options.valueAsString ? value : JSON.stringify(value), path);
|
22 | }
|
23 | catch (e) {
|
24 | devkit_1.logger.info(`NX Please manually update ${path}`);
|
25 | devkit_1.logger.warn(`Could not automatically add the following property to ${path}:`);
|
26 | devkit_1.logger.warn(`${propertyName}: ${JSON.stringify(value)}`);
|
27 | devkit_1.logger.warn(`Error: ${e.message}`);
|
28 | }
|
29 | }
|
30 | exports.addPropertyToJestConfig = addPropertyToJestConfig;
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 | function removePropertyFromJestConfig(host, path, propertyName) {
|
38 | if (!host.exists(path)) {
|
39 | throw new Error(`Cannot find '${path}' in your workspace.`);
|
40 | }
|
41 | try {
|
42 | const configObject = (0, functions_1.jestConfigObjectAst)(host.read(path, 'utf-8'));
|
43 | const propertyAssignment = (0, functions_1.removeProperty)(configObject, propertyName.split('.'));
|
44 | if (propertyAssignment) {
|
45 | const file = host.read(path, 'utf-8');
|
46 | const commaNeeded = file[propertyAssignment.end] === ',';
|
47 | const updatedFile = (0, devkit_1.applyChangesToString)(file, [
|
48 | {
|
49 | type: devkit_1.ChangeType.Delete,
|
50 | start: propertyAssignment.getStart(),
|
51 | length: `${propertyAssignment.getText()}${commaNeeded ? ',' : ''}`
|
52 | .length,
|
53 | },
|
54 | ]);
|
55 | host.write(path, updatedFile);
|
56 | return;
|
57 | }
|
58 | }
|
59 | catch (e) {
|
60 | devkit_1.logger.info(`NX Please manually update ${path}`);
|
61 | devkit_1.logger.warn(`Could not automatically remove the '${propertyName}' property from ${path}:`);
|
62 | }
|
63 | }
|
64 | exports.removePropertyFromJestConfig = removePropertyFromJestConfig;
|
65 | function addImportStatementToJestConfig(host, path, importStatement) {
|
66 | const currentContents = host.read(path, 'utf-8');
|
67 | const newContents = `${importStatement}
|
68 |
|
69 | ${currentContents}`;
|
70 | host.write(path, newContents);
|
71 | }
|
72 | exports.addImportStatementToJestConfig = addImportStatementToJestConfig;
|
73 |
|
\ | No newline at end of file |