forked from kkoch986/sourcemap-lookup
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
executable file
·168 lines (136 loc) · 5.78 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/usr/bin/env node
require('colors');
function showHelp() {
console.log("Lookup of production uglified,minified,compressed,... javascript stack trace line and column number and display actual source code location.");
console.log("");
console.log("Usage: \n\tonline-sourcemap-lookup <http://domain/path/to/js/file.js>:<line number>:<column number> [options]");
console.log("");
console.log("valid [options]:");
console.log("\t-h, --help\t\t Show this help message.");
console.log("\t-v, --version\t\t Show current version.");
console.log("\t-A\t\t\t The number of lines to print after the target line. Default is 5.");
console.log("\t-B\t\t\t The number of lines to print before the target line. Default is 5.");
console.log("\t-C\t\t\t The number of lines to print before and after the target line. If supplied, -A and -B are ignored.");
console.log("\t-s <sourcepath> \t Provide a path to the actual source files, this will be used to find the file to use when printing the lines from the source file. Default is ./");
console.log("\t-m <source-map-url> \t Provide a URL to the actual source maps file, this will be used to lookup source maps. Default is the same as .js URL location. Example: http://domain/path/to/js/source/maps/\n\n");
process.exit(0);
}
if (process.argv.length <= 2) {
showHelp();
return;
}
var argv = require('minimist')(process.argv.slice(2), {
alias: {
"h": "help",
"v": "version",
"s": "source-path"
}
});
if (argv["help"]) {
showHelp();
return;
}
if (argv["version"]) {
console.log("");
console.log("online-sourcemap-lookup v" + require("./package.json").version);
console.log("\tby Jernej Gololicic<jernej.gololicic@gmail.com> © 2019");
console.log("");
return;
}
var source = argv._[0].split(":");
var col = parseInt(source.pop());
var line = parseInt(source.pop());
var url = source.join(":");
if (url.indexOf('.js') === -1 || isNaN(line) || isNaN(col)) {
showHelp();
process.exit(0);
}
var sourceDirectory = argv["source-path"] || "./";
var linesBefore = parseInt(argv["C"] || argv["B"]) || 5;
var linesAfter = parseInt(argv["C"] || argv["A"]) || 5;
// make sure a string is always the same length
function pad(str, len) {
str = str + "";
while (str.length < len) {
str = str + " ";
}
return str;
}
console.log("");
var fs = require("fs");
var sourceMap = require('source-map');
var http = require("http");
url = url.split('/');
var fileName = url.pop();
fileName += '.map';
var sourceMapsLocation = argv['m'] || url.join('/');
url = sourceMapsLocation + "/" +fileName;
console.log("Downloading sourcemap file from", url);
http.get(url, function (response) {
var data = '';
if (response.statusMessage !== "OK") {
console.log('ERROR: ', response.statusCode, ' ', response.statusMessage);
process.exit(1);
}
response.setEncoding('utf8');
response.on('data', function (chunk) {
data += chunk;
});
response.on('end', function () {
var obj = JSON.parse(data);
var smc = new sourceMap.SourceMapConsumer(obj);
var originalPosition = smc.originalPositionFor({
line: line,
column: col
});
console.log("Original Position: \n\t" + originalPosition.source + ", Line " + originalPosition.line + ":" + originalPosition.column);
console.log("");
// remove the webpack stuff and try to find the real file
var originalFileName = (sourceDirectory + originalPosition.source).replace("webpack:///", "").replace("/~/", "/node_modules/").replace(/\?[0-9a-zA-Z\*\=]+$/, "");
var sourceIndex = obj.sources.indexOf(originalFileName);
if (sourceIndex !== -1) {
showFileContent(obj.sourcesContent[sourceIndex]);
} else {
fs.access(originalFileName, fs.R_OK, function (err) {
if (err) {
console.log("Unable to access source file, " + originalFileName);
} else {
fs.readFile(originalFileName, function (err, data) {
if (err) throw err;
showFileContent(data.toString('utf-8'));
});
}
});
}
function showFileContent(content) {
// Data is a buffer that we need to convert to a string
// Improvement: loop over the buffer and stop when the line is reached
var lines = content.split("\n");
var line = originalPosition.line;
if (line > lines.length) {
console.log("Line " + line + " outside of file bounds (" + lines.length + " lines total).");
} else {
var minLine = Math.max(0, line - (linesBefore + 1));
var maxLine = Math.min(lines.length, line + linesAfter);
var code = lines.slice(minLine, maxLine);
console.log("Code Section: ");
var padLength = Math.max(("" + minLine).length, ("" + maxLine).length) + 1;
function formatLineNumber(currentLine) {
if (currentLine == line) {
return (pad(currentLine, padLength - 1) + ">| ").bold.red;
} else {
return pad(currentLine, padLength) + "| ";
}
}
var currentLine = minLine;
for (var i = 0; i < code.length; i++) {
console.log(formatLineNumber(++currentLine) + code[i]);
if (currentLine == line && originalPosition.column) {
console.log(pad('', padLength + 2 + originalPosition.column) + '^'.bold.red);
}
}
}
console.log("\n");
}
});
});