forked from jestjs/jest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuildTs.mjs
262 lines (225 loc) · 6.95 KB
/
buildTs.mjs
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import {strict as assert} from 'assert';
import * as os from 'os';
import * as path from 'path';
import chalk from 'chalk';
import execa from 'execa';
import {glob} from 'glob';
import fs from 'graceful-fs';
import pLimit from 'p-limit';
import stripJsonComments from 'strip-json-comments';
import {getPackagesWithTsConfig} from './buildUtils.mjs';
const packagesWithTs = getPackagesWithTsConfig();
const {stdout: allWorkspacesString} = await execa('yarn', [
'workspaces',
'list',
'--json',
]);
const workspacesWithTs = new Map(
JSON.parse(`[${allWorkspacesString.split('\n').join(',')}]`)
.filter(({location}) =>
packagesWithTs.some(({packageDir}) => packageDir.endsWith(location)),
)
.map(({location, name}) => [name, location]),
);
for (const {packageDir, pkg} of packagesWithTs) {
assert.ok(pkg.types, `Package ${pkg.name} is missing \`types\` field`);
assert.strictEqual(
pkg.types,
pkg.main.replace(/\.js$/, '.d.ts'),
`\`main\` and \`types\` field of ${pkg.name} does not match`,
);
const jestDependenciesOfPackage = [
...Object.keys(pkg.dependencies || {}),
...Object.keys(pkg.devDependencies || {}),
]
.filter(dep => {
if (!workspacesWithTs.has(dep)) {
return false;
}
// nothing should depend on these
if (dep === 'jest-circus' || dep === 'jest-jasmine2') {
return false;
}
// these are just `require.resolve`-ed
if (pkg.name === 'jest-config') {
if (dep === '@jest/test-sequencer' || dep === 'babel-jest') {
return false;
}
}
// only test files depend on '@jest/test-utils', i.e. it is always a dev dependency
// see additional checks below
if (dep === '@jest/test-utils') {
return false;
}
return true;
})
.map(dep =>
path.relative(
packageDir,
`${packageDir}/../../${workspacesWithTs.get(dep)}`,
),
)
.sort();
if (jestDependenciesOfPackage.length > 0) {
const tsConfig = JSON.parse(
stripJsonComments(fs.readFileSync(`${packageDir}/tsconfig.json`, 'utf8')),
);
const references = tsConfig.references.map(({path}) => path);
assert.deepStrictEqual(
references,
jestDependenciesOfPackage,
`Expected declared references to match dependencies in package ${
pkg.name
}. Got:\n\n${references.join(
'\n',
)}\nExpected:\n\n${jestDependenciesOfPackage.join('\n')}`,
);
}
let hasJestTestUtils = Object.keys(pkg.dependencies || {}).includes(
'@jest/test-utils',
);
if (hasJestTestUtils) {
throw new Error(
chalk.red(
`Package '${pkg.name}' declares '@jest/test-utils' as dependency, but it must be declared as dev dependency`,
),
);
}
hasJestTestUtils = Object.keys(pkg.devDependencies || {}).includes(
'@jest/test-utils',
);
const tsConfigPaths = glob.sync('**/__tests__/tsconfig.json', {
absolute: true,
cwd: packageDir,
});
const testUtilsReferences = tsConfigPaths.filter(tsConfigPath => {
const tsConfig = JSON.parse(
stripJsonComments(fs.readFileSync(tsConfigPath, 'utf8')),
);
return tsConfig.references.some(
({path}) => path && path.endsWith('test-utils'),
);
});
if (hasJestTestUtils && testUtilsReferences.length === 0) {
throw new Error(
chalk.red(
`Package '${
pkg.name
}' declares '@jest/test-utils' as dev dependency, but it is not referenced in:\n\n${tsConfigPaths.join(
'\n',
)}`,
),
);
}
if (!hasJestTestUtils && testUtilsReferences.length > 0) {
throw new Error(
chalk.red(
`Package '${
pkg.name
}' does not declare '@jest/test-utils' as dev dependency, but it is referenced in:\n\n${testUtilsReferences.join(
'\n',
)}`,
),
);
}
}
const args = [
'tsc',
'-b',
...packagesWithTs.map(({packageDir}) => packageDir),
...process.argv.slice(2),
];
console.log(chalk.inverse(' Building TypeScript definition files '));
try {
await execa('yarn', args, {stdio: 'inherit'});
console.log(
chalk.inverse.green(' Successfully built TypeScript definition files '),
);
} catch (error) {
console.error(
chalk.inverse.red(' Unable to build TypeScript definition files '),
);
throw error;
}
console.log(chalk.inverse(' Validating TypeScript definition files '));
// we want to limit the number of processes we spawn
const cpus = Math.max(
1,
(typeof os.availableParallelism === 'function'
? os.availableParallelism()
: os.cpus().length) - 1,
);
const typesReferenceDirective = '/// <reference types';
const typesNodeReferenceDirective = `${typesReferenceDirective}="node" />`;
try {
const mutex = pLimit(cpus);
await Promise.all(
packagesWithTs.map(({packageDir, pkg}) =>
mutex(async () => {
const matched = glob.sync('build/**/*.d.ts', {
absolute: true,
cwd: packageDir,
});
const files = await Promise.all(
matched.map(file =>
Promise.all([file, fs.promises.readFile(file, 'utf8')]),
),
);
const filesWithTypeReferences = files
.filter(([, content]) => content.includes(typesReferenceDirective))
.filter(hit => hit.length > 0);
const filesWithReferences = filesWithTypeReferences
.map(([name, content]) => [
name,
content
.split('\n')
.map(line => line.trim())
.filter(line => line.includes(typesReferenceDirective))
.filter(line => line !== typesNodeReferenceDirective)
.join('\n'),
])
.filter(([, content]) => content.length > 0)
.filter(hit => hit.length > 0)
.map(([file, references]) =>
chalk.red(
`${chalk.bold(
file,
)} has the following non-node type references:\n\n${references}\n`,
),
)
.join('\n\n');
if (filesWithReferences) {
throw new Error(filesWithReferences);
}
const filesWithNodeReference = filesWithTypeReferences.map(
([filename]) => filename,
);
if (filesWithNodeReference.length > 0) {
assert.ok(
pkg.dependencies,
`Package \`${pkg.name}\` is missing \`dependencies\``,
);
assert.strictEqual(
pkg.dependencies['@types/node'],
'*',
`Package \`${pkg.name}\` is missing a dependency on \`@types/node\``,
);
}
}),
),
);
} catch (error) {
console.error(
chalk.inverse.red(' Unable to validate TypeScript definition files '),
);
throw error;
}
console.log(
chalk.inverse.green(' Successfully validated TypeScript definition files '),
);