-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.ts
76 lines (67 loc) · 1.78 KB
/
main.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/**
* The main file, where all the cli commands are created.
*/
import { join } from 'https://deno.land/[email protected]/path/posix.ts';
import { exists, git, set } from './util.ts';
import { script } from './hook.ts';
/**
* Install hooks
*/
export async function install(dir = '.hooks') {
if ((await git(['rev-parse']).status()).code !== 0) {
throw new Error(
'Not a git repository. You can only use this in a git repository',
);
}
if (!exists('.git/')) {
throw new Error(
'.git directory not found. Run again in the git repository\'s top level directory.',
);
}
try {
// Create dirs
Deno.mkdirSync(join(dir, '_'), { recursive: true });
// Create .gitignore
Deno.writeFileSync(
join(dir, '_/.gitignore'),
new TextEncoder().encode('*'),
);
// Write hook.sh file using string from hook.ts
Deno.writeFileSync(
join(dir, '_/hook.sh'),
new TextEncoder().encode(script),
);
// Copy hook.sh file to the repos .hooks/_hook.sh
// Deno.copyFileSync('./hook.sh', join(dir, '_/hook.sh'));
const p = git(['config', 'core.hooksPath', dir]);
const rawError = await p.stderrOutput();
if ((await p.status()).code !== 0) {
throw rawError;
}
} catch (e) {
console.error('Failed to install hook');
throw e;
}
console.log('Successfully installed hooks');
}
/**
* Adds a new hook or append the command to an existing hook file
*/
export function add(file: string, cmd: string) {
if (exists(file)) {
Deno.writeFileSync(file, new TextEncoder().encode(`${cmd}\n`), {
append: true,
});
} else {
set(file, cmd);
}
}
/**
* Uninstall hooks by resetting git hook path to default
*/
export function uninstall() {
git(['config', '--unset', 'core.hooksPath']);
console.log(
'Successfully reset git hook path. You can delete your hook directory.',
);
}