-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
Copy pathcompile-tsc.js
95 lines (79 loc) · 2.27 KB
/
compile-tsc.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
/* eslint-disable no-console */
const fs = require('fs-extra');
const path = require('path');
const execa = require('execa');
function getCommand(watch) {
const args = [
'--outDir ./dist/ts3.9',
'--listEmittedFiles false',
'--declaration true',
'--noErrorTruncation',
'--pretty',
];
/**
* Only emit declarations if it does not need to be compiled with tsc
* Currently, angular and storyshots (that contains an angular component) need to be compiled
* with tsc. (see comments in compile-babel.js)
*/
const isAngular = process.cwd().includes(path.join('app', 'angular'));
const isStoryshots = process.cwd().includes(path.join('addons', 'storyshots'));
if (!isAngular && !isStoryshots) {
args.push('--emitDeclarationOnly');
}
if (watch) {
args.push('-w', '--preserveWatchOutput');
}
return `yarn run -T tsc ${args.join(' ')}`;
}
function handleExit(code, stderr, errorCallback) {
if (code !== 0) {
if (errorCallback && typeof errorCallback === 'function') {
errorCallback(stderr);
}
process.exit(code);
}
}
async function run({ watch, silent, errorCallback }) {
return new Promise((resolve, reject) => {
const command = getCommand(watch);
const child = execa.command(command, {
buffer: false,
});
let stderr = '';
if (watch) {
child.stdout.pipe(process.stdout);
child.stderr.pipe(process.stderr);
} else {
child.stderr.on('data', (data) => {
stderr += data.toString();
});
child.stdout.on('data', (data) => {
stderr += data.toString();
});
}
child.on('exit', (code) => {
resolve();
handleExit(code, stderr, errorCallback);
});
});
}
async function tscfy(options = {}) {
const { watch = false, silent = false, errorCallback } = options;
const tsConfigFile = 'tsconfig.json';
if (!(await fs.pathExists(tsConfigFile))) {
if (!silent) {
console.log(`No ${tsConfigFile}`);
}
return;
}
const tsConfig = await fs.readJSON(tsConfigFile);
if (!(tsConfig && tsConfig.lerna && tsConfig.lerna.disabled === true)) {
await run({ watch, silent, errorCallback });
}
if (!watch) {
await execa.command('yarn run -T downlevel-dts dist/ts3.9 dist/ts3.4');
}
}
module.exports = {
tscfy,
};