forked from all-contributors/cli
/
cli.js
executable file
·177 lines (162 loc) · 4.67 KB
/
cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/usr/bin/env node
/* eslint-disable no-console */
const path = require('path')
const yargs = require('yargs')
const chalk = require('chalk')
const inquirer = require('inquirer')
const init = require('./init')
const generate = require('./generate')
const util = require('./util')
const repo = require('./repo')
const updateContributors = require('./contributors')
const cwd = process.cwd()
const defaultRCFile = path.join(cwd, '.all-contributorsrc')
const yargv = yargs
.help('help')
.alias('h', 'help')
.alias('v', 'version')
.version()
.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)
.default('contributors', [])
.default('config', defaultRCFile)
.config('config', configPath => {
try {
return util.configFile.readConfig(configPath)
} catch (error) {
if (configPath !== defaultRCFile) {
onError(error)
}
}
}).argv
function startGeneration(argv) {
return Promise.all(
argv.files.map(file => {
const filePath = path.join(cwd, file)
return util.markdown.read(filePath).then(fileContent => {
const newFileContent = generate(argv, argv.contributors, fileContent)
return util.markdown.write(filePath, newFileContent)
})
}),
)
}
function addContribution(argv) {
const username = argv._[1]
const 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)
}
})
})
}
function checkContributors(argv) {
const configData = util.configFile.readConfig(argv.config)
return repo
.getContributors(
configData.projectOwner,
configData.projectName,
configData.repoType,
configData.repoHost,
configData.privateToken,
)
.then(repoContributors => {
const checkKey = repo.getCheckKey(configData.repoType)
const knownContributions = configData.contributors.reduce((obj, item) => {
obj[item[checkKey]] = item.contributions
return obj
}, {})
const knownContributors = configData.contributors.map(
contributor => contributor[checkKey],
)
const missingInConfig = repoContributors.filter(
key => !knownContributors.includes(key),
)
const missingFromRepo = knownContributors.filter(key => {
return (
!repoContributors.includes(key) &&
(knownContributions[key].includes('code') ||
knownContributions[key].includes('test'))
)
})
if (missingInConfig.length) {
process.stdout.write(
chalk.bold('Missing contributors in .all-contributorsrc:\n'),
)
process.stdout.write(` ${missingInConfig.join(', ')}\n`)
}
if (missingFromRepo.length) {
process.stdout.write(
chalk.bold('Unknown contributors found in .all-contributorsrc:\n'),
)
process.stdout.write(`${missingFromRepo.join(', ')}\n`)
}
})
}
function onError(error) {
if (error) {
console.error(error.message)
process.exit(1)
}
process.exit(0)
}
function promptForCommand(argv) {
const questions = [
{
type: 'list',
name: 'command',
message: 'What do you want to do?',
choices: [
{
name: 'Add new contributor or edit contribution type',
value: 'add',
},
{
name: 'Re-generate the contributors list',
value: 'generate',
},
{
name:
'Compare contributors from the repository with the credited ones',
value: 'check',
},
],
when: !argv._[0],
default: 0,
},
]
return inquirer.prompt(questions).then(answers => {
return answers.command || argv._[0]
})
}
promptForCommand(yargv)
.then(command => {
switch (command) {
case 'init':
return init()
case 'generate':
return startGeneration(yargv)
case 'add':
return addContribution(yargv)
case 'check':
return checkContributors(yargv)
default:
throw new Error(`Unknown command ${command}`)
}
})
.catch(onError)