嘿,會(huì)Node.js 的小伙伴們!今天咱們來(lái)聊聊一個(gè)超級(jí)實(shí)用的 Node.js 庫(kù)——inquirer.js。如果你想要讓你的命令行工具變得交互式,那這個(gè)庫(kù)絕對(duì)是你的不二之選。它能讓你輕松創(chuàng)建出美觀、易用的文本界面,讓用戶(hù)的輸入體驗(yàn)更上一層樓。
安裝起來(lái)也是分分鐘的事,用 npm 或 yarn 都可以:
npm install inquirer
或者
yarn add inquirer
安裝完了,咱們就可以開(kāi)始玩耍了。先來(lái)個(gè)簡(jiǎn)單的示例,看看 inquirer.js 是怎么用的:
const inquirer = require('inquirer');const questions = [{ type: 'input', name: 'username', message: '嘿,你叫啥呀?',},{ type: 'password', name: 'password', message: '密碼悄悄告訴我:',},];inquirer.prompt(questions).then(answers => { console.log('用戶(hù)名:', answers.username); console.log('密碼:', answers.password);});
這段代碼里,咱們定義了兩個(gè)問(wèn)題:用戶(hù)名和密碼。type 參數(shù)告訴 inquirer.js 我們想要的輸入類(lèi)型,name 是問(wèn)題的答案名稱(chēng),message 是咱們要問(wèn)用戶(hù)的問(wèn)題。
inquirer.js 提供了一大堆問(wèn)題類(lèi)型,滿足各種不同的需求:
就是普通的文本輸入。
{ type: 'input', name: 'name', message: '你的名字是啥?',}
和輸入一樣,但輸入的內(nèi)容不會(huì)顯示出來(lái)。
{ type: 'password', name: 'password', message: '密碼多少呀?',}
用戶(hù)可以通過(guò)輸入 y 或 n 來(lái)回答“是”或“否”。
{ type: 'confirm', name: 'continue', message: '咱們繼續(xù)嗎?',}
讓用戶(hù)從列表里挑一個(gè)。
{ type: 'list', name: 'theme', message: '選個(gè)主題唄:', choices: ['亮堂的', '暗夜的', '自定義的'],}
下拉列表,用戶(hù)可以用方向鍵選擇多個(gè)選項(xiàng)。
{ type: 'expand', name: 'abilities', message: '超能力選哪個(gè)?', choices: [ { key: 'p', name: '力大無(wú)窮', value: 'power', }, { key: 's', name: '快如閃電', value: 'speed', }, // 更多選項(xiàng)...],}
這個(gè)厲害了,讓用戶(hù)在外部編輯器里寫(xiě)東西。
{ type: 'editor', name: 'bio', message: '來(lái),寫(xiě)個(gè)自我介紹:',}
inquirer.prompt() 方法返回一個(gè) Promise,用戶(hù)一提交答案,Promise 就會(huì)帶著所有答案的對(duì)象來(lái)解析。你可以拿這些答案去做各種酷炫的事。
inquirer.prompt(questions).then(answers => { // 用 answers 對(duì)象干點(diǎn)啥 console.log('用戶(hù)名:', answers.username);});
有時(shí)候,你可能想問(wèn)的問(wèn)題取決于用戶(hù)之前的回答。inquirer.js 支持這種智能模式,用 when 屬性就能搞定。
const questions = [ { type: 'confirm', name: 'hasAccount', message: '有賬戶(hù)不?', }, { type: 'input', name: 'username', message: '用戶(hù)名是啥?', when: answers => answers.hasAccount }, { type: 'input', name: 'email', message: '郵箱地址呢?', when: answers => !answers.hasAccount }];
在這個(gè)例子里,用戶(hù)名的輸入框會(huì)不會(huì)顯示,取決于用戶(hù)對(duì) hasAccount 的回答。
inquirer.js 還讓你能通過(guò) prefix 屬性來(lái)加個(gè)前綴,或者用 transformer 函數(shù)來(lái)美化顯示給用戶(hù)的答案。
{ type: 'input', name: 'name', message: '叫啥?', prefix: '稱(chēng)呼:', transformer: input => `你好啊,${input}!`,}
inquirer.js 可以和其他 Node.js 工具一起用,比如和 cross-spawn 模塊搭配,根據(jù)用戶(hù)的回答執(zhí)行不同的命令。
const { spawn } = require('cross-spawn');inquirer.prompt([ { type: 'list', name: 'action', message: '想干點(diǎn)啥?', choices: ['裝點(diǎn)依賴(lài)', '跑個(gè)測(cè)試', '撤了'], },]).then(answers => { const command = answers.action === '裝點(diǎn)依賴(lài)' ? 'npm install' : 'npm test'; spawn.sync(command, [], { stdio: 'inherit' });});
inquirer.js 是一個(gè)功能超群的庫(kù),能讓你的命令行工具變得既強(qiáng)大又用戶(hù)友好。無(wú)論是簡(jiǎn)單的數(shù)據(jù)收集,還是復(fù)雜的多步驟交互,inquirer.js 都能幫你搞定。今天咱們學(xué)了 inquirer.js 的基本用法,現(xiàn)在你應(yīng)該對(duì)這個(gè)庫(kù)有個(gè)大概的了解了。
本文鏈接:http://www.tebozhan.com/showinfo-26-85867-0.htmlNode.js 中的交互式命令行:玩轉(zhuǎn) Inquirer.js
聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。郵件:2376512515@qq.com
上一篇: Rust 機(jī)器學(xué)習(xí),一定要知道的這些庫(kù),可以替代 Python 庫(kù)了