-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathinstall-git-hooks.mts
More file actions
66 lines (59 loc) · 2.04 KB
/
install-git-hooks.mts
File metadata and controls
66 lines (59 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env node
/**
* @file Configure git to use .git-hooks/ as the local hooks dir. Replaces husky
* — same end-state (committed hook source + auto-install on `pnpm install`),
* one fewer dependency. Idempotent: re-running is a no-op when core.hooksPath
* already points at .git-hooks. Safe to invoke from `prepare`. Skipped when:
*
* - Not inside a git repo (e.g. running in a tarball install).
* - .git-hooks/ doesn't exist (e.g. the template scaffold hasn't been cascaded
* into this repo yet).
*/
import { spawnSync } from '@socketsecurity/lib-stable/process/spawn/child'
import { existsSync } from 'node:fs'
import path from 'node:path'
import process from 'node:process'
import { fileURLToPath } from 'node:url'
const HOOKS_DIR = '.git-hooks'
// Anchor on the script's own location instead of process.cwd(). The
// `prepare` hook normally runs from the package root, but some
// invocations (e.g. `pnpm --filter <pkg> install` from a parent
// dir, or workspace `prepare` chains) execute with a cwd that
// differs from the script's repo root. `scripts/install-git-hooks.mts`
// is always at `<repo-root>/scripts/install-git-hooks.mts`, so the
// parent of __dirname is the repo root.
const REPO_ROOT = path.join(path.dirname(fileURLToPath(import.meta.url)), '..')
function main(): void {
if (!existsSync(path.join(REPO_ROOT, '.git'))) {
return
}
if (!existsSync(path.join(REPO_ROOT, HOOKS_DIR))) {
return
}
const current = spawnSync(
'git',
['config', '--local', '--get', 'core.hooksPath'],
{
cwd: REPO_ROOT,
stdio: ['ignore', 'pipe', 'pipe'],
},
)
if (current.status === 0 && String(current.stdout).trim() === HOOKS_DIR) {
return
}
const set = spawnSync(
'git',
['config', '--local', 'core.hooksPath', HOOKS_DIR],
{
cwd: REPO_ROOT,
stdio: ['ignore', 'pipe', 'pipe'],
},
)
if (set.status !== 0) {
process.stderr.write(
`[install-git-hooks] failed to set core.hooksPath: ${String(set.stderr).trim()}\n`,
)
process.exitCode = 1
}
}
main()