|
| 1 | +var path = require('path'), |
| 2 | + fs = require('fs'), |
| 3 | + childProcess = require('child_process'); |
| 4 | + |
| 5 | +// Get the path to the phantomjs application |
| 6 | +function getPhantomFileName() { |
| 7 | + var phantomPath = path.join(__dirname, 'node_modules', 'phantomjs', 'bin', 'phantomjs'); |
| 8 | + if (process.env.DEBUG && fs.existsSync(phantomPath)) { |
| 9 | + return phantomPath; |
| 10 | + } |
| 11 | + return path.join(__dirname, 'phantomjs'); |
| 12 | +} |
| 13 | + |
| 14 | +// Call the casperJS script |
| 15 | +function runCasper(scriptName, callback) { |
| 16 | + var casperPath = path.join(__dirname, 'node_modules', 'casperjs', 'bin', 'casperjs'); |
| 17 | + var outputData = []; |
| 18 | + var error = null; |
| 19 | + var childArgs = [ |
| 20 | + path.join(__dirname, scriptName) |
| 21 | + ]; |
| 22 | + |
| 23 | + console.log('Calling casperJS: ', casperPath, childArgs); |
| 24 | + |
| 25 | + var ps = childProcess.execFile(casperPath, childArgs); |
| 26 | + |
| 27 | + ps.stdout.on('data', function (data) { |
| 28 | + console.log(data); |
| 29 | + outputData.push(data); |
| 30 | + }); |
| 31 | + |
| 32 | + ps.stderr.on('data', function (data) { |
| 33 | + console.log('casper error ---:> ' + data); |
| 34 | + error = new Error(data); |
| 35 | + }); |
| 36 | + |
| 37 | + ps.on('exit', function (code) { |
| 38 | + console.log('child process exited with code ' + code); |
| 39 | + callback(error, outputData); |
| 40 | + }); |
| 41 | +} |
| 42 | + |
| 43 | +// Entry Point |
| 44 | +exports.handler = function (event, context) { |
| 45 | + // Execute the casperJS call and exit |
| 46 | + runCasper('casperjs-script.js', context.done); |
| 47 | +}; |
0 commit comments