1
+ import semver from 'semver' ;
1
2
import { getGitHookPaths } from '../../util/git.js' ;
2
3
import { timerify } from '../../util/Performance.js' ;
3
4
import { getDependenciesFromScripts , hasDependency , loadFile } from '../../util/plugin.js' ;
@@ -11,15 +12,35 @@ const ENABLERS = ['husky'];
11
12
12
13
const isEnabled : IsPluginEnabledCallback = ( { dependencies } ) => hasDependency ( dependencies , ENABLERS ) ;
13
14
14
- const gitHookPaths = getGitHookPaths ( '.husky' ) ;
15
+ const gitHooksPathInV8 = getGitHookPaths ( '.husky' , true ) ;
16
+ // husky v9 registers hooks in .husky/_/ to git and calls user defined hooks in .husky/ from there
17
+ const gitHookPathsInV9 = getGitHookPaths ( '.husky' , false ) ;
15
18
16
- const CONFIG_FILE_PATTERNS = [ ...gitHookPaths ] ;
19
+ // Add patterns for both v8 and v9 because we can't know which version is installed at this point
20
+ const CONFIG_FILE_PATTERNS = [ ...gitHooksPathInV8 , ...gitHookPathsInV9 ] ;
17
21
18
22
const findHuskyDependencies : GenericPluginCallback = async ( configFilePath , options ) => {
19
- const { isProduction } = options ;
23
+ const { isProduction, manifest } = options ;
20
24
21
25
if ( isProduction ) return [ ] ;
22
26
27
+ const huskyVersion = manifest . devDependencies ?. husky ?? manifest . dependencies ?. husky ?? '*' ;
28
+
29
+ // Ignore config files that are not used by the installed husky version
30
+ const isV8OrLower = semver . intersects ( huskyVersion , '<9' , {
31
+ includePrerelease : true ,
32
+ } ) ;
33
+ if ( ! isV8OrLower && gitHooksPathInV8 . some ( path => configFilePath . includes ( path ) ) ) {
34
+ return [ ] ;
35
+ }
36
+
37
+ const isV9OrHigher = semver . intersects ( huskyVersion , '>=9' , {
38
+ includePrerelease : true ,
39
+ } ) ;
40
+ if ( ! isV9OrHigher && gitHookPathsInV9 . some ( path => configFilePath . includes ( path ) ) ) {
41
+ return [ ] ;
42
+ }
43
+
23
44
const script = await loadFile ( configFilePath ) ;
24
45
25
46
if ( ! script ) return [ ] ;
0 commit comments