1
1
const { cos} = require ( 'tencent-cloud-sdk' )
2
+ const cdn = require ( '../cdn/index' )
2
3
const util = require ( 'util' )
3
4
const path = require ( 'path' )
4
5
const fs = require ( 'fs' )
@@ -373,22 +374,35 @@ class CosUtils {
373
374
}
374
375
375
376
async website ( inputs = { } ) {
377
+
378
+ await this . createBucket ( inputs )
379
+
380
+ inputs . acl = {
381
+ permissions : 'public-read' ,
382
+ grantRead : '' ,
383
+ grantWrite : '' ,
384
+ grantFullControl : ''
385
+ }
386
+ await this . setAcl ( inputs )
387
+
388
+ await this . setWebsite ( inputs )
389
+
390
+ // Build environment variables
376
391
const envPath = inputs . code . envPath || inputs . code . root
377
392
if ( inputs . env && Object . keys ( inputs . env ) . length && envPath ) {
378
393
let script = 'window.env = {};\n'
379
394
inputs . env = inputs . env || { }
380
395
for ( const e in inputs . env ) {
381
- // eslint-disable-line
382
- script += `window.env.${ e } = ${ JSON . stringify ( inputs . env [ e ] ) } ;\n` // eslint-disable-line
396
+ script += `window.env.${ e } = ${ JSON . stringify ( inputs . env [ e ] ) } ;\n`
383
397
}
384
398
const envFilePath = path . join ( envPath , 'env.js' )
385
- await utils . writeFile ( envFilePath , script )
399
+ await fs . writeFileSync ( envFilePath , script )
386
400
}
387
401
388
402
389
403
// If a hook is provided, build the website
390
404
if ( inputs . code . hook ) {
391
- const options = { cwd : inputs . code . root }
405
+ const options = { cwd : inputs . code . root }
392
406
try {
393
407
await exec ( inputs . code . hook , options )
394
408
} catch ( err ) {
@@ -398,6 +412,34 @@ class CosUtils {
398
412
}
399
413
}
400
414
415
+ // upload
416
+ const dirToUploadPath = inputs . code . src || inputs . code . root
417
+ const uploadDict = {
418
+ bucket : inputs . bucket
419
+ }
420
+ if ( fs . lstatSync ( dirToUploadPath ) . isDirectory ( ) ) {
421
+ uploadDict . dir = dirToUploadPath
422
+ } else {
423
+ uploadDict . file = dirToUploadPath
424
+ }
425
+ await this . upload ( uploadDict )
426
+
427
+ // add user domain
428
+ if ( inputs . hosts && inputs . hosts . length > 0 ) {
429
+ const cosOriginAdd = `${ inputs . bucket } .cos-website.${ this . region } .myqcloud.com`
430
+ for ( let i = 0 ; i < inputs . hosts . length ; i ++ ) {
431
+ const cdnInputs = inputs . hosts [ i ]
432
+ cdnInputs . hostType = 'cos'
433
+ cdnInputs . serviceType = 'web'
434
+ cdnInputs . fwdHost = cosOriginAdd
435
+ cdnInputs . origin = cosOriginAdd
436
+ }
437
+ const cdn = new Cdn ( this . credentials , this . region )
438
+ const outputs = await cdn . deploy ( inputs )
439
+ inputs . cndOutput = outputs
440
+ }
441
+
442
+ return inputs
401
443
}
402
444
403
445
async deploy ( inputs = { } ) {
0 commit comments