Skip to content

Commit afcd931

Browse files
author
dfounderliu
committed
add website
1 parent bb173bb commit afcd931

File tree

1 file changed

+46
-4
lines changed

1 file changed

+46
-4
lines changed

src/baas/cos/index.js

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const {cos} = require('tencent-cloud-sdk')
2+
const cdn = require('../cdn/index')
23
const util = require('util')
34
const path = require('path')
45
const fs = require('fs')
@@ -373,22 +374,35 @@ class CosUtils {
373374
}
374375

375376
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
376391
const envPath = inputs.code.envPath || inputs.code.root
377392
if (inputs.env && Object.keys(inputs.env).length && envPath) {
378393
let script = 'window.env = {};\n'
379394
inputs.env = inputs.env || {}
380395
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`
383397
}
384398
const envFilePath = path.join(envPath, 'env.js')
385-
await utils.writeFile(envFilePath, script)
399+
await fs.writeFileSync(envFilePath, script)
386400
}
387401

388402

389403
// If a hook is provided, build the website
390404
if (inputs.code.hook) {
391-
const options = { cwd: inputs.code.root }
405+
const options = {cwd: inputs.code.root}
392406
try {
393407
await exec(inputs.code.hook, options)
394408
} catch (err) {
@@ -398,6 +412,34 @@ class CosUtils {
398412
}
399413
}
400414

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
401443
}
402444

403445
async deploy(inputs = {}) {

0 commit comments

Comments
 (0)