-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
69 lines (62 loc) · 2.56 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
#!/usr/bin/env node
const child_process = require('child_process');
const source_map = require('source-map');
const fs = require('fs');
const spin = require('io-spin');
const program = require('commander');
const map_name = './tmp/bundle.map';
program
.option('-c, --cache', 'cache map file')
.option('-a, --android', 'android platform')
.parse(process.argv);
const platform = program.android ? 'android' : 'ios';
const spinner = spin('mapping ' + platform + ' source file...');
showCrashInfo();
function showCrashInfo() {
spinner.start();
if (fs.existsSync(map_name)) {
mapSourceFile();
} else {
const bundle_command = 'react-native bundle --entry-file index.js --platform ' + platform + ' --dev false --bundle-output ./tmp/main.jsbundle --assets-dest ./tmp/bundle --sourcemap-output ' + map_name;
const bundle_process = child_process.exec('mkdir tmp;' + bundle_command, function (err, stdout) {
if (err) console.log(err);
bundle_process.kill();
spinner.update('mapping completed...');
mapSourceFile();
});
}
}
function mapSourceFile() {
fs.readFile(map_name, 'utf8', function (err, data) {
const smc = new source_map.SourceMapConsumer(data);
const mapResult = smc.originalPositionFor({
line: parseInt(process.argv[2]),
column: parseInt(process.argv[3]),
});
fs.readFile(mapResult.source, 'utf8', function (err, fileData) {
spinner.stop();
let info = mapResult.source;
info += ' at line ' + mapResult.line + ':' + mapResult.column;
let lines = fileData.toString().split(/[\n\r]/);
lines = lines.map((item, index) => ({ marked: index + 1 === mapResult.line, text: ((index + 1) + '. ' + item).replace(/ /g, "\u00a0") }));
lines = lines.slice(mapResult.line - 5, mapResult.line + 5);
console.log('\x1B[32m%s\x1B[39m ', info);
console.log('--------------------------------');
lines.forEach(item => {
if (item.marked) {
console.log('\x1B[45m\x1B[33m%s\x1b[0m\x1B[49m', item.text);
} else {
console.log(item.text);
}
});
console.log('--------------------------------');
});
});
if (!program.cache) {
const remove_process = child_process.exec('rm -rf ./tmp', function (err, stdout) {
if (err) console.log(err);
console.log(stdout);
remove_process.kill();
});
}
}