Skip to content

Commit

Permalink
feat: check git status before bump if option.all is falsy (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
litingyes authored Oct 9, 2024
1 parent 81afd3c commit 5f78126
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/cli/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { VersionBumpProgress } from '../types/version-bump-progress'
import process from 'node:process'
import symbols from 'log-symbols'
import * as ezSpawn from '@jsdevtools/ez-spawn'
import { version as packageVersion } from '../../package.json'
import { ProgressEvent } from '../types/version-bump-progress'
import { versionBump } from '../version-bump'
Expand Down Expand Up @@ -28,6 +29,10 @@ export async function main(): Promise<void> {
process.exit(ExitCode.Success)
}
else {
if (!options.all && !options.noGitCheck) {
checkGitStatus()
}

if (!quiet)
options.progress = options.progress ? options.progress : progress

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

export function checkGitStatus() {
const { stdout } = ezSpawn.sync('git', ['status', '--porcelain'])
if (stdout.trim()) {
throw new Error(`Git working tree is not clean:\n${stdout}`)
}
}

function progress({ event, script, updatedFiles, skippedFiles, newVersion }: VersionBumpProgress): void {
switch (event) {
case ProgressEvent.FileUpdated:
Expand Down
2 changes: 2 additions & 0 deletions src/cli/parse-args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export async function parseArgs(): Promise<ParsedArgs> {
sign: args.sign,
push: args.push,
all: args.all,
noGitCheck: args.noGitCheck,
confirm: !args.yes,
noVerify: !args.verify,
files: [...(args['--'] || []), ...resultArgs],
Expand Down Expand Up @@ -75,6 +76,7 @@ export function loadCliArgs(argv = process.argv) {
.usage('[...files]')
.option('--preid <preid>', 'ID for prerelease')
.option('--all', `Include all files (default: ${bumpConfigDefaults.all})`)
.option('--no-git-check', `Skip git check`, { default: bumpConfigDefaults.noGitCheck })
.option('-c, --commit [msg]', 'Commit message', { default: true })
.option('--no-commit', 'Skip commit', { default: false })
.option('-t, --tag [tag]', 'Tag name', { default: true })
Expand Down
1 change: 1 addition & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export const bumpConfigDefaults: VersionBumpOptions = {
confirm: true,
ignoreScripts: false,
all: false,
noGitCheck: true,
files: [],
}

Expand Down
7 changes: 7 additions & 0 deletions src/types/version-bump-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ export interface VersionBumpOptions {
*/
all?: boolean

/**
* Indicates whether the git working tree needs to be cleared before bumping.
*
* Defaults to `true`.
*/
noGitCheck?: boolean

/**
* Prompt for confirmation
*
Expand Down

0 comments on commit 5f78126

Please sign in to comment.