Skip to content

Commit 38e3b85

Browse files
committed
initial
1 parent 58355dd commit 38e3b85

File tree

4 files changed

+678
-1
lines changed

4 files changed

+678
-1
lines changed

README.md

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,45 @@
11
# stacktracify
2-
Make unreadable production stacktraces readable using source maps
2+
3+
Have you ever been faced with a stacktrace that looks like this?
4+
5+
```
6+
TypeError h is not a function. (In 'h()', 'h' is undefined)
7+
main.jsbundle:954:5353
8+
main.jsbundle:112:423 p
9+
main.jsbundle:112:1740
10+
main.jsbundle:112:423 p
11+
main.jsbundle:112:898 n
12+
main.jsbundle:112:1273
13+
main.jsbundle:50:205 c
14+
main.jsbundle:50:1623 b
15+
main.jsbundle:50:488 _
16+
[native code] value
17+
[native code] value
18+
```
19+
20+
...perhaps from production from a minified web JS bundle or a React Native error report.
21+
22+
**stacktracify takes a source map and a stack trace from your clipboard (or from a file) and outputs a readable stacktrace with proper line numbers for each line**
23+
24+
## Install
25+
26+
```
27+
npm install -g stacktracify
28+
```
29+
30+
## Usage
31+
32+
**Copy a minified stacktrace to your clipboard** - then run:
33+
34+
```
35+
stacktracify /path/to/js.map
36+
```
37+
38+
Can also read stacktrace from file. For more info:
39+
```
40+
stacktracify --help
41+
```
42+
43+
## See also
44+
45+
- https://github.com/gabmontes/source-map-cli (only takes one line at a time)

index.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
})();

package.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "stacktracify",
3+
"version": "1.0.0",
4+
"description": "Make unreadable production stacktraces readable using source maps",
5+
"main": "index.js",
6+
"author": "Mikael Finstad <finstaden@gmail.com>",
7+
"license": "MIT",
8+
"bin": {
9+
"stacktracify": "index.js"
10+
},
11+
"repository": {
12+
"type": "git",
13+
"url": "git+https://github.com/mifi/stacktracify.git"
14+
},
15+
16+
"dependencies": {
17+
"clipboardy": "^2.1.0",
18+
"fs-extra": "^8.1.0",
19+
"meow": "^6.0.0",
20+
"source-map": "^0.7.3",
21+
"stacktrace-parser": "^0.1.8"
22+
},
23+
"files": [
24+
"index.js"
25+
]
26+
}

0 commit comments

Comments
 (0)