-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoutput.js
51 lines (43 loc) · 1.53 KB
/
output.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
function parse(output, sourceMap) {
let toLog = []
output.forEach(line => {
line.values.forEach(val => {
switch(val.type) {
case 'object':
toLog.push(convertObject(val, sourceMap))
break
default:
toLog.push(val.value)
break
}
})
})
return toLog
}
function getError(boxObject) {
// Check syntax error, webpack should prevent this
if (boxObject.prototype === "SyntaxError" && boxObject.value["line"] && boxObject.value["column"] && boxObject.value["source"]) {
return `${boxObject.value["source"].value}\n${' '.repeat(boxObject.value["column"].value)}^\nSyntaxError on (${boxObject.value["line"].value}:${boxObject.value["column"].value})`
}
// Ducktype checking for error
if (boxObject.prototype && boxObject.value["message"] && boxObject.value["stack"]) {
return boxObject.value["stack"].value
}
return false
}
function convertObject(boxObject, sourceMap) {
const errCheck = getError(boxObject, sourceMap)
if (errCheck) return errCheck
let object = new Object()
for (let element in boxObject.value) {
object[element] = boxObject.value[element].value
}
// Check if value is compatible as an array type
if (boxObject.value["length"]) {
if (Array.from(object).length === boxObject.value["length"].value) {
return Array.from(object)
}
}
return object
}
module.exports = { parse }