forked from nrwl/nx
/
update-8-8-0.ts
119 lines (104 loc) · 3.45 KB
/
update-8-8-0.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import {
Tree,
Rule,
chain,
SchematicContext
} from '@angular-devkit/schematics';
import { readWorkspace, insert, formatFiles } from '@nrwl/workspace';
import * as ts from 'typescript';
import {
ReplaceChange,
updateJsonInTree,
readJsonInTree,
getSourceNodes
} from '@nrwl/workspace/src/utils/ast-utils';
import { stripIndents } from '@angular-devkit/core/src/utils/literals';
export default function update(): Rule {
return chain([
displayInformation,
updateDependencies,
updateJestConfigs,
formatFiles()
]);
}
function displayInformation(host: Tree, context: SchematicContext) {
const config = readJsonInTree(host, 'package.json');
if (config.devDependencies && config.devDependencies['jest-preset-angular']) {
context.logger.info(stripIndents`
\`jest-preset-angular\` 8.0.0 has restructured folders, introducing breaking changes to
jest.config.js files.
We are updating snapshotSerializers in each Angular project to include appropriate paths.
See: https://github.com/thymikee/jest-preset-angular/releases/tag/v8.0.0
`);
}
}
const updateDependencies = updateJsonInTree('package.json', json => {
json.devDependencies = json.devDependencies || {};
if (json.devDependencies['jest-preset-angular']) {
json.devDependencies['jest-preset-angular'] = '8.0.0';
}
return json;
});
function updateJestConfigs(host: Tree) {
const config = readJsonInTree(host, 'package.json');
if (config.devDependencies && config.devDependencies['jest-preset-angular']) {
const workspaceConfig = readWorkspace(host);
const jestConfigsToUpdate = [];
Object.keys(workspaceConfig.projects).forEach(name => {
const project = workspaceConfig.projects[name];
if (
project.architect &&
project.architect.test &&
project.architect.test.builder === '@nrwl/jest:jest' &&
project.architect.test.options &&
project.architect.test.options.jestConfig ===
project.root + 'jest.config.js'
) {
jestConfigsToUpdate.push(project.root + 'jest.config.js');
}
});
jestConfigsToUpdate.forEach(configPath => {
if (host.exists(configPath)) {
const contents = host.read(configPath).toString();
const sourceFile = ts.createSourceFile(
configPath,
contents,
ts.ScriptTarget.Latest
);
const changes: ReplaceChange[] = [];
getSourceNodes(sourceFile).forEach(node => {
if (node && ts.isStringLiteral(node)) {
const nodeText = node.text;
if (
nodeText === 'jest-preset-angular/AngularSnapshotSerializer.js'
) {
changes.push(
new ReplaceChange(
configPath,
node.getStart(sourceFile) + 1,
nodeText,
'jest-preset-angular/build/AngularSnapshotSerializer.js'
)
);
}
if (nodeText === 'jest-preset-angular/HTMLCommentSerializer.js') {
changes.push(
new ReplaceChange(
configPath,
node.getStart(sourceFile) + 1,
nodeText,
'jest-preset-angular/build/HTMLCommentSerializer.js'
)
);
}
}
});
insert(
host,
configPath,
changes.sort((a, b) => (a.order > b.order ? -1 : 1))
);
}
});
}
}