@@ -8,13 +8,6 @@ const download = require('download')
88const { TypeError } = require ( 'tencent-component-toolkit/src/utils/error' )
99const CONFIGS = require ( './config' )
1010
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-
1811/*
1912 * Generates a random id
2013 */
@@ -23,18 +16,28 @@ const generateId = () =>
2316 . toString ( 36 )
2417 . substring ( 6 )
2518
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
3437}
3538
3639const getCodeZipPath = async ( instance , inputs ) => {
37- console . log ( `Packaging ${ CONFIGS . compNameFullname } application...` )
40+ console . log ( `Packaging ${ CONFIGS . compFullname } application...` )
3841
3942 // unzip source zip file
4043 let zipPath
@@ -43,7 +46,7 @@ const getCodeZipPath = async (instance, inputs) => {
4346 const downloadPath = `/tmp/${ generateId ( ) } `
4447 const filename = 'template'
4548
46- console . log ( `Installing Default ${ CONFIGS . compNameFullname } App...` )
49+ console . log ( `Installing Default ${ CONFIGS . compFullname } App...` )
4750 try {
4851 await download ( CONFIGS . templateUrl , downloadPath , {
4952 filename : `${ filename } .zip`
@@ -59,6 +62,16 @@ const getCodeZipPath = async (instance, inputs) => {
5962 return zipPath
6063}
6164
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+
6275/**
6376 * Upload code to COS
6477 * @param {Component } instance serverless component instance
@@ -235,8 +248,18 @@ const prepareInputs = async (instance, credentials, inputs = {}) => {
235248 fromClientRemark,
236249 layers : ensureIterable ( tempFunctionConf . layers ? tempFunctionConf . layers : inputs . layers , {
237250 default : [ ]
238- } )
251+ } ) ,
252+ publish : inputs . publish ,
253+ traffic : inputs . traffic ,
254+ lastVersion : instance . state . lastVersion
239255 }
256+
257+ // validate traffic
258+ if ( inputs . traffic !== undefined ) {
259+ validateTraffic ( inputs . traffic )
260+ }
261+ functionConf . needSetTraffic = inputs . traffic !== undefined && functionConf . lastVersion
262+
240263 functionConf . tags = ensureObject ( tempFunctionConf . tags ? tempFunctionConf . tags : inputs . tag , {
241264 default : null
242265 } )
@@ -267,7 +290,7 @@ const prepareInputs = async (instance, credentials, inputs = {}) => {
267290
268291 // 对apigw inputs进行标准化
269292 const apigatewayConf = inputs . apigatewayConf ? inputs . apigatewayConf : { }
270- apigatewayConf . isDisabled = apigatewayConf . isDisabled === true
293+ apigatewayConf . isDisabled = inputs . apigatewayConf === true
271294 apigatewayConf . fromClientRemark = fromClientRemark
272295 apigatewayConf . serviceName = inputs . serviceName
273296 apigatewayConf . description = `Serverless Framework Tencent-${ capitalString (
@@ -286,7 +309,8 @@ const prepareInputs = async (instance, credentials, inputs = {}) => {
286309 function : {
287310 isIntegratedResponse : apigatewayConf . isIntegratedResponse === false ? false : true ,
288311 functionName : functionConf . name ,
289- functionNamespace : functionConf . namespace
312+ functionNamespace : functionConf . namespace ,
313+ functionQualifier : functionConf . needSetTraffic ? '$DEFAULT' : '$LATEST'
290314 }
291315 }
292316 ]
@@ -371,7 +395,6 @@ const prepareInputs = async (instance, credentials, inputs = {}) => {
371395
372396module . exports = {
373397 generateId,
374- sleep,
375398 uploadCodeToCos,
376399 mergeJson,
377400 capitalString,
0 commit comments