@@ -8,13 +8,6 @@ const download = require('download')
8
8
const { TypeError } = require ( 'tencent-component-toolkit/src/utils/error' )
9
9
const CONFIGS = require ( './config' )
10
10
11
- /*
12
- * Pauses execution for the provided miliseconds
13
- *
14
- * @param ${number} wait - number of miliseconds to wait
15
- */
16
- const sleep = async ( wait ) => new Promise ( ( resolve ) => setTimeout ( ( ) => resolve ( ) , wait ) )
17
-
18
11
/*
19
12
* Generates a random id
20
13
*/
@@ -23,18 +16,28 @@ const generateId = () =>
23
16
. toString ( 36 )
24
17
. substring ( 6 )
25
18
26
- const getDirFiles = async ( dirPath ) => {
27
- const targetPath = path . resolve ( dirPath )
28
- const files = fs . readdirSync ( targetPath )
29
- const temp = { }
30
- files . forEach ( ( file ) => {
31
- temp [ file ] = path . join ( targetPath , file )
32
- } )
33
- return temp
19
+ const getType = ( obj ) => {
20
+ return Object . prototype . toString . call ( obj ) . slice ( 8 , - 1 )
21
+ }
22
+
23
+ const validateTraffic = ( num ) => {
24
+ if ( getType ( num ) !== 'Number' ) {
25
+ throw new TypeError (
26
+ `PARAMETER_${ CONFIGS . compName . toUpperCase ( ) } _TRAFFIC` ,
27
+ 'traffic must be a number'
28
+ )
29
+ }
30
+ if ( num < 0 || num > 1 ) {
31
+ throw new TypeError (
32
+ `PARAMETER_${ CONFIGS . compName . toUpperCase ( ) } _TRAFFIC` ,
33
+ 'traffic must be a number between 0 and 1'
34
+ )
35
+ }
36
+ return true
34
37
}
35
38
36
39
const getCodeZipPath = async ( instance , inputs ) => {
37
- console . log ( `Packaging ${ CONFIGS . compNameFullname } application...` )
40
+ console . log ( `Packaging ${ CONFIGS . compFullname } application...` )
38
41
39
42
// unzip source zip file
40
43
let zipPath
@@ -43,7 +46,7 @@ const getCodeZipPath = async (instance, inputs) => {
43
46
const downloadPath = `/tmp/${ generateId ( ) } `
44
47
const filename = 'template'
45
48
46
- console . log ( `Installing Default ${ CONFIGS . compNameFullname } App...` )
49
+ console . log ( `Installing Default ${ CONFIGS . compFullname } App...` )
47
50
try {
48
51
await download ( CONFIGS . templateUrl , downloadPath , {
49
52
filename : `${ filename } .zip`
@@ -59,6 +62,16 @@ const getCodeZipPath = async (instance, inputs) => {
59
62
return zipPath
60
63
}
61
64
65
+ const getDirFiles = async ( dirPath ) => {
66
+ const targetPath = path . resolve ( dirPath )
67
+ const files = fs . readdirSync ( targetPath )
68
+ const temp = { }
69
+ files . forEach ( ( file ) => {
70
+ temp [ file ] = path . join ( targetPath , file )
71
+ } )
72
+ return temp
73
+ }
74
+
62
75
/**
63
76
* Upload code to COS
64
77
* @param {Component } instance serverless component instance
@@ -235,8 +248,18 @@ const prepareInputs = async (instance, credentials, inputs = {}) => {
235
248
fromClientRemark,
236
249
layers : ensureIterable ( tempFunctionConf . layers ? tempFunctionConf . layers : inputs . layers , {
237
250
default : [ ]
238
- } )
251
+ } ) ,
252
+ publish : inputs . publish ,
253
+ traffic : inputs . traffic ,
254
+ lastVersion : instance . state . lastVersion
239
255
}
256
+
257
+ // validate traffic
258
+ if ( inputs . traffic !== undefined ) {
259
+ validateTraffic ( inputs . traffic )
260
+ }
261
+ functionConf . needSetTraffic = inputs . traffic !== undefined && functionConf . lastVersion
262
+
240
263
functionConf . tags = ensureObject ( tempFunctionConf . tags ? tempFunctionConf . tags : inputs . tag , {
241
264
default : null
242
265
} )
@@ -267,7 +290,7 @@ const prepareInputs = async (instance, credentials, inputs = {}) => {
267
290
268
291
// 对apigw inputs进行标准化
269
292
const apigatewayConf = inputs . apigatewayConf ? inputs . apigatewayConf : { }
270
- apigatewayConf . isDisabled = apigatewayConf . isDisabled === true
293
+ apigatewayConf . isDisabled = inputs . apigatewayConf === true
271
294
apigatewayConf . fromClientRemark = fromClientRemark
272
295
apigatewayConf . serviceName = inputs . serviceName
273
296
apigatewayConf . description = `Serverless Framework Tencent-${ capitalString (
@@ -286,7 +309,8 @@ const prepareInputs = async (instance, credentials, inputs = {}) => {
286
309
function : {
287
310
isIntegratedResponse : apigatewayConf . isIntegratedResponse === false ? false : true ,
288
311
functionName : functionConf . name ,
289
- functionNamespace : functionConf . namespace
312
+ functionNamespace : functionConf . namespace ,
313
+ functionQualifier : functionConf . needSetTraffic ? '$DEFAULT' : '$LATEST'
290
314
}
291
315
}
292
316
]
@@ -371,7 +395,6 @@ const prepareInputs = async (instance, credentials, inputs = {}) => {
371
395
372
396
module . exports = {
373
397
generateId,
374
- sleep,
375
398
uploadCodeToCos,
376
399
mergeJson,
377
400
capitalString,
0 commit comments