-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
191 lines (167 loc) · 6.07 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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/* eslint-disable global-require */
'use strict'
// Core
const fs = require('fs'),
path = require('path')
// Vendor
const merge = require('lodash.merge')
// Constants
const kDefaultDatabaseUrlEnvKey = 'DATABASE_URL',
kDefaultDatabaseKey = 'database'
/**
* @param {String} configDirectory - base config directory containing configuration files and subdirectories
* @param {Object} [config = {}] - common configuration regardless of environment (may be overridden by other files)
* @param {Object} [options = {}]
* @param {Boolean} [options.includeRootIndex = false] - if true will also load ${configDirectory}/index.js if one exists; false otherwise
* @param {String} [options.databaseUrlEnvKey = 'DATABASE_URL']
* @param {String} [options.databaseKey = 'database'] - key to set in config when parsing process.env[options.databaseEnvKey]
* @returns {Object}
*/
module.exports = function(configDirectory, config = {}, options = {}) {
if (!isDirectory(configDirectory))
throw new Error(`${configDirectory} is not a valid directory`)
// Initialize defaults
if (!config)
config = {} // eslint-disable-line no-param-reassign
if (!options.databaseUrlEnvKey)
options.databaseUrlEnvKey = kDefaultDatabaseUrlEnvKey
if (!options.databaseKey)
options.databaseKey = kDefaultDatabaseKey
options.includeRootIndex = !!options.includeRootIndex
// Load base configuration
mergeConfigFiles(config, configDirectory, options.includeRootIndex)
/**
* Load environment configuration
*
* Environment names are based on the NODE_ENV environment variable.
*
* Recommended names for the environment:
* 1. develop (assumed if NODE_ENV is falsy)
* 2. boom (unstable build)
* 3. staging (evaluation before pushing to production)
* 4. production
*
* Following the recommended names is not required and may be set to whatever names are desired.
*
* Several configuration files may co-exist side by side and are merged in the following order
* (precedence given to the modules loaded later):
*
* index.js
* ${environment name}/index.js
* local/index.js (not part of the repository)
*
* Finally, if the database configuration is set via the environment variable, DATABASE_URL, or
* whichever one is passed in the options, that takes precedence over any file configuration.
*/
let environmentName = process.env.NODE_ENV || 'develop',
environmentConfigDirectory = path.resolve(configDirectory, environmentName)
mergeConfigFiles(config, environmentConfigDirectory)
// Load local overrides - if NODE_ENV is set to 'local', these will get processed 2x (operator
// error)
let localConfigDirectory = path.resolve(configDirectory, 'local')
mergeConfigFiles(config, localConfigDirectory)
// Load any database environment configuration
let databaseUrl = process.env[options.databaseUrlEnvKey]
if (databaseUrl) {
let databaseConfigFromUrl = parseDatabaseUrl(databaseUrl)
if (databaseConfigFromUrl === false)
throw new Error(`Invalid database environment variable, ${options.databaseUrlEnvKey}: ${databaseUrl}`)
if (!config[options.databaseKey])
config[options.databaseKey] = databaseConfigFromUrl
else
merge(config[options.databaseKey], databaseConfigFromUrl)
}
return config
}
// --------------------------------------------------------
/**
* @param {Object} config - base configuration to be extended with the configuration files in ${directory}
* @param {String} directory - directory to search for configuration files
* @param {Boolean} [includeIndexFile = true]
*/
function mergeConfigFiles(config, directory, includeIndexFile = true) {
getConfigFileNames(directory, includeIndexFile)
.forEach((configFile) => {
let moreConfig = require(configFile)
if (typeof moreConfig === 'function')
moreConfig = moreConfig()
let baseName = path.basename(configFile, '.js')
if (baseName !== 'index') {
if (!config[baseName])
config[baseName] = moreConfig
else
merge(config[baseName], moreConfig)
}
else {
merge(config, moreConfig)
}
})
}
/**
* @param {String} directory - directory to search for configuration files
* @param {Boolean} includeIndexFile
* @returns {Array.<String>} - absolute paths to configuration files in ${directory}
*/
function getConfigFileNames(directory, includeIndexFile) {
if (!isDirectory(directory))
return []
let foundIndexFile = false,
result = fs.readdirSync(directory)
.filter((configFileName) => {
// Even if ${includeIndexFile} is true, do not include it here. We want it to be loaded
// before the other files, thus it is added to result in a later step.
if (configFileName === 'index.js') {
foundIndexFile = true
return false
}
if (!configFileName.endsWith('.js'))
return false
let resolvedPath = path.resolve(directory, configFileName),
stat = fs.statSync(resolvedPath)
return stat.isFile()
})
.map((fileName) => path.resolve(directory, fileName))
if (foundIndexFile && includeIndexFile)
// Process any index.js file *before* the other files
result.unshift(path.resolve(directory, 'index.js'))
return result
}
/**
* @param {String} directory
* @returns {Boolean} - true if ${directory} exists and is a directory; false otherwise
*/
function isDirectory(directory) {
let stat = null
try {
stat = fs.statSync(directory)
}
catch (error) {
// Noop
return false
}
return stat.isDirectory()
}
/**
* @param {String} databaseUrl
* @returns {Object|false} - if successfully parsed the DATABASE_URL, returns an Object with the relevant information; false otherwise
*/
function parseDatabaseUrl(databaseUrl) {
// e.g. dialect://user:password@host:port/name
// 1-----| 2--| 3------| 4--| 5--| 6--|
let matches = databaseUrl.match(/^([^:]+):\/\/([^:]+):([^@]+)@([^:]+):(\d+)\/(.+)/)
if (matches) {
return {
dialect: matches[1],
user: matches[2],
password: matches[3],
host: matches[4],
port: matches[5],
name: matches[6]
}
}
return false
}
// --------------------------------------------------------
// Export defaults for testing and/or globally redefining
module.exports.kDefaultDatabaseUrlEnvKey = kDefaultDatabaseUrlEnvKey
module.exports.kDefaultDatabaseKey = kDefaultDatabaseKey