Skip to content

Instantly share code, notes, and snippets.

@pgilad pgilad/script.js
Created Dec 2, 2018

Embed
What would you like to do?
Minimal compiler for typescript files
const ts = require('typescript');
const tsConfig = require('./tsconfig.json');
const config = tsConfig.compilerOptions;
function compile(fileNames, options) {
if (fileNames.filter(Boolean).length === 0) {
console.log('No files');
return;
}
let program = ts.createProgram(fileNames, options);
let emitResult = program.emit();
let allDiagnostics = ts.getPreEmitDiagnostics(program).concat(emitResult.diagnostics);
allDiagnostics.forEach(diagnostic => {
if (diagnostic.file) {
let { line, character } = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start);
let message = ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n');
console.log(`${diagnostic.file.fileName} (${line + 1},${character + 1}): ${message}`);
} else {
console.log(`${ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n')}`);
}
});
let exitCode = emitResult.emitSkipped ? 1 : 0;
console.log(`Process exiting with code '${exitCode}'.`);
process.exit(exitCode);
}
compile(process.argv.slice(2), { ...config, noEmit: true });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.