UNPKG

3.33 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.addImportStatementToJestConfig = exports.removePropertyFromJestConfig = exports.addPropertyToJestConfig = void 0;
4const devkit_1 = require("@nrwl/devkit");
5const functions_1 = require("./functions");
6/**
7 * Add a property to the jest config
8 * @param host
9 * @param path - path to the jest config file
10 * @param propertyName - Property to update. Can be dot delimited to access deeply nested properties
11 * @param value
12 * @param options - set `valueAsString` option to true if the `value` being passed represents a string of the code that should be associated with the `propertyName`
13 */
14function 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}
30exports.addPropertyToJestConfig = addPropertyToJestConfig;
31/**
32 * Remove a property value from the jest config
33 * @param host
34 * @param path
35 * @param propertyName - Property to remove. Can be dot delimited to access deeply nested properties
36 */
37function 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}
64exports.removePropertyFromJestConfig = removePropertyFromJestConfig;
65function 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}
72exports.addImportStatementToJestConfig = addImportStatementToJestConfig;
73//# sourceMappingURL=update-config.js.map
\No newline at end of file