如何打造本身的CLI?

有哪些CLI?

  • Vue-cli
  • Gulp
  • create-react-app
  • webpack
  • yeoman
  • express-generator
  • ...

为何须要CLI?

  1. 减小重复性工做。
  2. 根据动态命令更方便的生成开发环境。
  3. 团队协同,效率高。

咱们要达到的设计?

  1. Node-Simple-BFF-Architecture init node-BFFphp

  2. Node-Simple-BFF-Architecture listnode

最终的效果图

步骤:

一. 新建文件,进入文件夹 npm initreact

二. 在package.json文件夹下加入:webpack

bin:{
           "Node-Simple-BFF-Architecture": "./bin/index.js"    
        }
复制代码

三.在目录下新建文件夹 bin,在bin下新建index.jsgit

四. 执行命令:

npm link
复制代码

五.在index.js文件夹下写入:github

#!/usr/bin/env node
    
    
    //1.获取用户输入的命令 
    
    console.log(process.argv)
复制代码

六. 在控制台执行:web

Node-Simple-BFF-Architecture 
复制代码

即可以看见控制台有这个Node-Simple-BFF-Architecture命令了。express

七. 代码(我都有注释):npm

#!/usr/bin/env node 
//逻辑:
  //1.获取用户输入的命令 console.log(process.argv)
  //2.根据命令进行操做。。。。。




// 处理 用户 命令 好比: Node-Simple-BFF-Architecture -version 
        //处理用户命令
const program = require('commander'),
        //下载
       download = require('download-git-repo'),
       //模板引擎看成字符串
       handlebars = require('handlebars'),
       // 交互
       inquirer = require('inquirer'),
       //视觉美化
       ora = require('ora'),
       //字体颜色
       chalk = require('chalk'),
       //路径
       fs = require('fs'),
       //对勾优化
       logSymbols = require('log-symbols'),
       //自定义字符画
       chars = [
        "",
                " ▍ ★∴",
         " s .t .▍▍a...r.█▍ ☆ ★∵t ..../ ",
        "  ◥█▅▅██▅▅██▅▅▅▅▅███◤ ",
        "   .◥███████████████◤",
        "~~~~◥█████████████◤~~~~",
        "~~~~~~~~~~~~~~~~~~~~~~~~"
    ]


//查看版本号
program
  .version('1.0.0')

//设计命令 初始化命令
//Node-Simple-BFF-Architecture init node-BFF
program
  .command('init <template>')
  .description('Initializing BFF architecture')
  .action(function(template){
    download('https://github.com:1049229070/node-middleware-php#master', template, function (err) {

        const spinner = ora(chalk.green('Download in progress...')).start();

        if(err){
            spinner.fail(chalk.red('Download failed...'));
            return false
        }

        spinner.succeed();
        
        inquirer.prompt([{
          type:'input',
          name:'name',
          message:'Please enter the project name?'

        }]).then(answers => {

            const packagePath = `${template}/package.json`;
            const packageJson =  fs.readFileSync(packagePath,'utf-8');
            const packageResult =  handlebars.compile(packageJson)(answers);
            fs.writeFileSync(packagePath,packageResult);

            console.log(logSymbols.success,chalk.green(chars.join('\n')))
           
        });
      })
  });

//设计命令 查看全部帮助
//Node-Simple-BFF-Architecture list
program
  .command('list')
  .description('View all commands') 
  .action(function(){
    console.log('mode');
  })

//设计命令 其余命令处理
program
  .command('*')
  .action(function(env){
    console.log('No operation was found.', env);
  });

program.parse(process.argv);
复制代码

七.发布(须要注册一个npm号):json

1. npm login
    
2. npm publish
复制代码

八. 发布过程当中遇到的问题。

1. publish Failed PUT 400
复制代码

package.json中name须要小写:

"name": "node-simple-bff-architecture"
复制代码

九. 疑惑。 1.我但愿在与用户交互的时候多一些动画效果,不知道有没有大神能给我推荐一些npm包。记得给我流言哦,但愿这篇文章对你们理解CLI实际工做原理有用。

相关文章
相关标签/搜索