6
6
7
7
import { globalLogger } from "./logger" ;
8
8
import { Matcher , basicMatcher , loadMatcher } from "./matcher" ;
9
- import { InbuiltMergeStrategies , Config , RuleTree , StrategyFn } from "./types" ;
9
+ import { InbuiltMergeStrategies , Config , RuleTree } from "./types" ;
10
10
11
11
export interface StrategyItem {
12
12
name : string ;
@@ -26,15 +26,11 @@ export interface NormalizedRules {
26
26
default : StrategyItem [ ] ;
27
27
}
28
28
29
- export interface NormalizedConfig {
29
+ export interface NormalizedConfig < T extends string = InbuiltMergeStrategies , TContext = unknown >
30
+ extends Omit < Config < T , TContext > , "byStrategy" | "rules" | "defaultStrategy" > {
30
31
rules : NormalizedRules ;
31
32
matcher : Matcher ;
32
33
fileFilter : ( filepath : string ) => boolean ;
33
- customStrategies : Record < string , StrategyFn > ;
34
- includeNonConflicted : boolean ;
35
- debug : boolean ;
36
- strictArrays : boolean ;
37
- writeConflictSidecar : boolean ;
38
34
}
39
35
40
36
/** Defaults */
@@ -53,29 +49,30 @@ export const DEFAULT_CONFIG = {
53
49
export const normalizeConfig = async < T extends string = InbuiltMergeStrategies > (
54
50
config : Config < T > ,
55
51
) : Promise < NormalizedConfig > => {
56
- const userConfig = { ...DEFAULT_CONFIG , ...config } ;
52
+ const { rules, byStrategy, defaultStrategy, matcher, ...userConfig } = {
53
+ ...DEFAULT_CONFIG ,
54
+ ...config ,
55
+ } ;
57
56
58
- const rules : NormalizedRules = {
57
+ const normalizedRules : NormalizedRules = {
59
58
exact : Object . create ( null ) ,
60
59
exactFields : Object . create ( null ) ,
61
60
patterns : Object . create ( null ) ,
62
- default : normalizeDefault ( userConfig . defaultStrategy ) ,
61
+ default : normalizeDefault ( defaultStrategy ) ,
63
62
} ;
64
63
65
- const matcher =
66
- typeof userConfig . matcher === "string"
67
- ? await loadMatcher ( userConfig . matcher )
68
- : ( userConfig . matcher ?? basicMatcher ) ;
64
+ const resolvedMatcher =
65
+ typeof matcher === "string" ? await loadMatcher ( matcher ) : ( matcher ?? basicMatcher ) ;
69
66
70
67
let order = 0 ;
71
68
72
- if ( userConfig . byStrategy ) {
73
- for ( const [ rawStrategy , fields ] of Object . entries ( userConfig . byStrategy ) ) {
69
+ if ( byStrategy ) {
70
+ for ( const [ rawStrategy , fields ] of Object . entries ( byStrategy ) ) {
74
71
if ( ! fields ) continue ;
75
72
const { name, important : strategyImportant } = parseStrategyName ( rawStrategy ) ;
76
73
for ( const raw of fields as string [ ] ) {
77
74
const { key, important : keyImportant } = parseImportance ( raw ) ;
78
- addRule ( rules , key , {
75
+ addRule ( normalizedRules , key , {
79
76
strategies : [ { name, important : strategyImportant || keyImportant } ] ,
80
77
order : order ++ ,
81
78
source : key ,
@@ -84,14 +81,14 @@ export const normalizeConfig = async <T extends string = InbuiltMergeStrategies>
84
81
}
85
82
}
86
83
87
- if ( userConfig . rules ) {
88
- expandRuleTree ( userConfig . rules , [ ] , ( pathKey , strategyNames ) => {
84
+ if ( rules ) {
85
+ expandRuleTree ( rules , [ ] , ( pathKey , strategyNames ) => {
89
86
const { key, important : keyImportant } = parseImportance ( pathKey ) ;
90
87
const strategies = strategyNames . map ( s => {
91
88
const { name, important } = parseStrategyName ( s ) ;
92
89
return { name, important : important || keyImportant } ;
93
90
} ) ;
94
- addRule ( rules , key , {
91
+ addRule ( normalizedRules , key , {
95
92
strategies,
96
93
order : order ++ ,
97
94
source : key ,
@@ -103,20 +100,17 @@ export const normalizeConfig = async <T extends string = InbuiltMergeStrategies>
103
100
if ( config . debug ) {
104
101
globalLogger . info ( "all" , `[normalizer] Filtering ${ filepath } ` ) ;
105
102
}
106
- if ( ! matcher . isMatch ( filepath , userConfig . include ) ) return false ;
107
- if ( matcher . isMatch ( filepath , [ ...userConfig . exclude , "**/node_modules/**" ] ) ) return false ;
103
+ if ( ! resolvedMatcher . isMatch ( filepath , userConfig . include ) ) return false ;
104
+ if ( resolvedMatcher . isMatch ( filepath , [ ...userConfig . exclude , "**/node_modules/**" ] ) )
105
+ return false ;
108
106
return true ;
109
107
} ;
110
108
111
109
return {
112
- rules,
113
- matcher,
110
+ ...userConfig ,
111
+ rules : normalizedRules ,
112
+ matcher : resolvedMatcher ,
114
113
fileFilter,
115
- customStrategies : userConfig . customStrategies ?? { } ,
116
- includeNonConflicted : userConfig . includeNonConflicted ?? false ,
117
- debug : userConfig . debug ?? false ,
118
- strictArrays : userConfig . strictArrays ?? false ,
119
- writeConflictSidecar : userConfig . writeConflictSidecar ?? false ,
120
114
} ;
121
115
} ;
122
116
@@ -175,7 +169,7 @@ const addRule = (target: NormalizedRules, key: string, entry: StrategyList) => {
175
169
return ;
176
170
}
177
171
178
- const hasWildcards = / [ * ? \ [\] ] / . test ( key ) ;
172
+ const hasWildcards = / [ * ? [ \] ] / . test ( key ) ;
179
173
if ( hasWildcards ) {
180
174
push ( target . patterns , key , entry ) ;
181
175
return ;
0 commit comments