[CLI] Convert a Single Command CLI into a Multi Command CLI with Oclif and TypeScript
typescript and with Command CLI INTO multi convert
2023-09-14 09:00:47 时间
Heavy duty CLI's like gatsby and npm do more than one thing. The convention is to namespace them with a command name after the CLI name, like gatsby new
or gatsby build
or npm install
or npm uninstall
. We should be comfortable converting our single purpose CLI's into multi command CLI's as our needs grow, as well as understand how to share logic between commands to keep code DRY.
Folder stucture:
We can put orignal index.ts into /commands/init.ts:
import {Command, flags} from '@oclif/command' class Mycli extends Command { static description = 'describe the command here' static flags = { // add --version flag to show CLI version version: flags.version({char: 'v'}), help: flags.help({char: 'h'}), // flag with a value (-n, --name=VALUE) name: flags.string({char: 'n', description: 'name to print'}), // flag with no value (-f, --force) force: flags.boolean({char: 'f'}), } static args = [{name: 'file'}] static strict = false async run() { const {argv, flags} = this.parse(Mycli) const name = flags.name || 'world' this.log(`hello egg ${name} from ./src/index.ts`) } } export = Mycli
/commands/build.ts
Have similar code as init.ts.
index.ts:
export {run} from '@oclif/command'
Run the init / build:
yarn mycli init
yarn mycli build
相关文章
- ESLint: Typescript + React + TSX 集成
- 「使用 webpack5从0到1搭建React+TypeScript 项目环境」1. React 与 TypeScript 集成
- 记一次vscode踩坑记录:"TypeScript 语言服务在其启动后已中止 5 次。将不会重启该服务。"
- TypeScript:Uncaught TypeError: Cannot read properties of null 错误
- TypeScript介绍和使用
- 软件开发入门教程网之TypeScript 类
- Laravel + Vue 3(Vite、TypeScript)SPA 设置
- 用TodoList实例告诉你怎么在项目中使用TypeScript
- 硬核观察 | Python 之父对 Rust、Go、Julia 和 TypeScript 的看法
- MySQL中AND的使用方法及含义解析(mysql中and的含义)
- MySQL中的AND逻辑操作符是什么(mysql中and是什么)