-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
update-8-3-0.ts
140 lines (125 loc) · 3.68 KB
/
update-8-3-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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import {
chain,
Rule,
SchematicContext,
Tree
} from '@angular-devkit/schematics';
import { stripIndents } from '@angular-devkit/core/src/utils/literals';
import {
formatFiles,
insert,
readJsonInTree,
updateJsonInTree,
updatePackageJsonDependencies
} from '@nrwl/workspace';
import {
createSourceFile,
isImportDeclaration,
isStringLiteral,
ScriptTarget
} from 'typescript';
import { ReplaceChange } from '@nrwl/workspace/src/utils/ast-utils';
import { relative } from 'path';
import { testingLibraryVersion } from '../../utils/versions';
const ignore = require('ignore');
export default function update(): Rule {
return chain([
displayInformation,
updateDependencies,
updateImports,
formatFiles()
]);
}
function displayInformation(host: Tree, context: SchematicContext) {
context.logger.info(stripIndents`
React Testing Library has been repackaged as \`@testing-library/react\` as of version 8.
We are replacing your \`react-testing-library\` imports with \`@testing-library/react\`.
See: https://github.com/testing-library/react-testing-library/releases/tag/v8.0.0
`);
}
function updateDependencies(tree: Tree) {
const removeOld = updateJsonInTree(
'package.json',
(json, context: SchematicContext) => {
json.dependencies = json.dependencies || {};
json.devDependencies = json.devDependencies || {};
// Just in case user installed it to dependencies instead of devDependencies.
delete json.dependencies['react-testing-library'];
delete json.devDependencies['react-testing-library'];
context.logger.info(`Removing \`react-testing-library\` as a dependency`);
return json;
}
);
const packageJson = readJsonInTree(tree, '/package.json');
const candidates = {
react: '16.8.6',
'react-dom': '16.8.6',
'react-router-dom': '5.0.1',
'@types/react': '16.8.23',
'@types/react-dom': '16.8.23',
'styled-components': '4.3.2',
'@types/styled-components': '4.1.18',
'@emotion/styled': '10.0.14'
};
const updated = Object.entries(candidates).reduce(
(acc, [m, v]) => {
if (packageJson.dependencies[m]) {
acc.dependencies[m] = v;
} else if (packageJson.devDependencies[m]) {
acc.devDependencies[m] = v;
}
return acc;
},
{
dependencies: {},
devDependencies: {
'@testing-library/react': testingLibraryVersion
}
}
);
const addNew = updatePackageJsonDependencies(
updated.dependencies,
updated.devDependencies
);
return chain([removeOld, addNew]);
}
function updateImports(host: Tree) {
let ig = ignore().add(['*', '!*.ts', '!*.tsx']); // include only .tsx? files
if (host.exists('.gitignore')) {
ig = ig.add(host.read('.gitignore').toString());
}
host.visit(path => {
if (ig.ignores(relative('/', path))) {
return;
}
const sourceFile = createSourceFile(
path,
host.read(path).toString(),
ScriptTarget.Latest,
true
);
const changes = [];
sourceFile.statements.forEach(statement => {
if (
isImportDeclaration(statement) &&
isStringLiteral(statement.moduleSpecifier)
) {
const nodeText = statement.moduleSpecifier.getText(sourceFile);
const modulePath = statement.moduleSpecifier
.getText(sourceFile)
.substr(1, nodeText.length - 2);
if (modulePath === 'react-testing-library') {
changes.push(
new ReplaceChange(
path,
statement.moduleSpecifier.getStart(sourceFile),
nodeText,
`'@testing-library/react'`
)
);
}
}
});
insert(host, path, changes);
});
}