cli命令行界面 demo |
您所在的位置:网站首页 › 第二章本章自测题答案 › cli命令行界面 demo |
cli命令行界面 demo 下次您有一个想法💡“我知道...我为什么不编写一个脚本来自动执行此操作?” 实际上,您不应该编写CLI 代替。 CLI具有比脚本更好的结构。 而且,CLI使用命令标志和帮助文本来防止您犯一些愚蠢的错误。 例如: $ moa --help Moa makes it easier to run your local node.js apps. USAGE $ moa OPTIONS -c, --commands=commands [default: start] commands to run. -h, -- help show CLI help -r, --root=root [default: ../] root path to run. DESCRIPTION ... Have some fun脚本很乱,可能会失控。 在Taggun ,我们运行了很多node.js应用程序。 顺便说一下,我们制作了用于收据OCR扫描的 API。 我建议使用oclif:Heroku的Open CLI框架,用于创建和管理CLI。 如何创建新的CLI首先安装node.js。 从搭建CLI开始 $ npx oclif single moa ? npm package name (moa): moa $cd moa修改src\index.js标志 MoaCommand.flags = {// add --help flag to show CLI version help: flags.help({ char : 'h' }), commands : flags.string({ char : 'c' , description : 'commands to run. Comma-separated.' , default : 'start' }), root : flags.string({ char : 'r' , description : 'root path to run.' , default : '../' }), }在src\index.js修改您的描述 MoaCommand.description =`Moa makes it easier to run your local node.js apps.在src\index.js修改您的命令 class MoaCommand extends Command { async run() { const {flags} = this .parse(MoaCommand) const commands = flags.commands.split( ',' ) const {root} = flags find.file( /(? { files.forEach( async file => { const cwd = require ( 'path' ).dirname(file) commands.forEach( async command => { this .log(cwd, `npm run ${command} ` ) const npm = spawn( 'npm' , [ 'run' , command], {cwd}) for await ( const output of npm.stdout) { this .log( ` ${file} : ${output} ` ) } }) }) }) } }在此示例中,您将需要run npm i find来安装find npm模块。 这个简单的CLI将在根文件夹中查找所有node.js应用程序,然后运行npm run [command] 。 如果您需要运行多个node.js应用程序以开始编码,那就太好了。 运行 ./bin/run -c start -c ../taggun/发布和共享您的CLI 参见https://oclif.io/docs/releasing 翻译自: https://hackernoon.com/making-command-line-interface-cli-for-fun-and-profit-np2636ym cli命令行界面 demo |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |