forked from fivetanley/ember-cli-dotenv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
112 lines (92 loc) · 3.1 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
'use strict';
const fs = require('fs');
const path = require('path');
const dotenv = require('dotenv');
const parseArgs = require('minimist');
module.exports = {
name: require('./package').name,
/**
* NOTE: dotenv needs to be invoked before the app config is materialized
* so that the process.env's are set appropriately. Previously this was done
* within the `config` hook. As of 2.15.x, that is too late in the process
* and needed to be moved into `init`.
*/
init() {
this._super.init && this._super.init.apply(this, arguments);
let root = this.project.root;
let configFactory = path.join(root, 'config', 'dotenv.js');
let options = {
path: path.join(root, '.env'),
clientAllowedKeys: [],
fastbootAllowedKeys: [],
failOnMissingKey: false,
};
if (fs.existsSync(configFactory)) {
this._config = Object.assign(options, require(configFactory)(this._resolveEnvironment()));
} else {
this._config = options;
}
let loadedConfig = dotenv.config({path: options.path});
this._envConfig = loadedConfig.parsed;
// It might happen that environment config is missing or corrupted
if (loadedConfig.error) {
let loadingErrMsg = `[ember-cli-dotenv]: ${loadedConfig.error.message}`;
if (options.failOnMissingKey) {
throw new Error(loadingErrMsg);
} else {
console.warn(loadingErrMsg); // eslint-disable-line no-console
}
}
},
_resolveEnvironment() {
if (process.env.EMBER_ENV) {
return process.env.EMBER_ENV;
}
let args = parseArgs(process.argv);
let env = args.e || args.env || args.environment;
// Is it "ember b -prod" command?
if (!env && process.argv.indexOf('-prod') > -1) {
env = 'production'
}
// Is it "ember test" or "ember t" command without explicit env specified?
if (!env && (process.argv.indexOf('test') > -1 || process.argv.indexOf('t') > -1)) {
env = 'test'
}
return env || 'development';
},
config() {
let allowedKeys = this._config.clientAllowedKeys || [];
return this._pickConfigKeys(allowedKeys);
},
/**
* Reset values listed in `clientAllowedKeys` to null and
* add values from `fastbootAllowedKeys` to FastBoot manifest in package.json.
*
* Users may list in same keys both in `clientAllowedKeys` and
* `fastbootAllowedKeys`
*
* @returns {Object}
*/
fastbootConfigTree() {
let allowedKeys = this._config.fastbootAllowedKeys || [];
// `fastbootConfigTree` expects key name as app/engine name
return {
[this.app.name]: this._pickConfigKeys(allowedKeys)
};
},
_pickConfigKeys(keys) {
let envConfig = this._envConfig || {};
return keys.reduce((accumulator, key) => {
if (envConfig[key] === undefined) {
let errMsg = '[ember-cli-dotenv]: Required environment variable \'' + key + '\' is missing.';
if (this._config.failOnMissingKey) {
throw new Error(errMsg);
} else {
this.ui.writeWarnLine(errMsg);
}
}
accumulator[key] = envConfig[key];
return accumulator;
}, {});
}
};