1
1
const path = require ( 'path' )
2
- const { copySync } = require ( 'fs-extra' )
3
- const { Domain } = require ( 'tencent-component-toolkit' )
2
+ const { Domain, Cos } = require ( 'tencent-component-toolkit' )
4
3
const ensureObject = require ( 'type/object/ensure' )
5
4
const ensureIterable = require ( 'type/iterable/ensure' )
6
5
const ensureString = require ( 'type/string/ensure' )
@@ -21,52 +20,93 @@ const generateId = () =>
21
20
Math . random ( )
22
21
. toString ( 36 )
23
22
. substring ( 6 )
24
- /*
25
- * Packages framework app and injects shims and sdk
26
- *
27
- * @param ${instance} instance - the component instance
28
- * @param ${object} config - the component config
29
- */
30
- const packageCode = async ( instance , inputs ) => {
23
+
24
+ const getCodeZipPath = async ( instance , inputs ) => {
31
25
console . log ( `Packaging ${ CONFIGS . frameworkFullname } application...` )
32
26
33
27
// unzip source zip file
34
- console . log ( `Unzipping ${ inputs . code . src || 'files' } ...` )
35
- let sourceDirectory
28
+ let zipPath
36
29
if ( ! inputs . code . src ) {
37
- // add default nextjs template
30
+ // add default template
38
31
const downloadPath = `/tmp/${ generateId ( ) } `
39
32
const filename = 'template'
40
33
41
34
console . log ( `Installing Default ${ CONFIGS . frameworkFullname } App...` )
42
35
await download ( CONFIGS . templateUrl , downloadPath , {
43
36
filename : `${ filename } .zip`
44
37
} )
45
- const tempPath = await instance . unzip ( `${ downloadPath } /${ filename } .zip` )
46
- sourceDirectory = `${ tempPath } /src`
38
+ zipPath = `${ downloadPath } /${ filename } .zip`
47
39
} else {
48
- sourceDirectory = await instance . unzip ( inputs . code . src )
40
+ zipPath = inputs . code . src
49
41
}
50
- console . log ( `Files unzipped into ${ sourceDirectory } ...` )
51
42
52
- // add shim to the source directory
53
- console . log ( `Installing ${ CONFIGS . frameworkFullname } + SCF handler...` )
54
- copySync ( path . join ( __dirname , '_shims' ) , path . join ( sourceDirectory , '_shims' ) )
43
+ return zipPath
44
+ }
55
45
56
- // add sdk to the source directory, add original handler
57
- console . log ( `Installing Serverless Framework SDK...` )
58
- instance . state . handler = await instance . addSDK ( sourceDirectory , '_shims/handler.handler' )
46
+ /**
47
+ * Upload code to COS
48
+ * @param {Component } instance serverless component instance
49
+ * @param {string } appId app id
50
+ * @param {object } credentials credentials
51
+ * @param {object } inputs component inputs parameters
52
+ * @param {string } region region
53
+ */
54
+ const uploadCodeToCos = async ( instance , appId , credentials , inputs , region ) => {
55
+ const bucketName = inputs . code . bucket || `sls-cloudfunction-${ region } -code`
56
+ const objectName = inputs . code . object || `${ inputs . name } -${ Math . floor ( Date . now ( ) / 1000 ) } .zip`
57
+ // if set bucket and object not pack code
58
+ if ( ! inputs . code . bucket || ! inputs . code . object ) {
59
+ const zipPath = await getCodeZipPath ( instance , inputs )
60
+ console . log ( `Code zip path ${ zipPath } ` )
59
61
60
- // zip the source directory with the shim and the sdk
62
+ // save the zip path to state for lambda to use it
63
+ instance . state . zipPath = zipPath
61
64
62
- console . log ( `Zipping files...` )
63
- const zipPath = await instance . zip ( sourceDirectory )
64
- console . log ( `Files zipped into ${ zipPath } ...` )
65
+ const cos = new Cos ( credentials , region )
65
66
66
- // save the zip path to state for lambda to use it
67
- instance . state . zipPath = zipPath
67
+ if ( ! inputs . code . bucket ) {
68
+ // create default bucket
69
+ await cos . deploy ( {
70
+ bucket : bucketName + '-' + appId ,
71
+ force : true ,
72
+ lifecycle : [
73
+ {
74
+ status : 'Enabled' ,
75
+ id : 'deleteObject' ,
76
+ filter : '' ,
77
+ expiration : { days : '10' } ,
78
+ abortIncompleteMultipartUpload : { daysAfterInitiation : '10' }
79
+ }
80
+ ]
81
+ } )
82
+ }
68
83
69
- return zipPath
84
+ // upload code to cos
85
+ if ( ! inputs . code . object ) {
86
+ console . log ( `Getting cos upload url for bucket ${ bucketName } ` )
87
+ const uploadUrl = await cos . getObjectUrl ( {
88
+ bucket : bucketName + '-' + appId ,
89
+ object : objectName ,
90
+ method : 'PUT'
91
+ } )
92
+ const slsSDKEntries = instance . getSDKEntries ( '_shims/handler.handler' )
93
+
94
+ console . log ( `Uploading code to bucket ${ bucketName } ` )
95
+ await instance . uploadSourceZipToCOS ( zipPath , uploadUrl , slsSDKEntries , {
96
+ _shims : path . join ( __dirname , '_shims' )
97
+ } )
98
+ console . log ( `Upload ${ objectName } to bucket ${ bucketName } success` )
99
+ }
100
+ }
101
+
102
+ // save bucket state
103
+ instance . state . bucket = bucketName
104
+ instance . state . object = objectName
105
+
106
+ return {
107
+ bucket : bucketName ,
108
+ object : objectName
109
+ }
70
110
}
71
111
72
112
const mergeJson = ( sourceJson , targetJson ) => {
@@ -300,7 +340,7 @@ const prepareInputs = async (instance, credentials, inputs = {}) => {
300
340
module . exports = {
301
341
generateId,
302
342
sleep,
303
- packageCode ,
343
+ uploadCodeToCos ,
304
344
mergeJson,
305
345
capitalString,
306
346
getDefaultProtocol,
0 commit comments