Skip to content

Commit

Permalink
fix: Prevent the active environment to be set up in the start script …
Browse files Browse the repository at this point in the history
…of the package.json. Replace that with the now.json's env.active property or default to 'default'
  • Loading branch information
nicolasdao committed Dec 30, 2017
1 parent 9126492 commit 6fe6809
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 36 deletions.
9 changes: 2 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const fs = require('fs')
const shortid = require('shortid')
const path = require('path')
const { getRouteDetails, matchRoute } = require('./routing')
const { reqUtil, pkgJson } = require('./utils')
const { reqUtil } = require('./utils')
require('colors')

/*eslint-disable */
Expand Down Expand Up @@ -61,16 +61,11 @@ let _allowedMethods = getAllowedMethods(_config) // Object
let _preEvent = () => Promise.resolve(null)
let _postEvent = () => Promise.resolve(null)

// This guarentees that any environment variables defined in the package.json's start scripts get assigned even
// if the package.json's start script is not executed (which is the case with FaaS like Google Cloud Functions
// or AWS Lambda)
pkgJson.initStartVariables()

//////////////////////// END - INITIALIZE APP ////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

const getEnvName = () => ((_config || {}).env || {}).active || getProcessEnv().WEBFUNC_ENV || 'default'
const getEnvName = () => ((_config || {}).env || {}).active || 'default'
const getEnv = () => ((_config || {}).env || {})[getEnvName()] || {}
const getHostingType = () => getEnv().hostingType || 'localhost'

Expand Down
29 changes: 0 additions & 29 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,37 +95,8 @@ const getParams = req => {
})
}

const initStartVariables = () => {
try {
const pkg = require(cwdPath('package.json')) || {}
if (pkg.scripts && pkg.scripts.start)
(pkg.scripts.start.trim()).split(' ')
.filter(x => x)
.map(x => x.split('='))
.filter(x => x.length == 2)
.forEach(x => {
/*eslint-disable */
if (x[0] != 'NODE_ENV')
process.env[x[0]] = x[1]
else
process.env.WEBFUNC_ENV = x[1]
/*eslint-enable */
})
/*eslint-disable */
if (!process.env.WEBFUNC_ENV && process.env.NODE_ENV)
process.env.WEBFUNC_ENV = process.env.NODE_ENV
/*eslint-enable */
}
catch(err) {
console.error(err.message)
}
}

module.exports = {
reqUtil: {
getParams
},
pkgJson: {
initStartVariables
}
}

0 comments on commit 6fe6809

Please sign in to comment.