Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,8 @@
"zod-to-json-schema": "^3.25.0"
},
"simple-git-hooks": {
"pre-commit": "pnpm lint-staged && pnpm typecheck",
"commit-msg": "node scripts/verify-commit.js"
"pre-commit": "node scripts/pre-commit.mjs",
"commit-msg": "node scripts/verify-commit.js \"$1\""
},
"lint-staged": {
"*.js": [
Expand Down
61 changes: 61 additions & 0 deletions scripts/pre-commit.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// @ts-check
import { spawnSync } from 'node:child_process'

const tryCommand = (cmd, args) => {
const result = spawnSync(cmd, args, { stdio: 'ignore' })
if (result.error) {
return false
}
return result.status === 0
}

const resolvePnpm = () => {
if (tryCommand('pnpm', ['--version'])) {
return { cmd: 'pnpm', argsPrefix: [], source: 'PATH' }
}
if (
tryCommand('mise', ['--version']) &&
tryCommand('mise', ['exec', '--', 'pnpm', '--version'])
) {
return { cmd: 'mise', argsPrefix: ['exec', '--', 'pnpm'], source: 'mise' }
}
return null
}

const runner = resolvePnpm()
if (!runner) {
console.error(
[
'ERROR: pnpm not found.',
'Expected to find pnpm on PATH or via "mise exec -- pnpm".',
'Fix one of:',
'- Ensure pnpm is on PATH for git hooks',
'- Or install/configure mise and run "mise install"',
].join('\n'),
)
process.exit(1)
}

const runPnpm = (args) => {
const result = spawnSync(runner.cmd, [...runner.argsPrefix, ...args], {
stdio: 'inherit',
})
if (result.error) {
console.error(`ERROR: failed to run pnpm via ${runner.source}`)
process.exit(1)
}
if (typeof result.status === 'number' && result.status !== 0) {
process.exit(result.status)
}
if (result.signal) {
process.exit(1)
}
}

const args = process.argv.slice(2)
if (args.length > 0) {
runPnpm(args)
} else {
runPnpm(['lint-staged'])
runPnpm(['typecheck'])
}
61 changes: 59 additions & 2 deletions scripts/verify-commit.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,66 @@
// @ts-check
import pico from 'picocolors'
import { readFileSync } from 'node:fs'
import { existsSync, readFileSync, statSync } from 'node:fs'
import path from 'node:path'

const msgPath = path.resolve('.git/COMMIT_EDITMSG')
const findGitPath = (startDir) => {
let current = startDir
while (true) {
const candidate = path.join(current, '.git')
if (existsSync(candidate)) {
return candidate
}
const parent = path.dirname(current)
if (parent === current) {
return null
}
current = parent
}
}

const resolveGitDir = (gitPath) => {
try {
const stat = statSync(gitPath)
if (stat.isDirectory()) {
return gitPath
}
const content = readFileSync(gitPath, 'utf-8').trim()
if (content.startsWith('gitdir:')) {
const gitDir = content.replace('gitdir:', '').trim()
return path.resolve(path.dirname(gitPath), gitDir)
}
} catch {
// ignore and fall through
}
return null
}

const resolveMsgPath = () => {
const argPath = process.argv[2]
if (argPath && existsSync(argPath)) {
return argPath
}

const envGitDir = process.env.GIT_DIR
if (envGitDir && existsSync(envGitDir)) {
const resolved = resolveGitDir(envGitDir)
if (resolved) {
return path.resolve(resolved, 'COMMIT_EDITMSG')
}
}

const gitPath = findGitPath(process.cwd())
if (gitPath) {
const resolved = resolveGitDir(gitPath)
if (resolved) {
return path.resolve(resolved, 'COMMIT_EDITMSG')
}
}

return path.resolve('.git/COMMIT_EDITMSG')
}

const msgPath = resolveMsgPath()
const msg = readFileSync(msgPath, 'utf-8').trim()

const commitRE =
Expand Down