Skip to content

Commit

Permalink
feat: migrate to husky v9
Browse files Browse the repository at this point in the history
  • Loading branch information
allohamora committed Feb 18, 2024
1 parent 0d3c64f commit 396cdec
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 12 deletions.
2 changes: 1 addition & 1 deletion __tests__/categories/js/husky/husky.entrypoint.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe('husky', () => {
test('should add husky prepare script', async () => {
await husky();

expect(npmMocked.addScripts).toBeCalledWith({ name: 'prepare', script: 'husky install' });
expect(npmMocked.addScripts).toBeCalledWith({ name: 'prepare', script: 'husky' });
});

test('should run prepare script', async () => {
Expand Down
1 change: 0 additions & 1 deletion __tests__/categories/js/husky/husky.utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ describe('addHook', () => {

await addHook(hookType, script);

expect(runCommandMocked.spawnCommand).toBeCalledWith('npx', ['husky', 'add', `.husky/${hookType}`, placeholder]);
expect(fspMocked.writeFile).toBeCalledWith(rootPath(`.husky/${hookType}`), script, { encoding: 'utf-8' });
});
});
Expand Down
2 changes: 1 addition & 1 deletion src/categories/js/husky/husky.entrypoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ import { PACKAGE_NAME } from './husky.const';

export const husky = async () => {
await installDevelopmentDependencies(PACKAGE_NAME);
await addScripts({ name: 'prepare', script: `${PACKAGE_NAME} install` });
await addScripts({ name: 'prepare', script: PACKAGE_NAME });
await runScript('prepare');
};
11 changes: 2 additions & 9 deletions src/categories/js/husky/husky.utils.ts
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);

0 comments on commit 396cdec

Please sign in to comment.