Skip to content

Commit 6bb8fdf

Browse files
committed
fix(scripts/publish): refuse to publish with a dirty working tree
Mirrors the guard in scripts/bump.mts. Without this, a developer with uncommitted edits in src/ could run pnpm publish and ship unreviewed code to npm. Adds a checkGitStatus() helper (same shape as bump.mts's) and a gate at the top of main() that aborts unless --force is passed.
1 parent 0b4be71 commit 6bb8fdf

1 file changed

Lines changed: 26 additions & 0 deletions

File tree

scripts/publish.mts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,22 @@ async function pushExistingTag(
341341
return true
342342
}
343343

344+
/**
345+
* Check if the git working tree is clean (no uncommitted changes).
346+
*/
347+
async function checkGitStatus(): Promise<boolean> {
348+
const result = await runCommandWithOutput('git', ['status', '--porcelain'])
349+
const stdout =
350+
typeof result.stdout === 'string' ? result.stdout : result.stdout.toString()
351+
if (stdout.trim()) {
352+
logger.error('Working directory is not clean')
353+
logger.info('Uncommitted changes:')
354+
console.log(stdout)
355+
return false
356+
}
357+
return true
358+
}
359+
344360
async function main(): Promise<void> {
345361
try {
346362
// Parse arguments.
@@ -401,6 +417,16 @@ async function main(): Promise<void> {
401417

402418
printHeader('Publish Runner', { borderChar: '=', width: 56 })
403419

420+
// Refuse to publish from a dirty working tree — would ship uncommitted
421+
// changes to npm. --force overrides for emergency republishes.
422+
const gitClean: boolean = await checkGitStatus()
423+
if (!gitClean && !values.force) {
424+
logger.error('Refusing to publish with uncommitted changes')
425+
logger.info('Commit or stash changes, or pass --force to override.')
426+
process.exitCode = 1
427+
return
428+
}
429+
404430
// Get current version.
405431
const version: string | undefined = await getCurrentVersion()
406432
logger.info(`Current version: ${version}`)

0 commit comments

Comments
 (0)