@midwayjs/fcli-command-core
基于插件生命周期 + hook的内核
Last updated 41 minutes ago by czy88840616 .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install @midwayjs/fcli-command-core 
SYNC missed versions from official npm registry.

Command Hook Core

基于插件生命周期 + hook的内核

内核使用文档

import { CommandHookCore } from '@midwayjs/fcli-command-core';
const core = new CommandHookCore({
    config: {                   // 会挂载到 core.coreInstance.config 上
        servicePath: baseDir,
    },
    commands: ['invoke'],       // 默认命令,多级命令依次传入数组
    service: this.spec,         // 会挂载到 core.coreInstance.service 上
    provider: 'providerName',   // 会比对与插件中的provider是都一致来决定插件是否加载
    options: this.argv,         // 参数,会作为第二个参数传递给插件构造函数,例如 { function: 'index' }
    log: console,               // 输出及错误捕获
    displayUsage: func          // 自定义如何展示帮助 displayUsage(commandsArray, usage, this)
});
core.addPlugin(Plugin);         // 载入插件,插件支持 class / 'npm:provider:packageName' / 'local:provider:path' 三种形式
await core.ready();             // 等待初始化
await core.invoke();            // 执行默认命令

core.coreInstance 会作为第一个参数传递给插件的构造函数,上面挂载了各种方法及属性,详见 ./src/interface/commandHookCore.ts ICoreInstance

options 作为第二个参数传递给插件构造函数

插件开发文档

提供了 BasePlugin 插件基类,可以继承此基类编写插件

import BasePlugin from 'command-core/lib/plugin';
class Plugin extends BasePlugin {
    provider = 'test'                      // provider,在插件加载时会比对provider,如果存在此属性并且与配置的provider不相同,则不加载
    commands = {                           // 命令列表
        invoke: {                          // 命令,例如 f invoke
            usage: 'test provider invoke', // 使用提示
            lifecycleEvents: ['one', 'two']// 生命周期,在执行 invoke 命令时 会依次按照 lifecycleEvents 里面声明的hook进行触发
        }
    }
    hooks = { // hook采用 prefix:command:lifesycle 的形式,prefix 包含 before和after,分别代表在之前执行还是之后执行。lifesycle即命令中 lifecycleEvents 指定的,如果hooks中不存在对应的钩子,则会跳过此生命周期
        'before:invoke:one': () => { this.core.cli.log('before:invoke:one'); }, // 所有的hooks均支持 async
        '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 { CommandHookCore } from '@midwayjs/fcli-command-core';
const core = new CommandHookCore({
    provider: 'providerName',
    options: {},
    commands: ['invoke'],
    log: console
});
core.addPlugin(Plugin);         // 载入你的插件
await core.ready();             // 等待初始化
await core.invoke();            // 执行对应的命令

Current Tags

  • 0.2.92-beta.1                                ...           beta (2 months ago)
  • 1.0.4                                ...           latest (41 minutes ago)

56 Versions

  • 1.0.4                                ...           41 minutes ago
  • 1.0.3                                ...           a day ago
  • 1.0.2                                ...           2 days ago
  • 1.0.1                                ...           2 days ago
  • 1.0.0                                ...           6 days ago
  • 0.3.6                                ...           8 days ago
  • 0.3.3                                ...           22 days ago
  • 0.3.0                                ...           a month ago
  • 0.2.97                                ...           2 months ago
  • 0.2.95                                ...           2 months ago
  • 0.2.94                                ...           2 months ago
  • 0.2.92                                ...           2 months ago
  • 0.2.92-beta.1                                ...           2 months ago
  • 0.2.91                                ...           2 months ago
  • 0.2.89                                ...           2 months ago
  • 0.2.88                                ...           2 months ago
  • 0.2.84                                ...           2 months ago
  • 0.2.82                                ...           3 months ago
  • 0.2.79                                ...           3 months ago
  • 0.2.78                                ...           3 months ago
  • 0.2.77                                ...           3 months ago
  • 0.2.76                                ...           3 months ago
  • 0.2.75                                ...           3 months ago
  • 0.2.74                                ...           3 months ago
  • 0.2.73                                ...           3 months ago
  • 0.2.72                                ...           3 months ago
  • 0.2.71                                ...           3 months ago
  • 0.2.70                                ...           3 months ago
  • 0.2.69                                ...           3 months ago
  • 0.2.67                                ...           3 months ago
  • 0.2.66                                ...           3 months ago
  • 0.2.65                                ...           3 months ago
  • 0.2.64                                ...           3 months ago
  • 0.2.63                                ...           3 months ago
  • 0.2.62                                ...           3 months ago
  • 0.2.61                                ...           3 months ago
  • 0.2.59                                ...           3 months ago
  • 0.2.58                                ...           3 months ago
  • 0.2.57                                ...           3 months ago
  • 0.2.55                                ...           4 months ago
  • 0.2.54                                ...           4 months ago
  • 0.2.51                                ...           4 months ago
  • 0.2.50                                ...           4 months ago
  • 0.2.49                                ...           4 months ago
  • 0.2.41                                ...           4 months ago
  • 0.2.40                                ...           4 months ago
  • 0.2.34                                ...           4 months ago
  • 0.2.33                                ...           4 months ago
  • 0.2.29                                ...           5 months ago
  • 0.2.27                                ...           5 months ago
  • 0.2.22                                ...           5 months ago
  • 0.2.20                                ...           5 months ago
  • 0.2.14                                ...           5 months ago
  • 0.2.10                                ...           6 months ago
  • 0.2.2                                ...           6 months ago
  • 0.2.0                                ...           6 months ago

Copyright 2014 - 2017 © taobao.org |