-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.types.mjs
46 lines (38 loc) · 1.54 KB
/
build.types.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
import typescript from 'typescript';
import { fileURLToPath } from 'url';
import { dirname, resolve } from 'path';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
// Read tsconfig.json
const configPath = resolve(__dirname, 'tsconfig.json');
const configFile = typescript.readConfigFile(configPath, typescript.sys.readFile);
const parsedConfig = typescript.parseJsonConfigFileContent(
configFile.config,
typescript.sys,
__dirname
);
// Create program and emit
const program = typescript.createProgram(parsedConfig.fileNames, parsedConfig.options);
const emitResult = program.emit();
// Report errors
const allDiagnostics = typescript
.getPreEmitDiagnostics(program)
.concat(emitResult.diagnostics);
allDiagnostics.forEach(diagnostic => {
if (diagnostic.file) {
const { line, character } = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start);
const message = typescript.flattenDiagnosticMessageText(diagnostic.messageText, '\n');
console.log(`${diagnostic.file.fileName} (${line + 1},${character + 1}): ${message}`);
} else {
console.log(typescript.flattenDiagnosticMessageText(diagnostic.messageText, '\n'));
}
});
// Print out which files were processed
const sourceFiles = program.getSourceFiles()
.filter(file => !file.isDeclarationFile)
.map(file => file.fileName);
console.log('\nProcessed files:');
sourceFiles.forEach(file => console.log(file));
const exitCode = emitResult.emitSkipped ? 1 : 0;
console.log(`\nProcess exiting with code '${exitCode}'.`);
process.exit(exitCode);