-
Notifications
You must be signed in to change notification settings - Fork 13
/
index.js
83 lines (68 loc) · 2.12 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env node
'use strict';
const meow = require('meow');
const stackTraceParser = require('stacktrace-parser');
const fs = require('fs-extra');
const clipboardy = require('clipboardy');
const { SourceMapConsumer } = require('source-map');
const cli = meow(`
Usage
$ stacktracify <map-path>
Options
--file, -f (default is read from clipboard)
Examples
$ stacktracify /path/to/js.map --file /path/to/my-stacktrace.txt
`, {
flags: {
file: {
type: 'string',
alias: 'f',
},
},
});
const { file } = cli.flags;
(async () => {
try {
const mapPath = cli.input[0];
if (!mapPath) cli.showHelp();
const mapContent = JSON.parse(await fs.readFile(mapPath, 'utf-8'));
// WTF? promise?
const smc = await new SourceMapConsumer(mapContent);
let str;
if (file !== undefined) {
str = await fs.readFile(file, 'utf-8');
} else {
str = await clipboardy.read();
}
let [header, ...lines] = str.trim().split(/\r?\n/);
lines = lines.map((line) => {
// stacktrace-parser doesn't seem to support stacktrace lines like this:
// index-12345678.js:1:2 a
const match = line.match(/^(\s+)([^\s]+:\d+:\d+)\s+([^\s]+)$/);
if (match) {
return `${match[1]}at ${match[3]} (${match[2]})`;
}
return line;
})
const stack = stackTraceParser.parse(lines.join('\n'));
if (stack.length === 0) throw new Error('No stack found');
if (header) console.log(header);
stack.forEach(({ methodName, lineNumber, column }) => {
try {
if (lineNumber == null || lineNumber < 1) {
console.log(` at ${methodName || '[unknown]'}`);
} else {
const pos = smc.originalPositionFor({ line: lineNumber, column });
if (pos && pos.line != null) {
console.log(` at ${pos.name || methodName || '[unknown]'} (${pos.source}:${pos.line}:${pos.column})`);
}
// console.log('src', smc.sourceContentFor(pos.source));
}
} catch (err) {
console.log(` at FAILED_TO_PARSE_LINE`);
}
});
} catch (err) {
console.error(err);
}
})();