UNPKG

3.12 kBJavaScriptView Raw
1/*───────────────────────────────────────────────────────────────────────────*\
2 │ Copyright (C) 2014 eBay Software Foundation │
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 \*───────────────────────────────────────────────────────────────────────────*/
16'use strict';
17
18var minimist = require('minimist');
19var common = require('./common');
20var debug = require('debuglog')('confit');
21
22
23exports.argv = function argv() {
24 var result, args;
25
26 result = {};
27 args = minimist(process.argv.slice(2));
28
29 Object.keys(args).forEach(function (key) {
30 if (key === '_') {
31 // Since the '_' args are standalone,
32 // just set keys with null values.
33 args._.forEach(function (prop) {
34 result[prop] = null;
35 });
36 return;
37 }
38
39 result[key] = args[key];
40 });
41
42 return result;
43};
44
45
46exports.env = function env() {
47 var result = {};
48
49 // process.env is not a normal object, so we
50 // need to map values.
51 Object.keys(process.env).forEach(function (env) {
52 result[env] = process.env[env];
53 });
54
55 return result;
56};
57
58
59exports.convenience = function convenience() {
60 var env, data;
61
62 env = process.env.NODE_ENV || 'development';
63 data = {};
64
65 debug('NODE_ENV set to \'%s\'', env);
66
67 // Normalize env and set convenience values.
68 Object.keys(common.env).forEach(function (current) {
69 var match;
70
71 match = common.env[current].test(env);
72 if (match) { env = current; }
73
74 data[current] = match;
75 });
76
77 debug('env:env set to \'%s\'', env);
78
79 // Set (or re-set) env:{nodeEnv} value in case
80 // NODE_ENV was not one of our predetermined env
81 // keys (so `config.get('env:blah')` will be true).
82 data[env] = true;
83 data.env = env;
84 return { env: data };
85};