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