2019-09-09 10:48:09 -07:00
|
|
|
const yaml = require('js-yaml');
|
|
|
|
const fs = require('fs');
|
|
|
|
|
|
|
|
function read(file) {
|
2019-09-11 12:42:44 -07:00
|
|
|
try {
|
|
|
|
return yaml.safeLoad(fs.readFileSync(file, 'utf8'));
|
|
|
|
} catch (error) {
|
|
|
|
if (error.name === 'YAMLException') {
|
|
|
|
error.message =
|
|
|
|
`\n\n\n` +
|
|
|
|
`\t====================================================================\n` +
|
|
|
|
`\tYour YAML file '${file}' is invalid\n` +
|
|
|
|
`\tUse e.g. https://jsonformatter.org/yaml-validator to find and fix the issue.\n` +
|
|
|
|
`\t====================================================================\n` +
|
|
|
|
`\n\n` +
|
|
|
|
error.message;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
throw error;
|
|
|
|
}
|
2019-09-09 10:48:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function readIfExists(file) {
|
|
|
|
return fs.existsSync(file) ? read(file) : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
function write(file, content) {
|
|
|
|
fs.writeFileSync(file, yaml.safeDump(content));
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {read, readIfExists, write};
|