/
buildTs.js
98 lines (81 loc) · 2.35 KB
/
buildTs.js
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
/**
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
const fs = require('fs');
const os = require('os');
const path = require('path');
const chalk = require('chalk');
const execa = require('execa');
const rimraf = require('rimraf');
const throat = require('throat');
const {getPackages} = require('./buildUtils');
const packages = getPackages();
const packagesWithTs = packages.filter(p =>
fs.existsSync(path.resolve(p, 'tsconfig.json'))
);
packagesWithTs.forEach(pkgDir => {
const pkg = require(pkgDir + '/package.json');
if (!pkg.types) {
throw new Error(`Package ${pkg.name} is missing \`types\` field`);
}
if (!pkg.typesVersions) {
throw new Error(`Package ${pkg.name} is missing \`typesVersions\` field`);
}
if (pkg.main.replace(/\.js$/, '.d.ts') !== pkg.types) {
throw new Error(
`\`main\` and \`types\` field of ${pkg.name} does not match`
);
}
});
const args = [
'--silent',
'tsc',
'-b',
...packagesWithTs,
...process.argv.slice(2),
];
console.log(chalk.inverse(' Building TypeScript definition files '));
try {
execa.sync('yarn', args, {stdio: 'inherit'});
console.log(
chalk.inverse.green(' Successfully built TypeScript definition files ')
);
} catch (e) {
console.error(
chalk.inverse.red(' Unable to build TypeScript definition files ')
);
console.error(e.stack);
process.exitCode = 1;
return;
}
const downlevelArgs = ['--silent', 'downlevel-dts', 'build', 'build/ts3.4'];
console.log(chalk.inverse(' Downleveling TypeScript definition files '));
// we want to limit the number of processes we spawn
const cpus = Math.max(1, os.cpus().length - 1);
Promise.all(
packagesWithTs.map(
throat(cpus, pkgDir => {
// otherwise we get nested `ts3.4` directories
rimraf.sync(path.resolve(pkgDir, 'build/ts3.4'));
return execa('yarn', downlevelArgs, {cwd: pkgDir, stdio: 'inherit'});
})
)
)
.then(() => {
console.log(
chalk.inverse.green(
' Successfully downleveled TypeScript definition files '
)
);
})
.catch(e => {
console.error(
chalk.inverse.red(' Unable to downlevel TypeScript definition files ')
);
console.error(e.stack);
process.exitCode = 1;
});