-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbump.ts
236 lines (209 loc) · 7.18 KB
/
bump.ts
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
import { Command, flags } from '@oclif/command'
import * as inquirer from 'inquirer'
import chalk from 'chalk'
import Listr from 'listr'
import getAppCurrentData from '../../helpers/get-app-current-data'
import updatePackageFile, { cleanUp, installDependencies } from '../../helpers/update-package-file'
import updateChangelog from '../../helpers/update-changelog'
import updateReadme from '../../helpers/update-readme'
import checkChangelog from '../../helpers/check-changelog'
import log from '../../helpers/log-messages'
import { ErrorEnums } from '../../types/error-enums'
type Semver = 'major' | 'minor' | 'patch'
interface CheckVersionsAnswers {
currentVersionWarning: boolean
otherVersion: string
}
interface OtherVersionQuestion {
type: string
name: string
message: string
validate: (value: string) => true | string
}
export default class Bump extends Command {
static description = 'bump the app version to the next major, minor or patch'
static flags = {
help: flags.help({ char: 'h' }),
// flag with a value (-v, --version)
version: flags.string({ char: 'v', description: 'new version' }),
}
private startLog = (appName?: string, version?: string): void => {
if (appName && version) {
this.log('')
this.log('Bumping the version of the app ' + chalk.magenta(`${appName} (${version})`))
this.log('')
}
}
private doneLog = (version: string): void => {
this.log('')
log(`Done! New version is ${chalk.magenta(version)}`, 'success')
}
private sameVersionLog = (version: string): void =>
log(
`Nothing to do here! Try setting a different version - ${chalk.magenta('current version ' + version)}`,
'warning'
)
private actions = async (version: string): Promise<void> => {
const tasks = new Listr([
{
title: 'Update package version',
task: () => updatePackageFile(version),
},
{
title: 'Cleanup',
task: () => cleanUp(),
},
{
title: 'Install dependencies',
task: () => installDependencies(),
},
{
title: 'Update CHANGELOG',
task: () => {
updateChangelog(version)
},
},
{
title: 'Update README',
task: () => {
updateReadme(version)
},
},
])
tasks
.run()
.then(() => this.doneLog(version))
.catch((error: Error) => {
console.error(error)
})
}
private showVersionBumped = (version: Semver): string | undefined => {
const appData = getAppCurrentData()
const tempVer = appData?.version?.split('.') || ''
switch (version) {
case 'major':
return `${Number(tempVer[0]) + 1}.${tempVer[1]}.${tempVer[2]}`
case 'minor':
return `${tempVer[0]}.${Number(tempVer[1]) + 1}.${tempVer[2]}`
case 'patch':
return `${tempVer[0]}.${tempVer[1]}.${Number(tempVer[2]) + 1}`
default:
break
}
}
private confirmLowerVersion = (version: string, otherVersionQuestion: OtherVersionQuestion): void => {
const appData = getAppCurrentData()
const checkVersionQuestions = [
{
type: 'confirm',
name: 'currentVersionWarning',
message: chalk`The version you are trying to set is lower than the current {blue [${appData?.version}]}. Would you like to proceed?`,
},
{
...otherVersionQuestion,
when: (answers: CheckVersionsAnswers) => !answers.currentVersionWarning,
},
]
inquirer.prompt(checkVersionQuestions).then((answers) => {
if (
!answers.currentVersionWarning &&
answers.otherVersion &&
typeof appData?.version !== 'undefined' &&
answers.otherVersion < appData?.version
) {
this.confirmLowerVersion(answers.otherVersion, otherVersionQuestion)
} else if (
!answers.currentVersionWarning &&
answers.otherVersion &&
typeof appData?.version !== 'undefined' &&
answers.otherVersion > appData?.version
) {
this.actions(answers.otherVersion)
} else {
this.actions(version)
}
})
}
async run(): Promise<string | boolean | void> {
const { flags: flag } = this.parse(Bump)
const appData = getAppCurrentData()
const checkForUpdates = await checkChangelog(true)
if (!checkForUpdates || checkForUpdates === ErrorEnums.ERROR_NO_CHANGELOG_UPDATES) {
return log('There are no changes in the CHANGELOG! You need to update it before using this command.', 'error')
}
const otherVersionQuestion = {
type: 'input',
name: 'otherVersion',
message: `Enter a version number ${chalk.blue('x.x.x')} =>`,
validate: function (value: string) {
const pass = value.match(/^(\d+)\.(\d+)\.(\d+)(?:-([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?(?:\+[0-9A-Za-z-]+)?$/i)
if (pass) {
return true
}
return 'Please enter a valid semver version'
},
}
// Start program
if (flag.version && typeof appData?.version !== 'undefined' && flag.version === appData.version) {
this.sameVersionLog(flag.version)
} else {
this.startLog(appData?.name, appData?.version)
}
// Check version from flag
if (flag.version && typeof appData?.version !== 'undefined' && flag.version < appData.version) {
this.confirmLowerVersion(flag.version, otherVersionQuestion)
} else if (flag.version && typeof appData?.version !== 'undefined' && flag.version > appData.version) {
this.actions(flag.version)
}
if (!flag.version) {
const questions = [
{
type: 'list',
name: 'nextVersion',
message: `Select next version, or specify one.`,
choices: [
{
name: `major ${chalk.gray(this.showVersionBumped('major'))}`,
value: this.showVersionBumped('major'),
},
{
name: `minor ${chalk.gray(this.showVersionBumped('minor'))}`,
value: this.showVersionBumped('minor'),
},
{
name: `patch ${chalk.gray(this.showVersionBumped('patch'))}`,
value: this.showVersionBumped('patch'),
},
new inquirer.Separator(),
{
name: `Other ${chalk.gray('Specify one')}`,
value: 'other',
},
],
},
]
inquirer.prompt(questions).then((listAnswers): void => {
if (listAnswers.nextVersion === 'other') {
inquirer.prompt([otherVersionQuestion]).then((finalAnswer): void => {
if (
finalAnswer.otherVersion &&
typeof appData?.version !== 'undefined' &&
finalAnswer.otherVersion < appData.version
) {
this.confirmLowerVersion(finalAnswer.otherVersion, otherVersionQuestion)
} else if (
finalAnswer.otherVersion &&
typeof appData?.version !== 'undefined' &&
finalAnswer.otherVersion > appData.version
) {
updatePackageFile(finalAnswer.otherVersion)
this.doneLog(finalAnswer.otherVersion)
}
})
} else {
this.actions(listAnswers.nextVersion)
}
})
}
}
}