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/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 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/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": { diff --git a/src/components/config.ts b/src/components/config.ts index 35a7670..db3f1aa 100644 --- a/src/components/config.ts +++ b/src/components/config.ts @@ -38,6 +38,8 @@ const FRAMEWORK_COMPONENTS = [ 'thinkphp', ]; +const NEW_STANDARD_COMPONENTS = ['http']; + const COMPONENTS = [...BASE_COMPONENTS, ...FRAMEWORK_COMPONENTS]; const isBaseComponent = (name: string) => { @@ -48,4 +50,8 @@ const isFrameworkComponent = (name = 'framework') => { return FRAMEWORK_COMPONENTS.indexOf(name) !== -1; }; -export { COMPONENTS, getDefaultConfig, isBaseComponent, isFrameworkComponent }; +const isNewStandardFramework = (component: string) => { + return NEW_STANDARD_COMPONENTS.includes(component); +}; + +export { COMPONENTS, getDefaultConfig, isBaseComponent, isFrameworkComponent, isNewStandardFramework }; diff --git a/src/parse.ts b/src/parse.ts index 9cdb987..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 } from './components/config'; +import { getDefaultConfig, isNewStandardFramework } from './components/config'; import { createLayerConfig } from './components/layer'; /** @@ -73,11 +73,19 @@ 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 (isNewStandardFramework(slsOptions.component)) { + 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; }