@@ -3,15 +3,70 @@ import { isPrettierInstalled } from '../prettier/prettier.utils';
3
3
import { addHook , isHuskyInstalled } from '../husky/husky.utils' ;
4
4
import { CLI_NAME } from './lint-staged.const' ;
5
5
import { CLI_NAME as PRETTIER_CLI_NAME } from '../prettier/prettier.const' ;
6
+ import { isJestInstalled } from '../jest/jest.utils' ;
7
+ import { isEslintInstalled } from '../eslint/eslint.utils' ;
8
+
9
+ type ScriptFileExtension = '*.js' | '*.ts' ;
10
+
11
+ interface OptionMutator {
12
+ check : ( config : LintStagedConfig , key : string , value : string ) => boolean ;
13
+ mutate : ( config : LintStagedConfig , key : string , value : string ) => void ;
14
+ }
15
+
16
+ const arrayOptionMutator : OptionMutator = {
17
+ check : ( config , key ) => Array . isArray ( config [ key ] ) ,
18
+ mutate : ( config , key , value ) => {
19
+ config [ key ] = Array . from ( new Set ( [ ...( config [ key ] as unknown [ ] ) , value ] ) ) ;
20
+ } ,
21
+ } ;
22
+
23
+ const stringOptionMutator : OptionMutator = {
24
+ check : ( config , key ) => typeof config [ key ] === 'string' ,
25
+ mutate : ( config , key , value ) => {
26
+ const array = Array . from ( new Set ( [ config [ key ] , value ] ) ) ;
27
+ config [ key ] = array . length === 0 ? array [ 0 ] : array ;
28
+ } ,
29
+ } ;
30
+
31
+ const undefinedOptionMutator : OptionMutator = {
32
+ check : ( config , key ) => typeof config [ key ] === 'undefined' ,
33
+ mutate : ( config , key , value ) => {
34
+ config [ key ] = value ;
35
+ } ,
36
+ } ;
37
+
38
+ const optionMutators : OptionMutator [ ] = [ arrayOptionMutator , stringOptionMutator , undefinedOptionMutator ] ;
39
+
40
+ export const addOptionToLintStagedConfig = ( config : LintStagedConfig , key : string , value : string ) => {
41
+ const finded = optionMutators . find ( ( { check } ) => check ( config , key , value ) ) ;
42
+
43
+ if ( ! finded ) {
44
+ throw new Error ( 'option mutator is not found!' ) ;
45
+ }
46
+
47
+ finded . mutate ( config , key , value ) ;
48
+ } ;
6
49
7
50
export const huskyIntegration = async ( ) => {
8
51
if ( await isHuskyInstalled ( ) ) {
9
52
await addHook ( 'pre-commit' , `npx --no-install ${ CLI_NAME } ` ) ;
10
53
}
11
54
} ;
12
55
56
+ export const jestMutator = ( fileExtension : ScriptFileExtension ) => async ( config : LintStagedConfig ) => {
57
+ if ( await isJestInstalled ( ) ) {
58
+ addOptionToLintStagedConfig ( config , fileExtension , 'jest --findRelatedTests' ) ;
59
+ }
60
+ } ;
61
+
62
+ export const eslintMutator = ( fileExtension : ScriptFileExtension ) => async ( config : LintStagedConfig ) => {
63
+ if ( await isEslintInstalled ( ) ) {
64
+ addOptionToLintStagedConfig ( config , fileExtension , 'eslint --fix' ) ;
65
+ }
66
+ } ;
67
+
13
68
export const prettierMutator = async ( config : LintStagedConfig ) => {
14
69
if ( await isPrettierInstalled ( ) ) {
15
- config [ '*.{js,json,yml,md}' ] = `${ PRETTIER_CLI_NAME } --write` ;
70
+ addOptionToLintStagedConfig ( config , '*.{js,json,yml,md}' , `${ PRETTIER_CLI_NAME } --write` ) ;
16
71
}
17
72
} ;
0 commit comments