@@ -14,22 +14,24 @@ export default class JavaStartupAccelerationComponent {
14
14
}
15
15
16
16
async index ( params ) {
17
+ await this . checkSBuildArtifacts ( ) ;
18
+
17
19
let args = await this . parseArgs ( params . argsObj ) ;
18
20
let debug = args . debug ;
19
21
if ( debug ) {
20
22
info ( "index args: " + JSON . stringify ( params ) ) ;
21
23
}
22
24
info ( "parsed args: " + JSON . stringify ( args ) ) ;
23
25
26
+ let fcEndpoint = await this . getFCEndpoint ( ) ;
24
27
let moduleName = args . moduleName ;
25
28
let serviceName = await this . getServiceConfig ( moduleName , 'name' ) ;
26
29
let functionName = await this . getFunctionConfig ( moduleName , 'name' ) ;
27
- let fcEndpoint = await this . getFCEndpoint ( ) ;
28
30
let initializer = await this . getFunctionConfig ( moduleName , 'initializer' ) ;
29
31
let runtime = await this . getFunctionConfig ( moduleName , 'runtime' ) ;
30
32
let region = await this . getModulesProps ( moduleName , 'region' ) ;
31
- let role = await this . getServiceConfig ( moduleName , 'role' ) ;
32
- let logConfig = await this . getServiceConfig ( moduleName , 'logConfig' ) ;
33
+ let role = await this . getServiceConfig ( moduleName , 'role' , false ) ;
34
+ let logConfig = await this . getServiceConfig ( moduleName , 'logConfig' , false ) ;
33
35
const access = params ?. project ?. access || this . defaultAccess ;
34
36
const credential = await this . getCredential ( access ) ;
35
37
let codeUri = await this . getFunctionConfig ( moduleName , 'codeUri' ) ;
@@ -39,12 +41,11 @@ export default class JavaStartupAccelerationComponent {
39
41
}
40
42
srpath = this . removeStrSuffix ( srpath , "/" ) ;
41
43
let sharedDirName = path . basename ( srpath ) ;
42
-
43
44
let downloader = args . downloader ;
44
45
let uploader = args . uploader ;
45
46
let ossUtilUrl , ossEndpoint ;
46
47
if ( downloader == OSS ) {
47
- ossUtilUrl = await this . getGlobalConfig ( 'ossUtilUrl' ) ;
48
+ ossUtilUrl = await this . getGlobalConfig ( 'ossUtilUrl' , false ) ;
48
49
ossEndpoint = await this . getModulesProps ( moduleName , 'ossEndpoint' , false ) ;
49
50
}
50
51
@@ -96,6 +97,7 @@ export default class JavaStartupAccelerationComponent {
96
97
let instanceType = args . instanceType ;
97
98
let features = args . features ;
98
99
let funcEnvVars = await this . getFunctionEnvVars ( moduleName ) ;
100
+
99
101
const instance = new JavaStartupAcceleration ( process . cwd ( ) , {
100
102
region,
101
103
fcEndpoint,
@@ -149,64 +151,103 @@ export default class JavaStartupAccelerationComponent {
149
151
}
150
152
}
151
153
152
- async getGlobalConfig ( key : string ) {
154
+ async getGlobalConfig ( key : string , errorIfNotExist : boolean = true ) {
153
155
const yaml = await this . getConfig ( ) ;
154
156
if ( yaml ) {
155
157
try {
156
- return yaml [ key ] ;
158
+ let value = await this . replaceReference ( yaml [ key ] ) ;
159
+ if ( value === undefined ) {
160
+ throw new Error ( "key " + key + " does not exist" ) ;
161
+ }
162
+ return value ;
157
163
} catch ( e ) {
158
- error ( 'read global config [' + key + '] error' ) ;
159
- throw e ;
164
+ error ( 'read global config [' + key + '] error: ' + e . message ) ;
165
+ if ( errorIfNotExist ) {
166
+ throw e ;
167
+ }
160
168
}
161
169
}
170
+ return null ;
162
171
}
163
172
164
173
async getModulesProps ( moduleName : string , key : string , errorIfNotExist : boolean = true ) {
165
174
const yaml = await this . getConfig ( ) ;
166
175
if ( yaml ) {
167
176
try {
168
- return yaml [ 'services' ] [ moduleName ] [ 'props' ] [ key ] ;
177
+ let value = await this . replaceReference ( yaml [ 'services' ] [ moduleName ] [ 'props' ] [ key ] ) ;
178
+ if ( value === undefined ) {
179
+ throw new Error ( "key " + key + " does not exist" ) ;
180
+ }
181
+ return value ;
169
182
} catch ( e ) {
183
+ info ( 'read module prop [' + key + '] error: ' + e . message ) ;
170
184
if ( errorIfNotExist ) {
171
185
throw e ;
172
- } else {
173
- info ( 'read module prop [' + key + '] error' ) ;
174
186
}
175
187
}
176
188
}
189
+ return null ;
177
190
}
178
191
179
- async getServiceConfig ( moduleName : string , key : string ) {
192
+ async getServiceConfig ( moduleName : string , key : string , errorIfNotExist : boolean = true ) {
180
193
const yaml = await this . getConfig ( ) ;
181
194
if ( yaml ) {
182
195
try {
183
- return yaml [ 'services' ] [ moduleName ] [ 'props' ] [ 'service' ] [ key ] ;
196
+ let value = await this . replaceReference ( yaml [ 'services' ] [ moduleName ] [ 'props' ] [ 'service' ] [ key ] ) ;
197
+ if ( value === undefined ) {
198
+ throw new Error ( "key " + key + " does not exist" ) ;
199
+ }
200
+ return value ;
184
201
} catch ( e ) {
185
- error ( 'read module config [' + key + '] error' ) ;
186
- throw e ;
202
+ error ( 'read module config [' + key + '] error: ' + e . message ) ;
203
+ if ( errorIfNotExist ) {
204
+ throw e ;
205
+ }
187
206
}
188
207
}
208
+ return null ;
189
209
}
190
210
191
211
async getFunctionConfig ( moduleName : string , key : string , errorIfNotExist : boolean = true ) {
192
212
const yaml = await this . getConfig ( ) ;
193
213
194
- let value = null ;
195
214
if ( yaml ) {
196
215
try {
197
- value = yaml [ 'services' ] [ moduleName ] [ 'props' ] [ 'function' ] [ key ] ;
216
+ let value = await this . replaceReference ( yaml [ 'services' ] [ moduleName ] [ 'props' ] [ 'function' ] [ key ] ) ;
217
+ if ( value === undefined ) {
218
+ throw new Error ( "key " + key + " does not exist" ) ;
219
+ }
220
+ return value ;
198
221
} catch ( e ) {
222
+ error ( 'read function config [' + key + '] error: ' + e . message ) ;
199
223
if ( errorIfNotExist ) {
200
224
throw e ;
201
- } else {
202
- info ( 'function config [' + key + '] does not exist:' + e . message ) ;
203
225
}
204
226
}
205
227
}
206
228
207
- return value ;
229
+ return null ;
208
230
}
209
231
232
+ async replaceReference ( value ) {
233
+ if ( typeof value !== 'string' ) {
234
+ return value ;
235
+ }
236
+ let myRegexp = new RegExp ( "\\${([a-zA-Z_.0-9]+)}" , "g" ) ;
237
+ let match = myRegexp . exec ( value ) ;
238
+ if ( match == null ) {
239
+ return value ;
240
+ }
241
+
242
+ let ref = match [ 1 ] . split ( '.' ) ;
243
+ let config = await this . getConfig ( ) ;
244
+ for ( let i = 0 ; i < ref . length ; i ++ ) {
245
+ config = config [ ref [ i ] ] ;
246
+ }
247
+ return config ;
248
+ }
249
+
250
+
210
251
async getFunctionEnvVar ( moduleName : string , name : string ) {
211
252
const environmentVariables = await this . getFunctionConfig ( moduleName , "environmentVariables" , false ) ;
212
253
@@ -227,6 +268,15 @@ export default class JavaStartupAccelerationComponent {
227
268
return YAML . load ( yamlContent ) ;
228
269
}
229
270
271
+ async checkSBuildArtifacts ( ) {
272
+ const path = join ( process . cwd ( ) , ".s" , "build" , "artifacts" ) ;
273
+ if ( existsSync ( path ) ) {
274
+ throw new Error ( "s build artifacts dir [" + path + "] should be deleted" ) ;
275
+ } else {
276
+ info ( "check s build artifacts: ok" ) ;
277
+ }
278
+ }
279
+
230
280
async readFileContent ( fileName : string ) {
231
281
const path = join ( process . cwd ( ) , fileName ) ;
232
282
const isExists = existsSync ( path ) ;
0 commit comments