-
-
Notifications
You must be signed in to change notification settings - Fork 92
/
Copy pathenv.js
48 lines (40 loc) · 1.4 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
const { load } = require("js-yaml")
const { readFileSync } = require("fs")
const defaultConfigPath = require("./utils/defaultConfigPath")
const NODE_ENVIRONMENTS = ["development", "production", "test"]
const DEFAULT = "production"
const configPath = require("./utils/configPath")
const railsEnv = process.env.RAILS_ENV
const rawNodeEnv = process.env.NODE_ENV
const nodeEnv =
rawNodeEnv && NODE_ENVIRONMENTS.includes(rawNodeEnv) ? rawNodeEnv : DEFAULT
const isProduction = nodeEnv === "production"
const isDevelopment = nodeEnv === "development"
let config
try {
config = load(readFileSync(configPath), "utf8")
} catch (error) {
if (error.code === "ENOENT") {
// File not found, use default configuration
config = load(readFileSync(defaultConfigPath), "utf8")
} else {
throw error
}
}
const availableEnvironments = Object.keys(config).join("|")
const regex = new RegExp(`^(${availableEnvironments})$`, "g")
const runningWebpackDevServer = process.env.WEBPACK_SERVE === "true"
const validatedRailsEnv = railsEnv && railsEnv.match(regex) ? railsEnv : DEFAULT
if (railsEnv && validatedRailsEnv !== railsEnv) {
/* eslint no-console:0 */
console.warn(
`Warning: '${railsEnv}' environment not found in the configuration. Using '${DEFAULT}' configuration as a fallback.`
)
}
module.exports = {
railsEnv: validatedRailsEnv,
nodeEnv,
isProduction,
isDevelopment,
runningWebpackDevServer
}