进阶 oclif.io 2026-10-18 02:37:03 · 45 阅读
第1章 介绍 oclif init 命令:在现有项目中初始化 oclif CLI
Introducing `oclif init` 命令
2024年4月10日 · 2 分钟阅读
感谢 @joshcanhelp 的贡献,我们在 4.8.0 版本中为 oclif CLI 发布了一个新命令 oclif init。
oclif init 命令可以在已有的项目目录中初始化 oclif,只添加最基本的内容:少量配置、bin 脚本,以及两个依赖(@oclif/core 和 ts-node)。这和 oclif generate 略有不同,后者会基于我们的模板创建一个全新的 oclif 项目。
oclif init 会添加以下内容:
- bin 脚本:bin/run.js、bin/run.cmd、bin/dev.js 和 bin/dev.cmd
- 在 package.json 中添加 oclif 配置项,包含 bin、dirname、commands 和 topicSeparator(详见 Configuring Your CLI)
- 将 @oclif/core 添加到 package.json 的 dependencies 中(如果尚不存在)
- 将 ts-node 添加到 package.json 的 devDependencies 中(如果尚不存在)
完成初始化后,就可以用这些 bin 脚本来运行你的 CLI,也可以通过 oclif generate command 和 oclif generate hook 等命令继续添加新功能。
❯ oclif init --help
Initialize a new oclif CLI
USAGE
$ oclif init [--bin ] [--module-type ESM|CommonJS] [--package-manager npm|yarn|pnpm] [--topic-separator
colons|spaces] [-d ] [-y]
FLAGS
-d, --output-dir= Directory to initialize the CLI in.
-y, --yes Use defaults for all prompts. Individual flags will override defaults.
--bin= Supply answer for prompt: Command bin name the CLI will export
--module-type=
为特定提示提供默认响应$ oclif init --topic-separator colons --bin mycli
祝你喜欢这条新命令!如果在使用过程中遇到任何问题,或者对这条命令有新想法,欢迎前往 oclif/oclif 仓库提交 Issue。 祝好, oclif 团队