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

Commit

Permalink
feat: cli & cli core
Browse files Browse the repository at this point in the history
  • Loading branch information
echosoar committed Dec 24, 2019
1 parent b9b4f48 commit b1fc539
Show file tree
Hide file tree
Showing 20 changed files with 913 additions and 1 deletion.
49 changes: 49 additions & 0 deletions packages/command-core/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Command Hook Core

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

## 插件开发文档

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

```js
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;
```


## 插件如何进行测试?

```js
import CommandHookCore from 'command-core';
const core = new CommandHookCore({
provider: 'providerName',
options: {},
log: console
});
core.addPlugin(Plugin); // 载入你的插件
await core.ready(); // 等待初始化
await core.invoke(['command']); // 执行对应的命令
```
27 changes: 27 additions & 0 deletions packages/command-core/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "@midwayjs/command-core",
"version": "0.1.5",
"main": "dist/index",
"typings": "dist/index.d.ts",
"dependencies": {
"deepmerge": "^4.2.2"
},
"devDependencies": {
"@types/mocha": "^5.2.5",
"midway-bin": "1"
},
"files": [
"dist",
"src"
],
"scripts": {
"build": "npm run lint && midway-bin build -c",
"lint": "../../node_modules/.bin/tslint --format prose -c ../../tslint.json src/**/*.ts test/**/*.ts",
"test": "npm run lint && NODE_ENV=test midway-bin test --ts --full-trace",
"debug": "npm run lint && NODE_ENV=test midway-bin test --ts --full-trace --inspect-brk=9229",
"cov": "NODE_ENV=unittest midway-bin cov --ts",
"clean": "midway-bin clean",
"autod": "midway-bin autod"
},
"gitHead": "5a6539a27cf1376a8ea9c9a8553fdfb2f78d55bd"
}
3 changes: 3 additions & 0 deletions packages/command-core/src/classes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export class CoreError {

}
48 changes: 48 additions & 0 deletions packages/command-core/src/errorMap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
const ErrorMap = {
commandIsEntrypoint: (info: any) => {
return {
info,
message: `command ${ info.command } is entrypoint, cannot invoke`
};
},
commandNotFound: (info: any) => {
return {
info,
message: `command ${ info.command } not found`
};
},
localPlugin: (info: any) => {
return {
info,
message: `load local plugin '${ info.path }' error '${ info.err.message }'`
};
},
npmPlugin: (info: any) => {
return {
info,
message: `load npm plugin '${ info.path }' error '${ info.err.message }'`
};
},
pluginType: (info: string) => {
return {
info,
message: `only support npm / local / class plugin`
};
}
};

interface ReturnValue<T> {
info: T;
message: string;
}

export default <T>(type: string, info: T): ReturnValue<T> => {
let error = ErrorMap[type];
if (!error) {
return {
info,
message: `error`
}
}
return error(info);
};
Loading

0 comments on commit b1fc539

Please sign in to comment.