命令插件开发

steamer-plugin-example

开发插件

  1. npm i -g steamerjs
  2. steamer develop --plugin [plugin name xxx]
  3. // 或
  4. steamer develop -p [plugin name xxx]
  5. // 命令运行后,会下载 [steamer-plugin-example](https://github.com/steamerjs/steamer-plugin-example)
  6. cd steamer-plugin-xxx
  7. // 将你的插件链接至全局路径,就可以直接使用 `steamer example`
  8. npm link
  9. // 当你完成开发,可以 `unlink` 你的插件
  10. npm unlink

steamer plugin 例子

完成这个example plugin之后,可以这样使用这个插件:

  1. steamer example -c config.js
  2. // 或
  3. steamer example --config config.js

如何写一个 steamerjs 插件

  • 创建一个类,继承 steamer-plugin。此插件有许多辅助方法,请此往插件文件进行查询。
  1. class ExamplePlugin extends SteamerPlugin {
  2. constructor(args) {
  3. super(args);
  4. this.argv = args;
  5. this.pluginName = 'steamer-plugin-example';
  6. this.description = 'steamer plugin example';
  7. }
  8. init() {
  9. }
  10. help() {
  11. }
  12. }

当在终端输入插件命令时,命令的参数将被传入这个函数

更多相关参数的文档,请参考 yargs.

  • init 函数

为类创建 init 方法, 插件命令在启动时会自动调用此函数。

  1. init() {
  2. }
  1. // 导出此类
  2. module.exports = ExamplePlugin;
  • help 函数

为插件创建 help 方法

  1. help() {
  2. }

当使用命令 steamer [plugin name] -h 或者 steamer [plugin name] --help 时,会自动调用 help 函数,用于输出插件帮助文档。

  • 在package.json中指定入口
  1. "main": "index.js"