This repository has been archived by the owner on Dec 10, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuninstallGitHooks.js
58 lines (54 loc) · 1.81 KB
/
uninstallGitHooks.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import {
assertAndNormalizeDirectoryUrl,
resolveUrl,
readFile,
urlToFileSystemPath,
removeFileSystemNode,
catchCancellation,
createCancellationTokenForProcess,
} from "@jsenv/util"
import { createLogger } from "@jsenv/logger"
import { HOOK_NAMES, hookIsGeneratedByUs } from "./internal/hook.js"
export const uninstallGitHooks = async ({
cancellationToken = createCancellationTokenForProcess(),
logLevel,
projectDirectoryUrl,
}) => {
return catchCancellation(async () => {
const logger = createLogger({ logLevel })
projectDirectoryUrl = assertAndNormalizeDirectoryUrl(projectDirectoryUrl)
await Promise.all(
HOOK_NAMES.map(async (hookName) => {
const hookFileUrl = resolveUrl(`.git/hooks/${hookName}`, projectDirectoryUrl)
logger.debug(`seach file for git ${hookName} hook at ${urlToFileSystemPath(hookFileUrl)}`)
let hookFileContent
try {
hookFileContent = await readFile(hookFileUrl)
} catch (e) {
if (e.code === "ENOENT") {
logger.debug(`no file for git ${hookName} hook`)
return
}
throw e
}
cancellationToken.throwIfRequested()
if (hookIsGeneratedByUs(hookFileContent)) {
logger.info(`remove git ${hookName} hook file at ${urlToFileSystemPath(hookFileUrl)}`)
await removeFileSystemNode(hookFileUrl)
} else {
logger.debug(
`ignore git ${hookName} hook at ${urlToFileSystemPath(
hookFileUrl,
)} because not generated by us.`,
)
}
}),
)
}).catch((e) => {
// this is required to ensure unhandledRejection will still
// set process.exitCode to 1 marking the process execution as errored
// preventing further command to run
process.exitCode = 1
throw e
})
}