Command Hook Core
基于插件生命周期 + hook的内核
内核使用文档
import { CommandCore } from '@midwayjs/fcli-command-core';
const core = new CommandCore({
config: {
servicePath: baseDir,
},
commands: ['invoke'],
service: this.spec,
provider: 'providerName',
options: this.argv,
log: console,
displayUsage: func
});
core.addPlugin(Plugin);
await core.ready();
await core.invoke();
core.coreInstance 会作为第一个参数传递给插件的构造函数,上面挂载了各种方法及属性,详见 ./src/interface/commandCore.ts ICoreInstance
options 作为第二个参数传递给插件构造函数
插件开发文档
提供了 BasePlugin
插件基类,可以继承此基类编写插件
import BasePlugin from 'command-core/lib/plugin';
class Plugin extends BasePlugin {
provider = 'test'
commands = {
invoke: {
usage: 'test provider invoke',
lifecycleEvents: ['one', 'two']
}
}
hooks = {
'before:invoke:one': () => { this.core.cli.log('before:invoke:one'); },
'invoke:one': async () => { this.core.cli.log('invoke:one'); },
'after:invoke:one': () => { this.core.cli.log('after:invoke:one'); },
'before:invoke:two': async () => { this.core.cli.log('before:invoke:two'); },
'invoke:two': () => { this.core.cli.log('invoke:two'); },
'after:invoke:two': async () => { this.core.cli.log('after:invoke:two'); },
}
async asyncInit() {
}
}
export default Plugin;
插件如何进行测试?
import { CommandCore } from '@midwayjs/fcli-command-core';
const core = new CommandCore({
provider: 'providerName',
options: {},
commands: ['invoke'],
log: console
});
core.addPlugin(Plugin);
await core.ready();
await core.invoke();