Skip to content

Commit ad33879

Browse files
committed
feat: optimize inputs initial
1 parent c4135c4 commit ad33879

9 files changed

+75
-33
lines changed

.editorconfig

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# EditorConfig is awesome: http://EditorConfig.org
2+
3+
# top-most EditorConfig file
4+
root = true
5+
6+
[*]
7+
charset = utf-8
8+
end_of_line = lf
9+
insert_final_newline = true
10+
indent_size = 2
11+
indent_style = space
12+
trim_trailing_whitespace = true
13+
14+
[*.md]
15+
trim_trailing_whitespace = false

.eslintrc.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ module.exports = {
6666
],
6767
semi: ['error', 'never'],
6868
'spaced-comment': 'error',
69-
strict: ['error', 'never'],
69+
strict: ['error', 'global'],
7070
'prettier/prettier': 'error'
7171
}
7272
}

README.en.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ MyComponent:
6161
inputs:
6262
region: ap-guangzhou
6363
functionName: flask-function
64-
codeUri: ./
64+
code: ./
6565
functionConf:
6666
timeout: 10
6767
memorySize: 128

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ MyComponent:
4848
inputs:
4949
region: ap-guangzhou
5050
functionName: flask-function
51-
codeUri: ./
51+
code: ./
5252
functionConf:
5353
timeout: 10
5454
memorySize: 128

package.json

+9-5
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
],
1616
"scripts": {
1717
"commitlint": "commitlint -f HEAD@{15}",
18+
"test": "tape *.test.js",
1819
"lint": "eslint --ext .js,.ts,.tsx .",
1920
"lint:fix": "eslint --fix --ext .js,.ts,.tsx .",
2021
"prettier": "prettier --check **/*.{css,html,js,json,md,yaml,yml}",
@@ -46,10 +47,12 @@
4647
}
4748
},
4849
"dependencies": {
49-
"@serverless/core": "^1.0.0",
50-
"@serverless/tencent-apigateway": "^1.2.0",
51-
"@serverless/tencent-scf": "^1.1.0",
52-
"@yugasun/python-requirements": "^0.1.1"
50+
"@serverless/core": "^1.1.1",
51+
"@serverless/tencent-apigateway": "^2.0.0",
52+
"@serverless/tencent-scf": "^2.0.1",
53+
"@yugasun/python-requirements": "^0.1.2",
54+
"ext": "^1.4.0",
55+
"type": "^2.0.0"
5356
},
5457
"devDependencies": {
5558
"@commitlint/cli": "^8.2.0",
@@ -63,6 +66,7 @@
6366
"husky": "^3.1.0",
6467
"lint-staged": "^9.5.0",
6568
"prettier": "^1.15.3",
66-
"standard-version": "^7.0.1"
69+
"standard-version": "^7.0.1",
70+
"tape": "^4.12.0"
6771
}
6872
}

serverless.test.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const test = require('tape')
2+
3+
const Component = require('./serverless')
4+
5+
test('getDefaultProtocol()', (t) => {
6+
t.plan(5)
7+
8+
const comp = new Component()
9+
t.equal(comp.getDefaultProtocol(['http']), 'http')
10+
t.equal(comp.getDefaultProtocol(['https']), 'https')
11+
t.equal(comp.getDefaultProtocol(['http', 'https']), 'https')
12+
t.equal(comp.getDefaultProtocol(['HTTP', 'hTTpS']), 'https')
13+
t.equal(comp.getDefaultProtocol(['http', 'ftp']), 'http')
14+
})

src/index.js

+31-21
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
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')
15
const path = require('path')
26
const { Component, utils } = require('@serverless/core')
37

@@ -8,32 +12,40 @@ const DEFAULTS = {
812
}
913

1014
class TencentFlask extends Component {
15+
getDefaultProtocol(protocols) {
16+
if (protocols.map((i) => i.toLowerCase()).includes('https')) {
17+
return 'https'
18+
}
19+
return 'http'
20+
}
21+
1122
/**
1223
* prepare create function inputs
1324
* @param {object} inputs inputs
1425
*/
1526
async prepareInputs(inputs = {}) {
16-
const len = 6
17-
const chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678'
18-
const maxPos = chars.length
19-
let result = ''
20-
for (let i = 0; i < len; i++) {
21-
result += chars.charAt(Math.floor(Math.random() * maxPos))
22-
}
27+
inputs.name =
28+
ensureString(inputs.functionName, { isOptional: true }) ||
29+
this.state.functionName ||
30+
`FlaskComponent_${random({ length: 6 })}`
31+
inputs.codeUri = ensureString(inputs.code, { isOptional: true }) || process.cwd()
32+
inputs.region = ensureString(inputs.region, { default: 'ap-guangzhou' })
33+
inputs.include = ensureIterable(inputs.include, { default: [], ensureItem: ensureString })
34+
inputs.exclude = ensureIterable(inputs.exclude, { default: [], ensureItem: ensureString })
35+
inputs.apigatewayConf = ensurePlainObject(inputs.apigatewayConf, { default: {} })
2336

2437
const shimsDir = path.join(__dirname, 'shims')
2538
inputs.include = [
2639
path.join(shimsDir, 'severless_wsgi.py'),
2740
path.join(shimsDir, 'api_service.py')
2841
]
29-
inputs.exclude = inputs.exclude || ['.git/**', '.gitignore', '.serverless', '.DS_Store']
30-
inputs.handler = inputs.handler || DEFAULTS.handler
31-
inputs.runtime = inputs.runtime || DEFAULTS.runtime
32-
inputs.name = inputs.functionName || 'FlaskComponent_' + result
33-
inputs.codeUri = inputs.codeUri || process.cwd()
42+
inputs.exclude.push('.git/**', '.gitignore', '.serverless', '.DS_Store')
3443

35-
const appFile = path.join(path.resolve(inputs.codeUri), 'app.py')
44+
inputs.handler = ensureString(inputs.handler, { default: DEFAULTS.handler })
45+
inputs.runtime = ensureString(inputs.runtime, { default: DEFAULTS.runtime })
46+
inputs.apigatewayConf = ensurePlainObject(inputs.apigatewayConf, { default: {} })
3647

48+
const appFile = path.join(path.resolve(inputs.codeUri), 'app.py')
3749
if (!(await utils.fileExists(appFile))) {
3850
throw new Error(`app.py not found in ${inputs.codeUri}`)
3951
}
@@ -48,9 +60,8 @@ class TencentFlask extends Component {
4860
inputs.vpcConfig = inputs.functionConf.vpcConfig
4961
}
5062
}
51-
if (!inputs.requirements) {
52-
inputs.requirements = {}
53-
}
63+
64+
inputs.requirements = ensurePlainObject(inputs.apigatewayConf, { default: {} })
5465
inputs.requirements.include = inputs.include
5566
inputs.requirements.runtime = inputs.runtime.toLowerCase()
5667
inputs.requirements.codeUri = inputs.codeUri
@@ -78,10 +89,7 @@ class TencentFlask extends Component {
7889
description: 'Serverless Framework tencent-flask Component',
7990
serviceId: inputs.serviceId,
8091
region: inputs.region,
81-
protocol:
82-
inputs.apigatewayConf && inputs.apigatewayConf.protocol
83-
? inputs.apigatewayConf.protocol
84-
: 'http',
92+
protocols: inputs.apigatewayConf.protocols || ['http'],
8593
environment:
8694
inputs.apigatewayConf && inputs.apigatewayConf.environment
8795
? inputs.apigatewayConf.environment
@@ -110,7 +118,9 @@ class TencentFlask extends Component {
110118
region: inputs.region,
111119
functionName: inputs.name,
112120
apiGatewayServiceId: tencentApiGatewayOutputs.serviceId,
113-
url: `${tencentApiGatewayOutputs.protocol}://${tencentApiGatewayOutputs.subDomain}/${tencentApiGatewayOutputs.environment}/`
121+
url: `${this.getDefaultProtocol(tencentApiGatewayOutputs.protocols)}://${
122+
tencentApiGatewayOutputs.subDomain
123+
}/${tencentApiGatewayOutputs.environment}/`
114124
}
115125

116126
await this.save()

src/requirements.txt

-2
This file was deleted.

test/serverless.yml

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ MyFlask:
33
inputs:
44
region: ap-guangzhou
55
functionName: flask-function
6-
codeUri: ./
6+
code: ./
77
functionConf:
88
timeout: 10
99
memorySize: 128
@@ -14,5 +14,6 @@ MyFlask:
1414
subnetId: ''
1515
vpcId: ''
1616
apigatewayConf:
17-
protocol: https
17+
protocols:
18+
- https
1819
environment: release

0 commit comments

Comments
 (0)