-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathconfig.js
89 lines (77 loc) · 2.65 KB
/
config.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
'use strict';
const nconf = require('nconf');
nconf.formats.yaml = require('nconf-yaml');
class Config {
init(args) {
const configFile = (args.parent && args.parent.config) ? args.parent.config : './speccy.yaml';
this.load(configFile, {
quiet: args.quiet,
jsonSchema: args.jsonSchema,
verbose: args.verbose,
// Command specific options
lint: {
rules: this.notEmptyArray(args.rules),
skip: this.notEmptyArray(args.skip),
format: args.format,
},
resolve: {
output: args.output,
internalRefs: args.internalRefs
},
serve: {
port: args.port,
}
});
}
load(file, supplied) {
// 1, check the supplied values
nconf.add('supplied', {
type: 'literal',
store: this.cleanObject(supplied),
});
// 2, look in config file (e.g: speccy.yaml)
nconf.add('local', {
type: 'file',
format: nconf.formats.yaml,
file,
});
if (!nconf.get('quiet') && nconf.get('verbose') > 2) {
console.error('LOADING CONFIG', file);
}
}
get(key, defaultValue) {
// Search through all known stores for the value
const value = nconf.get(key);
const result = (value === undefined) ? defaultValue : value;
if (!nconf.get('quiet') && nconf.get('verbose') > 2) {
console.error(`CONFIG VALUE ${key} = ${result} (default = ${defaultValue})`)
}
return result
}
// Don't want an object full of null
cleanObject(object) {
const cleaned = {};
Object.keys(object).forEach(key => {
const value = object[key];
if (value === undefined || value === null || (Array.isArray(value) && value.length === 0)) {
return;
} else if (typeof value === "object" && !Array.isArray(value)) {
cleaned[key] = this.cleanObject(value);
} else {
cleaned[key] = value;
}
});
return cleaned;
}
/**
* Check and returns the given array if it is not empty. Otherwise it returns 'undefined'.
* Useful for configuration options where an empty array has no meaning.
*
* @param array The array instance to check.
* @returns {array|undefined} The given array; or 'undefined' if array is 'undefined' or empty.
*/
notEmptyArray(array) {
return array && array.length > 0 ? array : undefined;
}
}
module.exports = new Config;