-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.js
More file actions
79 lines (74 loc) · 2.29 KB
/
parse.js
File metadata and controls
79 lines (74 loc) · 2.29 KB
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
const fs = require('fs');
const path = require('path');
const { SourceMapConsumer } = require('source-map');
function getFileName(filePath) {
const pathList = filePath.split('/');
return pathList[pathList.length - 1];
}
const parse = async (stack, dirPath) => {
const stackLines = stack.split(/\n/);
const lines = [];
const cacheConsumers = {};
for (const line of stackLines) {
const match = line.match(/(^\s*at .*?\(?)([^()]+)(:([0-9]+):([0-9]+)\)?.*$)/);
if (!match) {
lines.push(line);
continue;
}
const filename = getFileName(match[2]);
const lineN = parseInt(match[4]);
const columnN = parseInt(match[5]);
const filePath = path.join(dirPath, filename + '.map');
if (fs.existsSync(filePath)) {
let consumer = cacheConsumers[filePath];
if (consumer == null) {
const sourceContent = fs.readFileSync(filePath);
const sourceMap = JSON.parse(sourceContent.toString());
consumer = await new SourceMapConsumer(sourceMap);
}
const position = consumer.originalPositionFor({
line: lineN, // line: 1-based
column: columnN - 1, // column: 0-based
});
lines.push({
path: filePath,
source: position.source,
line: position.line,
column: position.column,
name: position.name,
});
} else {
lines.push(line);
}
}
Object.keys(cacheConsumers).map(key => {
cacheConsumers[key].destroy();
});
return lines;
};
const getContent = async (filePath, sourceFile) => {
if (fs.existsSync(filePath)) {
const sourceContent = fs.readFileSync(filePath);
const sourceMap = JSON.parse(sourceContent.toString());
const consumer = await new SourceMapConsumer(sourceMap);
const content = consumer.sourceContentFor(sourceFile);
consumer.destroy();
return content;
}
return null;
};
const getSourcemapFiles = async(filename, basePath) => {
const filePath = path.join(basePath, filename);
if (fs.existsSync(filePath)) {
const sourceContent = fs.readFileSync(filePath);
const sourceMap = JSON.parse(sourceContent.toString());
const filteredFiles = sourceMap.sources.filter(file => !file.includes('node_modules'));
return filteredFiles.sort();
}
return [];
}
module.exports = {
parse,
getContent,
getSourcemapFiles,
};