@@ -89,37 +89,23 @@ export default class Scf {
89
89
oldList : TriggerType [ ] ,
90
90
) {
91
91
const deleteList : ( TriggerType | null ) [ ] = deepClone ( oldList ) ;
92
- const createList : ( OriginTriggerType | null ) [ ] = deepClone ( events ) ;
93
92
const deployList : ( TriggerType | null ) [ ] = [ ] ;
94
- // const noKeyTypes = ['apigw'];
95
- const updateList : ( OriginTriggerType | null ) [ ] = [ ] ;
96
93
97
- for ( let index = 0 ; index < events . length ; index ++ ) {
98
- const event = events [ index ] ;
99
- const Type = Object . keys ( event ) [ 0 ] ;
100
- const TriggerClass = TRIGGERS [ Type ] ;
101
- const triggerInstance : BaseTrigger = new TriggerClass ( {
102
- credentials : this . credentials ,
103
- region : this . region ,
104
- } ) ;
105
- const { triggerKey } = await triggerInstance . formatInputs ( {
106
- region : this . region ,
107
- inputs : {
108
- namespace : funcInfo . Namespace ,
109
- functionName : funcInfo . FunctionName ,
110
- ...event [ Type ] ,
111
- } ,
112
- } ) ;
113
- deployList [ index ] = {
114
- NeedCreate : true ,
115
- Type,
116
- ...event [ Type ] ,
117
- } ;
118
-
119
- for ( let i = 0 ; i < oldList . length ; i ++ ) {
120
- const oldTrigger = oldList [ i ] ;
94
+ const compareTriggerKey = async ( {
95
+ triggerType,
96
+ newIndex,
97
+ newKey,
98
+ oldTriggerList,
99
+ } : {
100
+ triggerType : string ;
101
+ newIndex : number ;
102
+ newKey : string ;
103
+ oldTriggerList : TriggerType [ ] ;
104
+ } ) => {
105
+ for ( let i = 0 ; i < oldTriggerList . length ; i ++ ) {
106
+ const oldTrigger = oldTriggerList [ i ] ;
121
107
// 如果类型不一致或者已经比较过(key值一致),则继续下一次循环
122
- if ( oldTrigger . Type !== Type || oldTrigger . compared === true ) {
108
+ if ( oldTrigger . Type !== triggerType || oldTrigger . compared === true ) {
123
109
continue ;
124
110
}
125
111
const OldTriggerClass = TRIGGERS [ oldTrigger . Type ] ;
@@ -130,29 +116,77 @@ export default class Scf {
130
116
const oldKey = await oldTriggerInstance . getKey ( oldTrigger ) ;
131
117
132
118
// 如果 key 不一致则继续下一次循环
133
- if ( oldKey !== triggerKey ) {
119
+ if ( oldKey !== newKey ) {
134
120
continue ;
135
121
}
136
122
137
123
oldList [ i ] . compared = true ;
138
124
139
125
deleteList [ i ] = null ;
140
- updateList . push ( createList [ index ] ) ;
141
- if ( CAN_UPDATE_TRIGGER . indexOf ( Type ) === - 1 ) {
142
- createList [ index ] = null ;
143
- deployList [ index ] = {
126
+
127
+ if ( CAN_UPDATE_TRIGGER . indexOf ( triggerType ) === - 1 ) {
128
+ deployList [ newIndex ] = {
144
129
NeedCreate : false ,
145
130
...oldTrigger ,
146
131
} ;
147
132
}
148
133
// 如果找到 key 值一样的,直接跳出循环
149
134
break ;
150
135
}
136
+ } ;
137
+
138
+ for ( let index = 0 ; index < events . length ; index ++ ) {
139
+ const event = events [ index ] ;
140
+ const Type = Object . keys ( event ) [ 0 ] ;
141
+ const TriggerClass = TRIGGERS [ Type ] ;
142
+ const triggerInstance : BaseTrigger = new TriggerClass ( {
143
+ credentials : this . credentials ,
144
+ region : this . region ,
145
+ } ) ;
146
+ deployList [ index ] = {
147
+ NeedCreate : true ,
148
+ Type,
149
+ ...event [ Type ] ,
150
+ } ;
151
+
152
+ // 需要特殊比较 API 网关触发器,因为一个触发器配置中,可能包含多个 API 触发器
153
+ if ( Type === 'apigw' ) {
154
+ const { parameters = { } } = event [ Type ] ;
155
+ const { endpoints = [ { path : '/' , method : 'ANY' } ] } = parameters ;
156
+ for ( const item of endpoints ) {
157
+ const newKey = await triggerInstance . getKey ( {
158
+ TriggerDesc : {
159
+ serviceId : parameters . serviceId ,
160
+ path : item . path ,
161
+ method : item . method ,
162
+ } ,
163
+ } ) ;
164
+ await compareTriggerKey ( {
165
+ triggerType : Type ,
166
+ newIndex : index ,
167
+ newKey : newKey ,
168
+ oldTriggerList : oldList ,
169
+ } ) ;
170
+ }
171
+ } else {
172
+ const { triggerKey } = await triggerInstance . formatInputs ( {
173
+ region : this . region ,
174
+ inputs : {
175
+ namespace : funcInfo . Namespace ,
176
+ functionName : funcInfo . FunctionName ,
177
+ ...event [ Type ] ,
178
+ } ,
179
+ } ) ;
180
+ await compareTriggerKey ( {
181
+ triggerType : Type ,
182
+ newIndex : index ,
183
+ newKey : triggerKey ,
184
+ oldTriggerList : oldList ,
185
+ } ) ;
186
+ }
151
187
}
152
188
return {
153
- updateList,
154
189
deleteList : deleteList . filter ( ( item ) => item ) as TriggerType [ ] ,
155
- createList : createList . filter ( ( item ) => item ) as OriginTriggerType [ ] ,
156
190
deployList : deployList . map ( ( item ) => {
157
191
delete item ?. compared ;
158
192
return item as TriggerType ;
0 commit comments