|
| 1 | +# Plugins |
| 2 | + |
| 3 | +OpenCLI supports community-contributed plugins. Install third-party adapters from GitHub, and they're automatically discovered alongside built-in commands. |
| 4 | + |
| 5 | +## Quick Start |
| 6 | + |
| 7 | +```bash |
| 8 | +# Install a plugin |
| 9 | +opencli plugin install github:ByteYue/opencli-plugin-github-trending |
| 10 | + |
| 11 | +# List installed plugins |
| 12 | +opencli plugin list |
| 13 | + |
| 14 | +# Use the plugin (it's just a regular command) |
| 15 | +opencli github-trending repos --limit 10 |
| 16 | + |
| 17 | +# Remove a plugin |
| 18 | +opencli plugin uninstall github-trending |
| 19 | +``` |
| 20 | + |
| 21 | +## How Plugins Work |
| 22 | + |
| 23 | +Plugins live in `~/.opencli/plugins/<name>/`. Each subdirectory is scanned at startup for `.yaml`, `.ts`, or `.js` command files — the same formats used by built-in adapters. |
| 24 | + |
| 25 | +### Supported Source Formats |
| 26 | + |
| 27 | +```bash |
| 28 | +opencli plugin install github:user/repo |
| 29 | +opencli plugin install https://github.com/user/repo |
| 30 | +``` |
| 31 | + |
| 32 | +The repo name prefix `opencli-plugin-` is automatically stripped for the local directory name. For example, `opencli-plugin-hot-digest` becomes `hot-digest`. |
| 33 | + |
| 34 | +## Creating a Plugin |
| 35 | + |
| 36 | +### Option 1: YAML Plugin (Simplest) |
| 37 | + |
| 38 | +Zero dependencies, no build step. Just create a `.yaml` file: |
| 39 | + |
| 40 | +``` |
| 41 | +my-plugin/ |
| 42 | +├── my-command.yaml |
| 43 | +└── README.md |
| 44 | +``` |
| 45 | + |
| 46 | +Example `my-command.yaml`: |
| 47 | + |
| 48 | +```yaml |
| 49 | +site: my-plugin |
| 50 | +name: my-command |
| 51 | +description: My custom command |
| 52 | +strategy: public |
| 53 | +browser: false |
| 54 | + |
| 55 | +args: |
| 56 | + limit: |
| 57 | + type: int |
| 58 | + default: 10 |
| 59 | + |
| 60 | +pipeline: |
| 61 | + - fetch: |
| 62 | + url: https://api.example.com/data |
| 63 | + - map: |
| 64 | + title: ${{ item.title }} |
| 65 | + score: ${{ item.score }} |
| 66 | + - limit: ${{ args.limit }} |
| 67 | + |
| 68 | +columns: [title, score] |
| 69 | +``` |
| 70 | +
|
| 71 | +### Option 2: TypeScript Plugin |
| 72 | +
|
| 73 | +For richer logic (multi-source aggregation, custom transformations, etc.): |
| 74 | +
|
| 75 | +``` |
| 76 | +my-plugin/ |
| 77 | +├── package.json |
| 78 | +├── my-command.ts |
| 79 | +└── README.md |
| 80 | +``` |
| 81 | + |
| 82 | +`package.json`: |
| 83 | + |
| 84 | +```json |
| 85 | +{ |
| 86 | + "name": "opencli-plugin-my-plugin", |
| 87 | + "version": "0.1.0", |
| 88 | + "type": "module", |
| 89 | + "peerDependencies": { |
| 90 | + "@jackwener/opencli": ">=1.0.0" |
| 91 | + } |
| 92 | +} |
| 93 | +``` |
| 94 | + |
| 95 | +`my-command.ts`: |
| 96 | + |
| 97 | +```typescript |
| 98 | +import { cli, Strategy } from '@jackwener/opencli/registry'; |
| 99 | + |
| 100 | +cli({ |
| 101 | + site: 'my-plugin', |
| 102 | + name: 'my-command', |
| 103 | + description: 'My custom command', |
| 104 | + strategy: Strategy.PUBLIC, |
| 105 | + browser: false, |
| 106 | + args: [ |
| 107 | + { name: 'limit', type: 'int', default: 10, help: 'Number of items' }, |
| 108 | + ], |
| 109 | + columns: ['title', 'score'], |
| 110 | + func: async (_page, kwargs) => { |
| 111 | + const res = await fetch('https://api.example.com/data'); |
| 112 | + const data = await res.json(); |
| 113 | + return data.items.slice(0, kwargs.limit).map((item: any, i: number) => ({ |
| 114 | + title: item.title, |
| 115 | + score: item.score, |
| 116 | + })); |
| 117 | + }, |
| 118 | +}); |
| 119 | +``` |
| 120 | + |
| 121 | +### TS Plugin Install Lifecycle |
| 122 | + |
| 123 | +When you run `opencli plugin install`, TS plugins are automatically set up: |
| 124 | + |
| 125 | +1. **Clone** — `git clone --depth 1` from GitHub |
| 126 | +2. **npm install** — Resolves regular dependencies |
| 127 | +3. **Host symlink** — Links the running `@jackwener/opencli` into the plugin's `node_modules/` so `import from '@jackwener/opencli/registry'` always resolves against the host |
| 128 | +4. **Transpile** — Compiles `.ts` → `.js` via `esbuild` (production `node` cannot load `.ts` directly) |
| 129 | + |
| 130 | +On startup, if both `my-command.ts` and `my-command.js` exist, the `.js` version is loaded to avoid duplicate registration. |
| 131 | + |
| 132 | +## Example Plugins |
| 133 | + |
| 134 | +| Repo | Type | Description | |
| 135 | +|------|------|-------------| |
| 136 | +| [opencli-plugin-github-trending](https://github.com/ByteYue/opencli-plugin-github-trending) | YAML | GitHub Trending repositories | |
| 137 | +| [opencli-plugin-hot-digest](https://github.com/ByteYue/opencli-plugin-hot-digest) | TS | Multi-platform trending aggregator (zhihu, weibo, bilibili, v2ex, stackoverflow, reddit, linux-do) | |
| 138 | + |
| 139 | +## Troubleshooting |
| 140 | + |
| 141 | +### Command not found after install |
| 142 | + |
| 143 | +Restart opencli (or open a new terminal) — plugins are discovered at startup. |
| 144 | + |
| 145 | +### TS plugin import errors |
| 146 | + |
| 147 | +If you see `Cannot find module '@jackwener/opencli/registry'`, the host symlink may be broken. Reinstall the plugin: |
| 148 | + |
| 149 | +```bash |
| 150 | +opencli plugin uninstall my-plugin |
| 151 | +opencli plugin install github:user/opencli-plugin-my-plugin |
| 152 | +``` |
0 commit comments