Skip to content

Commit 5f78126

Browse files
authoredOct 9, 2024
feat: check git status before bump if option.all is falsy (#44)
1 parent 81afd3c commit 5f78126

File tree

4 files changed

+22
-0
lines changed

4 files changed

+22
-0
lines changed
 

‎src/cli/index.ts

+12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { VersionBumpProgress } from '../types/version-bump-progress'
22
import process from 'node:process'
33
import symbols from 'log-symbols'
4+
import * as ezSpawn from '@jsdevtools/ez-spawn'
45
import { version as packageVersion } from '../../package.json'
56
import { ProgressEvent } from '../types/version-bump-progress'
67
import { versionBump } from '../version-bump'
@@ -28,6 +29,10 @@ export async function main(): Promise<void> {
2829
process.exit(ExitCode.Success)
2930
}
3031
else {
32+
if (!options.all && !options.noGitCheck) {
33+
checkGitStatus()
34+
}
35+
3136
if (!quiet)
3237
options.progress = options.progress ? options.progress : progress
3338

@@ -39,6 +44,13 @@ export async function main(): Promise<void> {
3944
}
4045
}
4146

47+
export function checkGitStatus() {
48+
const { stdout } = ezSpawn.sync('git', ['status', '--porcelain'])
49+
if (stdout.trim()) {
50+
throw new Error(`Git working tree is not clean:\n${stdout}`)
51+
}
52+
}
53+
4254
function progress({ event, script, updatedFiles, skippedFiles, newVersion }: VersionBumpProgress): void {
4355
switch (event) {
4456
case ProgressEvent.FileUpdated:

‎src/cli/parse-args.ts

+2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export async function parseArgs(): Promise<ParsedArgs> {
3636
sign: args.sign,
3737
push: args.push,
3838
all: args.all,
39+
noGitCheck: args.noGitCheck,
3940
confirm: !args.yes,
4041
noVerify: !args.verify,
4142
files: [...(args['--'] || []), ...resultArgs],
@@ -75,6 +76,7 @@ export function loadCliArgs(argv = process.argv) {
7576
.usage('[...files]')
7677
.option('--preid <preid>', 'ID for prerelease')
7778
.option('--all', `Include all files (default: ${bumpConfigDefaults.all})`)
79+
.option('--no-git-check', `Skip git check`, { default: bumpConfigDefaults.noGitCheck })
7880
.option('-c, --commit [msg]', 'Commit message', { default: true })
7981
.option('--no-commit', 'Skip commit', { default: false })
8082
.option('-t, --tag [tag]', 'Tag name', { default: true })

‎src/config.ts

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export const bumpConfigDefaults: VersionBumpOptions = {
1414
confirm: true,
1515
ignoreScripts: false,
1616
all: false,
17+
noGitCheck: true,
1718
files: [],
1819
}
1920

‎src/types/version-bump-options.ts

+7
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,13 @@ export interface VersionBumpOptions {
7171
*/
7272
all?: boolean
7373

74+
/**
75+
* Indicates whether the git working tree needs to be cleared before bumping.
76+
*
77+
* Defaults to `true`.
78+
*/
79+
noGitCheck?: boolean
80+
7481
/**
7582
* Prompt for confirmation
7683
*

0 commit comments

Comments
 (0)