-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathuninstall.js
42 lines (36 loc) · 1.12 KB
/
uninstall.js
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
'use strict'
const fs = require('node:fs')
const path = require('node:path')
const exists = fs.existsSync || path.existsSync
const root = path.resolve(__dirname, '..', '..', '..')
let git = path.resolve(root, '.git')
//
// Resolve git directory for submodules
//
if (exists(git) && fs.lstatSync(git).isFile()) {
const gitinfo = fs.readFileSync(git).toString()
const gitdirmatch = /gitdir: (.+)/.exec(gitinfo)
const gitdir = gitdirmatch.length === 2 ? gitdirmatch[1] : null
if (gitdir !== null) {
git = path.resolve(root, gitdir)
}
}
//
// Location of pre-commit hook, if it exists
//
const precommit = path.resolve(git, 'hooks', 'pre-commit')
//
// Bail out if we don't have pre-commit file, it might be removed manually.
//
if (!exists(precommit)) process.exit(0)
//
// If we don't have an old file, we should just remove the pre-commit hook. But
// if we do have an old precommit file we want to restore that.
//
if (!exists(precommit + '.old')) {
fs.unlinkSync(precommit)
} else {
fs.writeFileSync(precommit, fs.readFileSync(precommit + '.old'))
fs.chmodSync(precommit, '755')
fs.unlinkSync(precommit + '.old')
}