|
1 | 1 | const ensureIterable = require('type/iterable/ensure') |
2 | | -const ensurePlainObject = require('type/plain-object/ensure') |
3 | | -const ensureString = require('type/string/ensure') |
4 | | -const random = require('ext/string/random') |
5 | 2 | const path = require('path') |
6 | 3 | const { Component } = require('@serverless/core') |
7 | 4 | const fs = require('fs') |
@@ -39,121 +36,57 @@ class TencentWerobot extends Component { |
39 | 36 | } |
40 | 37 | } |
41 | 38 |
|
42 | | - async prepareInputs(inputs = {}) { |
43 | | - inputs.name = |
44 | | - ensureString(inputs.functionName, { isOptional: true }) || |
45 | | - this.state.functionName || |
46 | | - `WerobotComponent_${random({ length: 6 })}` |
47 | | - inputs.codeUri = ensureString(inputs.code, { isOptional: true }) || process.cwd() |
48 | | - inputs.region = ensureString(inputs.region, { default: 'ap-guangzhou' }) |
49 | | - inputs.namespace = ensureString(inputs.namespace, { default: 'default' }) |
50 | | - inputs.include = ensureIterable(inputs.include, { default: [], ensureItem: ensureString }) |
51 | | - inputs.exclude = ensureIterable(inputs.exclude, { default: [], ensureItem: ensureString }) |
52 | | - inputs.apigatewayConf = ensurePlainObject(inputs.apigatewayConf, { default: {} }) |
| 39 | + async default(inputs = {}) { |
| 40 | + if (!inputs.werobotProjectName) { |
| 41 | + throw new Error(`'werobotProjectName' is required in serverless.yaml`) |
| 42 | + } |
| 43 | + if (!inputs.werobotAttrName) { |
| 44 | + throw new Error(`'werobotAttrName' is required in serverless.yaml`) |
| 45 | + } |
| 46 | + const cachePath = path.join(inputs.code, '.cache') |
| 47 | + inputs.include = ensureIterable(inputs.include, { default: [] }) |
| 48 | + inputs.include.push(cachePath) |
53 | 49 |
|
54 | 50 | const src = path.join(__dirname, 'component') |
55 | | - const dst = path.join(inputs.codeUri, '.cache') |
56 | | - await this.copyDir(src, dst) |
| 51 | + await this.copyDir(src, cachePath) |
57 | 52 | const indexPyFile = await fs.readFileSync( |
58 | | - path.join(path.resolve(inputs.codeUri), '.cache', 'index.py'), |
| 53 | + path.join(path.resolve(inputs.code), '.cache', 'index.py'), |
59 | 54 | 'utf8' |
60 | 55 | ) |
| 56 | + |
61 | 57 | const replacedFile = indexPyFile |
62 | 58 | .replace(eval('/{{werobot_project}}/g'), inputs.werobotProjectName) |
63 | 59 | .replace(eval('/{{attribute}}/g'), inputs.werobotAttrName) |
64 | | - await fs.writeFileSync( |
65 | | - path.join(path.resolve(inputs.codeUri), '.cache', 'index.py'), |
66 | | - replacedFile |
67 | | - ) |
68 | | - |
69 | | - inputs.include = [path.join(inputs.codeUri, '.cache')] |
70 | | - inputs.exclude.push('.git/**', '.gitignore', '.serverless', '.DS_Store') |
71 | | - |
72 | | - inputs.handler = ensureString(inputs.handler, { default: DEFAULTS.handler }) |
73 | | - inputs.runtime = ensureString(inputs.runtime, { default: DEFAULTS.runtime }) |
74 | | - inputs.apigatewayConf = ensurePlainObject(inputs.apigatewayConf, { default: {} }) |
75 | | - |
76 | | - if (inputs.functionConf) { |
77 | | - inputs.timeout = inputs.functionConf.timeout ? inputs.functionConf.timeout : 3 |
78 | | - inputs.memorySize = inputs.functionConf.memorySize ? inputs.functionConf.memorySize : 128 |
79 | | - if (inputs.functionConf.environment) { |
80 | | - inputs.environment = inputs.functionConf.environment |
81 | | - } |
82 | | - if (inputs.functionConf.vpcConfig) { |
83 | | - inputs.vpcConfig = inputs.functionConf.vpcConfig |
84 | | - } |
85 | | - } |
86 | | - |
87 | | - return inputs |
88 | | - } |
89 | | - |
90 | | - async default(inputs = {}) { |
91 | | - inputs = await this.prepareInputs(inputs) |
92 | 60 |
|
93 | | - const tencentCloudFunction = await this.load('@serverless/tencent-scf') |
94 | | - const tencentApiGateway = await this.load('@serverless/tencent-apigateway') |
| 61 | + await fs.writeFileSync(path.join(path.resolve(inputs.code), '.cache', 'index.py'), replacedFile) |
95 | 62 |
|
96 | | - inputs.fromClientRemark = inputs.fromClientRemark || 'tencent-werobot' |
97 | | - const tencentCloudFunctionOutputs = await tencentCloudFunction(inputs) |
98 | | - const apigwParam = { |
99 | | - serviceName: inputs.serviceName, |
100 | | - description: 'Serverless Framework Tencent-Werobot Component', |
101 | | - serviceId: inputs.serviceId, |
102 | | - region: inputs.region, |
103 | | - protocols: inputs.apigatewayConf.protocols || ['http'], |
104 | | - environment: |
105 | | - inputs.apigatewayConf && inputs.apigatewayConf.environment |
106 | | - ? inputs.apigatewayConf.environment |
107 | | - : 'release', |
108 | | - endpoints: [ |
109 | | - { |
110 | | - path: '/', |
111 | | - method: 'ANY', |
112 | | - function: { |
113 | | - isIntegratedResponse: true, |
114 | | - functionName: tencentCloudFunctionOutputs.Name, |
115 | | - functionNamespace: inputs.namespace |
116 | | - } |
117 | | - } |
118 | | - ], |
119 | | - customDomain: inputs.apigatewayConf.customDomain |
120 | | - } |
| 63 | + inputs.handelr = DEFAULTS.handler |
| 64 | + inputs.runtime = DEFAULTS.runtime |
121 | 65 |
|
122 | | - if (inputs.apigatewayConf && inputs.apigatewayConf.auth) { |
123 | | - apigwParam.endpoints[0].usagePlan = inputs.apigatewayConf.usagePlan |
124 | | - } |
125 | | - if (inputs.apigatewayConf && inputs.apigatewayConf.auth) { |
126 | | - apigwParam.endpoints[0].auth = inputs.apigatewayConf.auth |
127 | | - } |
128 | | - |
129 | | - apigwParam.fromClientRemark = inputs.fromClientRemark || 'tencent-werobot' |
130 | | - const tencentApiGatewayOutputs = await tencentApiGateway(apigwParam) |
131 | | - const outputs = { |
132 | | - region: inputs.region, |
133 | | - functionName: inputs.name, |
134 | | - apiGatewayServiceId: tencentApiGatewayOutputs.serviceId, |
135 | | - url: `${this.getDefaultProtocol(tencentApiGatewayOutputs.protocols)}://${ |
136 | | - tencentApiGatewayOutputs.subDomain |
137 | | - }/${tencentApiGatewayOutputs.environment}/` |
138 | | - } |
| 66 | + const Framework = await this.load('@serverless/tencent-framework') |
139 | 67 |
|
140 | | - this.state = outputs |
| 68 | + const framworkOutpus = await Framework({ |
| 69 | + ...inputs, |
| 70 | + ...{ |
| 71 | + framework: 'werobot' |
| 72 | + } |
| 73 | + }) |
141 | 74 |
|
| 75 | + this.state = framworkOutpus |
142 | 76 | await this.save() |
143 | | - |
144 | | - return outputs |
| 77 | + return framworkOutpus |
145 | 78 | } |
146 | 79 |
|
147 | 80 | async remove(inputs = {}) { |
148 | | - const removeInput = { |
149 | | - fromClientRemark: inputs.fromClientRemark || 'tencent-werobot' |
150 | | - } |
151 | | - const tencentCloudFunction = await this.load('@serverless/tencent-scf') |
152 | | - const tencentApiGateway = await this.load('@serverless/tencent-apigateway') |
153 | | - |
154 | | - await tencentCloudFunction.remove(removeInput) |
155 | | - await tencentApiGateway.remove(removeInput) |
156 | | - |
| 81 | + const Framework = await this.load('@serverless/tencent-framework') |
| 82 | + await Framework.remove({ |
| 83 | + ...inputs, |
| 84 | + ...{ |
| 85 | + framework: 'werobot' |
| 86 | + } |
| 87 | + }) |
| 88 | + this.state = {} |
| 89 | + await this.save() |
157 | 90 | return {} |
158 | 91 | } |
159 | 92 | } |
|
0 commit comments