diff --git a/packages/faas-cli-command-core/src/core.ts b/packages/faas-cli-command-core/src/core.ts index a907f0d0..daf2998f 100644 --- a/packages/faas-cli-command-core/src/core.ts +++ b/packages/faas-cli-command-core/src/core.ts @@ -149,7 +149,6 @@ export class CommandHookCore implements ICommandHooksCore { private getCoreInstance(): ICoreInstance { const { provider, - log, service, config, extensions, @@ -166,11 +165,13 @@ export class CommandHookCore implements ICommandHooksCore { classes: { Error: CoreError, }, - cli: log || console, + store: new Map(), + cli: this.getLog(), config: config || {}, getProvider: this.getProvider.bind(this), invoke: this.invoke.bind(this), spawn: this.spawn.bind(this), + debug: this.debug.bind(this), processedInput: { options: {}, commands: commands || [], @@ -383,6 +384,10 @@ export class CommandHookCore implements ICommandHooksCore { } } + private getLog() { + return this.options.log || console; + } + error(type: string, info?: T) { const errObj: { info?: T; @@ -397,4 +402,11 @@ export class CommandHookCore implements ICommandHooksCore { } process.exit(1); } + + debug(...args) { + if (!this.options.options.V && !this.options.options.verbose) { + return; + } + this.getLog().log('[Verbose] ', ...args); + } } diff --git a/packages/faas-cli-command-core/src/interface/commandHookCore.ts b/packages/faas-cli-command-core/src/interface/commandHookCore.ts index dc50352f..7392733d 100644 --- a/packages/faas-cli-command-core/src/interface/commandHookCore.ts +++ b/packages/faas-cli-command-core/src/interface/commandHookCore.ts @@ -25,6 +25,11 @@ export interface ICommandHooksCore { addPlugin(plugin: IPlugin): void; } +interface IStore { + [index: string]: T; + [index: number]: T; +} + export interface ICoreInstance { classes: any; cli: ILog | Console; @@ -32,6 +37,8 @@ export interface ICoreInstance { getProvider(providerName: string): IProviderInstance; invoke(commandsArray?: string[], allowEntryPoints?: boolean, options?: any); pluginManager: ICommandHooksCore; + store: IStore; + debug: any; service: { service?: { name: string; diff --git a/packages/faas-cli-command-core/src/plugin.ts b/packages/faas-cli-command-core/src/plugin.ts index 2bc7f9fd..86d963ca 100644 --- a/packages/faas-cli-command-core/src/plugin.ts +++ b/packages/faas-cli-command-core/src/plugin.ts @@ -10,10 +10,24 @@ export class BasePlugin implements IPluginInstance { public options: any; public commands: IPluginCommands; public hooks: IPluginHooks; + private name: string = this.getName(); + constructor(core: ICoreInstance, options: any) { this.core = core; this.options = options; this.commands = {}; this.hooks = {}; } + + public getName() { + return this.constructor.name; + } + + public setStore(type: string, value: any) { + this.core.store.set(`${this.name}:${type}`, value); + } + + public getStore(type: string, name?: string) { + return this.core.store.get(`${name || this.name}:${type}`); + } } diff --git a/packages/faas-cli-command-core/test/index.test.ts b/packages/faas-cli-command-core/test/index.test.ts index c90a2260..370e0190 100644 --- a/packages/faas-cli-command-core/test/index.test.ts +++ b/packages/faas-cli-command-core/test/index.test.ts @@ -2,6 +2,9 @@ import { CommandHookCore } from '../src'; import InvokePlugin from './plugins/test.invoke'; import LogPlugin from './plugins/test.lg'; import OnePlugin from './plugins/one.common'; +import StoreSet from './plugins/store.set'; +import StoreGet from './plugins/store.get'; + import * as assert from 'assert'; describe('load plugin', () => { @@ -89,4 +92,16 @@ describe('invoke', () => { result[3] === 'before:invoke:two' ); }); + + it('store set', async () => { + const core = new CommandHookCore({ + provider: '', + options: {}, + }); + core.addPlugin(StoreGet); + core.addPlugin(StoreSet); + await core.ready(); + await core.invoke(['store']); + assert((core as any).coreInstance.store.get('StoreGet:get') === 123456); + }); }); diff --git a/packages/faas-cli-command-core/test/plugins/store.get.ts b/packages/faas-cli-command-core/test/plugins/store.get.ts new file mode 100644 index 00000000..a609cd59 --- /dev/null +++ b/packages/faas-cli-command-core/test/plugins/store.get.ts @@ -0,0 +1,17 @@ + +import { BasePlugin } from '../../src'; + +class StoreGet extends BasePlugin { + commands = { + store: { + lifecycleEvents: ['main'], + }, + }; + hooks = { + 'after:store:main': async () => { + this.setStore('get', this.getStore('set', 'StoreSet')); + }, + }; +} + +export default StoreGet; diff --git a/packages/faas-cli-command-core/test/plugins/store.set.ts b/packages/faas-cli-command-core/test/plugins/store.set.ts new file mode 100644 index 00000000..73c2563a --- /dev/null +++ b/packages/faas-cli-command-core/test/plugins/store.set.ts @@ -0,0 +1,16 @@ +import { BasePlugin } from '../../src'; + +class StoreSet extends BasePlugin { + commands = { + store: { + lifecycleEvents: ['main'], + }, + }; + hooks = { + 'store:main': async () => { + this.setStore('set', 123456); + }, + }; +} + +export default StoreSet; diff --git a/packages/faas-cli-plugin-create/src/index.ts b/packages/faas-cli-plugin-create/src/index.ts index 72681616..828220cd 100644 --- a/packages/faas-cli-plugin-create/src/index.ts +++ b/packages/faas-cli-plugin-create/src/index.ts @@ -74,6 +74,7 @@ export class CreatePlugin extends BasePlugin { async create() { this.core.cli.log('Generating boilerplate...'); + this.core.debug('options', this.options); if (this.options['template']) { this.npmPackageName = this.templateList[this.options.template].package; await this.createFromTemplate(); @@ -121,6 +122,8 @@ export class CreatePlugin extends BasePlugin { const boilerplatePath = this.options.path || ''; const newPath = join(this.servicePath, boilerplatePath); + this.setStore('path', newPath); + this.core.debug('path', newPath); const lightGenerator = new LightGenerator(); let generator; if (this.npmPackageName) { @@ -154,6 +157,8 @@ export class CreatePlugin extends BasePlugin { show: this.showPrompt, }); const parameters = await this.prompt.run(); + this.setStore('parameters', parameters); + this.core.debug('parameters', parameters); await this.readyGenerate(); await generator.run(parameters); } else { diff --git a/packages/faas-cli-plugin-invoke/src/index.ts b/packages/faas-cli-plugin-invoke/src/index.ts index 304354aa..2ff57838 100644 --- a/packages/faas-cli-plugin-invoke/src/index.ts +++ b/packages/faas-cli-plugin-invoke/src/index.ts @@ -61,7 +61,8 @@ export class InvokePlugin extends BasePlugin { debug: this.options.debug, data: this.options.data || '{}', handler: funcConf.handler, - clean: this.options.clean ? this.options.clean !== 'false' : null + clean: this.options.clean ? this.options.clean !== 'false' : null, + verbose: this.options.V }; return invoke(options); } diff --git a/packages/faas-cli/src/index.ts b/packages/faas-cli/src/index.ts index 1a20bc51..7cfd520d 100644 --- a/packages/faas-cli/src/index.ts +++ b/packages/faas-cli/src/index.ts @@ -36,20 +36,6 @@ export class CLI extends BaseCLI { } } - loadExtensions() { - return { - debug: this.debug - }; - } - - debug(...args) { - if (!this.argv.V && !this.argv.verbose) { - return; - } - const log = this.loadLog(); - log.log(...args); - } - displayVersion() { const log = this.loadLog(); try { diff --git a/packages/serverless-invoke/src/core.ts b/packages/serverless-invoke/src/core.ts index 5c2efa05..f083c1e0 100644 --- a/packages/serverless-invoke/src/core.ts +++ b/packages/serverless-invoke/src/core.ts @@ -27,6 +27,7 @@ interface InvokeOptions { sourceDir?: string; // 函数源码目录 incremental?: boolean; // 开启增量编译 (会无视 clean true) clean?: boolean; // 清理调试目录 + verbose?: boolean; // 输出详细日志 } export abstract class InvokeCore implements IInvoke { @@ -103,7 +104,7 @@ export abstract class InvokeCore implements IInvoke { { cwd: baseDir } ); if (!fileChanges || !fileChanges.length) { - console.log('Auto skip ts compile'); + this.debug('Auto skip ts compile'); return; } } @@ -245,4 +246,11 @@ export abstract class InvokeCore implements IInvoke { } return options; } + + debug(...args) { + if (!this.options.verbose) { + return; + } + console.log('[Verbose] ', ...args); + } } diff --git a/packages/serverless-invoke/src/interface.ts b/packages/serverless-invoke/src/interface.ts index 4e2cf357..c82ec71a 100644 --- a/packages/serverless-invoke/src/interface.ts +++ b/packages/serverless-invoke/src/interface.ts @@ -18,6 +18,7 @@ export interface InvokeOptions { sourceDir?: string; // 一体化目录结构下,函数的目录,比如 src/apis,这个影响到编译 clean?: boolean; // 清理调试目录 incremental?: boolean; // 增量编译 + verbose?: boolean; // 输出更多信息 } export interface IInvoke { diff --git a/packages/serverless-meta-json/src/index.ts b/packages/serverless-meta-json/src/index.ts index 4677618e..9829a819 100644 --- a/packages/serverless-meta-json/src/index.ts +++ b/packages/serverless-meta-json/src/index.ts @@ -1,4 +1,4 @@ -import { basename, join } from 'path'; +import { basename } from 'path'; import { readdirSync } from 'fs'; import { IGateway, IOptions, IPathMethodInfo, IPathInfo, ITriggerItem } from './inter'; export const generator = async (options: IOptions) => { diff --git a/packages/serverless-meta-json/tsconfig.json b/packages/serverless-meta-json/tsconfig.json new file mode 100644 index 00000000..1c5c3183 --- /dev/null +++ b/packages/serverless-meta-json/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compileOnSave": true, + "compilerOptions": { + "target": "ES2018", + "module": "commonjs", + "moduleResolution": "node", + "experimentalDecorators": true, + "emitDecoratorMetadata": true, + "inlineSourceMap":true, + "noImplicitThis": true, + "noUnusedLocals": true, + "stripInternal": true, + "pretty": true, + "declaration": true, + "outDir": "dist" + }, + "exclude": [ + "dist", + "node_modules", + "test" + ] +} +