forked from sindresorhus/conduct
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·91 lines (77 loc) · 2.39 KB
/
cli.js
File metadata and controls
executable file
·91 lines (77 loc) · 2.39 KB
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
#!/usr/bin/env node
'use strict';
const path = require('path');
const fs = require('fs');
const meow = require('meow');
const inquirer = require('inquirer');
const globby = require('globby');
const getEmails = require('get-emails');
const chalk = require('chalk');
const Conf = require('conf');
const execa = require('execa');
const logSymbols = require('log-symbols');
const config = new Conf();
const cli = meow(`
Usage
$ conduct
`);
if (cli.flags.email) {
config.set('email', cli.flags.email);
}
function findEmail() {
let email;
try {
email = execa.sync('git', ['config', 'user.email']).stdout.trim();
} catch (err) {}
return email;
}
function write(filepath, email) {
const src = fs.readFileSync(path.join(__dirname, 'vendor/code_of_conduct.md'), 'utf8');
fs.writeFileSync(filepath, src.replace('[INSERT EMAIL ADDRESS]', email));
}
function generate(filepath, email) {
write(filepath, email);
console.log(`${logSymbols.success} Added a Code of Conduct to your project ❤️\n\n${chalk.bold('Please carefully read this document and be ready to enforce it.')}\n\nAdd the following to your contributing.md or readme.md:\nPlease note that this project is released with a [Contributor Code of Conduct](${filepath}). By participating in this project you agree to abide by its terms.`);
}
function init() {
const results = globby.sync([
'code_of_conduct.*',
'code-of-conduct.*',
'.github/code_of_conduct.*',
'.github/code-of-conduct.*'
], {nocase: true});
// Update existing
if (results.length > 0) {
const filepath = results[0];
const existingSrc = fs.readFileSync(filepath, 'utf8');
const email = Array.from(getEmails(existingSrc))[0];
write(filepath, cli.flags.email || email);
console.log(`${logSymbols.success} Updated your Code of Conduct`);
return;
}
const filepath = 'code-of-conduct.md';
if (config.has('email')) {
generate(filepath, config.get('email'));
return;
}
const email = findEmail();
if (email) {
config.set('email', email);
generate(filepath, email);
return;
}
if (process.stdout.isTTY) {
inquirer.prompt([{
type: 'input',
name: 'email',
message: `Couldn't infer your email. Please enter your email:`,
validate: x => x.includes('@')
}]).then(answers => {
generate(filepath, answers.email);
});
} else {
console.error(`Run \`${chalk.cyan('conduct --email=your@email.com')}\` once to save your email.`);
process.exit(1);
}
}
init();