-
Notifications
You must be signed in to change notification settings - Fork 0
/
serverless.js
94 lines (81 loc) · 2.66 KB
/
serverless.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
const ensureIterable = require('type/iterable/ensure')
const path = require('path')
const { Component } = require('@serverless/core')
const fs = require('fs')
const DEFAULTS = {
handler: 'index.main_handler',
runtime: 'Python3.6',
exclude: ['.git/**', '.gitignore', '.DS_Store']
}
class TencentWerobot extends Component {
getDefaultProtocol(protocols) {
if (protocols.map((i) => i.toLowerCase()).includes('https')) {
return 'https'
}
return 'http'
}
async copyDir(src, dst) {
const paths = await fs.readdirSync(src)
if (!fs.existsSync(dst)) {
await fs.mkdirSync(dst)
}
for (let i = 0; i < paths.length; i++) {
const thisFileStat = await fs.statSync(path.join(src, paths[i]))
if (thisFileStat.isFile()) {
const readable = await fs.readFileSync(path.join(src, paths[i]))
await fs.writeFileSync(path.join(dst, paths[i]), readable)
} else {
if (!fs.existsSync(path.join(dst, paths[i]))) {
await fs.mkdirSync(path.join(dst, paths[i]))
}
await this.copyDir(path.join(src, paths[i]), path.join(dst, paths[i]))
}
}
}
async default(inputs = {}) {
if (!inputs.werobotProjectName) {
throw new Error(`'werobotProjectName' is required in serverless.yaml`)
}
if (!inputs.werobotAttrName) {
throw new Error(`'werobotAttrName' is required in serverless.yaml`)
}
const cachePath = path.join(inputs.code, '.cache')
inputs.include = ensureIterable(inputs.include, { default: [] })
inputs.include.push(cachePath)
const src = path.join(__dirname, 'component')
await this.copyDir(src, cachePath)
const indexPyFile = await fs.readFileSync(
path.join(path.resolve(inputs.code), '.cache', 'index.py'),
'utf8'
)
const replacedFile = indexPyFile
.replace(eval('/{{werobot_project}}/g'), inputs.werobotProjectName)
.replace(eval('/{{attribute}}/g'), inputs.werobotAttrName)
await fs.writeFileSync(path.join(path.resolve(inputs.code), '.cache', 'index.py'), replacedFile)
inputs.handelr = DEFAULTS.handler
inputs.runtime = DEFAULTS.runtime
const Framework = await this.load('@serverless/tencent-framework')
const framworkOutpus = await Framework({
...inputs,
...{
framework: 'werobot'
}
})
this.state = framworkOutpus
await this.save()
return framworkOutpus
}
async remove(inputs = {}) {
const Framework = await this.load('@serverless/tencent-framework')
await Framework.remove({
...inputs,
...{
framework: 'werobot'
}
})
this.state = {}
await this.save()
return {}
}
}
module.exports = TencentWerobot