1.0.0 • Published 1 year ago
cli-lzy v1.0.0
手写一个cli工具
步骤
- 初始化项目
- 新建bin文件夹
- package.json新增bin配置
- npm link 软连接到全局
- 定义命令行界面
HBS模版
搭建cli
command
- 总命令管理工具
inquirer
- 录入用户的交互信息到一个json文件
type = input | comfirm | list | rawlist| checkbox |expand | password | editor
-- input
{
type: 'input',
message: '设置一个用户名:',
name: 'name',
default: "test_user" // 默认值
validate():boolean; // 校验成功之后需要返回true,返回其他信息表示失败
}
-- comfirm
{
type:"comfirm",
message:"真的确定吗",
name:"real",//answers对象中的key值
suffiex: "真的确定吗",
when:function(answers){
// 当前所有交互值的对象
// 返回值为true的时候 出现这个comfirm
return answers.comfirm;
}
}
-- list
{
type: 'list',
message: '请选择一种水果:',
name: 'fruit',
choices: [
"Apple",
"Pear",
"Banana"
],
filter: function (val) { // 使用filter将回答变为小写
return val.toLowerCase();
}
}
-- rawlist
{
type: 'rawlist',
message: '请选择一种水果:',
name: 'fruit',
choices: [
"Apple",
"Pear",
"Banana"
]
}
-- expand [通过输入值的方式筛选值]
{
type: "expand",
message: "请选择一种水果:",
name: "fruit",
choices: [
{
key: "a",
name: "Apple",
value: "apple"
},
{
key: "O",
name: "Orange",
value: "orange"
},
{
key: "p",
name: "Pear",
value: "pear"
}
]
}
;
-- checkbox[多选]
{
type: "checkbox",
message: "选择颜色:",
name: "color",
choices: [
{
name: "red"
},
new inquirer.Separator(), // 添加分隔符
{
name: "blur",
checked: true // 默认选中
},
{
name: "green"
},
new inquirer.Separator("--- 分隔符 ---"), // 自定义分隔符
{
name: "yellow"
}
],
pageSize: 2 // 设置 最大显示行数
}
}
-- password [输入密码]
{
type: "password", // 密码为密文输入
message: "请输入密码:",
name: "pwd"
}
-- editor [用于输入其他信息]
{
type: "editor",
message: "请输入备注:",
name: "editor"
}
chalk
-- 改变输出在控制台的样式
{
console.log(chalk.green('Added successfully!\n'))
}
ora
-- 加载的loading
{
const spinner = ora("Downloading...");// init
spinner.start() // launch
spinner.fail() // close -- error icon
spinner.succeed() // close -- success icon
}
download-git-repo
-- 下载 git 仓库 { // 默认下载master分支 download(repository:远程仓库, destination:目标文件夹, options, callback); // 指定下载的分支 params - repository:仓库名#分支名
}