Skip to content

Commit 618973a

Browse files
committed
Create init command
Rebaseeeee
1 parent 5eefffd commit 618973a

File tree

9 files changed

+4002
-217
lines changed

9 files changed

+4002
-217
lines changed

.babelrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"presets": ["es2015", "es2017"],
3-
"plugins": ["transform-runtime"],
3+
"plugins": ["transform-runtime", "transform-object-rest-spread"],
44
"env": {
55
"test": {
66
"plugins": ["istanbul"]

lib/_options.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ module.exports = {
4545
description: 'The token generated with repo access'
4646
},
4747
{
48-
short: '-U',
48+
short: '-a',
4949
name: 'api-url',
5050
valueType: '<url>',
5151
description: 'Override the GitHub API URL, allows gren to connect to a private GHE installation'
@@ -76,7 +76,7 @@ module.exports = {
7676
defaultValue: 'issues'
7777
},
7878
{
79-
short: '-a',
79+
short: '-N',
8080
name: 'include-messages',
8181
valueType: '<merge|commits|all>',
8282
description: 'Filter the messages added to the release notes. Only used when --data-source used is commits [commits]',

lib/gren-init.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/usr/bin/env node
2+
3+
import gren from 'commander';
4+
import { green } from 'chalk';
5+
import ObjectAssignDeep from 'object-assign-deep';
6+
import init from '../dist/_init';
7+
import utils from '../dist/_utils';
8+
import fs from 'fs';
9+
10+
gren
11+
.name(`${green('gren')} release`)
12+
.description('Initialise the module options.')
13+
.parse(process.argv);
14+
15+
init()
16+
.then(({
17+
fileExist,
18+
apiUrlType,
19+
ignoreCommitsWithConfirm,
20+
ignoreLabelsConfirm,
21+
ignoreIssuesWithConfirm,
22+
ignoreTagsWithConfirm,
23+
fileType,
24+
...data
25+
}) => {
26+
if (fileExist === 'abort') {
27+
console.log('Command aborted.');
28+
return;
29+
}
30+
31+
if (fileExist === 'override') {
32+
const fileContent = utils.writeConfigToFile(fileType, data);
33+
34+
utils.cleanConfig(true);
35+
fs.writeFileSync(fileType, fileContent);
36+
37+
console.log(green(`\nGreat news! Your ${fileType} as been created!`));
38+
return;
39+
}
40+
41+
const currentConfig = utils.getConfigFromFile(process.cwd());
42+
const fileContent = utils.writeConfigToFile(fileType, ObjectAssignDeep({}, currentConfig, data));
43+
44+
fs.writeFileSync(fileType, fileContent);
45+
46+
console.log(green(`\nGreat news! Your ${fileType} as been created!`));
47+
})
48+
.catch(error => {
49+
console.log(error);
50+
process.exit(1);
51+
});

lib/gren.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ gren
1717
.version(version)
1818
.description(`gren (🤖 ) ${description}`)
1919
.usage('<command> [options]')
20+
.command('init', 'initialise the module')
2021
.command('release', 'Release into chunk').alias('r')
2122
.command('changelog', 'Write a motherfucking changelog').alias('c')
2223
.command('examples', 'Show few examples of stuff that you can do <cmd>')

lib/src/_init.js

Lines changed: 248 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,248 @@
1+
import inquirer from 'inquirer';
2+
import utils from './_utils';
3+
import GitHubInfo from './GitHubInfo';
4+
import GitHub from 'github-api';
5+
import chalk from 'chalk';
6+
import { isUri } from 'valid-url';
7+
8+
const githubApi = new GitHubInfo();
9+
const prompt = inquirer.createPromptModule();
10+
const { GREN_GITHUB_TOKEN } = process.env;
11+
12+
if (!GREN_GITHUB_TOKEN) {
13+
console.error(chalk.red('Can\'t find GREN_GITHUB_TOKEN. Please configure your environment') + chalk.blue('\nSee https://github.com/github-tools/github-release-notes#setup'));
14+
15+
process.exit(1);
16+
}
17+
18+
const getInfo = async() => {
19+
try {
20+
const infos = await githubApi.repo;
21+
22+
return infos;
23+
} catch (error) {
24+
throw chalk.red('You have to run this command in a git repo folder');
25+
}
26+
};
27+
28+
const getLabels = async() => {
29+
const { username, repo } = await getInfo();
30+
31+
try {
32+
const gitHub = new GitHub({
33+
GREN_GITHUB_TOKEN
34+
});
35+
const issues = gitHub.getIssues(username, repo);
36+
const { data: labels } = await issues.listLabels();
37+
38+
return labels;
39+
} catch (error) {
40+
console.warn(chalk.bgYellow(chalk.black('I can\'t get your repo labels, make sure you are online to use the complete initialisation')));
41+
return false;
42+
}
43+
};
44+
45+
const getQuestions = async() => {
46+
const labels = await getLabels();
47+
48+
return [
49+
{
50+
name: 'apiUrlType',
51+
type: 'list',
52+
message: 'What type of APIs do you need?',
53+
choices: [{
54+
name: 'Normal',
55+
value: false
56+
},
57+
{
58+
name: 'GitHub Enterprise',
59+
value: 'ghe'
60+
}]
61+
},
62+
{
63+
name: 'apiUrl',
64+
type: 'input',
65+
message: 'Write your Enterprise url',
66+
suffix: chalk.blueBright(' e.g. https://MY_ENTERPRISE_DOMAIN/api/v3'),
67+
when: ({ apiUrlType }) => apiUrlType === 'ghe',
68+
validate: value => isUri(value) ? true : 'Please type a valid url'
69+
},
70+
{
71+
name: 'dataSource',
72+
type: 'list',
73+
message: 'Where shall I get the informations from?',
74+
choices: [{
75+
value: 'issues',
76+
name: 'Issues (Time based)'
77+
},
78+
{
79+
value: 'milestones',
80+
name: 'Issues (Milestone based)'
81+
},
82+
{
83+
value: 'commits',
84+
name: 'Commits'
85+
},
86+
{
87+
value: 'prs',
88+
name: 'Pull Requests'
89+
}]
90+
},
91+
{
92+
name: 'prefix',
93+
type: 'input',
94+
suffix: chalk.blueBright(' e.g. v'),
95+
message: 'Do you want to add a prefix to release titles?'
96+
},
97+
{
98+
name: 'includeMessages',
99+
type: 'list',
100+
message: 'Which type of commits do you want to include?',
101+
choices: [{
102+
value: 'merges',
103+
name: 'Merges'
104+
},
105+
{
106+
value: 'commits',
107+
name: 'Commits'
108+
},
109+
{
110+
value: 'all',
111+
name: 'All'
112+
}],
113+
when: ({ dataSource }) => dataSource === 'commits'
114+
},
115+
{
116+
name: 'ignoreCommitsWithConfirm',
117+
type: 'confirm',
118+
default: false,
119+
message: 'Do you want to ignore commits containing certain words?',
120+
when: ({ dataSource }) => dataSource === 'commits'
121+
},
122+
{
123+
name: 'ignoreCommitsWith',
124+
type: 'input',
125+
message: 'Which ones? Use commas to separate.',
126+
suffix: chalk.blueBright(' e.g. changelog,release'),
127+
when: ({ ignoreCommitsWithConfirm, dataSource }) => dataSource === 'commits' && ignoreCommitsWithConfirm,
128+
filter: value => value.replace(/\s/g).split(',')
129+
},
130+
{
131+
name: 'ignoreLabelsConfirm',
132+
type: 'confirm',
133+
default: false,
134+
message: 'Do you want to not output certain labels in the notes?',
135+
when: ({ dataSource }) => Array.isArray(labels) && dataSource !== 'commits'
136+
},
137+
{
138+
name: 'ignoreLabels',
139+
type: 'checkbox',
140+
message: 'Select the labels that should be excluded',
141+
when: ({ ignoreLabelsConfirm }) => ignoreLabelsConfirm,
142+
choices: Array.isArray(labels) && labels.map(({ name }) => name)
143+
},
144+
{
145+
name: 'ignoreIssuesWithConfirm',
146+
type: 'confirm',
147+
message: 'Do you want to ignore issues/prs that have certain labels?',
148+
default: false,
149+
when: ({ dataSource }) => Array.isArray(labels) && dataSource !== 'commits'
150+
},
151+
{
152+
name: 'ignoreIssuesWith',
153+
type: 'checkbox',
154+
message: 'Select the labels that should exclude the issue',
155+
when: ({ ignoreIssuesWithConfirm }) => ignoreIssuesWithConfirm,
156+
choices: Array.isArray(labels) && labels.map(({ name }) => name)
157+
},
158+
{
159+
name: 'onlyMilestones',
160+
type: 'confirm',
161+
message: 'Do you want to only include issues/prs that belong to a milestone?',
162+
default: false,
163+
when: ({ dataSource }) => dataSource === 'issues' || dataSource === 'prs'
164+
},
165+
{
166+
name: 'ignoreTagsWithConfirm',
167+
type: 'confirm',
168+
default: false,
169+
message: 'Do you want to ignore tags containing certain words?'
170+
},
171+
{
172+
name: 'ignoreTagsWith',
173+
type: 'input',
174+
message: 'Which ones? Use commas to separate',
175+
suffix: chalk.blueBright(' e.g. -rc,-alpha,test'),
176+
filter: value => value.replace(/\s/g).split(','),
177+
when: ({ ignoreTagsWithConfirm }) => ignoreTagsWithConfirm
178+
},
179+
{
180+
name: 'groupBy',
181+
type: 'list',
182+
message: 'Do you want to group your notes?',
183+
when: ({ dataSource }) => dataSource !== 'commits',
184+
choices: [{
185+
value: false,
186+
name: 'No'
187+
},
188+
{
189+
value: 'label',
190+
name: 'Use existing labels'
191+
},
192+
{
193+
value: {},
194+
name: 'Use custom configuration'
195+
}]
196+
},
197+
{
198+
name: 'milestoneMatch',
199+
type: 'input',
200+
default: 'Release {{tag_name}}',
201+
message: 'How can I link your tags to Milestone titles?',
202+
when: ({ dataSource }) => dataSource === 'milestones'
203+
},
204+
{
205+
name: 'changelogFilename',
206+
default: 'CHANGELOG.md',
207+
message: 'What file name do you want for your changelog?',
208+
vaidate: value => {
209+
console.log(utils.getFileExtension(value));
210+
return utils.getFileExtension(value) === 'md' ? true : 'Has to be a markdown file!';
211+
}
212+
},
213+
{
214+
name: 'fileExist',
215+
type: 'list',
216+
message: 'Looks like you already have a configuration file. What do you want me to do?',
217+
choices: [{
218+
value: 'abort',
219+
name: 'Oops, stop this'
220+
},
221+
{
222+
value: 'override',
223+
name: 'Override my existing file'
224+
},
225+
{
226+
value: 'merge',
227+
name: 'Merge these settings over existing ones'
228+
}],
229+
when: () => Object.keys(utils.getConfigFromFile(process.cwd())).length > 0
230+
},
231+
{
232+
name: 'fileType',
233+
type: 'list',
234+
message: 'Which extension would you like for your file?',
235+
choices: utils.getFileTypes(),
236+
when: ({ fileExist }) => fileExist !== 'abort'
237+
}
238+
];
239+
};
240+
241+
const configure = async() => {
242+
const questions = await getQuestions();
243+
process.stdout.write('\n🤖 : Hello, I\'m going to ask a couple of questions, to set gren up!\n\n');
244+
245+
return prompt(questions);
246+
};
247+
248+
export default configure;

0 commit comments

Comments
 (0)