-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.js
executable file
·133 lines (109 loc) · 3.1 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/env node
var fs = require("fs");
var SourceMapConsumer = require("source-map").SourceMapConsumer;
var convert = require("convert-source-map");
var kexec = require("kexec");
var path = require("path");
var minimist = require("minimist");
require("colors");
var argv = minimist(process.argv.slice(2), {
default: {
padding: 10
},
alias: {
p: "padding",
h: "help"
},
boolean: [
"help",
"vim",
"emacs",
"nano",
"less",
"gedit",
"path"
]
});
if (argv.help) {
process.stdout.write(fs.readFileSync(__dirname + "/README.md"));
process.exit(0);
}
var line = 0;
var column = 0;
// remove file:// prefix if any
var file = argv._[0].replace(/^file\:\/\//, "");
var match;
if (match = file.match(/^(.*?)(\:[0-9]+)(\:[0-9]+|$)/)) {
file = match[1];
line = parseInt(match[2].slice(1), 10);
if (match[3]) column = parseInt(match[3].slice(1), 10);
}
var source = fs.readFileSync(file).toString();
var converter;
// --map
if (argv.map) {
var mapSource = fs.readFileSync(argv.map).toString();
converter = convert.fromJSON(mapSource);
}
// inline base64 source map
// //# sourceMappingURL=data:application/json;base64,eyJ2....
if (!converter) {
converter = convert.fromSource(source);
}
// With link to file name
// //# sourceMappingURL=filename.map
if (!converter) {
converter = convert.fromMapFileSource(source, path.dirname(file));
}
// Just guess
if (!converter) {
var guessFile = file.replace(/\.js$/, "") + ".map";
var mapSource = fs.readFileSync(guessFile).toString();
converter = convert.fromJSON(mapSource);
}
if (!converter || !converter.sourcemap) {
console.error("Cannot find source map from", file);
process.exit(1);
}
var smc = new SourceMapConsumer(converter.sourcemap);
var origpos = smc.originalPositionFor({ line: line, column: column });
if (argv.vim) {
kexec("vim", [
"+call cursor(" + origpos.line + ", " + origpos.column +")",
origpos.source
]);
}
function startEditorOnLine(editor) {
// Used by the most editors
kexec(editor, [ "+" + origpos.line, origpos.source ]);
}
if (argv.emacs) startEditorOnLine("emacs");
if (argv.gedit) startEditorOnLine("gedit");
if (argv.less) startEditorOnLine("less");
if (argv.nano) startEditorOnLine("nano");
if (argv.path) {
process.stdout.write(origpos.source + "\n");
process.exit(0);
}
try {
var originalSource = fs.readFileSync(origpos.source).toString();
} catch (err) {
console.error("Failed to open original source file from", origpos.source, err.code);
}
if (originalSource) {
var preview = originalSource
.split("\n")
.map(function(line, i) {
var linenum = i + 1;
var out = linenum + ": " + line;
if (linenum == origpos.line) out = out.red;
return out;
})
.slice(origpos.line - argv.padding, origpos.line + argv.padding)
.join("\n")
;
console.log(preview);
console.log();
}
console.log("file:", origpos.source);
console.log("line:", origpos.line, "column:", origpos.column);