-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathcompile.js
61 lines (49 loc) Β· 1.62 KB
/
compile.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
'use strict';
const {EOL} = require(`os`);
const path = require(`path`);
const ts = require(`typescript`);
/**
* @param {string} tsConfigPath
* @param {string} folder
*/
function compile(tsConfigPath, folder, ...opts) {
const emitDeclarationOnly = opts.includes(`--emitDeclarationOnly`);
const inline = opts.includes(`--inline`);
const parsedConfig = ts.parseJsonConfigFileContent({
extends: tsConfigPath,
compilerOptions: {
rootDir: `sources`,
outDir: inline ? `sources` : `lib`,
emitDeclarationOnly,
},
include: [`sources/**/*.ts`, `sources/**/*.tsx`],
}, ts.sys, folder);
const program = ts.createProgram({
options: parsedConfig.options,
rootNames: parsedConfig.fileNames,
configFileParsingDiagnostics: parsedConfig.errors,
});
const diagnostics = program.emit();
return reportErrors(diagnostics.diagnostics);
}
exports.compile = compile;
/**
* @param {readonly import('typescript').Diagnostic[]} allDiagnostics
*/
function reportErrors(allDiagnostics) {
const errorsAndWarnings = allDiagnostics.filter(d => {
return d.category !== ts.DiagnosticCategory.Message;
});
if (errorsAndWarnings.length === 0)
return 0;
const formatDiagnosticsHost = {
getCurrentDirectory: () => path.dirname(__dirname),
getCanonicalFileName: fileName => fileName,
getNewLine: () => EOL,
};
for (const errorAndWarning of errorsAndWarnings)
console.error(ts.formatDiagnostic(errorAndWarning, formatDiagnosticsHost));
return 1;
}
if (process.mainModule === module)
process.exitCode = compile(path.resolve(__dirname, `../tsconfig.json`), ...process.argv.slice(2));