-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
46 lines (43 loc) · 1.35 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
var spawn = require('child_process').spawn;
exports.handler = function(event, context) {
// setup paths and env
var bin = __dirname + '/lib/ruby/bin/ruby';
var app = __dirname + '/lib/app/app.rb';
var args = ['-rbundler/setup', app];
spawnEnv = {};
for (e in process.env) {
if (e != 'BUNDLE_IGNORE_CONFIG') {
spawnEnv[e] = process.env[e];
}
}
spawnEnv.BUNDLE_GEMFILE = __dirname + '/lib/vendor/Gemfile';
// be sure to include our bundled gems in the GEM_PATH as well
spawnEnv.GEM_PATH = process.env.GEM_PATH + ":" + __dirname + '/lib/vendor/ruby/2.2.0/gems';
var options = { env: spawnEnv };
// cd to our dir and launch the child process
process.chdir(__dirname);
var child = spawn(bin, args, options);
var out_data = '';
// collect all output in chunks
child.stdout.on('data', function(data) {
if (data) {
out_data += data;
}
});
// log errors
child.stderr.on('data', function(data) {
if (data) {
console.log(data.toString());
}
});
// complete the lambda handler on close
child.on('close', function(code) {
if(code === 0) {
context.succeed(JSON.parse(out_data));
} else {
context.fail(new Error("Process exited with non-zero status code " + code));
}
});
// all handlers set, send the event data to start processing
child.stdin.end(JSON.stringify(event));
}