From 6fcc84c9507f1d86b056fd80f50ee47476547da9 Mon Sep 17 00:00:00 2001 From: yugasun Date: Fri, 18 Jun 2021 10:19:50 +0800 Subject: [PATCH 1/4] docs: update readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index cdcdaf7..f609fce 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ Yaml Parser for Serverless Framework. - [@slsplus/yaml-parser](#Serverless-Yaml-Parser) - [Installation](#installation) - [Usage](#usage) - - [parse](#Parse-serverless-config-file) + - [Parse serverless config file](#Parse-serverless-config-file) ## Installation From bd1e28d1a45cfc35620466766d0ee0c4be29c0c1 Mon Sep 17 00:00:00 2001 From: Avril Li Date: Mon, 26 Jul 2021 20:35:55 +0800 Subject: [PATCH 2/4] fix: support http component layer config --- __tests__/http/parse.http.test.ts | 184 ++++++++++++++++++++++++++++++ src/components/config.ts | 18 ++- src/parse.ts | 23 +++- 3 files changed, 218 insertions(+), 7 deletions(-) create mode 100644 __tests__/http/parse.http.test.ts diff --git a/__tests__/http/parse.http.test.ts b/__tests__/http/parse.http.test.ts new file mode 100644 index 0000000..8bdb22a --- /dev/null +++ b/__tests__/http/parse.http.test.ts @@ -0,0 +1,184 @@ +import { join } from 'path'; +import { removeSync, outputFileSync } from 'fs-extra'; +import { parse } from '../../src/parse'; + +describe('Parse command test', () => { + // inject environment variables + process.env.REGION = 'ap-guangzhou'; + + const configFileContent = `org: orgDemo +app: appDemo +stage: dev +component: http +name: httpDemo + +inputs: + src: + src: ./ + exclude: + - .env + region: \${env:REGION} + faas: + framework: express + runtime: Nodejs10.15 + apigw: + protocols: + - http + - https +`; + const demoPath = join(__dirname, 'demo'); + const outputPath = join(__dirname, 'output'); + const layerPath = join(__dirname, 'layer'); + const fileName = 'serverless.yml'; + + beforeAll(() => { + const configFile = join(demoPath, fileName); + outputFileSync(configFile, configFileContent); + }); + + afterAll(() => { + removeSync(demoPath); + removeSync(outputPath); + removeSync(layerPath); + }); + + const configFile = join(demoPath, fileName); + + test(`should success parse ${fileName} file`, async () => { + const res = parse({ + rootDir: __dirname, + input: configFile, + }); + expect(res).toEqual({ + org: 'orgDemo', + app: 'appDemo', + stage: 'dev', + component: 'http', + name: 'httpDemo', + inputs: { + src: { + src: './', + exclude: ['.env'], + }, + region: 'ap-guangzhou', + faas: { + framework: 'express', + runtime: 'Nodejs10.15', + }, + apigw: { + protocols: ['http', 'https'], + }, + }, + }); + }); + + test(`should success parse ${fileName} file using slsOptions`, async () => { + const res = parse({ + rootDir: __dirname, + input: configFile, + slsOptionsJson: '{"inputs":{"src":"./src"}}', + }); + expect(res).toEqual({ + org: 'orgDemo', + app: 'appDemo', + stage: 'dev', + component: 'http', + name: 'httpDemo', + inputs: { + src: './src', + region: 'ap-guangzhou', + faas: { + framework: 'express', + runtime: 'Nodejs10.15', + }, + apigw: { + protocols: ['http', 'https'], + }, + }, + }); + }); + + test(`should success parse ${fileName} file using slsOptions with new property`, async () => { + const res = parse({ + rootDir: __dirname, + input: configFile, + slsOptionsJson: '{"inputs":{"src":"./","test":1}}', + }); + expect(res).toEqual({ + org: 'orgDemo', + app: 'appDemo', + stage: 'dev', + component: 'http', + name: 'httpDemo', + inputs: { + src: './', + test: 1, + region: 'ap-guangzhou', + faas: { + framework: 'express', + runtime: 'Nodejs10.15', + }, + apigw: { + protocols: ['http', 'https'], + }, + }, + }); + }); + + test(`should success parse ${fileName} file override by slsOptions `, async () => { + const res = parse({ + rootDir: __dirname, + input: configFile, + override: true, + slsOptionsJson: + '{"org": "orgDemo","app": "appDemo","stage": "dev","component": "http","name": "httpDemoTest","inputs":{"src":"./","region":"ap-guangzhou"}}', + }); + expect(res).toEqual({ + org: 'orgDemo', + app: 'appDemo', + stage: 'dev', + component: 'http', + name: 'httpDemoTest', + inputs: { + src: './', + region: 'ap-guangzhou', + }, + }); + }); + + test(`should success parse ${fileName} file using layerOptions of http component`, async () => { + const res = parse({ + rootDir: __dirname, + input: configFile, + layerOptionsJson: + '{"org":"orgDemo","app":"appDemo","stage":"dev","runtime":"Nodejs10.15","region":"ap-guangzhou"}', + }); + expect(res).toEqual({ + org: 'orgDemo', + app: 'appDemo', + stage: 'dev', + component: 'http', + name: 'httpDemo', + inputs: { + region: 'ap-guangzhou', + src: { + src: './', + exclude: ['.env'], + }, + faas: { + framework: 'express', + runtime: 'Nodejs10.15', + layers: [ + { + name: '${output:${stage}:${app}:appDemo-layer.name}', + version: '${output:${stage}:${app}:appDemo-layer.version}', + }, + ], + }, + apigw: { + protocols: ['http', 'https'], + }, + }, + }); + }); +}); diff --git a/src/components/config.ts b/src/components/config.ts index 35a7670..d9472c0 100644 --- a/src/components/config.ts +++ b/src/components/config.ts @@ -38,6 +38,18 @@ const FRAMEWORK_COMPONENTS = [ 'thinkphp', ]; +const HTTP_FRAMEWORKS = [ + 'express', + 'koa', + 'egg', + 'nextjs', + 'nuxtjs', + 'nest', + 'flask', + 'django', + 'laravel', +]; + const COMPONENTS = [...BASE_COMPONENTS, ...FRAMEWORK_COMPONENTS]; const isBaseComponent = (name: string) => { @@ -48,4 +60,8 @@ const isFrameworkComponent = (name = 'framework') => { return FRAMEWORK_COMPONENTS.indexOf(name) !== -1; }; -export { COMPONENTS, getDefaultConfig, isBaseComponent, isFrameworkComponent }; +const isHttpFramework = (component: string, framework?: string) => { + return component === 'http' && framework && HTTP_FRAMEWORKS.includes(framework); +}; + +export { COMPONENTS, getDefaultConfig, isBaseComponent, isFrameworkComponent, isHttpFramework }; diff --git a/src/parse.ts b/src/parse.ts index 9cdb987..9aca872 100644 --- a/src/parse.ts +++ b/src/parse.ts @@ -6,7 +6,7 @@ import chalk from 'chalk'; import { program } from 'commander'; import { fileExist, isJsonPath, isYamlPath, getFileExt, mergeObject } from './utils'; import { AnyObject, ParseOptions } from './typings'; -import { getDefaultConfig } from './components/config'; +import { getDefaultConfig, isHttpFramework } from './components/config'; import { createLayerConfig } from './components/layer'; /** @@ -73,11 +73,22 @@ function generateLayerYaml(rootDir: string, slsOptions: AnyObject, layerOptions const layerConfig = createLayerConfig(layerPath, JSON.parse(layerOptions)); // 2. update project serverless.yml slsOptions.inputs = slsOptions.inputs || {}; - slsOptions.inputs.layers = slsOptions.inputs.layers || []; - slsOptions.inputs.layers.push({ - name: '${output:${stage}:${app}:' + layerConfig.name + '.name}', - version: '${output:${stage}:${app}:' + layerConfig.name + '.version}', - }); + if ( + slsOptions.inputs?.faas?.framework && + isHttpFramework(slsOptions.component, slsOptions.inputs.faas.framework) + ) { + slsOptions.inputs.faas.layers = slsOptions.inputs.faas.layers || []; + slsOptions.inputs.faas.layers.push({ + name: '${output:${stage}:${app}:' + layerConfig.name + '.name}', + version: '${output:${stage}:${app}:' + layerConfig.name + '.version}', + }); + } else { + slsOptions.inputs.layers = slsOptions.inputs.layers || []; + slsOptions.inputs.layers.push({ + name: '${output:${stage}:${app}:' + layerConfig.name + '.name}', + version: '${output:${stage}:${app}:' + layerConfig.name + '.version}', + }); + } } return slsOptions; } From daa99a3042adec00257392485c4a2e4785c58099 Mon Sep 17 00:00:00 2001 From: Avril Li Date: Mon, 26 Jul 2021 20:48:47 +0800 Subject: [PATCH 3/4] chore: update inject faas layer logic --- src/components/config.ts | 18 ++++-------------- src/parse.ts | 7 ++----- 2 files changed, 6 insertions(+), 19 deletions(-) diff --git a/src/components/config.ts b/src/components/config.ts index d9472c0..db3f1aa 100644 --- a/src/components/config.ts +++ b/src/components/config.ts @@ -38,17 +38,7 @@ const FRAMEWORK_COMPONENTS = [ 'thinkphp', ]; -const HTTP_FRAMEWORKS = [ - 'express', - 'koa', - 'egg', - 'nextjs', - 'nuxtjs', - 'nest', - 'flask', - 'django', - 'laravel', -]; +const NEW_STANDARD_COMPONENTS = ['http']; const COMPONENTS = [...BASE_COMPONENTS, ...FRAMEWORK_COMPONENTS]; @@ -60,8 +50,8 @@ const isFrameworkComponent = (name = 'framework') => { return FRAMEWORK_COMPONENTS.indexOf(name) !== -1; }; -const isHttpFramework = (component: string, framework?: string) => { - return component === 'http' && framework && HTTP_FRAMEWORKS.includes(framework); +const isNewStandardFramework = (component: string) => { + return NEW_STANDARD_COMPONENTS.includes(component); }; -export { COMPONENTS, getDefaultConfig, isBaseComponent, isFrameworkComponent, isHttpFramework }; +export { COMPONENTS, getDefaultConfig, isBaseComponent, isFrameworkComponent, isNewStandardFramework }; diff --git a/src/parse.ts b/src/parse.ts index 9aca872..c22d74b 100644 --- a/src/parse.ts +++ b/src/parse.ts @@ -6,7 +6,7 @@ import chalk from 'chalk'; import { program } from 'commander'; import { fileExist, isJsonPath, isYamlPath, getFileExt, mergeObject } from './utils'; import { AnyObject, ParseOptions } from './typings'; -import { getDefaultConfig, isHttpFramework } from './components/config'; +import { getDefaultConfig, isNewStandardFramework } from './components/config'; import { createLayerConfig } from './components/layer'; /** @@ -73,10 +73,7 @@ function generateLayerYaml(rootDir: string, slsOptions: AnyObject, layerOptions const layerConfig = createLayerConfig(layerPath, JSON.parse(layerOptions)); // 2. update project serverless.yml slsOptions.inputs = slsOptions.inputs || {}; - if ( - slsOptions.inputs?.faas?.framework && - isHttpFramework(slsOptions.component, slsOptions.inputs.faas.framework) - ) { + if (isNewStandardFramework(slsOptions.component)) { slsOptions.inputs.faas.layers = slsOptions.inputs.faas.layers || []; slsOptions.inputs.faas.layers.push({ name: '${output:${stage}:${app}:' + layerConfig.name + '.name}', From 61150fbd631f348e892a79ce10b777390f723b13 Mon Sep 17 00:00:00 2001 From: slsplus Date: Tue, 27 Jul 2021 02:32:55 +0000 Subject: [PATCH 4/4] chore(release): version 0.1.2 ## [0.1.2](https://github.com/serverless-tencent/yaml-parser/compare/v0.1.1...v0.1.2) (2021-07-27) ### Bug Fixes * support http component layer config ([bd1e28d](https://github.com/serverless-tencent/yaml-parser/commit/bd1e28d1a45cfc35620466766d0ee0c4be29c0c1)) --- CHANGELOG.md | 7 +++++++ package.json | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f9c971d..8026738 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +## [0.1.2](https://github.com/serverless-tencent/yaml-parser/compare/v0.1.1...v0.1.2) (2021-07-27) + + +### Bug Fixes + +* support http component layer config ([bd1e28d](https://github.com/serverless-tencent/yaml-parser/commit/bd1e28d1a45cfc35620466766d0ee0c4be29c0c1)) + ## [0.1.1](https://github.com/serverless-tencent/yaml-parser/compare/v0.1.0...v0.1.1) (2021-06-17) diff --git a/package.json b/package.json index bcea0ca..b71663a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@slsplus/yaml-parser", - "version": "0.1.1", + "version": "0.1.2", "description": "Yaml Parser for serverless.yml", "main": "./dist/index.js", "bin": {