Created
September 14, 2013 06:07
-
-
Save sbimikesmullin/6559232 to your computer and use it in GitHub Desktop.
a filter that reformats the output of the TypeScript compiler e.g., `tsc -w *.ts --out /dev/null | coffee filter.coffee` so that it is more useful including source excerpts and little ansi colored arrows pointing to char, similar to the coffeescript compiler
This file contains 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
path = require 'path' | |
fs = require 'fs' | |
repeat = (s, n) -> r=''; r+=s while --n >= 0; r | |
process.stdin.setEncoding 'utf8' | |
process.stdin.resume() | |
buf = '' | |
parse = undefined | |
process.stdin.on 'data', (data) -> | |
buf += data | |
while (pos = buf.indexOf("\n")) isnt -1 | |
line = buf.substr 0, pos | |
buf = buf.substr pos+1 | |
parse line | |
parse = (line) -> | |
#console.log "line: ", line | |
if m = line.match /^(.+)\((\d+),(\d+)\): error (TS\d+): (.+)$/ | |
[nil, absfile, ln, char, code, msg] = m | |
file = path.relative process.cwd(), absfile | |
fs.readFile absfile, encoding: 'utf8', (err, src) -> | |
throw err if err | |
src = src.replace(/\r\n/, "\n").split "\n" | |
console.log "#{file}:#{ln}:#{char}: error: #{msg}" | |
console.log "#{src[ln-1]}" | |
process.stdout.write "#{Color.red}" | |
process.stdout.write repeat "-", char-1 | |
process.stdout.write repeat "^", 1 | |
process.stdout.write "#{Color.reset}" | |
process.stdout.write "\n" | |
Color = | |
reset: "\u001b[0m" | |
red: "\u001b[31m" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment