forked from antfu-collective/bumpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathversion-bump.ts
125 lines (105 loc) · 4.19 KB
/
version-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
import process from 'node:process'
import * as ezSpawn from '@jsdevtools/ez-spawn'
import c from 'picocolors'
import symbols from 'log-symbols'
import prompts from 'prompts'
import { getNewVersion } from './get-new-version'
import { getOldVersion } from './get-old-version'
import { formatVersionString, gitCommit, gitPush, gitTag } from './git'
import { Operation } from './operation'
import { runNpmScript } from './run-npm-script'
import type { VersionBumpOptions } from './types/version-bump-options'
import { NpmScript } from './types/version-bump-progress'
import type { VersionBumpResults } from './types/version-bump-results'
import { updateFiles } from './update-files'
/**
* Prompts the user for a version number and updates package.json and package-lock.json.
*
* @returns - The new version number
*/
export async function versionBump(): Promise<VersionBumpResults>
/**
* Bumps the version number in package.json, package-lock.json.
*
* @param release
* The release version or type. Can be one of the following:
*
* - The new version number (e.g. "1.23.456")
* - A release type (e.g. "major", "minor", "patch", "prerelease", etc.)
* - "prompt" to prompt the user for the version number
*/
export async function versionBump(release: string): Promise<VersionBumpResults>
/**
* Bumps the version number in one or more files, prompting the user if necessary.
* Optionally also commits, tags, and pushes to git.
*/
export async function versionBump(options: VersionBumpOptions & { oldVersion?: string }): Promise<VersionBumpResults>
/**
* Bumps the version number in one or more files, prompting the user if necessary.
* Optionally also commits, tags, and pushes to git.
*/
export async function versionBump(arg: (VersionBumpOptions & { oldVersion?: string }) | string = {}): Promise<VersionBumpResults | undefined> {
if (typeof arg === 'string')
arg = { release: arg }
const operation = await Operation.start(arg)
// Get the old and new version numbers
await getOldVersion(operation)
await getNewVersion(operation)
if (arg.confirm) {
printSummary(operation)
if (!await prompts({
name: 'yes',
type: 'confirm',
message: 'Bump?',
initial: true,
}).then(r => r.yes))
process.exit(1)
}
// Run npm preversion script, if any
await runNpmScript(NpmScript.PreVersion, operation)
// Update the version number in all files
await updateFiles(operation)
if (operation.options.execute) {
console.log(symbols.info, 'Executing script', operation.options.execute)
await ezSpawn.async(operation.options.execute, { stdio: 'inherit' })
console.log(symbols.success, 'Script finished')
}
// Run npm version script, if any
await runNpmScript(NpmScript.Version, operation)
// Git commit and tag, if enabled
await gitCommit(operation)
await gitTag(operation)
// Run npm postversion script, if any
await runNpmScript(NpmScript.PostVersion, operation)
// Push the git commit and tag, if enabled
await gitPush(operation)
return operation.results
}
function printSummary(operation: Operation) {
console.log()
console.log(` files ${operation.options.files.map(i => c.bold(i)).join('\n ')}`)
if (operation.options.commit)
console.log(` commit ${c.bold(formatVersionString(operation.options.commit.message, operation.state.newVersion))}`)
if (operation.options.tag)
console.log(` tag ${c.bold(formatVersionString(operation.options.tag.name, operation.state.newVersion))}`)
if (operation.options.execute)
console.log(` execute ${c.bold(operation.options.execute)}`)
if (operation.options.push)
console.log(` push ${c.cyan(c.bold('yes'))}`)
console.log()
console.log(` from ${c.bold(operation.state.oldVersion)}`)
console.log(` to ${c.green(c.bold(operation.state.newVersion))}`)
console.log()
}
/**
* Bumps the version number in one or more files, prompting users if necessary.
*/
export async function versionBumpInfo(arg: VersionBumpOptions | string = {}): Promise<Operation> {
if (typeof arg === 'string')
arg = { release: arg }
const operation = await Operation.start(arg)
// Get the old and new version numbers
await getOldVersion(operation)
await getNewVersion(operation)
return operation
}