Created
June 6, 2018 09:08
-
-
Save danielepiccone/32a7cd7ce8d053b1e7b9716622e14dbb to your computer and use it in GitHub Desktop.
pipe a full stack trace into trackingdog, preferred usage: xclip -o -selection clipboard | ./trackindog-multi
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
#!/usr/bin/env node | |
const TrackingDog = require('trackingdog/lib/TrackingDog'); | |
const exec = require('child_process').exec; | |
const stackFramesRe = /(https?:\/\/[^:]*:[^:]*:[^: ]*)/g; | |
const codeLineRe = /(https?:\/\/[^:]*):(\d*):(\d*)/; | |
const trackingdog = new TrackingDog({}); | |
function consumeStream(stream) { | |
return new Promise((resolve, reject) => { | |
let buffer = Buffer.from([]); | |
stream | |
.on('data', chunk => { | |
buffer = Buffer.concat([buffer, chunk]) | |
}) | |
.on('end', () => resolve(buffer)) | |
.on('error', reject); | |
}); | |
} | |
function trackUrl(codeLine) { | |
const [ , url, line, column ] = codeLine.match(codeLineRe); | |
if (!url || !line || !column) { | |
throw new Error(`trackingdog-multi: invalid ${url} ${line} ${column}`) | |
} | |
return trackingdog | |
.track({ | |
url, | |
line: parseInt(line, 10), | |
column: parseInt(column, 10) | |
}) | |
.then(({ url, line, column}) => { | |
const originalCodeLine = `${url}:${line}:${column}`; | |
console.log(originalCodeLine) | |
}) | |
.catch(error => { | |
// Do not fail, alert | |
console.error('Error: ', error); | |
}) | |
} | |
consumeStream(process.stdin) | |
.then(buffer => { | |
const data = buffer.toString(); | |
const matches = data.match(stackFramesRe); | |
return Promise.all(matches.map(trackUrl)); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment