官方有詳細(xì)的文檔,只記錄自己用到的,方便后面查閱
- 最近接觸到了兩個npm包inquirer,學(xué)習(xí)一下
inquirer.js ---- 提供命令行與用戶的交互接口
var inquirer = require('inquirer');
inquirer
.prompt([
/* Pass your questions in here */
])
.then(answers => {
// Use user feedback for... whatever!!
});
- 從示例可以看到,使用
.prompt()
來提出問題,得到一個封裝好的promise
,在then()
中執(zhí)行操作 - 一個問題由一個
question object
來表示,只記錄常用:
name (String) : 必選參數(shù),問題的名字,在問題被回答完畢后,需要通過answers.name
獲取到該問題的答案
type (String) :交互問題的類型.默認(rèn)是 input。支持: input, number, confirm, list, rawlist, expand, checkbox, password, editor
message (String | Function) : 提出的問題,也可以通過一個function
來定制,該方法會拿到當(dāng)前會話的answer
對象,你可以在funciton
中根據(jù)之前answer
中的回答來制定當(dāng)前的問題。
default (String | Number | Boolean | Array | Function) : 問題的默認(rèn)值,根據(jù)前面問題的類型而定,特殊的也可以類似于message
來通過方法來定制
choices (Array | Function) : 提供一個數(shù)組來為問題提供選擇,數(shù)組中可以提供簡單的直接值,也可以使用如下形式:
[{
name : "han", //展示出來的選項
value : "gaara han", //該選項實際對應(yīng)的值
short : "h", //選擇后展示的簡稱
},...]
還可以在其中使用inquirer
提供的分隔符類
簡單嘗試
const inquirer = require('inquirer');
inquirer.prompt([
{
name : 'my-name',
type : 'list',
message : 'which of this is my name?',
default : 'i don\'t know',
choices : [
{
name : 'gaara han',
value : 'gaara han',
short : 'gaara',
},
{
name : 'han',
value : 'han',
short : 'han',
},
{
name : "i don't know",
value : 'undefined',
short : 'no-name'
},
],
}
]).then(ans=>{
console.log(ans);
})
chalk --- 給輸出帶上顏色
-
chalk.js ,
chalk
的使用較為簡單, 通過官網(wǎng)的示例就可以很快了解用法
const chalk = require('chalk');
const log = console.log;
// 為普通字符串結(jié)合樣式
log(chalk.blue('Hello') + ' World' + chalk.red('!'));
// 可以同時添加多個樣式
log(chalk.blue.bgRed.bold('Hello world!'));
// 可以傳遞多個值
log(chalk.blue('Hello', 'World!', 'Foo', 'bar', 'biz', 'baz'));
// 可以相互嵌套
log(chalk.red('Hello', chalk.underline.bgBlue('world') + '!'));
log(chalk.green(
'I am a green line ' +
chalk.blue.underline.bold('with a blue substring') +
' that becomes green again!'
));
// 支持模版字符串
log(`
CPU: ${chalk.red('90%')}
RAM: ${chalk.green('40%')}
DISK: ${chalk.yellow('70%')}
`);
let cpu = { totalPercent : 80, }
let ram = { used : 4, tota : 8, }
let disk = { used : 20, total : 50, }
log(chalk`
CPU: {red ${cpu.totalPercent}%}
RAM: {green ${ram.used / ram.total * 100}%}
DISK: {rgb(255,131,0) ${disk.used / disk.total * 100}%}
`);
// 使用關(guān)鍵字,rgb,十六進(jìn)制
log(chalk.keyword('orange')('Yay for orange colored text!'));
log(chalk.rgb(123, 45, 67).underline('Underlined reddish color'));
log(chalk.hex('#DEADED').bold('Bold gray!'));