all-contributors-cli/cli.js

71 lines
1.8 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');
2016-03-01 22:30:14 +00:00
var generate = require('./lib/generate');
var markdown = require('./lib/markdown');
var getUserInfo = require('./lib/github');
var cwd = process.cwd();
var defaultRCFile = path.join(cwd, '.all-contributorsrc');
var argv = require('yargs')
2016-03-01 22:30:14 +00:00
.command('generate', 'Generate the list of contributors')
.usage('Usage: $0 generate')
.command('add', 'add a new contributor')
.usage('Usage: $0 add <username> <contribution>')
.demand(2)
.default('config', defaultRCFile)
.default('file', 'README.md')
2016-03-01 22:30:14 +00:00
.default('contributorsPerLine', 7)
.config('config', function(configPath) {
try {
return JSON.parse(fs.readFileSync(configPath, 'utf-8'));
} catch (error) {
if (configPath !== defaultRCFile) {
console.error(error.message);
process.exit(1);
}
2016-02-29 00:48:55 +00:00
}
})
2016-03-01 22:30:14 +00:00
.help('help')
.argv;
2016-02-29 00:48:55 +00:00
argv.file = path.join(cwd, argv.file);
2016-02-29 00:48:55 +00:00
2016-03-01 22:30:14 +00:00
function startGeneration(argv, cb) {
markdown.read(argv.file, function(error, fileContent) {
if (error) {
return cb(error);
}
var newFileContent = generate(argv, argv.contributors, fileContent);
markdown.write(argv.file, newFileContent, cb);
});
}
function onError(error) {
2016-02-29 00:48:55 +00:00
if (error) {
return console.error(error);
}
2016-03-01 22:30:14 +00:00
}
if (argv[0] === 'generate') {
startGeneration(argv, onError);
} else if (argv[0] === 'add') {
// Fetch user
argv.username = argv._[1];
argv.contributions = argv._[2].split(',');
getUserInfo(argv.username, function(error, user) {
2016-02-29 00:48:55 +00:00
if (error) {
return console.error(error);
}
2016-03-01 22:30:14 +00:00
// TODO
// Add him to the contributors
// Save rc file with updated contributors key
startGeneration(argv, onError);
2016-02-29 00:48:55 +00:00
});
2016-03-01 22:30:14 +00:00
}