@@ -2,27 +2,21 @@ import executeRule from '@commitlint/execute-rule';
2
2
import resolveExtends from '@commitlint/resolve-extends' ;
3
3
import {
4
4
LoadOptions ,
5
- ParserPreset ,
6
5
QualifiedConfig ,
7
6
QualifiedRules ,
7
+ PluginRecords ,
8
8
UserConfig ,
9
- UserPreset ,
10
9
} from '@commitlint/types' ;
11
10
import isPlainObject from 'lodash/isPlainObject' ;
12
11
import merge from 'lodash/merge' ;
13
- import mergeWith from 'lodash/mergeWith' ;
14
- import pick from 'lodash/pick' ;
15
- import union from 'lodash/union' ;
12
+ import uniq from 'lodash/uniq' ;
16
13
import Path from 'path' ;
17
14
import resolveFrom from 'resolve-from' ;
18
15
import { loadConfig } from './utils/load-config' ;
19
16
import { loadParserOpts } from './utils/load-parser-opts' ;
20
17
import loadPlugin from './utils/load-plugin' ;
21
18
import { pickConfig } from './utils/pick-config' ;
22
19
23
- const w = < T > ( _ : unknown , b : ArrayLike < T > | null | undefined | false ) =>
24
- Array . isArray ( b ) ? b : undefined ;
25
-
26
20
export default async function load (
27
21
seed : UserConfig = { } ,
28
22
options : LoadOptions = { }
@@ -35,11 +29,16 @@ export default async function load(
35
29
// Might amount to breaking changes, defer until 9.0.0
36
30
37
31
// Merge passed config with file based options
38
- const config = pickConfig ( merge ( { } , loaded ? loaded . config : null , seed ) ) ;
39
-
40
- const opts = merge (
41
- { extends : [ ] , rules : { } , formatter : '@commitlint/format' } ,
42
- pick ( config , 'extends' , 'plugins' , 'ignores' , 'defaultIgnores' )
32
+ const config = pickConfig (
33
+ merge (
34
+ {
35
+ extends : [ ] ,
36
+ plugins : [ ] ,
37
+ rules : { } ,
38
+ } ,
39
+ loaded ? loaded . config : null ,
40
+ seed
41
+ )
43
42
) ;
44
43
45
44
// Resolve parserPreset key
@@ -54,59 +53,35 @@ export default async function load(
54
53
}
55
54
56
55
// Resolve extends key
57
- const extended = resolveExtends ( opts , {
56
+ const extended = resolveExtends ( config , {
58
57
prefix : 'commitlint-config' ,
59
58
cwd : base ,
60
59
parserPreset : config . parserPreset ,
61
- } ) ;
62
-
63
- const preset = pickConfig (
64
- mergeWith ( extended , config , w )
65
- ) as unknown as UserPreset ;
66
- preset . plugins = { } ;
67
-
68
- // TODO: check if this is still necessary with the new factory based conventional changelog parsers
69
- // config.extends = Array.isArray(config.extends) ? config.extends : [];
60
+ } ) as unknown as UserConfig ;
70
61
71
- // Resolve parser-opts from preset
72
- if ( typeof preset . parserPreset === 'object' ) {
73
- preset . parserPreset . parserOpts = await loadParserOpts (
74
- preset . parserPreset . name ,
75
- // TODO: fix the types for factory based conventional changelog parsers
76
- preset . parserPreset as any
77
- ) ;
62
+ if ( ! extended . formatter || typeof extended . formatter !== 'string' ) {
63
+ extended . formatter = '@commitlint/format' ;
78
64
}
79
65
80
- // Resolve config-relative formatter module
81
- if ( typeof config . formatter === 'string' ) {
82
- preset . formatter =
83
- resolveFrom . silent ( base , config . formatter ) || config . formatter ;
84
- }
85
-
86
- // Read plugins from extends
66
+ let plugins : PluginRecords = { } ;
87
67
if ( Array . isArray ( extended . plugins ) ) {
88
- config . plugins = union ( config . plugins , extended . plugins || [ ] ) ;
89
- }
90
-
91
- // resolve plugins
92
- if ( Array . isArray ( config . plugins ) ) {
93
- config . plugins . forEach ( ( plugin ) => {
68
+ uniq ( extended . plugins || [ ] ) . forEach ( ( plugin ) => {
94
69
if ( typeof plugin === 'string' ) {
95
- loadPlugin ( preset . plugins , plugin , process . env . DEBUG === 'true' ) ;
70
+ plugins = loadPlugin ( plugins , plugin , process . env . DEBUG === 'true' ) ;
96
71
} else {
97
- preset . plugins . local = plugin ;
72
+ plugins . local = plugin ;
98
73
}
99
74
} ) ;
100
75
}
101
76
102
- const rules = preset . rules ? preset . rules : { } ;
103
- const qualifiedRules = (
77
+ const rules = (
104
78
await Promise . all (
105
- Object . entries ( rules || { } ) . map ( ( entry ) => executeRule < any > ( entry ) )
79
+ Object . entries ( extended . rules || { } ) . map ( ( entry ) => executeRule ( entry ) )
106
80
)
107
81
) . reduce < QualifiedRules > ( ( registry , item ) => {
108
- const [ key , value ] = item as any ;
109
- ( registry as any ) [ key ] = value ;
82
+ // type of `item` can be null, but Object.entries always returns key pair
83
+ const [ key , value ] = item ! ;
84
+ registry [ key ] = value ;
110
85
return registry ;
111
86
} , { } ) ;
112
87
@@ -118,17 +93,24 @@ export default async function load(
118
93
: 'https://github.com/conventional-changelog/commitlint/#what-is-commitlint' ;
119
94
120
95
const prompt =
121
- preset . prompt && isPlainObject ( preset . prompt ) ? preset . prompt : { } ;
96
+ extended . prompt && isPlainObject ( extended . prompt ) ? extended . prompt : { } ;
122
97
123
98
return {
124
- extends : preset . extends ! ,
125
- formatter : preset . formatter ! ,
126
- parserPreset : preset . parserPreset ! as ParserPreset ,
127
- ignores : preset . ignores ! ,
128
- defaultIgnores : preset . defaultIgnores ! ,
129
- plugins : preset . plugins ! ,
130
- rules : qualifiedRules ,
131
- helpUrl,
99
+ extends : Array . isArray ( extended . extends )
100
+ ? extended . extends
101
+ : typeof extended . extends === 'string'
102
+ ? [ extended . extends ]
103
+ : [ ] ,
104
+ // Resolve config-relative formatter module
105
+ formatter :
106
+ resolveFrom . silent ( base , extended . formatter ) || extended . formatter ,
107
+ // Resolve parser-opts from preset
108
+ parserPreset : await loadParserOpts ( extended . parserPreset ) ,
109
+ ignores : extended . ignores ,
110
+ defaultIgnores : extended . defaultIgnores ,
111
+ plugins : plugins ,
112
+ rules : rules ,
113
+ helpUrl : helpUrl ,
132
114
prompt,
133
115
} ;
134
116
}
0 commit comments