-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
62 lines (52 loc) · 1.83 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
var path = require('path'),
fs = require('fs'),
http = require('http'),
childProcess = require('child_process');
// Get the path to the phantomjs application
function getPhantomFileName(callback) {
var nodeModulesPath = path.join(__dirname, 'node_modules');
fs.exists(nodeModulesPath, function(exists) {
if (exists) {
return callback(path.join(__dirname, 'node_modules','phantomjs', 'bin', 'phantomjs'));
}
callback(path.join(__dirname, 'phantomjs'));
});
}
// Call the phantomJS script
function runPhantom(scriptName, callback) {
getPhantomFileName(function(phantomPath) {
var outputData = [];
var error = null;
var childArgs = [
path.join(__dirname, scriptName)
];
// This option causes the shared library loader to output
// useful information if phantomjs can't start.
process.env['LD_WARN'] = 'true';
// Tell the loader to look in this script's directory for
// the shared libraries that Phantom.js 2.0.0 needs directly.
// This shouldn't be necessary once
// https://github.com/ariya/phantomjs/issues/12948
// is fixed.
process.env['LD_LIBRARY_PATH'] = __dirname;
console.log('Calling phantomJS: ', phantomPath, childArgs);
var ps = childProcess.execFile(phantomPath, childArgs);
ps.stdout.on('data', function (data) { // register one or more handlers
console.log(data);
outputData.push(data);
});
ps.stderr.on('data', function (data) {
console.log('phantom error ---:> ' + data);
error = new Error(data);
});
ps.on('exit', function (code) {
console.log('child process exited with code ' + code);
callback(error, outputData);
});
});
}
// Entry Point
exports.handler = function( event, context ) {
// Execute the phantomJS call and exit
runPhantom('phantomjs-script.js', context.done);
}