Created
March 24, 2019 19:01
-
-
Save longlho/39e91697e49b69fecbc53c8fbfd1a9a8 to your computer and use it in GitHub Desktop.
TS Compiler Wrapper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function compiler (configFilePath: string) { | |
| // tslint:disable-next-line no-any | |
| const host: ts.ParseConfigFileHost = ts.sys as any; | |
| // Fix after https://github.com/Microsoft/TypeScript/issues/18217 | |
| host.onUnRecoverableConfigFileDiagnostic = printDiagnostic; | |
| const parsedCmd = ts.getParsedCommandLineOfConfigFile(configFilePath, undefined, host); | |
| host.onUnRecoverableConfigFileDiagnostic = undefined; | |
| const {options, fileNames} = parsedCmd; | |
| const program = ts.createProgram({ | |
| rootNames: fileNames, | |
| options, | |
| }); | |
| const emitResult = program.emit( | |
| undefined, | |
| undefined, | |
| undefined, | |
| undefined, | |
| { | |
| before: [], | |
| after: [], | |
| afterDeclarations: [], | |
| } | |
| ); | |
| ts.getPreEmitDiagnostics(program) | |
| .concat(emitResult.diagnostics) | |
| .forEach(diagnostic => { | |
| let msg = ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n'); | |
| if (diagnostic.file) { | |
| const {line, character} = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start); | |
| msg = `${diagnostic.file.fileName} (${line + 1},${character + 1}): ${msg}`; | |
| } | |
| console.error(msg); | |
| }); | |
| const exitCode = emitResult.emitSkipped ? 1 : 0; | |
| if (exitCode) { | |
| console.log(red(`Process exiting with code '${exitCode}'.`)); | |
| process.exit(exitCode); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment