This repository has been archived by the owner on Aug 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
env.js
111 lines (98 loc) · 2.9 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/* @flow */
const { parse } = require('dotenv')
const forOwn = require('lodash/forOwn')
const has = require('lodash/has')
const keys = require('lodash/keys')
const pull = require('lodash/pull')
const { readFileSync } = require('fs')
const { join } = require('path')
/*::
export type EnvOptions = {
path?: string;
example?: string;
debug?: boolean;
}
*/
/**
* @typedef {Object} EnvOptions
* @param {string} [path='<cwd>/.env'] - a custom path if your file containing environment variables is located elsewhere
* @param {string} [example='<cwd>/.env.example'] - path to example environment file
* @param {boolean} [debug=false] - turns on logging to help debug why certain keys or values are not being set as you expect
* @example
* import type { EnvOptions } from 'secondwheel/env'
*/
/**
* @typedef {Error} EnvError
* @property {Set<string>} missing - a list of variable names
*/
const pattern = /\${\s*(\w+)\s*}/g
const interpolate = (value, parsed, missing) =>
value.replace(pattern, (match, key) => {
if (!has(parsed, key) && !has(process.env, key)) {
missing.add(key)
}
return interpolate(process.env[key] || parsed[key] || '', parsed, missing)
})
class EnvError extends Error {
/*:: missing: Set<string>; */
constructor (missing) {
super(`missing environment variables: ${Array.from(missing).join(', ')}`)
Error.captureStackTrace(this, EnvError)
this.name = 'EnvError'
this.missing = missing
}
}
/**
* Loads environment variables from a file.
*
* Performs validation based on the existence of variables rather than their value.
* Hence, a variable with no value is considered declared.
*
* @throws {EnvError} validation failure
* @example(properties)
* # .env.example (every key defined here must also be present in the environment)
* FULL_NAME=John Doe
* PASSWORD=
* @example(properties)
* # .env (supports javascript-style interpolation)
* AGE=30
* LAST_NAME=Smith
* FULL_NAME=${FIRST_NAME} ${LAST_NAME}
* # EnvError: missing environment variables: FIRST_NAME, PASSWORD
* @example
* import env from 'secondwheel/env'
*
* env()
*
* // process.env is configured
*/
const env = (options/*: ?EnvOptions */) => {
const cwd = process.cwd()
const config = {
path: join(cwd, '.env'),
example: join(cwd, '.env.example'),
debug: false,
...options
}
const { path, example, debug } = config
const parsed = parse(readFileSync(path), config)
const missing = new Set(
pull(
keys(parse(readFileSync(example), config)),
...keys(parsed),
...keys(process.env)
)
)
forOwn(parsed, (value, key) => {
parsed[key] = interpolate(value, parsed, missing)
})
if (missing.size) throw new EnvError(missing)
forOwn(parsed, (value, key) => {
if (!has(process.env, key)) {
process.env[key] = value
} else if (debug) {
console.log(`"${key}" is already defined in process.env`)
}
})
}
module.exports = env