forked from ctx-core/dev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
env.js
58 lines (58 loc) · 1.73 KB
/
env.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
// TODO: make this composable
// ENV configuration
// censible-core.env
// CTX_ENV=./censible-core.env,./another.env
if (typeof window === 'object') {
throw 'env cannot be run in browser environments'
}
require('ctx-core/package/lib').verify__version__node()
import {assign,clone} from 'ctx-core/object/lib'
import uuid from 'uuid'
import {throw__error} from 'ctx-core/error/lib'
import {log,debug} from 'ctx-core/logger/lib'
const logPrefix = 'ctx-core/env'
log(logPrefix)
export const env__process = process.env
if (!env__process.NODE_ENV) {
require('dotenv').config()
if (!env__process.NODE_ENV) {
throw__missing__env('NODE_ENV')
}
}
const localhost = $env__process('LOCALHOST')
, isLocalhost = !!localhost
, WEB_CONCURRENCY =
$env__process('WEB_CONCURRENCY')
|| 4
, NODE_ENV = $env__process('NODE_ENV')
let env = clone(env__process, {
noJson: () => {},
whitelist_salt: Object.freeze(uuid()),
isDevelopment: NODE_ENV == 'development',
isLocalhost: !!isLocalhost,
isProduction: NODE_ENV == 'production',
isTest: NODE_ENV == 'test',
NODE_ENV: NODE_ENV,
PORT: env__process.PORT || 3002,
WEB_CONCURRENCY
})
env.minify = !env.isLocalhost && !env.isTest
export default env
export {env}
export function assign__env() {
return assign(env, ...arguments)
}
export function $env__process(...keys) {
for (let i=0; i < keys.length; i++) {
const key = keys[i]
, $ = env__process[key]
if ($) return $
}
}
export function throw__missing__env(name__env) {
throw__error({}, {
error_message: `${name__env} environment variable not set.\n` +
`development: make sure ${name__env} is set in your .env file\n` +
`heroku: make sure ${name__env} is set using \`heroku config:set\``
})
}