Skip to content

Commit 399649a

Browse files
committed
feat: refactor deploy flow using download
1 parent 03f902c commit 399649a

File tree

8 files changed

+99
-86
lines changed

8 files changed

+99
-86
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2020 Tencent Cloud Inc.
3+
Copyright (c) 2020 Tencent Cloud, Inc.
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

docs/configure.md

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ inputs:
1818
# hook: npm run build
1919
index: index.html
2020
error: index.html
21-
# websitePath: ./
2221
region: ap-guangzhou
2322
bucketName: my-bucket
2423
protocol: http
@@ -103,14 +102,13 @@ inputs:
103102
104103
### 执行目录
105104
106-
| 参数名称 | 是否必选 | 默认值 | 描述 |
107-
| ----------- | :------: | :--------: | :----------------------------------------------------------- |
108-
| src | 是 | | 你构建的项目代码目录。 |
109-
| websitePath | 否 | | 网站根目录。 |
110-
| dist | 否 | | 钩子脚本执行构建后,输出的目录。如果配置 `hook`, 此参数必填 |
111-
| hook | 否 | | 钩子脚本。在你项目代码上传之前执行。 |
112-
| index | 否 | index.html | 网站 index 页面 |
113-
| error | 否 | error.html | 网站 error 页面 |
105+
| 参数名称 | 是否必选 | 默认值 | 描述 |
106+
| -------- | :------: | :--------: | :----------------------------------------------------------- |
107+
| src | 是 | | 你构建的项目代码目录。 |
108+
| dist | 否 | | 钩子脚本执行构建后,输出的目录。如果配置 `hook`, 此参数必填 |
109+
| hook | 否 | | 钩子脚本。在你项目代码上传之前执行。 |
110+
| index | 否 | index.html | 网站 index 页面 |
111+
| error | 否 | error.html | 网站 error 页面 |
114112

115113
### 跨域配置
116114

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@serverless/website",
3-
"version": "0.0.6",
4-
"main": "./serverless.js",
3+
"version": "0.0.8",
4+
"main": "./src/serverless.js",
55
"publishConfig": {
66
"access": "public"
77
},

serverless.component.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
name: website
2-
version: 0.0.6
2+
version: 0.0.8
33
author: Tencent Cloud, Inc.
44
org: Tencent Cloud, Inc.
55
description: Deploys Tencent Website.
66
keywords: tencent, serverless, website
77
repo: https://github.com/serverless-components/tencent-website/tree/v2
8-
readme: https://github.com/serverless-components/tencent-website/tree/v2
8+
readme: https://github.com/serverless-components/tencent-website/tree/v2/README.md
99
license: MIT
1010
main: ./src

src/config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const CONFIGS = {
22
templateUrl:
3-
'https://serverless-templates-1300862921.cos.ap-beijing.myqcloud.com/express-demo.zip',
3+
'https://serverless-templates-1300862921.cos.ap-beijing.myqcloud.com/website-demo.zip',
44
region: 'ap-guangzhou',
55
indexPage: 'index.html',
66
errorPage: 'error.html',

src/package.json

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,12 @@
11
{
22
"name": "@serverless/website",
3-
"version": "0.0.1",
43
"main": "./serverless.js",
54
"publishConfig": {
65
"access": "public"
76
},
8-
"scripts": {
9-
"test": "echo \"Error: no test specified\" && exit 1",
10-
"lint": "eslint . --fix --cache"
11-
},
12-
"author": "anycodes",
13-
"license": "Apache",
147
"dependencies": {
15-
"fs-extra": "^8.1.0",
16-
"request": "^2.88.2",
17-
"string-random": "^0.1.3",
18-
"tencent-component-toolkit": "^1.7.2",
8+
"download": "^8.0.0",
9+
"tencent-component-toolkit": "^1.8.4",
1910
"type": "^2.0.0"
2011
}
2112
}

src/serverless.js

Lines changed: 8 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
const { Component } = require('@serverless/core')
2-
const path = require('path')
3-
const fs = require('fs')
4-
const request = require('request')
5-
const stringRandom = require('string-random')
62
const { Cos, Cdn } = require('tencent-component-toolkit')
73
const CONFIGS = require('./config')
4+
const { prepareInputs } = require('./utils')
85

96
class ServerlessComponent extends Component {
107
getCredentials() {
@@ -30,30 +27,6 @@ class ServerlessComponent extends Component {
3027
return 'http'
3128
}
3229

33-
async downloadDefaultZip() {
34-
const scfUrl = CONFIGS.templateUrl
35-
const loacalPath = '/tmp/' + stringRandom(10)
36-
fs.mkdirSync(loacalPath)
37-
return new Promise(function(resolve, reject) {
38-
request(scfUrl, function(error, response) {
39-
if (!error && response.statusCode == 200) {
40-
const stream = fs.createWriteStream(path.join(loacalPath, 'demo.zip'))
41-
request(scfUrl)
42-
.pipe(stream)
43-
.on('close', function() {
44-
resolve(path.join(loacalPath, 'demo.zip'))
45-
})
46-
} else {
47-
if (error) {
48-
reject(error)
49-
} else {
50-
reject(new Error(`Download error, status code: ${response.statusCode}`))
51-
}
52-
}
53-
})
54-
})
55-
}
56-
5730
async deployCdn({ credentials, domains = [], cosOrigin, originPullProtocol }) {
5831
console.log(`Deploying CDN ...`)
5932
let tencentCdnOutput
@@ -101,50 +74,25 @@ class ServerlessComponent extends Component {
10174
console.log(`Deploying Tencent Website ...`)
10275

10376
const credentials = this.getCredentials()
104-
const appid = this.credentials.tencent.tmpSecrets.appId
10577

106-
// 默认值
107-
const region = inputs.region || CONFIGS.region
78+
// 标准化website inputs
79+
const websiteInputs = await prepareInputs(this, inputs)
80+
81+
const { region } = websiteInputs
82+
10883
const output = {
10984
region
11085
}
11186

112-
// 判断是否需要测试模板
113-
if (!inputs.srcOriginal) {
87+
if (websiteInputs.useDefault) {
11488
output.templateUrl = CONFIGS.templateUrl
115-
inputs.srcOriginal = inputs.src || {}
116-
inputs.src = await this.downloadDefaultZip()
117-
inputs.srcOriginal.websitePath = './src'
11889
}
11990

120-
const sourceDirectory = await this.unzip(inputs.src)
91+
inputs.srcOriginal = inputs.srcOriginal || {}
12192

12293
// 创建cos对象
12394
const cos = new Cos(credentials, region)
12495

125-
// 标准化website inputs
126-
const websiteInputs = {
127-
code: {
128-
src: inputs.srcOriginal.websitePath
129-
? path.join(sourceDirectory, inputs.srcOriginal.websitePath)
130-
: sourceDirectory,
131-
index: inputs.srcOriginal.index || CONFIGS.indexPage,
132-
error: inputs.srcOriginal.error || CONFIGS.errorPage
133-
},
134-
bucket: inputs.bucketName + '-' + appid,
135-
region,
136-
protocol: inputs.protocol || CONFIGS.protocol
137-
}
138-
if (inputs.env) {
139-
websiteInputs.env = inputs.env
140-
}
141-
if (inputs.srcOriginal.envPath) {
142-
websiteInputs.code.envPath = path.join(websiteInputs.code.src, inputs.srcOriginal.envPath)
143-
}
144-
if (inputs.cors) {
145-
websiteInputs.cors = inputs.cors
146-
}
147-
14896
console.log(`Deploying Website ...`)
14997

15098
// 部署网站

src/utils.js

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
const download = require('download')
2+
const path = require('path')
3+
const CONFIGS = require('./config')
4+
5+
/*
6+
* Generates a random id
7+
*/
8+
const generateId = () =>
9+
Math.random()
10+
.toString(36)
11+
.substring(6)
12+
13+
const getCodeZipPath = async (instance, code) => {
14+
console.log(`Packaging ${CONFIGS.frameworkFullname} application...`)
15+
16+
// unzip source zip file
17+
let zipPath
18+
if (!code.src) {
19+
// add default template
20+
const downloadPath = `/tmp/${generateId()}`
21+
const filename = 'template'
22+
23+
console.log(`Installing Default ${CONFIGS.frameworkFullname} App...`)
24+
await download(CONFIGS.templateUrl, downloadPath, {
25+
filename: `${filename}.zip`
26+
})
27+
zipPath = `${downloadPath}/${filename}.zip`
28+
} else {
29+
zipPath = code.src
30+
}
31+
32+
return zipPath
33+
}
34+
35+
const prepareInputs = async (instance, inputs) => {
36+
console.log('inputs', inputs)
37+
let code = inputs.src || ''
38+
code =
39+
typeof code === 'string'
40+
? {
41+
src: code
42+
}
43+
: {
44+
...code
45+
}
46+
47+
const zipPath = await getCodeZipPath(this, code)
48+
const { appId } = instance.credentials.tencent.tmpSecrets
49+
const envPath = (inputs.srcOriginal && inputs.srcOriginal.envPath) || './'
50+
let sourceDirectory = await instance.unzip(zipPath)
51+
if (!code.src) {
52+
sourceDirectory = `${sourceDirectory}/src`
53+
}
54+
console.log(`Files unzipped into ${sourceDirectory}...`)
55+
56+
return {
57+
useDefault: !code.src,
58+
code: {
59+
src: sourceDirectory,
60+
index: (inputs.srcOriginal && inputs.srcOriginal.index) || CONFIGS.indexPage,
61+
error: (inputs.srcOriginal && inputs.srcOriginal.error) || CONFIGS.errorPage,
62+
envPath: path.join(sourceDirectory, envPath)
63+
},
64+
env: inputs.env,
65+
bucket: `${inputs.bucketName}-${appId}`,
66+
region: inputs.region || CONFIGS.region,
67+
protocol: inputs.protocol || CONFIGS.protocolc,
68+
cors: inputs.cors
69+
}
70+
}
71+
72+
module.exports = {
73+
generateId,
74+
getCodeZipPath,
75+
prepareInputs
76+
}

0 commit comments

Comments
 (0)