|
| 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