UNPKG

3.4 kBJavaScriptView Raw
1/*───────────────────────────────────────────────────────────────────────────*\
2 │ Copyright (C) 2016 PayPal │
3 │ │
4 │ Licensed under the Apache License, Version 2.0 (the "License"); │
5 │ you may not use this file except in compliance with the License. │
6 │ You may obtain a copy of the License at │
7 │ │
8 │ http://www.apache.org/licenses/LICENSE-2.0 │
9 │ │
10 │ Unless required by applicable law or agreed to in writing, software │
11 │ distributed under the License is distributed on an "AS IS" BASIS, │
12 │ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. │
13 │ See the License for the specific language governing permissions and │
14 │ limitations under the License. │
15 \*───────────────────────────────────────────────────────────────────────────*/
16import minimist from 'minimist';
17import debuglog from 'debuglog';
18import Common from './common.js';
19
20
21const debug = debuglog('confit');
22
23export default {
24
25 argv() {
26 let result = {};
27 let args = minimist(process.argv.slice(2));
28
29 for (let key of Object.keys(args)) {
30 if (key === '_') {
31 // Since the '_' args are standalone,
32 // just set keys with null values.
33 for (let prop of args._) {
34 result[prop] = null;
35 }
36 } else {
37 result[key] = args[key];
38 }
39 }
40
41 return result;
42 },
43
44 env(ignore) {
45 let result = {};
46
47 // process.env is not a normal object, so we
48 // need to map values.
49 for (let env of Object.keys(process.env)) {
50 //env:env is decided by process.env.NODE_ENV.
51 //Not allowing process.env.env to override the env:env value.
52 if (ignore.indexOf(env) < 0) {
53 result[env] = process.env[env];
54 }
55 }
56
57 return result;
58 },
59
60
61 convenience() {
62 var nodeEnv, env;
63
64 nodeEnv = process.env.NODE_ENV || 'development';
65 env = {};
66
67 debug(`NODE_ENV set to ${nodeEnv}`);
68
69 // Normalize env and set convenience values.
70 for (let current of Object.keys(Common.env)) {
71 let match = Common.env[current].test(nodeEnv);
72 nodeEnv = match ? current : nodeEnv;
73 env[current] = match;
74 }
75
76 debug(`env:env set to ${nodeEnv}`);
77
78 // Set (or re-set) env:{nodeEnv} value in case
79 // NODE_ENV was not one of our predetermined env
80 // keys (so `config.get('env:blah')` will be true).
81 env[nodeEnv] = true;
82 env.env = nodeEnv;
83 return { env };
84 }
85}