-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
122 lines (98 loc) · 3.22 KB
/
index.js
File metadata and controls
122 lines (98 loc) · 3.22 KB
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
113
114
115
116
117
118
119
120
121
122
const path = require("path");
const fs = require("fs");
/**
* Read and write configuration settins in config file
*
* @param string name Namespace name of this configuration. You may have one
* namespace per CliConfigurator object, but you may have many CliConfigurators
* @param string env (Optional) Environment the configurator will operate in.
* Default is "default". This is typically set to process.env.NODE_ENV || 'default'
* @param string folder (Optional) Name of the config folder to use. Default is
* "config"
*/
module.exports = function(name, env = "default", folder = "config") {
this.filename = path.normalize(folder + '/' + env + ".json");
this.filepath = path.dirname(this.filename);
/**
* Pretty-stringify version of JSON.stringify
*
* @param Object payload Payload to stringify
* @return string Returns the stringified string
*/
this.stringify = (payload) => {
return JSON.stringify(payload, null, '\t');
};
/**
* Write a new config file
*
* @param object payload Configuration payload to write to the file
* @return boolean Returns true or throws an exception on error.
*/
this.write = (payload) => {
// Namespace the payload
let newPayload = new Object();
newPayload[name] = payload;
payload = newPayload;
// Check if directory exists
if (!fs.existsSync(folder)) {
try {
fs.mkdirSync(this.filepath);
}
catch (ex) {
throw "Could not create configuration directory.";
}
}
// Check if file exists
if (!fs.existsSync(this.filename)) {
// Create file
try {
fs.writeFileSync(this.filename, this.stringify(payload));
}
catch (ex) {
throw "Could not create config file.";
}
}
else {
// Append to file
let existing = JSON.parse(fs.readFileSync(this.filename));
if (existing) {
payload = Object.assign(existing, payload);
fs.writeFileSync(this.filename, this.stringify(payload));
}
else {
throw "Could not load existing configuration file.";
}
}
return true;
};
/**
* Read from the config
*
* @return Object Returns the JSON data of the config namespace
*/
this.read = function() {
let payload = false;
// Check if the config exists
if (fs.existsSync(this.filename)) {
// Try to load the file
try {
payload = JSON.parse(fs.readFileSync(this.filename));
}
catch (ex) {
throw "Could not read configuration file.";
}
// Grab the namespace out of the config
try {
payload = payload[name];
}
catch (ex) {
throw "Configuration file loaded but could not load '" + name +
"' configuration.";
}
}
else {
payload = false;
}
return payload;
};
};