|
| 1 | +#!/usr/bin/env node |
| 2 | +'use strict'; |
| 3 | +const meow = require('meow'); |
| 4 | +const stackTraceParser = require('stacktrace-parser'); |
| 5 | +const fs = require('fs-extra'); |
| 6 | +const clipboardy = require('clipboardy'); |
| 7 | +const { SourceMapConsumer } = require('source-map'); |
| 8 | + |
| 9 | + |
| 10 | +const cli = meow(` |
| 11 | + Usage |
| 12 | + $ stacktracify <map-path> |
| 13 | +
|
| 14 | + Options |
| 15 | + --file, -f (default is read from clipboard) |
| 16 | +
|
| 17 | + Examples |
| 18 | + $ stacktracify /path/to/js.map --file /path/to/my-stacktrace.txt |
| 19 | +`, { |
| 20 | + flags: { |
| 21 | + file: { |
| 22 | + type: 'string', |
| 23 | + alias: 'f', |
| 24 | + } |
| 25 | + } |
| 26 | +}); |
| 27 | + |
| 28 | + |
| 29 | +const file = cli.flags.file; |
| 30 | + |
| 31 | +(async () => { |
| 32 | + try { |
| 33 | + const mapPath = cli.input[0]; |
| 34 | + if (!mapPath) cli.showHelp(); |
| 35 | + const mapContent = JSON.parse(await fs.readFile(mapPath, 'utf-8')); |
| 36 | + // WTF? promise? |
| 37 | + const smc = await new SourceMapConsumer(mapContent); |
| 38 | + |
| 39 | + let str; |
| 40 | + if (file !== undefined) { |
| 41 | + str = await fs.readFile(file, 'utf-8'); |
| 42 | + } else { |
| 43 | + str = await clipboardy.read(); |
| 44 | + } |
| 45 | + const stack = stackTraceParser.parse(str); |
| 46 | + if (stack.length === 0) throw new Error('No stack found'); |
| 47 | + |
| 48 | + const header = str.split('\n').find(line => line.trim().length > 0); |
| 49 | + |
| 50 | + if (header) console.log(header); |
| 51 | + |
| 52 | + stack.forEach(({ lineNumber, column }) => { |
| 53 | + const pos = smc.originalPositionFor({ line: lineNumber, column }); |
| 54 | + if (pos && pos.line != null) { |
| 55 | + console.log(` at ${pos.name || ''} (${pos.source}:${pos.line}:${pos.column})`); |
| 56 | + } |
| 57 | + |
| 58 | + // console.log('src', smc.sourceContentFor(pos.source)); |
| 59 | + }); |
| 60 | + } catch (err) { |
| 61 | + console.error(err); |
| 62 | + } |
| 63 | +})(); |
0 commit comments