From 1ddd1581cb15a79a6d5d11a344f4b4873863c655 Mon Sep 17 00:00:00 2001 From: Harry Chen Date: Fri, 7 Aug 2020 14:22:19 +0800 Subject: [PATCH 1/8] fix: fix windows word when output --- packages/faas-cli-command-core/src/npm.ts | 2 +- packages/faas-cli-plugin-package/src/index.ts | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/packages/faas-cli-command-core/src/npm.ts b/packages/faas-cli-command-core/src/npm.ts index 4fa120f44619..13cd153cc210 100644 --- a/packages/faas-cli-command-core/src/npm.ts +++ b/packages/faas-cli-command-core/src/npm.ts @@ -62,7 +62,7 @@ export async function installNpm(options: INpmInstallOptions) { slience, registerPath, } = options; - const cmd = `${baseDir ? `cd ${baseDir};` : ''}${register} i ${npmName}${ + const cmd = `${baseDir ? `cd ${baseDir} && ;` : ''}${register} i ${npmName}${ mode ? ` --${mode}` : ' --no-save' }${registerPath ? ` --registry=${registerPath}` : ''}`; diff --git a/packages/faas-cli-plugin-package/src/index.ts b/packages/faas-cli-plugin-package/src/index.ts index 4b1ca0110e9f..b2e113914201 100644 --- a/packages/faas-cli-plugin-package/src/index.ts +++ b/packages/faas-cli-plugin-package/src/index.ts @@ -151,12 +151,12 @@ export class PackagePlugin extends BasePlugin { this.core.cli.log(` - BaseDir: ${this.servicePath}`); this.core.cli.log(' - AnalyzeResult'); this.core.cli.log( - ` ◎ ProjectType: ${this.codeAnalyzeResult.projectType}` + ` - ProjectType: ${this.codeAnalyzeResult.projectType}` ); if (this.codeAnalyzeResult.midwayRoot) { // 输出 midway-* 项目根路径 this.core.cli.log( - ` ◎ MidwayRoot: ${ + ` - MidwayRoot: ${ this.servicePath === this.codeAnalyzeResult.midwayRoot ? '.' : relative(this.servicePath, this.codeAnalyzeResult.midwayRoot) @@ -164,7 +164,7 @@ export class PackagePlugin extends BasePlugin { ); // 输出 ts 代码根路径 this.core.cli.log( - ` ◎ TSCodeRoot: ${relative( + ` - TSCodeRoot: ${relative( this.servicePath, this.codeAnalyzeResult.tsCodeRoot )}` @@ -175,15 +175,15 @@ export class PackagePlugin extends BasePlugin { ); if (this.codeAnalyzeResult.integrationProject) { this.core.cli.log( - ` ◎ TSBuildTemporaryRoot: ${this.integrationDistTempDirectory}` + ` - TSBuildTemporaryRoot: ${this.integrationDistTempDirectory}` ); await remove(join(this.servicePath, this.integrationDistTempDirectory)); } else { - this.core.cli.log(' ◎ TSBuildTemporaryRoot: dist'); + this.core.cli.log(' - TSBuildTemporaryRoot: dist'); } // 输出构建产物根路径 this.core.cli.log( - ` ◎ PackageRoot: ${relative(this.servicePath, this.midwayBuildPath)}` + ` - PackageRoot: ${relative(this.servicePath, this.midwayBuildPath)}` ); } await remove(this.midwayBuildPath); @@ -215,7 +215,7 @@ export class PackagePlugin extends BasePlugin { ), exclude: packageObj.exclude, log: path => { - this.core.cli.log(` ◎ Copy ${path}`); + this.core.cli.log(` - Copy ${path}`); }, }); if (this.codeAnalyzeResult.integrationProject) { From 0e9cf8a2f20804515a37c4e3628e36b22b853e9a Mon Sep 17 00:00:00 2001 From: Harry Chen Date: Fri, 7 Aug 2020 15:23:54 +0800 Subject: [PATCH 2/8] fix: fix method when array --- .../serverless-spec-builder/src/fc/builder.ts | 3 + .../serverless-spec-builder/test/fc.test.ts | 17 +++ .../test/fixtures/fc/f-event-http.yml | 9 ++ .../test/fixtures/wrapper/aggre.js | 124 ------------------ .../test/fixtures/wrapper/index.js | 57 -------- .../test/fixtures/wrapper/render.js | 57 -------- .../test/wrapper.test.ts | 12 +- 7 files changed, 40 insertions(+), 239 deletions(-) delete mode 100644 packages/serverless-spec-builder/test/fixtures/wrapper/aggre.js delete mode 100644 packages/serverless-spec-builder/test/fixtures/wrapper/index.js delete mode 100644 packages/serverless-spec-builder/test/fixtures/wrapper/render.js diff --git a/packages/serverless-spec-builder/src/fc/builder.ts b/packages/serverless-spec-builder/src/fc/builder.ts index 458315e6d892..e7d68bbf7bff 100644 --- a/packages/serverless-spec-builder/src/fc/builder.ts +++ b/packages/serverless-spec-builder/src/fc/builder.ts @@ -224,7 +224,10 @@ function convertMethods(methods: string | string[]): HTTPEventType[] { } methods = [methods]; + } else if (methods?.length) { + // has value } else { + // empty return ['GET', 'PUT', 'POST', 'DELETE', 'HEAD']; } diff --git a/packages/serverless-spec-builder/test/fc.test.ts b/packages/serverless-spec-builder/test/fc.test.ts index 3ce341e8f67c..c504bfe24be3 100644 --- a/packages/serverless-spec-builder/test/fc.test.ts +++ b/packages/serverless-spec-builder/test/fc.test.ts @@ -79,6 +79,23 @@ describe('/test/fc.test.ts', () => { Type: 'HTTP', }, }); + + // third function + const funResult3 = result['Resources']['serverless-hello-world']['index3']; + assert(funResult3['Type'] === 'Aliyun::Serverless::Function'); + assert(funResult3['Properties']['Initializer'] === 'index.initializer'); + assert(funResult3['Properties']['Handler'] === 'index.handler'); + assert(funResult3['Properties']['Runtime'] === 'nodejs10'); + + assert.deepStrictEqual(funResult3['Events'], { + 'http-index3': { + Properties: { + AuthType: 'ANONYMOUS', + Methods: ['GET', 'POST'], + }, + Type: 'HTTP', + }, + }); }); it('test http events no method', () => { diff --git a/packages/serverless-spec-builder/test/fixtures/fc/f-event-http.yml b/packages/serverless-spec-builder/test/fixtures/fc/f-event-http.yml index e4fbc0e94422..62445e689a0a 100644 --- a/packages/serverless-spec-builder/test/fixtures/fc/f-event-http.yml +++ b/packages/serverless-spec-builder/test/fixtures/fc/f-event-http.yml @@ -21,5 +21,14 @@ functions: role: 'acs:ram::1234567890:role/fc-invoke-test' version: LATEST + index3: + handler: index.handler + events: + - http: + method: + - get + - post + path: / + package: artifact: code.zip diff --git a/packages/serverless-spec-builder/test/fixtures/wrapper/aggre.js b/packages/serverless-spec-builder/test/fixtures/wrapper/aggre.js deleted file mode 100644 index 8a5986652cf3..000000000000 --- a/packages/serverless-spec-builder/test/fixtures/wrapper/aggre.js +++ /dev/null @@ -1,124 +0,0 @@ -const { FaaSStarter } = require('@midwayjs/faas'); -const { asyncWrapper, start } = require('testStarter'); - -const { registerFunctionToIocByConfig } = require('./registerFunction.js'); -const { join } = require('path'); - -const picomatch = require('picomatch'); -const layers = []; - -try { - const layer_npm_testNpm = require('test'); - layers.push(layer_npm_testNpm); -} catch(e) { - console.error('npm layer "test" not install', e); -} - -try { - const layer_oss_remote_debug = require('remote-debug'); - layers.push(layer_oss_remote_debug); -} catch(e) { - console.error('oss layer "remote-debug" not install', e); -} - -try { - const layer_oss_testOss = require('test'); - layers.push(layer_oss_testOss); -} catch(e) { - console.error('oss layer "test" not install', e); -} - - -let starter; -let runtime; -let inited = false; - - -const initializeMethod = async (initializeContext = {}) => { - - runtime = await start({ - layers: layers, - getHandler: getHandler - }); - starter = new FaaSStarter({ baseDir: __dirname, initializeContext, applicationAdapter: runtime, middleware: [ - "test1", - "test2" -] }); - - - registerFunctionToIocByConfig({ - "functionList": [ - { - "functionName": "test", - "functionHandler": "index.handler", - "functionFilePath": "fun-index.js", - "argsPath": "ctx.request.data.args" - } - ] -}, { - baseDir: join(__dirname, 'dist'), - context: starter.loader.getApplicationContext() - }); - - await starter.start(); - inited = true; - -}; - -const getHandler = (hanlderName) => { - - if (hanlderName === 'handler') { - return async (ctx) => { - const allHandlers = [ - { - "handler": "index.handler", - "router": "/api/test", - "pureRouter": "/api/test", - "level": 2 - }, - { - "handler": "render.handler", - "router": "/**", - "pureRouter": "/", - "level": 1 - } -]; - let handler = null; - let ctxPath = ctx && ctx.path || ''; - if (ctxPath) { - handler = allHandlers.find(handler => { - return picomatch.isMatch(ctxPath, handler.router) - }); - } - - if (handler) { - return starter.handleInvokeWrapper(handler.handler)(ctx); - } - ctx.status = 404; - ctx.set('Content-Type', 'text/html'); - return '

404 Page Not Found


Request path: ' + ctxPath + '
Powered by Midway Serverless
'; - }; - } - -} - - - - -exports.initializeUserDefine = asyncWrapper(async (...args) => { - if (!inited) { - await initializeMethod(...args); - } -}); - - -exports.handler = asyncWrapper(async (...args) => { - if (!inited) { - await initializeMethod(); - } - - - const handler = getHandler('handler'); - return runtime.asyncEvent(handler)(...args); -}); - diff --git a/packages/serverless-spec-builder/test/fixtures/wrapper/index.js b/packages/serverless-spec-builder/test/fixtures/wrapper/index.js deleted file mode 100644 index b7b1d91be075..000000000000 --- a/packages/serverless-spec-builder/test/fixtures/wrapper/index.js +++ /dev/null @@ -1,57 +0,0 @@ -const { FaaSStarter } = require('@midwayjs/faas'); -const { asyncWrapper, start } = require('testStarter'); - -const picomatch = require('picomatch'); -const layers = []; - - -let starter; -let runtime; -let inited = false; - - -const initializeMethod = async (initializeContext = {}) => { - - runtime = await start({ - layers: layers, - getHandler: getHandler - }); - starter = new FaaSStarter({ baseDir: __dirname, initializeContext, applicationAdapter: runtime, middleware: [ - "test1", - "test2" -] }); - - - await starter.start(); - inited = true; - -}; - -const getHandler = (hanlderName) => { - - if (hanlderName === 'handler') { - return starter.handleInvokeWrapper('index.handler'); - } - -} - - - - -exports.initializeUserDefine = asyncWrapper(async (...args) => { - if (!inited) { - await initializeMethod(...args); - } -}); - - -exports.handler = asyncWrapper(async (...args) => { - if (!inited) { - await initializeMethod(); - } - - - const handler = getHandler('handler'); - return runtime.asyncEvent(handler)(...args); -}); - diff --git a/packages/serverless-spec-builder/test/fixtures/wrapper/render.js b/packages/serverless-spec-builder/test/fixtures/wrapper/render.js deleted file mode 100644 index 7e7b9bd187fa..000000000000 --- a/packages/serverless-spec-builder/test/fixtures/wrapper/render.js +++ /dev/null @@ -1,57 +0,0 @@ -const { FaaSStarter } = require('@midwayjs/faas'); -const { asyncWrapper, start } = require('testStarter'); - -const picomatch = require('picomatch'); -const layers = []; - - -let starter; -let runtime; -let inited = false; - - -const initializeMethod = async (initializeContext = {}) => { - - runtime = await start({ - layers: layers, - getHandler: getHandler - }); - starter = new FaaSStarter({ baseDir: __dirname, initializeContext, applicationAdapter: runtime, middleware: [ - "test1", - "test2" -] }); - - - await starter.start(); - inited = true; - -}; - -const getHandler = (hanlderName) => { - - if (hanlderName === 'handler') { - return starter.handleInvokeWrapper('render.handler'); - } - -} - - - - -exports.initializeUserDefine = asyncWrapper(async (...args) => { - if (!inited) { - await initializeMethod(...args); - } -}); - - -exports.handler = asyncWrapper(async (...args) => { - if (!inited) { - await initializeMethod(); - } - - - const handler = getHandler('handler'); - return runtime.asyncEvent(handler)(...args); -}); - diff --git a/packages/serverless-spec-builder/test/wrapper.test.ts b/packages/serverless-spec-builder/test/wrapper.test.ts index 732e658aa692..85ee038ce6f8 100644 --- a/packages/serverless-spec-builder/test/wrapper.test.ts +++ b/packages/serverless-spec-builder/test/wrapper.test.ts @@ -1,10 +1,20 @@ import { writeWrapper, formetAggregationHandlers } from '../src/wrapper'; import { resolve } from 'path'; import * as assert from 'assert'; -import { existsSync, readFileSync, remove } from 'fs-extra'; +import { existsSync, readFileSync, remove, mkdirpSync } from 'fs-extra'; describe('/test/wrapper.test.ts', () => { + const wrapperPath = resolve(__dirname, './fixtures/wrapper'); + + after(async () => { + await remove(wrapperPath); + }); + describe('test all format', () => { + beforeEach(async () => { + await remove(wrapperPath); + mkdirpSync(wrapperPath); + }); it('writeWrapper functionMap', async () => { const wrapperPath = resolve(__dirname, './fixtures/wrapper'); const registerFunction = resolve(wrapperPath, 'registerFunction.js'); From adf9a5e35cdf83b47c76ed2de7c73c7a1b76a85c Mon Sep 17 00:00:00 2001 From: Harry Chen Date: Tue, 11 Aug 2020 11:06:29 +0800 Subject: [PATCH 3/8] fix: remove semicolon --- packages/faas-cli-command-core/src/npm.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/faas-cli-command-core/src/npm.ts b/packages/faas-cli-command-core/src/npm.ts index 13cd153cc210..174378a801bd 100644 --- a/packages/faas-cli-command-core/src/npm.ts +++ b/packages/faas-cli-command-core/src/npm.ts @@ -62,7 +62,7 @@ export async function installNpm(options: INpmInstallOptions) { slience, registerPath, } = options; - const cmd = `${baseDir ? `cd ${baseDir} && ;` : ''}${register} i ${npmName}${ + const cmd = `${baseDir ? `cd ${baseDir} && ` : ''}${register} i ${npmName}${ mode ? ` --${mode}` : ' --no-save' }${registerPath ? ` --registry=${registerPath}` : ''}`; From 28f079ee4ba1767a9334bef42e9beb10476de52a Mon Sep 17 00:00:00 2001 From: Harry Chen Date: Tue, 11 Aug 2020 12:18:25 +0800 Subject: [PATCH 4/8] fix: functions not empty --- packages/faas-cli-plugin-package/src/index.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/packages/faas-cli-plugin-package/src/index.ts b/packages/faas-cli-plugin-package/src/index.ts index b2e113914201..10d223ecbc00 100644 --- a/packages/faas-cli-plugin-package/src/index.ts +++ b/packages/faas-cli-plugin-package/src/index.ts @@ -368,7 +368,7 @@ export class PackagePlugin extends BasePlugin { } this.core.cli.log(' - Using tradition build mode'); this.program.emit(); - this.core.cli.log(' - Build Midway FaaS complete'); + this.core.cli.log(' - Build project complete'); } // 生成默认入口 @@ -648,8 +648,10 @@ export class PackagePlugin extends BasePlugin { defaultBeforeGenerateSpec() { const service: any = this.core.service; if (service?.deployType) { + this.core.cli.log(` - found deployType: ${service?.deployType}`); // add default function - if (!service.functions) { + if (!service.functions || Object.keys(service.functions).length === 0) { + this.core.cli.log(` - create default functions`); service.functions = { app_index: { handler: 'index.handler', @@ -663,16 +665,19 @@ export class PackagePlugin extends BasePlugin { } if (service?.deployType === 'egg') { + this.core.cli.log(` - create default layer: egg`); service.layers['eggLayer'] = { path: 'npm:@midwayjs/egg-layer' }; } if (service?.deployType === 'express') { + this.core.cli.log(` - create default layer: express`); service.layers['expressLayer'] = { path: 'npm:@midwayjs/express-layer', }; } if (service?.deployType === 'koa') { + this.core.cli.log(` - create default layer: koa`); service.layers['koaLayer'] = { path: 'npm:@midwayjs/koa-layer' }; } } From 0d15384a1d6aa8d495b5bc681d8bdc055e7f4eb0 Mon Sep 17 00:00:00 2001 From: Harry Chen Date: Tue, 11 Aug 2020 12:19:06 +0800 Subject: [PATCH 5/8] test: add entry file check --- packages/faas-cli-plugin-package/test/package-app.test.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/faas-cli-plugin-package/test/package-app.test.ts b/packages/faas-cli-plugin-package/test/package-app.test.ts index e0dcf0e9ec5b..1da880bdd45a 100644 --- a/packages/faas-cli-plugin-package/test/package-app.test.ts +++ b/packages/faas-cli-plugin-package/test/package-app.test.ts @@ -31,6 +31,7 @@ describe('/test/package-a[[.test.ts', () => { assert(existsSync(join(buildPath, 'f.yml'))); assert(existsSync(join(buildPath, 'app'))); assert(existsSync(join(buildPath, 'config'))); + assert(existsSync(join(buildPath, 'index.js'))); assert( /npm:@midwayjs\/egg-layer/.test( readFileSync(join(buildPath, 'f.yml')).toString('utf8') From 9069cf958fe20ed01ff6322dfbcb9a9cbca88015 Mon Sep 17 00:00:00 2001 From: Harry Chen Date: Tue, 11 Aug 2020 12:26:09 +0800 Subject: [PATCH 6/8] chore: update text --- packages/faas-cli-plugin-package/src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/faas-cli-plugin-package/src/index.ts b/packages/faas-cli-plugin-package/src/index.ts index 10d223ecbc00..90ec261b1152 100644 --- a/packages/faas-cli-plugin-package/src/index.ts +++ b/packages/faas-cli-plugin-package/src/index.ts @@ -361,7 +361,7 @@ export class PackagePlugin extends BasePlugin { async emit() { const isTsDir = existsSync(join(this.servicePath, 'tsconfig.json')); - this.core.cli.log('Building Midway FaaS directory files...'); + this.core.cli.log('Building project directory files...'); if (!isTsDir) { this.core.cli.log(' - Not found tsconfig.json and skip build'); return; From 003972a71e9ee6e0586b2df9eeac8b502d977779 Mon Sep 17 00:00:00 2001 From: Harry Chen Date: Tue, 11 Aug 2020 12:38:36 +0800 Subject: [PATCH 7/8] test: add fc plugin --- packages/faas-cli-plugin-package/test/package-app.test.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/faas-cli-plugin-package/test/package-app.test.ts b/packages/faas-cli-plugin-package/test/package-app.test.ts index 1da880bdd45a..4389f66071c6 100644 --- a/packages/faas-cli-plugin-package/test/package-app.test.ts +++ b/packages/faas-cli-plugin-package/test/package-app.test.ts @@ -4,7 +4,9 @@ import { resolve, join } from 'path'; import { existsSync, remove, readFileSync } from 'fs-extra'; import * as assert from 'assert'; -describe('/test/package-a[[.test.ts', () => { +import { AliyunFCPlugin } from '../../faas-cli-plugin-fc'; + +describe.only('/test/package-a[[.test.ts', () => { describe('package application layer project', () => { const baseDir = resolve(__dirname, './fixtures/app-layer'); @@ -25,6 +27,7 @@ describe('/test/package-a[[.test.ts', () => { log: console, }); core.addPlugin(PackagePlugin); + core.addPlugin(AliyunFCPlugin); await core.ready(); await core.invoke(['package']); const buildPath = join(baseDir, '.serverless'); From a676687756a20fe709b9cf07421779dce69377fd Mon Sep 17 00:00:00 2001 From: Harry Chen Date: Tue, 11 Aug 2020 14:10:42 +0800 Subject: [PATCH 8/8] chore: remove only --- packages/faas-cli-plugin-package/test/package-app.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/faas-cli-plugin-package/test/package-app.test.ts b/packages/faas-cli-plugin-package/test/package-app.test.ts index 4389f66071c6..7bc3716ba62e 100644 --- a/packages/faas-cli-plugin-package/test/package-app.test.ts +++ b/packages/faas-cli-plugin-package/test/package-app.test.ts @@ -6,7 +6,7 @@ import * as assert from 'assert'; import { AliyunFCPlugin } from '../../faas-cli-plugin-fc'; -describe.only('/test/package-a[[.test.ts', () => { +describe('/test/package-a[[.test.ts', () => { describe('package application layer project', () => { const baseDir = resolve(__dirname, './fixtures/app-layer');