This repository has been archived by the owner on Aug 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
71 lines (55 loc) · 1.98 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
63
64
65
66
67
68
69
70
71
const _ = require('@sailshq/lodash');
const rttc = require('rttc');
const packageForHttpEvent = require('./lib/http-event');
const packageForGenericEvent = require('./lib/generic-event');
module.exports = function machineAsLambda(optsOrMachineDefOrMachine, options) {
// Set up global sails mock.
global.sails = {
config: {
appPath: process.cwd(),
},
log: {
info: console.log,
debug: console.log,
warn: console.log,
error: console.log,
verbose: process.env.sails_log__level==='verbose' || process.env.sails_log__level==='silly' ? console.log : () => {},
silly: process.env.sails_log__level==='silly' ? console.log : () => {},
blank: () => {}
},
on: () => {},
once: () => {}
};
// Parse env vars into config.
const prefix = 'sails_';
// Cache the prefix length so we don't have to keep looking it up.
const l = prefix.length;
// Loop through the env vars, looking for ones with the right prefix.
_.each(process.env, function(val, key) {
// If this var's name has the right prefix...
if((key.indexOf(prefix)) === 0) {
// Replace double-underscores with dots, to work with Lodash _.set().
var keypath = key.substring(l).replace(/__/g,'.');
// Attempt to parse the value as JSON.
try {
val = rttc.parseHuman(val, 'json');
}
// If that doesn't work, humanize the value without providing a schema.
catch(unusedErr) {
val = rttc.parseHuman(val);
}
// Override the current value at this keypath in `conf` (which currently contains
// the string value of the env var) with the now (possibly) humanized value.
_.set(sails.config, keypath, val);
}
});
// Default options.
options = options || {};
_.defaults(options, {
eventType: 'http'
});
if (options.eventType === 'http') {
return packageForHttpEvent(optsOrMachineDefOrMachine, options);
}
return packageForGenericEvent(optsOrMachineDefOrMachine, options);
};