-
Notifications
You must be signed in to change notification settings - Fork 1
/
githooks.ts
55 lines (47 loc) · 1.35 KB
/
githooks.ts
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
export type Githooks = {
githooks: Record<string, string[]>;
};
export type GithooksOptions = {
getConfig: () => Promise<Githooks>;
verbose?: boolean;
};
export async function readJson(filePath: string): Promise<unknown> {
try {
const jsonString = await Deno.readTextFile(filePath);
return JSON.parse(jsonString);
} catch (err) {
err.message = `${filePath}: ${err.message}`;
throw err;
}
}
export function readDenoJson(): Promise<Githooks> {
return readJson("./deno.json") as Promise<Githooks>;
}
const defaultOptions: GithooksOptions = {
getConfig: readDenoJson,
verbose: true,
};
export async function setupGithooks(opts: GithooksOptions = defaultOptions) {
const config = await opts.getConfig();
if (!config.githooks) {
opts.verbose &&
console.log("No githooks found in deno.json — skipping setup.");
} else {
const hooks = Object.keys(config.githooks);
for (const hook of hooks) {
const task = config.githooks[hook];
const hookPath = `./.git/hooks/${hook}`;
const hookScript = `#!/bin/sh
exec deno task ${task}
`;
await Deno.writeTextFile(hookPath, hookScript);
await Deno.chmod(hookPath, 0o755);
}
opts.verbose &&
console.log("Githooks setup successfully:", hooks.join(", "));
}
}
// Execute when called directly
if (import.meta.main) {
await setupGithooks();
}