-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.js
37 lines (34 loc) · 786 Bytes
/
utils.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
const fs = require('fs');
/**
* Reads a config file and parses it's contents as JSON.
* If the file does not exist, returns an empty object.
* Throws an error if a parsing error occurs.
*
* @param {string} file
*/
const readConfig = file => {
try {
return JSON.parse(fs.readFileSync(file, 'utf8'));
} catch (err) {
if (err.code === 'ENOENT') {
return {};
} else {
logError('formspree.json could not be parsed');
throw err;
}
}
};
/**
* Stringifys the given config object to pretty JSON and
* writes it to the given file path.
*
* @param {string} file
* @param {object} config
*/
const writeConfig = (file, config) => {
fs.writeFileSync(file, JSON.stringify(config, null, 2));
};
module.exports = {
readConfig,
writeConfig
};