Skip to content

Commit b5fbacc

Browse files
committed
feat(hooks): appending commit hook and creating new install script
Signed-off-by: Dhruv Arora <dhruv.arora1@autodesk.com>
1 parent 5aafdb8 commit b5fbacc

4 files changed

Lines changed: 68 additions & 3 deletions

File tree

fission/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,4 +132,4 @@ These core systems make up the lion's share of the fission source code. Each sys
132132
| `assetpack` | Downloads the assetpack and unzips/installs it in the correct location. |
133133
| `assetpack:update` | Downloads the assetpack and unzips/installs it in the correct location, replacing the old directory if it exists. |
134134
| `playwright:install` | Downloads the Playwright browsers. |
135-
| `hooks:install` | Points `core.hooksPath` at `fission/scripts/hooks`, enabling the Biome pre-commit hook. Run automatically by `init`. |
135+
| `prepare` | Run automatically by `bun i`. Adds a line to `.git/hooks/pre-commit` which runs `scripts/hooks/pre-commit`. |

fission/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +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": "git config core.hooksPath fission/scripts/hooks",
30+
"prepare": "f=$(git rev-parse --git-path hooks/pre-commit 2>/dev/null) || exit 0; [ -e \"$f\" ] || printf '#!/bin/sh\\n' > \"$f\"; grep -q fission/scripts/hooks/pre-commit \"$f\" || printf 'sh ./fission/scripts/hooks/pre-commit\\n' >> \"$f\"; chmod +x \"$f\"",
3131
"lockfile:clean": "bun x replace-regex --from='https:\\/\\/npm.autodesk.com\\/artifactory\\/[a-zA-Z0-9_.\\/@-]*' --to='' ./bun.lock",
3232
"postinstall": "bun run lockfile:clean | bun x dev-null"
3333
},

fission/scripts/install-hooks.ts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#!/usr/bin/env bun
2+
import { chmodSync, existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs"
3+
import { EOL } from "node:os"
4+
import { isAbsolute, join, resolve } from "node:path"
5+
import { execFileSync } from "node:child_process"
6+
7+
const BEGIN_MARKER = "# >>> fission biome pre-commit (bun run hooks:install) >>>"
8+
const END_MARKER = "# <<< fission biome pre-commit <<<"
9+
10+
const HOOK_BLOCK = [
11+
BEGIN_MARKER,
12+
'"$(git rev-parse --show-toplevel)/fission/scripts/hooks/pre-commit" "$@" || exit $?',
13+
END_MARKER,
14+
].join("\n")
15+
16+
function git(...args: string[]): string {
17+
return execFileSync("git", args, { encoding: "utf8" }).trim()
18+
}
19+
20+
function resolveHooksDir(): string {
21+
let configuredPath: string | undefined
22+
try {
23+
configuredPath = git("config", "--get", "core.hooksPath")
24+
} catch {
25+
// no-op
26+
}
27+
28+
if (configuredPath) {
29+
return isAbsolute(configuredPath)
30+
? configuredPath
31+
: resolve(git("rev-parse", "--show-toplevel"), configuredPath)
32+
}
33+
34+
return join(resolve(git("rev-parse", "--git-common-dir")), "hooks")
35+
}
36+
37+
const hooksDir = resolveHooksDir()
38+
const hookFile = join(hooksDir, "pre-commit")
39+
40+
const existing = existsSync(hookFile) ? readFileSync(hookFile, "utf8") : undefined
41+
42+
if (existing?.includes(BEGIN_MARKER)) {
43+
console.log(`Biome pre-commit hook already installed in ${hookFile}`)
44+
process.exit(0)
45+
}
46+
47+
mkdirSync(hooksDir, { recursive: true })
48+
49+
const contents =
50+
existing === undefined
51+
? `#!/usr/bin/env sh\n\n${HOOK_BLOCK}\n`
52+
: `${existing.replace(/\n*$/, "\n")}\n${HOOK_BLOCK}\n`
53+
54+
writeFileSync(hookFile, contents)
55+
chmodSync(hookFile, 0o755)
56+
57+
console.log(
58+
[
59+
existing === undefined
60+
? `Installed Biome pre-commit hook at ${hookFile}`
61+
: `Appended Biome pre-commit hook to the existing ${hookFile}`,
62+
"Bypass it for a single commit with: git commit --no-verify",
63+
`Uninstall it by deleting the block between the "${BEGIN_MARKER.slice(2)}" markers.`,
64+
].join(EOL)
65+
)

fission/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"composite": true,
3333
"types": ["vite-plugin-glsl/ext"]
3434
},
35-
"include": ["src/**/*.ts", "src/**/*.tsx", "update_manifest.ts"],
35+
"include": ["src/**/*.ts", "src/**/*.tsx", "update_manifest.ts", "scripts/**/*.ts"],
3636
"references": [
3737
{
3838
"path": "./tsconfig.node.json"

0 commit comments

Comments
 (0)