Plugin Development Guide

Getting started

A CLI plugin is an npm package that can add additional features or context api to the Weex Toolkit, that can make the Weex Toolkit to be more powerful.

In addition, weex-toolkit supports expanding your CLI's ecosystem with a robust set of easy-to-write plugins and extensions.

Code

!> As a third-part plugin, we recommend that you use weex-cli-plugin- as your package prefix, e.g weex-cli-plugin-logger.

Let's start with write a Weex Toolkit plugin.

$ weex create weex-templates/weex-toolkit-plugin weex-cli-plugin-logger && cd weex-cli-plugin-logger

You can open the folder and see this demo, it's very simple that we can easily know how to write our own plugins.

Extensions

Extensions are some attach function that provide you ability to expand the Context API.

// attach context api
module.exports = context => {
  context.log = message => {
    console.log("Welcome to Weex Toolkit");
    console.log(message);
  };
};

The extensions will be loaded before the commands being called, so you can extend the contextapi capability and use it safely in the command.

In addition, if your plugin wants to rely on the extensions provided by other plugins, you can ensure that your commands work correctly by specifying pluginDependencies in package.json. e.g:

{
  ...
  "pluginDependencies": {
    "@weex-cli/compile": "latest"
  }
  ...
}

Commands

Commands are simple objects that provide a name, optional aliases, and a function to run.

// build log command
module.exports = {
  name: "log",
  description: "My first Weex Toolkit Plugin",
  alias: "l",
  run: context => {
    context.log("My first Weex Toolkit Plugin");
  }
};

You can use the API from the context, also you can include modules to build your command.

See the Context API docs for more details on what you can do.

What's under the context?

We've assembled an all star cast of libraries to help you build your CLI. ⭐️ chrome-opn for open file
⭐️ inquirer for prompts
⭐️ semver for version investigations
⭐️ semver for version investigations
⭐️ semver for version investigations
⭐️ fs-jetpack for the filesystem
⭐️ yargs-parser, enquirer, colors, ora and cli-table3 for the command line
⭐️ axios & apisauce for web & apis
⭐️ cosmiconfig for flexible configuration ⭐️ cross-spawn for running sub-commands ⭐️ execa for running more sub-commands ⭐️ node-which for finding executables ⭐️ pluralize for manipulating strings

How to test the plugin

  1. publish to npm

After that, you can install the plugin by weex install [packagename], such as weex install weex-cli-plugin-logger.

  1. edit the ~/.wx/modules/stores.json file

Add your local path of the project into the configuration file, and modify the config by yourself. e.g:

{
  ...
  "weex-cli-plugin-logger": {
    "type": 0,
    "description": "A Weex Toolkit plugin project",
    "version": "1.0.0",
    "next_version": "",
    "is_next": true,
    "changelog": "",
    "local": "/Users/kw/.wx/modules/node_modules/weex-cli-plugin-logger",
    "commands": [
      {
        "name": "log",
        "alias": "l",
        "showed": true,
        "description": "My first Weex Toolkit Plugin"
      }
    ]
  }
  ...
}
  1. use weex link

TODO