all-contributors-cli/cli.js

115 lines
2.8 KiB
JavaScript
Raw Normal View History

#!/usr/bin/env node
2016-03-06 23:20:24 +00:00
/* eslint-disable no-console */
'use strict';
2016-02-29 00:48:55 +00:00
var path = require('path');
2016-03-27 15:08:25 +00:00
var yargs = require('yargs');
var inquirer = require('inquirer');
2016-03-21 21:40:32 +00:00
var init = require('./lib/init');
2016-03-01 22:30:14 +00:00
var generate = require('./lib/generate');
var util = require('./lib/util');
2016-03-02 22:45:23 +00:00
var updateContributors = require('./lib/contributors');
var cwd = process.cwd();
var defaultRCFile = path.join(cwd, '.all-contributorsrc');
2016-03-27 15:08:25 +00:00
var argv = yargs
2016-03-02 22:45:23 +00:00
.help('help')
.alias('h', 'help')
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>')
2016-03-21 21:40:32 +00:00
.command('init', 'Prepare the project to be used with this tool')
.usage('Usage: $0 init')
.boolean('commit')
.default('files', ['README.md'])
2016-03-01 22:30:14 +00:00
.default('contributorsPerLine', 7)
2016-03-02 22:45:23 +00:00
.default('contributors', [])
.default('config', defaultRCFile)
2016-03-06 23:20:24 +00:00
.config('config', function (configPath) {
try {
return util.configFile.readConfig(configPath);
} catch (error) {
if (configPath !== defaultRCFile) {
onError(error);
}
2016-02-29 00:48:55 +00:00
}
})
.argv;
2016-02-29 00:48:55 +00:00
function startGeneration(argv) {
return Promise.all(
argv.files.map(file => {
const filePath = path.join(cwd, file);
return util.markdown.read(filePath)
.then(fileContent => {
var newFileContent = generate(argv, argv.contributors, fileContent);
return util.markdown.write(filePath, newFileContent);
});
})
);
2016-03-01 22:30:14 +00:00
}
function addContribution(argv) {
var username = argv._[1];
var contributions = argv._[2];
// Add or update contributor in the config file
return updateContributors(argv, username, contributions)
.then(data => {
argv.contributors = data.contributors;
return startGeneration(argv)
.then(() => {
if (argv.commit) {
return util.git.commit(argv, data);
}
});
});
}
2016-03-01 22:30:14 +00:00
function onError(error) {
2016-02-29 00:48:55 +00:00
if (error) {
console.error(error.message);
process.exit(1);
2016-02-29 00:48:55 +00:00
}
process.exit(0);
2016-03-01 22:30:14 +00:00
}
function promptForCommand(argv) {
var questions = [{
type: 'list',
name: 'command',
2016-05-05 14:27:26 +00:00
message: 'What do you want to do?',
choices: [{
name: 'Add a new contributor or add a new contribution type',
value: 'add'
}, {
name: 'Re-generate the contributors list',
value: 'generate'
}],
2016-03-30 20:03:24 +00:00
when: !argv._[0],
default: 0
}];
return inquirer.prompt(questions)
.then(answers => {
return answers.command || argv._[0];
});
2016-03-01 22:30:14 +00:00
}
promptForCommand(argv)
.then(command => {
switch (command) {
case 'init':
return init();
case 'generate':
return startGeneration(argv);
case 'add':
return addContribution(argv);
default:
throw new Error(`Unknown command ${command}`);
}
})
.catch(onError);