Skip to content

Commit a09966f

Browse files
committed
fix: husky invalid command bug
1 parent cf1135c commit a09966f

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

src/categories/js/husky.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
11
import { addScripts, installDevelopmentDependencies, runScript } from "src/utils/npm";
22
import { spawnCommand } from "src/utils/run-command";
3+
import fps from 'fs/promises';
4+
import path from 'path';
5+
import { ROOT_PATH } from "src/utils/path";
36

47
type HookName = 'pre-commit' | 'commit-msg';
8+
const ADD_HOOK_PLACEHOLDER = 'placeholder';
59

610
export const addHook = async (name: HookName, script: string) => {
7-
await spawnCommand('npx', ['husky', 'add', `.husky/${name}`, `'${script}'`]);
11+
const huskyPath = `.husky/${name}`;
12+
await spawnCommand('npx', ['husky', 'add', huskyPath, ADD_HOOK_PLACEHOLDER]);
13+
14+
const fileWithPlaceholder = await fps.readFile(huskyPath, { encoding: 'utf-8' });
15+
const fileWithScript = fileWithPlaceholder.replace(ADD_HOOK_PLACEHOLDER, script);
16+
17+
const filePath = path.join(ROOT_PATH, huskyPath);
18+
await fps.writeFile(filePath, fileWithScript, { encoding: 'utf-8' });
819
};
920

1021
export const husky = async () => {

0 commit comments

Comments
 (0)