all-contributors-cli/lib/util/configFile.js

35 lines
837 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 formatCommaFirst(o) {
return JSON.stringify(o, null, 2)
.split(/(,\n\s+)/)
.map(function (e, i) {
2016-03-06 23:20:24 +00:00
return i % 2 ? '\n' + e.substring(4) + ', ' : e;
2016-03-02 22:45:23 +00:00
})
2016-03-06 23:20:24 +00:00
.join('') +
'\n';
2016-03-02 22:45:23 +00:00
}
function readConfig(configPath) {
return JSON.parse(fs.readFileSync(configPath, 'utf-8'));
}
2016-03-21 21:40:32 +00:00
function writeConfig(configPath, content, cb) {
return fs.writeFile(configPath, formatCommaFirst(content), cb);
}
2016-03-02 22:45:23 +00:00
function writeContributors(configPath, contributors, cb) {
var config = readConfig(configPath);
2016-03-06 23:20:24 +00:00
var content = _.assign(config, {contributors: contributors});
2016-03-02 22:45:23 +00:00
return fs.writeFile(configPath, formatCommaFirst(content), cb);
}
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
};