2016-02-29 10:45:19 +00:00
|
|
|
#!/usr/bin/env node
|
2016-03-06 23:20:24 +00:00
|
|
|
/* eslint-disable no-console */
|
2016-02-29 10:45:19 +00:00
|
|
|
'use strict';
|
2016-02-29 00:48:55 +00:00
|
|
|
|
|
|
|
var fs = require('fs');
|
|
|
|
var path = require('path');
|
2016-02-29 10:45:19 +00:00
|
|
|
|
2016-03-01 22:30:14 +00:00
|
|
|
var generate = require('./lib/generate');
|
2016-02-29 10:45:19 +00:00
|
|
|
var markdown = require('./lib/markdown');
|
2016-03-02 22:45:23 +00:00
|
|
|
var updateContributors = require('./lib/contributors');
|
2016-02-29 10:45:19 +00:00
|
|
|
|
2016-02-29 23:15:56 +00:00
|
|
|
var cwd = process.cwd();
|
|
|
|
var defaultRCFile = path.join(cwd, '.all-contributorsrc');
|
2016-02-29 10:45:19 +00:00
|
|
|
|
|
|
|
var argv = require('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')
|
2016-02-29 21:08:23 +00:00
|
|
|
.command('add', 'add a new contributor')
|
|
|
|
.usage('Usage: $0 add <username> <contribution>')
|
2016-02-29 10:45:19 +00:00
|
|
|
.demand(2)
|
2016-03-06 15:49:46 +00:00
|
|
|
.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) {
|
2016-02-29 10:45:19 +00:00
|
|
|
try {
|
|
|
|
return JSON.parse(fs.readFileSync(configPath, 'utf-8'));
|
|
|
|
} catch (error) {
|
2016-02-29 23:15:56 +00:00
|
|
|
if (configPath !== defaultRCFile) {
|
2016-02-29 10:45:19 +00:00
|
|
|
console.error(error.message);
|
|
|
|
process.exit(1);
|
|
|
|
}
|
2016-02-29 00:48:55 +00:00
|
|
|
}
|
2016-02-29 10:45:19 +00:00
|
|
|
})
|
|
|
|
.argv;
|
2016-02-29 00:48:55 +00:00
|
|
|
|
2016-03-01 22:30:14 +00:00
|
|
|
function startGeneration(argv, cb) {
|
2016-03-06 15:49:46 +00:00
|
|
|
argv.files
|
2016-03-06 23:20:24 +00:00
|
|
|
.map(function (file) {
|
2016-03-06 15:49:46 +00:00
|
|
|
return path.join(cwd, file);
|
|
|
|
})
|
2016-03-06 23:20:24 +00:00
|
|
|
.forEach(function (file) {
|
|
|
|
markdown.read(file, function (error, fileContent) {
|
2016-03-06 15:49:46 +00:00
|
|
|
if (error) {
|
|
|
|
return cb(error);
|
|
|
|
}
|
|
|
|
var newFileContent = generate(argv, argv.contributors, fileContent);
|
|
|
|
markdown.write(file, newFileContent, cb);
|
|
|
|
});
|
|
|
|
});
|
2016-03-01 22:30:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-03-02 22:45:23 +00:00
|
|
|
var command = argv._[0];
|
|
|
|
|
|
|
|
if (command === 'generate') {
|
2016-03-01 22:30:14 +00:00
|
|
|
startGeneration(argv, onError);
|
2016-03-02 22:45:23 +00:00
|
|
|
} else if (command === 'add') {
|
|
|
|
var username = argv._[1];
|
|
|
|
var contributions = argv._[2];
|
2016-03-06 15:49:46 +00:00
|
|
|
// Add or update contributor in the config file
|
2016-03-06 23:20:24 +00:00
|
|
|
updateContributors(argv, username, contributions, function (error, contributors) {
|
2016-02-29 00:48:55 +00:00
|
|
|
if (error) {
|
2016-03-02 22:45:23 +00:00
|
|
|
return onError(error);
|
2016-02-29 00:48:55 +00:00
|
|
|
}
|
2016-03-02 22:45:23 +00:00
|
|
|
argv.contributors = contributors;
|
2016-03-01 22:30:14 +00:00
|
|
|
startGeneration(argv, onError);
|
2016-02-29 00:48:55 +00:00
|
|
|
});
|
2016-03-01 22:30:14 +00:00
|
|
|
}
|