all-contributors-cli/cli.js

55 lines
1.4 KiB
JavaScript
Raw Normal View History

#!/usr/bin/env node
'use strict';
2016-02-29 00:48:55 +00:00
var fs = require('fs');
var path = require('path');
var assign = require('lodash.assign');
var markdown = require('./lib/markdown');
var getUserInfo = require('./lib/github');
var defaultEmojis = require('./lib/emoji');
var addContributor = require('./lib/addContributor');
var defaultRCFile = '.all-contributorsrc';
var argv = require('yargs')
.usage('Usage: $0 <username> <contribution>')
.demand(2)
.default('config', defaultRCFile)
.default('file', 'README.md')
.config('config', function(configPath) {
try {
return JSON.parse(fs.readFileSync(configPath, 'utf-8'));
} catch (error) {
if (configPath !== path.join(__dirname, defaultRCFile)) {
console.error(error.message);
process.exit(1);
}
2016-02-29 00:48:55 +00:00
}
})
.default('emoji', {})
.pkgConf('all-contributors')
.argv;
2016-02-29 00:48:55 +00:00
argv.emoji = assign({}, defaultEmojis, argv.emoji);
argv.username = argv._[0];
argv.contributions = argv._[1].split(',');
argv.file = path.join(__dirname, argv.file);
2016-02-29 00:48:55 +00:00
getUserInfo(argv.username, function(error, user) {
2016-02-29 00:48:55 +00:00
if (error) {
return console.error(error);
}
markdown.read(argv.file, function(error, fileContent) {
2016-02-29 00:48:55 +00:00
if (error) {
return console.error(error);
}
var newFileContent = addContributor(argv, user, fileContent);
markdown.write(argv.file, newFileContent, function(error, fileContent) {
2016-02-29 00:48:55 +00:00
if (error) {
return console.error(error);
}
});
});
});