diff --git a/packages/faas-cli-command-core/src/npm.ts b/packages/faas-cli-command-core/src/npm.ts index 4fa120f44619..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}` : ''}`; diff --git a/packages/faas-cli-plugin-package/src/index.ts b/packages/faas-cli-plugin-package/src/index.ts index 4b1ca0110e9f..90ec261b1152 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) { @@ -361,14 +361,14 @@ 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; } 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' }; } } 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..7bc3716ba62e 100644 --- a/packages/faas-cli-plugin-package/test/package-app.test.ts +++ b/packages/faas-cli-plugin-package/test/package-app.test.ts @@ -4,6 +4,8 @@ import { resolve, join } from 'path'; import { existsSync, remove, readFileSync } from 'fs-extra'; import * as assert from 'assert'; +import { AliyunFCPlugin } from '../../faas-cli-plugin-fc'; + describe('/test/package-a[[.test.ts', () => { describe('package application layer project', () => { const baseDir = resolve(__dirname, './fixtures/app-layer'); @@ -25,12 +27,14 @@ 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'); 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') 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 '