简体   繁体   English

如何使用commander生成命令

[英]How to use commander to generate command

I am working on a cli tool with commander.我正在与指挥官一起开发一个 cli 工具。 I have this app.ts file.我有这个 app.ts 文件。

import { Command } from 'commander';
const program = new Command();
program.version('0.0.1');

const zoneConf = program.command('zone-conf');
const generate = zoneConf.command('generate');
generate.command('tx-commands').action(() => {
    console.log('Ran `zone-conf generate tx-commands`');
});

program.parse(process.argv);

I expect that when I run tsc && node app.js , I would be able to do run a command like zone-conf generate tx-commands .我希望当我运行tsc && node app.js时,我能够运行像zone-conf generate tx-commands这样的命令。 When I do run it the output is zsh: command not found: zone-conf .当我运行它时, output 是zsh: command not found: zone-conf

Is therw a step I am missing with commander in nodejs.是我在 nodejs 中缺少指挥官的一个步骤吗?

For your terminal to recognize your command you can use npm link .为了让您的终端识别您的命令,您可以使用npm link Which will create a global link for your package.这将为您的 package 创建一个全局链接。

You should first build your application and then run the link command.您应该首先构建您的应用程序,然后运行链接命令。

In your project folder run:在您的项目文件夹中运行:

$ tsc
$ npm link

If this is not something you want to leave it linked, after you are done using it, you can use the npm uninstall to remove it:如果这不是你想要保持链接的东西,在你使用完它之后,你可以使用npm uninstall来删除它:

$ npm uninstall -g <your-package-name>

If you don't want to link it globally, the comment from @shadowspawn should work:如果您不想全局链接它,@shadowspawn 的评论应该有效:

tsc && node app.js zone-conf generate tx-commands

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM