Skip to content

Commit aba223b

Browse files
committed
feat: Add FaaS support for environment variables defined withing the package.json start script
1 parent 681cc20 commit aba223b

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

src/webfunc.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,24 @@ const addEnvToProcess = (key,value) => {
2222
}
2323
/*eslint-enable */
2424

25+
const setUpProcessVariables = () => {
26+
try {
27+
const pkg = require(cwdPath('package.json')) || {}
28+
if (pkg.scripts && pkg.scripts.start)
29+
pkg.scripts.start.split(' ')
30+
.map(x => x.split('='))
31+
.filter(x => x.length == 2)
32+
.forEach(x => { process.env[x[0]] = x[1]})
33+
}
34+
catch(err) {
35+
console.error(err.message)
36+
}
37+
}
38+
// This guarentees that any environment variables defined in the package.json's start scripts get assigned even
39+
// if the package.json's start script is not executed (which is the case with FaaS like Google Cloud Functions
40+
// or AWS Lambda)
41+
setUpProcessVariables()
42+
2543
let _appconfig = null
2644
const getAppConfig = memoize => {
2745
const skipMemoization = memoize == undefined ? false : !memoize

0 commit comments

Comments
 (0)