Skip to content

Commit

Permalink
fix: simplify husky plugin (#537)
Browse files Browse the repository at this point in the history
The one change needed to support Husky v9 is to stop follow the configured git hook path and always check in `.husky`, the rest of the logic in #500 causes issues rather than helps.

I simply reverted the plugin to the implementation prior to #500 and simply added the `false` to `getGitHookPaths('.husky', false)` and that makes all tests pass – both Husky v8 and Husky v9 – and works in both kinds of projects for me.

Side note: Husky v9 is backwards compatible with Husky v8 and says that one shouldn't have to do anything when upgrading for it to work.
  • Loading branch information
voxpelli authored Mar 10, 2024
1 parent 02bf8bf commit 35b0f9a
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 28 deletions.
2 changes: 0 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions packages/knip/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@
"picocolors": "1.0.0",
"picomatch": "4.0.1",
"pretty-ms": "9.0.0",
"semver": "7.6.0",
"smol-toml": "1.1.4",
"strip-json-comments": "5.0.1",
"summary": "2.1.0",
Expand All @@ -92,7 +91,6 @@
"@types/minimist": "^1.2.5",
"@types/npmcli__map-workspaces": "^3.0.4",
"@types/npmcli__package-json": "^4.0.4",
"@types/semver": "7.5.8",
"@types/webpack": "^5.28.5",
"c8": "9.1.0",
"eslint": "^8.57.0",
Expand Down
28 changes: 4 additions & 24 deletions packages/knip/src/plugins/husky/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import semver from 'semver';
import { getGitHookPaths } from '../../util/git.js';
import { timerify } from '../../util/Performance.js';
import { getDependenciesFromScripts, hasDependency, loadFile } from '../../util/plugin.js';
Expand All @@ -12,35 +11,16 @@ const ENABLERS = ['husky'];

const isEnabled: IsPluginEnabledCallback = ({ dependencies }) => hasDependency(dependencies, ENABLERS);

const gitHooksPathInV8 = getGitHookPaths('.husky', true);
// husky v9 registers hooks in .husky/_/ to git and calls user defined hooks in .husky/ from there
const gitHookPathsInV9 = getGitHookPaths('.husky', false);
// husky v9 registers hooks in .husky/_/, so need to set "false" here to get same lookup as in v8
const gitHookPaths = getGitHookPaths('.husky', false);

// Add patterns for both v8 and v9 because we can't know which version is installed at this point
const CONFIG_FILE_PATTERNS = [...gitHooksPathInV8, ...gitHookPathsInV9];
const CONFIG_FILE_PATTERNS = [...gitHookPaths];

const findHuskyDependencies: GenericPluginCallback = async (configFilePath, options) => {
const { isProduction, manifest } = options;
const { isProduction } = options;

if (isProduction) return [];

const huskyVersion = manifest.devDependencies?.husky ?? manifest.dependencies?.husky ?? '*';

// Ignore config files that are not used by the installed husky version
const isV8OrLower = semver.intersects(huskyVersion, '<9', {
includePrerelease: true,
});
if (!isV8OrLower && gitHooksPathInV8.some(path => configFilePath.includes(path))) {
return [];
}

const isV9OrHigher = semver.intersects(huskyVersion, '>=9', {
includePrerelease: true,
});
if (!isV9OrHigher && gitHookPathsInV9.some(path => configFilePath.includes(path))) {
return [];
}

const script = await loadFile(configFilePath);

if (!script) return [];
Expand Down

0 comments on commit 35b0f9a

Please sign in to comment.