Skip to content

Commit 60ec4c0

Browse files
committed
feat(git): biome pre-commit hook
blocks the commit when it finds biome issues with the changed code
1 parent cb9bb89 commit 60ec4c0

3 files changed

Lines changed: 71 additions & 0 deletions

File tree

fission/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"assetpack:update": "bun update_manifest.ts && cd public && zip -FS -r assetpack.zip Downloadables -x '**/.*' -x '**/__MACOSX'",
2828
"assetpack:merge": "git checkout --theirs public/assetpack.zip && rm -rf public/Downloadables && tar -xf public/assetpack.zip -C public/ && git checkout --ours public/assetpack.zip && tar -xf public/assetpack.zip -C public/ && bun run assetpack:update",
2929
"playwright:install": "bun x playwright install",
30+
"hooks:install": "bun scripts/hooks/install.ts",
3031
"lockfile:clean": "bun x replace-regex --from='https:\\/\\/npm.autodesk.com\\/artifactory\\/[a-zA-Z0-9_.\\/@-]*' --to='' ./bun.lock",
3132
"postinstall": "bun run lockfile:clean | bun x dev-null"
3233
},

fission/scripts/hooks/install.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { execFileSync } from "node:child_process"
2+
import { chmodSync, copyFileSync, existsSync, mkdirSync } from "node:fs"
3+
import { join } from "node:path"
4+
5+
const HOOKS = ["pre-commit"]
6+
7+
function gitCommonDir(): string | null {
8+
try {
9+
return execFileSync("git", ["rev-parse", "--path-format=absolute", "--git-common-dir"], {
10+
encoding: "utf8",
11+
stdio: ["ignore", "pipe", "ignore"],
12+
}).trim()
13+
} catch {
14+
return null
15+
}
16+
}
17+
18+
const commonDir = gitCommonDir()
19+
if (!commonDir) {
20+
console.log("hooks:install: not a git repository, skipping.")
21+
process.exit(0)
22+
}
23+
24+
const hooksDir = join(commonDir, "hooks")
25+
mkdirSync(hooksDir, { recursive: true })
26+
27+
for (const hook of HOOKS) {
28+
const src = join(import.meta.dirname, hook)
29+
if (!existsSync(src)) {
30+
console.warn(`hooks:install: source hook missing: ${src}`)
31+
continue
32+
}
33+
const dest = join(hooksDir, hook)
34+
copyFileSync(src, dest)
35+
chmodSync(dest, 0o755)
36+
console.log(`hooks:install: installed ${hook} -> ${dest}`)
37+
}

fission/scripts/hooks/pre-commit

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/usr/bin/env sh
2+
#
3+
# Pre-commit hook: block commits that fail Biome format/lint in fission/.
4+
5+
set -e
6+
7+
repo_root=$(git rev-parse --show-toplevel)
8+
fission_dir="$repo_root/fission"
9+
10+
export GIT_WORK_TREE="$repo_root"
11+
12+
if ! command -v bun >/dev/null 2>&1; then
13+
echo "pre-commit: bun not found on PATH, skipping Biome check." >&2
14+
exit 0
15+
fi
16+
17+
cd "$fission_dir"
18+
19+
staged_list=$(mktemp)
20+
trap 'rm -f "$staged_list"' EXIT
21+
git diff --cached --name-only --diff-filter=ACMR --relative -z -- . >"$staged_list"
22+
23+
if [ ! -s "$staged_list" ]; then
24+
exit 0
25+
fi
26+
27+
if ! xargs -0 bun x biome check --no-errors-on-unmatched --files-ignore-unknown=true <"$staged_list"; then
28+
echo "" >&2
29+
echo "pre-commit: Biome found issues in staged fission/ files (see above)." >&2
30+
echo " Fix them with: cd fission && bun run style:fix" >&2
31+
echo " Then re-stage and commit again." >&2
32+
exit 1
33+
fi

0 commit comments

Comments
 (0)