all-contributors-cli/lib/util/config-file.js

37 lines
852 B
JavaScript
Raw Normal View History

2016-03-02 22:45:23 +00:00
'use strict';
var fs = require('fs');
var _ = require('lodash/fp');
function readConfig(configPath) {
try {
return JSON.parse(fs.readFileSync(configPath, 'utf-8'));
} catch (error) {
if (error.code === 'ENOENT') {
throw new Error('Configuration file not found: ' + configPath);
}
throw error;
}
2016-03-02 22:45:23 +00:00
}
2016-03-21 21:40:32 +00:00
function writeConfig(configPath, content, cb) {
return fs.writeFile(configPath, JSON.stringify(content, null, 2) + '\n', cb);
2016-03-21 21:40:32 +00:00
}
2016-03-02 22:45:23 +00:00
function writeContributors(configPath, contributors, cb) {
var config;
try {
config = readConfig(configPath);
} catch (error) {
return cb(error);
}
2016-03-06 23:20:24 +00:00
var content = _.assign(config, {contributors: contributors});
return writeConfig(configPath, content, cb);
2016-03-02 22:45:23 +00:00
}
module.exports = {
readConfig: readConfig,
2016-03-21 21:40:32 +00:00
writeConfig: writeConfig,
2016-03-02 22:45:23 +00:00
writeContributors: writeContributors
2016-03-06 23:20:24 +00:00
};