1 | #!/usr/bin/env node
|
2 |
|
3 |
|
4 | "use strict";
|
5 |
|
6 | var path = require('path');
|
7 |
|
8 | var yargs = require('yargs');
|
9 |
|
10 | var chalk = require('chalk');
|
11 |
|
12 | var inquirer = require('inquirer');
|
13 |
|
14 | var init = require('./init');
|
15 |
|
16 | var generate = require('./generate');
|
17 |
|
18 | var util = require('./util');
|
19 |
|
20 | var repo = require('./repo');
|
21 |
|
22 | var updateContributors = require('./contributors');
|
23 |
|
24 | var cwd = process.cwd();
|
25 | var defaultRCFile = path.join(cwd, '.all-contributorsrc');
|
26 | var yargv = yargs.help('help').alias('h', 'help').alias('v', 'version').version().recommendCommands().command('generate', 'Generate the list of contributors').usage('Usage: $0 generate').command('add', 'add a new contributor').usage('Usage: $0 add <username> <contribution>').command('init', 'Prepare the project to be used with this tool').usage('Usage: $0 init').command('check', 'Compares contributors from the repository with the ones credited in .all-contributorsrc').usage('Usage: $0 check').boolean('commit').default('files', ['README.md']).default('contributorsPerLine', 7).option('contributorsSortAlphabetically', {
|
27 | type: 'boolean',
|
28 | default: false,
|
29 | description: 'Sort the list of contributors alphabetically in the generated list'
|
30 | }).default('contributors', []).default('config', defaultRCFile).config('config', function (configPath) {
|
31 | try {
|
32 | return util.configFile.readConfig(configPath);
|
33 | } catch (error) {
|
34 | if (error instanceof SyntaxError || configPath !== defaultRCFile) {
|
35 | onError(error);
|
36 | }
|
37 | }
|
38 | }).argv;
|
39 |
|
40 | function startGeneration(argv) {
|
41 | return Promise.all(argv.files.map(function (file) {
|
42 | var filePath = path.join(cwd, file);
|
43 | return util.markdown.read(filePath).then(function (fileContent) {
|
44 | var newFileContent = generate(argv, argv.contributors, fileContent);
|
45 | return util.markdown.write(filePath, newFileContent);
|
46 | });
|
47 | }));
|
48 | }
|
49 |
|
50 | function addContribution(argv) {
|
51 | var username = argv._[1] === undefined ? undefined : String(argv._[1]);
|
52 | var contributions = argv._[2];
|
53 |
|
54 | return updateContributors(argv, username, contributions).then(function (data) {
|
55 | argv.contributors = data.contributors;
|
56 | return startGeneration(argv).then(function () {
|
57 | if (argv.commit) {
|
58 | return util.git.commit(argv, data);
|
59 | }
|
60 | });
|
61 | });
|
62 | }
|
63 |
|
64 | function checkContributors(argv) {
|
65 | var configData = util.configFile.readConfig(argv.config);
|
66 | return repo.getContributors(configData.projectOwner, configData.projectName, configData.repoType, configData.repoHost).then(function (repoContributors) {
|
67 | var checkKey = repo.getCheckKey(configData.repoType);
|
68 | var knownContributions = configData.contributors.reduce(function (obj, item) {
|
69 | obj[item[checkKey]] = item.contributions;
|
70 | return obj;
|
71 | }, {});
|
72 | var knownContributors = configData.contributors.map(function (contributor) {
|
73 | return contributor[checkKey];
|
74 | });
|
75 | var missingInConfig = repoContributors.filter(function (key) {
|
76 | return !knownContributors.includes(key);
|
77 | });
|
78 | var missingFromRepo = knownContributors.filter(function (key) {
|
79 | return !repoContributors.includes(key) && (knownContributions[key].includes('code') || knownContributions[key].includes('test'));
|
80 | });
|
81 |
|
82 | if (missingInConfig.length) {
|
83 | process.stdout.write(chalk.bold('Missing contributors in .all-contributorsrc:\n'));
|
84 | process.stdout.write(` ${missingInConfig.join(', ')}\n`);
|
85 | }
|
86 |
|
87 | if (missingFromRepo.length) {
|
88 | process.stdout.write(chalk.bold('Unknown contributors found in .all-contributorsrc:\n'));
|
89 | process.stdout.write(`${missingFromRepo.join(', ')}\n`);
|
90 | }
|
91 | });
|
92 | }
|
93 |
|
94 | function onError(error) {
|
95 | if (error) {
|
96 | console.error(error.message);
|
97 | process.exit(1);
|
98 | }
|
99 |
|
100 | process.exit(0);
|
101 | }
|
102 |
|
103 | function promptForCommand(argv) {
|
104 | var questions = [{
|
105 | type: 'list',
|
106 | name: 'command',
|
107 | message: 'What do you want to do?',
|
108 | choices: [{
|
109 | name: 'Add new contributor or edit contribution type',
|
110 | value: 'add'
|
111 | }, {
|
112 | name: 'Re-generate the contributors list',
|
113 | value: 'generate'
|
114 | }, {
|
115 | name: 'Compare contributors from the repository with the credited ones',
|
116 | value: 'check'
|
117 | }],
|
118 | when: !argv._[0],
|
119 | default: 0
|
120 | }];
|
121 | return inquirer.prompt(questions).then(function (answers) {
|
122 | return answers.command || argv._[0];
|
123 | });
|
124 | }
|
125 |
|
126 | promptForCommand(yargv).then(function (command) {
|
127 | switch (command) {
|
128 | case 'init':
|
129 | return init();
|
130 |
|
131 | case 'generate':
|
132 | return startGeneration(yargv);
|
133 |
|
134 | case 'add':
|
135 | return addContribution(yargv);
|
136 |
|
137 | case 'check':
|
138 | return checkContributors(yargv);
|
139 |
|
140 | default:
|
141 | throw new Error(`Unknown command ${command}`);
|
142 | }
|
143 | }).catch(onError); |
\ | No newline at end of file |