Skip to content
This repository has been archived by the owner on Jul 2, 2020. It is now read-only.

Commit

Permalink
feat: command core store (#72)
Browse files Browse the repository at this point in the history
* feat: command core store

* feat: create verbose

* fix: remove invoke console

* fix: tsbuild

* test: pass test
  • Loading branch information
echosoar committed Mar 6, 2020
1 parent d2e7148 commit 10e68a9
Show file tree
Hide file tree
Showing 13 changed files with 124 additions and 19 deletions.
16 changes: 14 additions & 2 deletions packages/faas-cli-command-core/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,6 @@ export class CommandHookCore implements ICommandHooksCore {
private getCoreInstance(): ICoreInstance {
const {
provider,
log,
service,
config,
extensions,
Expand All @@ -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 || [],
Expand Down Expand Up @@ -383,6 +384,10 @@ export class CommandHookCore implements ICommandHooksCore {
}
}

private getLog() {
return this.options.log || console;
}

error<T>(type: string, info?: T) {
const errObj: {
info?: T;
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,20 @@ export interface ICommandHooksCore {
addPlugin(plugin: IPlugin): void;
}

interface IStore<T> {
[index: string]: T;
[index: number]: T;
}

export interface ICoreInstance {
classes: any;
cli: ILog | Console;
config: any;
getProvider(providerName: string): IProviderInstance;
invoke(commandsArray?: string[], allowEntryPoints?: boolean, options?: any);
pluginManager: ICommandHooksCore;
store: IStore<any>;
debug: any;
service: {
service?: {
name: string;
Expand Down
14 changes: 14 additions & 0 deletions packages/faas-cli-command-core/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
}
}
15 changes: 15 additions & 0 deletions packages/faas-cli-command-core/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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);
});
});
17 changes: 17 additions & 0 deletions packages/faas-cli-command-core/test/plugins/store.get.ts
Original file line number Diff line number Diff line change
@@ -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;
16 changes: 16 additions & 0 deletions packages/faas-cli-command-core/test/plugins/store.set.ts
Original file line number Diff line number Diff line change
@@ -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;
5 changes: 5 additions & 0 deletions packages/faas-cli-plugin-create/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion packages/faas-cli-plugin-invoke/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
14 changes: 0 additions & 14 deletions packages/faas-cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
10 changes: 9 additions & 1 deletion packages/serverless-invoke/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ interface InvokeOptions {
sourceDir?: string; // 函数源码目录
incremental?: boolean; // 开启增量编译 (会无视 clean true)
clean?: boolean; // 清理调试目录
verbose?: boolean; // 输出详细日志
}

export abstract class InvokeCore implements IInvoke {
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -245,4 +246,11 @@ export abstract class InvokeCore implements IInvoke {
}
return options;
}

debug(...args) {
if (!this.options.verbose) {
return;
}
console.log('[Verbose] ', ...args);
}
}
1 change: 1 addition & 0 deletions packages/serverless-invoke/src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export interface InvokeOptions {
sourceDir?: string; // 一体化目录结构下,函数的目录,比如 src/apis,这个影响到编译
clean?: boolean; // 清理调试目录
incremental?: boolean; // 增量编译
verbose?: boolean; // 输出更多信息
}

export interface IInvoke {
Expand Down
2 changes: 1 addition & 1 deletion packages/serverless-meta-json/src/index.ts
Original file line number Diff line number Diff line change
@@ -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) => {
Expand Down
23 changes: 23 additions & 0 deletions packages/serverless-meta-json/tsconfig.json
Original file line number Diff line number Diff line change
@@ -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"
]
}

0 comments on commit 10e68a9

Please sign in to comment.