Skip to content

Instantly share code, notes, and snippets.

@longlho
Created March 24, 2019 19:01
Show Gist options
  • Select an option

  • Save longlho/39e91697e49b69fecbc53c8fbfd1a9a8 to your computer and use it in GitHub Desktop.

Select an option

Save longlho/39e91697e49b69fecbc53c8fbfd1a9a8 to your computer and use it in GitHub Desktop.
TS Compiler Wrapper
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