forked from faressoft/terminalizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utility.js
171 lines (135 loc) · 3.44 KB
/
utility.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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/**
* Provide utility functions
*
* @author Mohammad Fares <[email protected]>
*/
/**
* Check, load, and parse YAML files
*
* - Add .yml extension when needed
*
* Throws
* - The provided file doesn't exit
* - The provided file is not a valid YAML file
*
* @param {String} filePath an absolute or a relative path
* @return {Object}
*/
function loadYAML(filePath) {
var file = null;
// Rsolve the path into an absolute path
filePath = di.path.resolve(filePath);
// The file doesn't exist
if (!di.fs.existsSync(filePath)) {
// A file with .yml suffix also doesn't exist
if (!di.fs.existsSync(filePath + '.yml')) {
throw new Error('The provided file doesn\'t exit');
} else {
filePath = filePath + '.yml';
}
}
// Read the file
try {
file = di.fs.readFileSync(filePath);
} catch (error) {
throw new Error(error);
}
// Parse the file
try {
return {
json: di.yaml.load(file),
raw: file.toString()
};
} catch (error) {
throw new Error('The provided file is not a valid YAML file');
}
}
/**
* Check, load, and parse JSON files
*
* - Add .json extension when needed
*
* Throws
* - The provided file doesn't exit
* - The provided file is not a valid JSON file
*
* @param {String} filePath an absolute or a relative path
* @return {Object}
*/
function loadJSON(filePath) {
var file = null;
// Rsolve the path into an absolute path
filePath = di.path.resolve(filePath);
// The file doesn't exist
if (!di.fs.existsSync(filePath)) {
// A file with .json suffix also doesn't exist
if (!di.fs.existsSync(filePath + '.json')) {
throw new Error('The provided file doesn\'t exit');
} else {
filePath = filePath + '.json';
}
}
// Read the file
try {
file = di.fs.readFileSync(filePath);
} catch (error) {
throw new Error(error);
}
// Parse the file
try {
return JSON.parse(file);
} catch (error) {
throw new Error('The provided file is not a valid JSON file');
}
}
/**
* Resolve a path and add an extension to the file name
*
* - Add the extension if not already added
*
* @param {String} filePath an absolute or a relative path
* @param {String} extension
* @return {String}
*/
function resolveFilePath(filePath, extension) {
var resolvedPath = di.path.resolve(filePath);
// The extension is not added
if (di.path.extname(resolvedPath) != '.' + extension) {
resolvedPath += '.' + extension;
}
return resolvedPath;
}
/**
* Get the default configurations
*
* @return {Object} {json, raw}
*/
function getDefaultConfig() {
var filePath = di.path.join(__dirname, 'config.yml');
return loadYAML(filePath);
}
/**
* Change a value for a specific key in YAML
*
* - Works only with the first level keys
* - Workds only with keys with a single value
* - Apply the changes on the json and raw
*
* @param {Object} data {json, raw}
* @param {String} key
* @param {*} value
*/
function changeYAMLValue(data, key, value) {
data.json[key] = value;
data.raw = data.raw.replace(new RegExp('^' + key + ':.+$', 'm'), key + ': ' + value);
}
////////////////////////////////////////////////////
// Module //////////////////////////////////////////
////////////////////////////////////////////////////
module.exports = {
loadYAML: loadYAML,
loadJSON: loadJSON,
resolveFilePath: resolveFilePath,
getDefaultConfig: getDefaultConfig,
changeYAMLValue: changeYAMLValue
};