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