阅读 114

yargs脚手架命令注册

  • 查看lerna源码用的就是yargs的库注册脚手架,@vue/cli是自己开发的

  • command 命令注册

  • options 参数注册

  • 常用方法

image.png

#!/usr/bin/env node const yargs = require("yargs/yargs"); const dedent = require("dedent"); // 使用了.parse(argv, context)就不需要传argv了 // const { hideBin } = require("yargs/helpers"); const pkg = require('../package.json'); const cli = yargs() const ListCmd = require('./command/list')  const context = {    rainbowVersion: pkg.version,  };  const argv = process.argv.slice(2)   // dedent去除缩进   cli   // 未知命令的提示     // .strict()     // $0 值为argv第一个参数     .usage("Usage: $0 <command> [options]")     .demandCommand(       1,       "A command is required. Pass --help to see all available commands and options."     )     // 如果命令输入错误,会提示     .recommendCommands()     .alias("h", "help")     .alias("v", "version")     // 定义多个option参数     .options({       debugger: {         defaultDescription: "",         describe: "bootstrap debugger mode",         type: "boolean",         alias: "d",       },     })     // 注册单个option参数     .option("register", {       describe: "Define Global Option",       type: "string",       alias: "r",     })     .option("ci", {       type: "boolean",       hidden: true,     })     // 定义命令分组     .group(["debugger"], "Dev Options")     .group(["register"], "Extra Options")     // 当输入错误命令的时候的提示内容     .fail((err,msg) => {       /* 此时会把命令中相近的进行提示         是指 init?         无法识别的选项:iii       */       console.log(err);     })     // 自定义yargs命令,有四个参数     // 可以使用rainbow-test init -h查看帮助文档     .command(       "[init [name]]",       "Do init a project",       (yargs) => {         yargs.option("name", {           type: "string",           describe: "Name of a Project",           alias: "n",         });       },       (argv) => {         console.log(argv); //查看参数         /*        {         _: [ 'init' ],         rainbowVersion: '1.1.0',         '$0': '/usr/local/bin/rainbow-test'       }      */       }     )     // Lerna源码使用的就是这种方式,但是yargs帮助文档并没有介绍     .command(ListCmd)     .command({       command: "test",       aliases: ["t", "te", "tes"],       describe: "test local packages",       // 在执行脚手架之前完成的东西 ,比如定义私有的options          builder: (yargs) => {         console.log("test the command");       },       // 具体执行逻辑       handler: function handler(argv) {         //   return require(".")(argv);         console.log(argv);         return {           initialize() {             console.log("initialize");           },           execute() {             // piping to `wc -l` should not yield 1 when no packages matched            console.log("execute");           },         };       },     })     .command({       command:"start",       alias:['s'],       describe:"start the project",       builder:(yargs)=>{         console.log('');       },       handler:(argv)=>{         console.log(argv);         console.log('start');         setTimeout(()=>{           console.log('setTimeout');         },0);         new Promise(()=>{           let chain = Promise.resolve();           chain.then(() => console.log("chain1"))           chain.then(() => console.log("chain2"))           chain.then(() => console.log("chain3"));         })         let chain = Promise.resolve();         chain.then(() => console.log("chain4"));         setTimeout(()=>{           console.log('setTimeout2');         },0)         console.log("end");       }     })     // 设置输出的宽度     .wrap(cli.terminalWidth())     // 结尾的时候定义内容     .epilogue(       dedent`         When a command fails, all logs are written to lerna-debug.log in the current working directory.       For more information, find our manual at https://github.com/lerna/lerna     `       // 注入参数,比如注册版本号     )     .parse(argv, context);


作者:rainbowdiary
链接:https://juejin.cn/post/7028038864153509925

文章分类
代码人生
文章标签
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐