-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup-husky.mjs
More file actions
55 lines (49 loc) · 1.37 KB
/
Copy pathsetup-husky.mjs
File metadata and controls
55 lines (49 loc) · 1.37 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
/**
* Install/update Husky hooks for VistaRemote sub-repos (Meta-Repo layout).
* Invoked from package.json: "prepare": "node ../tooling/scripts/setup-husky.mjs"
*/
import { chmodSync, existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
import { join } from 'node:path';
const repoRoot = process.cwd();
const huskyDir = join(repoRoot, '.husky');
const marker = '# @vistaremote/husky';
const toolingHusky = join(repoRoot, '..', 'tooling', 'husky');
const useSharedScripts = existsSync(join(toolingHusky, 'pre-commit.sh'));
const preCommit = useSharedScripts
? `#!/usr/bin/env sh
${marker}
set -e
cd "$(dirname "$0")/.."
sh ../tooling/husky/pre-commit.sh
`
: `#!/usr/bin/env sh
${marker}
set -e
cd "$(dirname "$0")/.."
pnpm exec biome check --staged --files-ignore-unknown=true --no-errors-on-unmatched
pnpm test
`;
const commitMsg = `#!/usr/bin/env sh
${marker}
set -e
cd "$(dirname "$0")/.."
pnpm exec commitlint --edit "$1"
`;
mkdirSync(huskyDir, { recursive: true });
function writeHook(name, content) {
const file = join(huskyDir, name);
if (existsSync(file)) {
const current = readFileSync(file, 'utf8');
if (!current.includes(marker)) {
return;
}
}
writeFileSync(file, content, { mode: 0o755 });
try {
chmodSync(file, 0o755);
} catch {
/* Windows */
}
}
writeHook('pre-commit', preCommit);
writeHook('commit-msg', commitMsg);