Skip to content

Commit e98acf7

Browse files
authored
Merge pull request #1 from serverless-tencent/fix/support-faas-layer
fix: support http component layer config
2 parents 6fcc84c + daa99a3 commit e98acf7

File tree

3 files changed

+205
-7
lines changed

3 files changed

+205
-7
lines changed

__tests__/http/parse.http.test.ts

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
import { join } from 'path';
2+
import { removeSync, outputFileSync } from 'fs-extra';
3+
import { parse } from '../../src/parse';
4+
5+
describe('Parse command test', () => {
6+
// inject environment variables
7+
process.env.REGION = 'ap-guangzhou';
8+
9+
const configFileContent = `org: orgDemo
10+
app: appDemo
11+
stage: dev
12+
component: http
13+
name: httpDemo
14+
15+
inputs:
16+
src:
17+
src: ./
18+
exclude:
19+
- .env
20+
region: \${env:REGION}
21+
faas:
22+
framework: express
23+
runtime: Nodejs10.15
24+
apigw:
25+
protocols:
26+
- http
27+
- https
28+
`;
29+
const demoPath = join(__dirname, 'demo');
30+
const outputPath = join(__dirname, 'output');
31+
const layerPath = join(__dirname, 'layer');
32+
const fileName = 'serverless.yml';
33+
34+
beforeAll(() => {
35+
const configFile = join(demoPath, fileName);
36+
outputFileSync(configFile, configFileContent);
37+
});
38+
39+
afterAll(() => {
40+
removeSync(demoPath);
41+
removeSync(outputPath);
42+
removeSync(layerPath);
43+
});
44+
45+
const configFile = join(demoPath, fileName);
46+
47+
test(`should success parse ${fileName} file`, async () => {
48+
const res = parse({
49+
rootDir: __dirname,
50+
input: configFile,
51+
});
52+
expect(res).toEqual({
53+
org: 'orgDemo',
54+
app: 'appDemo',
55+
stage: 'dev',
56+
component: 'http',
57+
name: 'httpDemo',
58+
inputs: {
59+
src: {
60+
src: './',
61+
exclude: ['.env'],
62+
},
63+
region: 'ap-guangzhou',
64+
faas: {
65+
framework: 'express',
66+
runtime: 'Nodejs10.15',
67+
},
68+
apigw: {
69+
protocols: ['http', 'https'],
70+
},
71+
},
72+
});
73+
});
74+
75+
test(`should success parse ${fileName} file using slsOptions`, async () => {
76+
const res = parse({
77+
rootDir: __dirname,
78+
input: configFile,
79+
slsOptionsJson: '{"inputs":{"src":"./src"}}',
80+
});
81+
expect(res).toEqual({
82+
org: 'orgDemo',
83+
app: 'appDemo',
84+
stage: 'dev',
85+
component: 'http',
86+
name: 'httpDemo',
87+
inputs: {
88+
src: './src',
89+
region: 'ap-guangzhou',
90+
faas: {
91+
framework: 'express',
92+
runtime: 'Nodejs10.15',
93+
},
94+
apigw: {
95+
protocols: ['http', 'https'],
96+
},
97+
},
98+
});
99+
});
100+
101+
test(`should success parse ${fileName} file using slsOptions with new property`, async () => {
102+
const res = parse({
103+
rootDir: __dirname,
104+
input: configFile,
105+
slsOptionsJson: '{"inputs":{"src":"./","test":1}}',
106+
});
107+
expect(res).toEqual({
108+
org: 'orgDemo',
109+
app: 'appDemo',
110+
stage: 'dev',
111+
component: 'http',
112+
name: 'httpDemo',
113+
inputs: {
114+
src: './',
115+
test: 1,
116+
region: 'ap-guangzhou',
117+
faas: {
118+
framework: 'express',
119+
runtime: 'Nodejs10.15',
120+
},
121+
apigw: {
122+
protocols: ['http', 'https'],
123+
},
124+
},
125+
});
126+
});
127+
128+
test(`should success parse ${fileName} file override by slsOptions `, async () => {
129+
const res = parse({
130+
rootDir: __dirname,
131+
input: configFile,
132+
override: true,
133+
slsOptionsJson:
134+
'{"org": "orgDemo","app": "appDemo","stage": "dev","component": "http","name": "httpDemoTest","inputs":{"src":"./","region":"ap-guangzhou"}}',
135+
});
136+
expect(res).toEqual({
137+
org: 'orgDemo',
138+
app: 'appDemo',
139+
stage: 'dev',
140+
component: 'http',
141+
name: 'httpDemoTest',
142+
inputs: {
143+
src: './',
144+
region: 'ap-guangzhou',
145+
},
146+
});
147+
});
148+
149+
test(`should success parse ${fileName} file using layerOptions of http component`, async () => {
150+
const res = parse({
151+
rootDir: __dirname,
152+
input: configFile,
153+
layerOptionsJson:
154+
'{"org":"orgDemo","app":"appDemo","stage":"dev","runtime":"Nodejs10.15","region":"ap-guangzhou"}',
155+
});
156+
expect(res).toEqual({
157+
org: 'orgDemo',
158+
app: 'appDemo',
159+
stage: 'dev',
160+
component: 'http',
161+
name: 'httpDemo',
162+
inputs: {
163+
region: 'ap-guangzhou',
164+
src: {
165+
src: './',
166+
exclude: ['.env'],
167+
},
168+
faas: {
169+
framework: 'express',
170+
runtime: 'Nodejs10.15',
171+
layers: [
172+
{
173+
name: '${output:${stage}:${app}:appDemo-layer.name}',
174+
version: '${output:${stage}:${app}:appDemo-layer.version}',
175+
},
176+
],
177+
},
178+
apigw: {
179+
protocols: ['http', 'https'],
180+
},
181+
},
182+
});
183+
});
184+
});

src/components/config.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ const FRAMEWORK_COMPONENTS = [
3838
'thinkphp',
3939
];
4040

41+
const NEW_STANDARD_COMPONENTS = ['http'];
42+
4143
const COMPONENTS = [...BASE_COMPONENTS, ...FRAMEWORK_COMPONENTS];
4244

4345
const isBaseComponent = (name: string) => {
@@ -48,4 +50,8 @@ const isFrameworkComponent = (name = 'framework') => {
4850
return FRAMEWORK_COMPONENTS.indexOf(name) !== -1;
4951
};
5052

51-
export { COMPONENTS, getDefaultConfig, isBaseComponent, isFrameworkComponent };
53+
const isNewStandardFramework = (component: string) => {
54+
return NEW_STANDARD_COMPONENTS.includes(component);
55+
};
56+
57+
export { COMPONENTS, getDefaultConfig, isBaseComponent, isFrameworkComponent, isNewStandardFramework };

src/parse.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import chalk from 'chalk';
66
import { program } from 'commander';
77
import { fileExist, isJsonPath, isYamlPath, getFileExt, mergeObject } from './utils';
88
import { AnyObject, ParseOptions } from './typings';
9-
import { getDefaultConfig } from './components/config';
9+
import { getDefaultConfig, isNewStandardFramework } from './components/config';
1010
import { createLayerConfig } from './components/layer';
1111

1212
/**
@@ -73,11 +73,19 @@ function generateLayerYaml(rootDir: string, slsOptions: AnyObject, layerOptions
7373
const layerConfig = createLayerConfig(layerPath, JSON.parse(layerOptions));
7474
// 2. update project serverless.yml
7575
slsOptions.inputs = slsOptions.inputs || {};
76-
slsOptions.inputs.layers = slsOptions.inputs.layers || [];
77-
slsOptions.inputs.layers.push({
78-
name: '${output:${stage}:${app}:' + layerConfig.name + '.name}',
79-
version: '${output:${stage}:${app}:' + layerConfig.name + '.version}',
80-
});
76+
if (isNewStandardFramework(slsOptions.component)) {
77+
slsOptions.inputs.faas.layers = slsOptions.inputs.faas.layers || [];
78+
slsOptions.inputs.faas.layers.push({
79+
name: '${output:${stage}:${app}:' + layerConfig.name + '.name}',
80+
version: '${output:${stage}:${app}:' + layerConfig.name + '.version}',
81+
});
82+
} else {
83+
slsOptions.inputs.layers = slsOptions.inputs.layers || [];
84+
slsOptions.inputs.layers.push({
85+
name: '${output:${stage}:${app}:' + layerConfig.name + '.name}',
86+
version: '${output:${stage}:${app}:' + layerConfig.name + '.version}',
87+
});
88+
}
8189
}
8290
return slsOptions;
8391
}

0 commit comments

Comments
 (0)