-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
48 lines (34 loc) · 1.38 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
const path = require('path');
const phantomjs = require('phantomjs-prebuilt');
const binPath = phantomjs.path;
var phantomPromise = function(plotlyData,layout=false,debug=false) {
return new Promise((resolve,reject)=> {
if(!Array.isArray(plotlyData)){
reject("Data needs to be an Array");
return;
}
let plotlyDataBase64 = new Buffer(JSON.stringify({payload: plotlyData})).toString('base64');
let dataArg = `--data=${plotlyDataBase64}`;
let generator = path.join(__dirname + '/phantom', 'generate.js');
let rootPath = `--rootPath=${__dirname}`;
let childArgs = [generator, dataArg,rootPath];
if (layout) {
var plotlyLayoutBase64 = new Buffer(JSON.stringify({layout: layout})).toString('base64');
var layoutArg = `--layout=${plotlyLayoutBase64}`;
childArgs.push(layoutArg);
}
if (debug) {
var debugArg = `--debug=true`;
childArgs.push(debugArg);
}
var phantomjsProcess = require('child_process').execFile(binPath, childArgs,function(error, stdout, stderr){
resolve(stdout);
});
phantomjsProcess.stderr.on('data', function (data) {
reject(data);
phantomjsProcess.kill('SIGINT');
return;
});
});
};
module.exports.render = phantomPromise;