Created
December 2, 2018 13:19
-
-
Save pgilad/7d61dbe4f5a39c47cc9dd25bc025997b to your computer and use it in GitHub Desktop.
Minimal compiler for typescript files
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
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