-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0d3c64f
commit 396cdec
Showing
4 changed files
with
4 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,15 @@ | ||
import fps from 'node:fs/promises'; | ||
import path from 'node:path'; | ||
import { spawnCommand } from 'src/utils/run-command'; | ||
import { ROOT_PATH } from 'src/utils/path'; | ||
import { HOOK_DIR, PACKAGE_NAME } from './husky.const'; | ||
import { isInstalledAndInRootCheck } from 'src/utils/installed'; | ||
|
||
export type HookName = 'pre-commit' | 'commit-msg'; | ||
const ADD_HOOK_PLACEHOLDER = 'placeholder'; | ||
|
||
export const addHook = async (name: HookName, script: string) => { | ||
const huskyPath = path.join(HOOK_DIR, name); | ||
await spawnCommand('npx', [PACKAGE_NAME, 'add', huskyPath, ADD_HOOK_PLACEHOLDER]); | ||
const hookPath = path.join(ROOT_PATH, HOOK_DIR, name); | ||
|
||
const fileWithPlaceholder = await fps.readFile(huskyPath, { encoding: 'utf-8' }); | ||
const fileWithScript = fileWithPlaceholder.replace(ADD_HOOK_PLACEHOLDER, script); | ||
|
||
const filePath = path.join(ROOT_PATH, huskyPath); | ||
await fps.writeFile(filePath, fileWithScript, { encoding: 'utf-8' }); | ||
await fps.writeFile(hookPath, script, { encoding: 'utf-8' }); | ||
}; | ||
|
||
export const isHuskyInstalled = isInstalledAndInRootCheck(PACKAGE_NAME, HOOK_DIR); |