-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconfig.js
80 lines (70 loc) · 2.02 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
'use strict';
var path = require('path');
var fs = require('fs');
var configKeys = ['githubUsername', 'authorName', 'authorEmail', 'authorUrl'];
var _defaultDependencies = [
{name: 'joi', description: 'Object schema validation'},
{name: 'lout', description: 'API documentation generator'},
{name: 'hoek', description: 'General purpose node utilities'}
];
/**
* The Generator settings
* This is used to store the generators meta and dependencies information in a persistent data source.
* @constructor
*/
var Config = module.exports = function Config(configPath) {
this._meta = {};
this._dependencies = [];
this._configPath = configPath || path.join(__dirname, './settings.json');
this._load();
};
/**
* Loads the meta and dependencies data from the settings file
*/
Config.prototype._load = function _load() {
if (fs.existsSync(this._configPath)) {
var content = fs.readFileSync(this._configPath);
content = JSON.parse(content);
this._meta = content.meta;
this._dependencies = content.dependencies;
} else {
this._dependencies = _defaultDependencies;
}
};
/**
* Store the meta and dependencies data in the settings file
*/
Config.prototype._write = function _write() {
var content = {
meta: this._meta,
dependencies: this._dependencies,
};
content = JSON.stringify(content, '', 2);
fs.writeFileSync(this._configPath, content);
};
/**
* Get the stored generators dependencies
* @return {Object} Generators dependencies
*/
Config.prototype.getDependencies = function getDependencies() {
return this._dependencies;
};
/**
* Get the stored generators meta data
* @return {Object} Generators metadata
*/
Config.prototype.getMeta = function getMeta() {
return this._meta;
};
/**
* Store the given metadata in the settings file
* @return {Object} Generators metadata
*/
Config.prototype.setMeta = function storeMeta(options) {
configKeys.forEach(function (val) {
if (options[val] && options[val].trim()) {
this._meta[val] = options[val];
}
}.bind(this));
this._write();
};