欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

Node.js中怎么構(gòu)建一個(gè)交互式命令行工具

這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)碛嘘P(guān)Node.js中怎么構(gòu)建一個(gè)交互式命令行工具,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

創(chuàng)新互聯(lián)為企業(yè)級(jí)客戶提高一站式互聯(lián)網(wǎng)+設(shè)計(jì)服務(wù),主要包括成都做網(wǎng)站、網(wǎng)站設(shè)計(jì)、成都App定制開發(fā)微信小程序定制開發(fā)、宣傳片制作、LOGO設(shè)計(jì)等,幫助客戶快速提升營銷能力和企業(yè)形象,創(chuàng)新互聯(lián)各部門都有經(jīng)驗(yàn)豐富的經(jīng)驗(yàn),可以確保每一個(gè)作品的質(zhì)量和創(chuàng)作周期,同時(shí)每年都有很多新員工加入,為我們帶來大量新的創(chuàng)意。 

開始

首先,創(chuàng)建一個(gè)新的 npm 包(NPM 是 JavaScript 包管理器)。

mkdir my-scriptcd my-scriptnpm init

NPM 將會(huì)問一些問題。隨后,我們需要安裝一些包。

npm install --save chalk figlet inquirer shelljs

這是我們需要的包:

  • Chalk:正確設(shè)定終端的字符樣式

  • Figlet:使用普通字符制作大字母的程序(LCTT 譯注:使用標(biāo)準(zhǔn)字符,拼湊出圖片)

  • Inquirer:通用交互式命令行用戶界面的集合

  • ShellJS:Node.js 版本的可移植 Unix Shell 命令行工具

創(chuàng)建一個(gè) index.js 文件

現(xiàn)在我們要使用下述內(nèi)容創(chuàng)建一個(gè) index.js 文件。

#!/usr/bin/env node const inquirer = require("inquirer");const chalk = require("chalk");const figlet = require("figlet");const shell = require("shelljs");

規(guī)劃命令行工具

在我們寫命令行工具所需的任何代碼之前,做計(jì)劃總是很棒的。這個(gè)命令行工具只做一件事:創(chuàng)建一個(gè)文件

它將會(huì)問兩個(gè)問題:文件名是什么以及文件后綴名是什么?然后創(chuàng)建文件,并展示一個(gè)包含了所創(chuàng)建文件路徑的成功信息。

// index.js const run = async () => {  // show script introduction  // ask questions  // create the file  // show success message}; run();

***個(gè)函數(shù)只是該腳本的介紹。讓我們使用 chalk 和 figlet 來把它完成。

const init = () => {  console.log(    chalk.green(      figlet.textSync("Node JS CLI", {        font: "Ghost",        horizontalLayout: "default",        verticalLayout: "default"      })    )  );} const run = async () => {  // show script introduction  init();   // ask questions  // create the file  // show success message}; run();

然后,我們來寫一個(gè)函數(shù)來問問題。

const askQuestions = () => {  const questions = [    {      name: "FILENAME",      type: "input",      message: "What is the name of the file without extension?"    },    {      type: "list",      name: "EXTENSION",      message: "What is the file extension?",      choices: [".rb", ".js", ".php", ".css"],      filter: function(val) {        return val.split(".")[1];      }    }  ];  return inquirer.prompt(questions);}; // ... const run = async () => {  // show script introduction  init();   // ask questions  const answers = await askQuestions();  const { FILENAME, EXTENSION } = answers;   // create the file  // show success message};

注意,常量 FILENAME 和 EXTENSIONS 來自 inquirer 包。

下一步將會(huì)創(chuàng)建文件。

const createFile = (filename, extension) => {  const filePath = `${process.cwd()}/${filename}.${extension}`  shell.touch(filePath);  return filePath;}; // ... const run = async () => {  // show script introduction  init();   // ask questions  const answers = await askQuestions();  const { FILENAME, EXTENSION } = answers;   // create the file  const filePath = createFile(FILENAME, EXTENSION);   // show success message};

***,重要的是,我們將展示成功信息以及文件路徑。

const success = (filepath) => {  console.log(    chalk.white.bgGreen.bold(`Done! File created at ${filepath}`)  );}; // ... const run = async () => {  // show script introduction  init();   // ask questions  const answers = await askQuestions();  const { FILENAME, EXTENSION } = answers;   // create the file  const filePath = createFile(FILENAME, EXTENSION);   // show success message  success(filePath);};

來讓我們通過運(yùn)行 node index.js 來測(cè)試這個(gè)腳本,這是我們得到的:

Node.js中怎么構(gòu)建一個(gè)交互式命令行工具

完整代碼

下述代碼為完整代碼:

#!/usr/bin/env node const inquirer = require("inquirer");const chalk = require("chalk");const figlet = require("figlet");const shell = require("shelljs"); const init = () => {  console.log(    chalk.green(      figlet.textSync("Node JS CLI", {        font: "Ghost",        horizontalLayout: "default",        verticalLayout: "default"      })    )  );}; const askQuestions = () => {  const questions = [    {      name: "FILENAME",      type: "input",      message: "What is the name of the file without extension?"    },    {      type: "list",      name: "EXTENSION",      message: "What is the file extension?",      choices: [".rb", ".js", ".php", ".css"],      filter: function(val) {        return val.split(".")[1];      }    }  ];  return inquirer.prompt(questions);}; const createFile = (filename, extension) => {  const filePath = `${process.cwd()}/${filename}.${extension}`  shell.touch(filePath);  return filePath;}; const success = filepath => {  console.log(    chalk.white.bgGreen.bold(`Done! File created at ${filepath}`)  );}; const run = async () => {  // show script introduction  init();   // ask questions  const answers = await askQuestions();  const { FILENAME, EXTENSION } = answers;   // create the file  const filePath = createFile(FILENAME, EXTENSION);   // show success message  success(filePath);}; run();

使用這個(gè)腳本

想要在其它地方執(zhí)行這個(gè)腳本,在你的 package.json 文件中添加一個(gè) bin 部分,并執(zhí)行 npm link

{  "name": "creator",  "version": "1.0.0",  "description": "",  "main": "index.js",  "scripts": {    "test": "echo \"Error: no test specified\" && exit 1",    "start": "node index.js"  },  "author": "",  "license": "ISC",  "dependencies": {    "chalk": "^2.4.1",    "figlet": "^1.2.0",    "inquirer": "^6.0.0",    "shelljs": "^0.8.2"  },  "bin": {    "creator": "./index.js"  }}

執(zhí)行 npm link 使得這個(gè)腳本可以在任何地方調(diào)用。

這就是是當(dāng)你運(yùn)行這個(gè)命令時(shí)的結(jié)果。

/usr/bin/creator -> /usr/lib/node_modules/creator/index.js/usr/lib/node_modules/creator -> /home/hugo/code/creator

這會(huì)連接 index.js 作為一個(gè)可執(zhí)行文件。這是完全可能的,因?yàn)檫@個(gè) CLI 腳本的***行是 #!/usr/bin/env node

現(xiàn)在我們可以通過執(zhí)行如下命令來調(diào)用。

$ creator

上述就是小編為大家分享的Node.js中怎么構(gòu)建一個(gè)交互式命令行工具了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

網(wǎng)站名稱:Node.js中怎么構(gòu)建一個(gè)交互式命令行工具
本文鏈接:http://chinadenli.net/article42/pgjjec.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)品牌網(wǎng)站設(shè)計(jì)網(wǎng)站維護(hù)虛擬主機(jī)Google小程序開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都app開發(fā)公司