forked from dfabulich/choicescript
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcside_message.js
More file actions
70 lines (62 loc) · 2.19 KB
/
cside_message.js
File metadata and controls
70 lines (62 loc) · 2.19 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
// Centralize functionality for overriding various ChoiceScript utilities
// in order to forward their output to CSIDE via IPC.
var LOG_TYPES = {
"INFO": "log",
"WARNING": "warn",
"ERROR": "error",
"PROGRESS": "progress",
"STATUS": "status"
}
var writeToOutputFile = false;
var fileStream = null;
function write(obj) {
if (writeToOutputFile) {
fileStream.write(obj.value + '\n', 'utf8');
}
process.send(obj);
}
if (typeof process !== "undefined") {
var consoleLog = console.log;
if (typeof process.send !== "undefined") {
// We're a subprocess, and should get ready to forward data to CSIDE.
process.on('uncaughtException', function(err) {
console.log('Caught exception: ' + err, { type: LOG_TYPES.ERROR });
});
process.on('exit', function(code) {
console.log("exit", { type: "exitCode", value: code})
});
console.log = function(message, data) {
type = "log";
if (data && data.type) {
type = data.type;
}
switch(type) {
case "config":
if (data && data.outputFile) {
writeToOutputFile = true;
fileStream = fs.createWriteStream(data.outputFile, {encoding: 'utf8'});
}
break;
case "progress":
process.send({ type: type, value: data.value });
break;
case "exitCode":
process.send({ type: type, value: data.value });
break;
case "error":
write({ type: type, value: message });
break;
default:
write({ type: type, value: message });
break;
}
}
} else {
// This isn't a CSIDE instantiation, so we
// re-override console.log with a version that
// mutes metadata output (the 'data' obj).
console.log = function(message, data) {
consoleLog(message);
}
}
}