脚手架初始化

(一)命令行工具需要的几个要点

1. node 环境

2. commander  ------ 用于定义命令 (一个 commander 包)

3. inquirer ------- 用于和用户交互 (比如五颜六色的命令行)

(二)搭建脚手架工具的流程

1. 编写脚手架工具项目

2. 配置 package.json

3. 发布到 npm  (本地测试可以用 npm link)

4. 运行命令

5. 解析命令

6. 下载模板 (根据解析的命令下载模板)

(三)实例来一个

脚手架~

1. 先使用 搭建项目

安装各种依赖

npm init -y
npm i commander --save
npm i inquirer --save

2. 配置 package.json

(1)创建 index.js

(2)定义指令 cli ,当命令行输入 cli 回车,执行 index.js

"bin": {
   "codelint": "index.js"
 },

(3)注册指令

执行:

npm link

npm link 的作用 :在本地开发npm模块的时候,我们可以使用npm link命令,将 npm 模块链接到对应的运行项目中去,方便地对模块进行调试和测试

index.js 文件

#!/usr/bin/env node
console.log(1);

第一行作用:指定用 js 语言编译,一定要写,不然编译器不知道是啥语言~

以上~初级配置完成~下面使用两个包~

index.js文件:

const cm = require('commander')
const inquirer = require('inquirer')
cm.version('^4.0.1','-v --version');
// .version指令:第一个参数版本号 第二个参数命令 当-v/--version 的时候,输出版本
cm.parse(process.argv) // 解析命令

cm.command('init').action((name) => {
  inquirer.prompt([{
    type: 'input',
    name: 'projectName',
    message: '项目名称'
  }, {
    type: 'confirm',
    message: '是否进行监听',
    name: "isWatch",
  }, {
    type: 'list',
    message: '请选择一种设备',
    name: 'device',
    choices: ['ios', 'android']
  }, {
    type: 'checkbox',
    name: 'color',
    choices: ["red", "blur", "green", "yellow"]
  }, {
    type: "password",
    message: "请输入密码:",
    name: "pwd"
  }, {
    type: "editor",
    message: "请输入备注:",
    name: "editor"
  }]).then((answers) => {
    console.log(answers) // 打印出答案
  })
})
cm.parse(process.argv) // 解析命令

解析命令一定要加在最后,不然命令不生效~ 

.command().action().then()

.command:触发命令

.action:触发命令执行的动作

.then:执行动作后的回调函数

inquirer.command 的 用法~

原文链接:https://blog.csdn.net/qq_26733915/article/details/80461257

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值