溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Linux系統(tǒng)中怎么使用Node.js構(gòu)建根據(jù)詢問創(chuàng)建文件的命令行工具

發(fā)布時(shí)間:2022-01-21 10:33:53 來源:億速云 閱讀:208 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要介紹了Linux系統(tǒng)中怎么使用Node.js構(gòu)建根據(jù)詢問創(chuàng)建文件的命令行工具的相關(guān)知識(shí),內(nèi)容詳細(xì)易懂,操作簡(jiǎn)單快捷,具有一定借鑒價(jià)值,相信大家閱讀完這篇Linux系統(tǒng)中怎么使用Node.js構(gòu)建根據(jù)詢問創(chuàng)建文件的命令行工具文章都會(huì)有所收獲,下面我們一起來看看吧。

Linux系統(tǒng)中怎么使用Node.js構(gòu)建根據(jù)詢問創(chuàng)建文件的命令行工具

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

mkdir my-scriptcd my-scriptnpm init

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

  1. 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 文件。

  1. #!/usr/bin/env node

  2. const inquirer = require("inquirer");

  3. const chalk = require("chalk");

  4. const figlet = require("figlet");

  5. const shell = require("shelljs");

規(guī)劃命令行工具

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

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

  1. // index.js

  2. const run = async () => {

  3. // show script introduction

  4. // ask questions

  5. // create the file

  6. // show success message

  7. };

  8. run();

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

  1. const init = () => {

  2. console.log(

  3. chalk.green(

  4. figlet.textSync("Node JS CLI", {

  5. font: "Ghost",

  6. horizontalLayout: "default",

  7. verticalLayout: "default"

  8. })

  9. )

  10. );

  11. }

  12. const run = async () => {

  13. // show script introduction

  14. init();

  15. // ask questions

  16. // create the file

  17. // show success message

  18. };

  19. run();

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

  1. const askQuestions = () => {

  2. const questions = [

  3. {

  4. name: "FILENAME",

  5. type: "input",

  6. message: "What is the name of the file without extension?"

  7. },

  8. {

  9. type: "list",

  10. name: "EXTENSION",

  11. message: "What is the file extension?",

  12. choices: [".rb", ".js", ".php", ".css"],

  13. filter: function(val) {

  14. return val.split(".")[1];

  15. }

  16. }

  17. ];

  18. return inquirer.prompt(questions);

  19. };

  20. // ...

  21. const run = async () => {

  22. // show script introduction

  23. init();

  24. // ask questions

  25. const answers = await askQuestions();

  26. const { FILENAME, EXTENSION } = answers;

  27. // create the file

  28. // show success message

  29. };

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

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

  1. const createFile = (filename, extension) => {

  2. const filePath ={filename}.${extension}“

  3. shell.touch(filePath);

  4. return filePath;

  5. };

  6. // ...

  7. const run = async () => {

  8. // show script introduction

  9. init();

  10. // ask questions

  11. const answers = await askQuestions();

  12. const { FILENAME, EXTENSION } = answers;

  13. // create the file

  14. const filePath = createFile(FILENAME, EXTENSION);

  15. // show success message

  16. };

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

  1. const success = (filepath) => {

  2. console.log(

  3. chalk.white.bgGreen.bold(Done! File created at ${filepath})

  4. );

  5. };

  6. // ...

  7. const run = async () => {

  8. // show script introduction

  9. init();

  10. // ask questions

  11. const answers = await askQuestions();

  12. const { FILENAME, EXTENSION } = answers;

  13. // create the file

  14. const filePath = createFile(FILENAME, EXTENSION);

  15. // show success message

  16. success(filePath);

  17. };

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

Linux系統(tǒng)中怎么使用Node.js構(gòu)建根據(jù)詢問創(chuàng)建文件的命令行工具

完整代碼

下述代碼為完整代碼:


  1. #!/usr/bin/env node

  2. const inquirer = require("inquirer");

  3. const chalk = require("chalk");

  4. const figlet = require("figlet");

  5. const shell = require("shelljs");

  6. const init = () => {

  7. console.log(

  8. chalk.green(

  9. figlet.textSync("Node JS CLI", {

  10. font: "Ghost",

  11. horizontalLayout: "default",

  12. verticalLayout: "default"

  13. })

  14. )

  15. );

  16. };

  17. const askQuestions = () => {

  18. const questions = [

  19. {

  20. name: "FILENAME",

  21. type: "input",

  22. message: "What is the name of the file without extension?"

  23. },

  24. {

  25. type: "list",

  26. name: "EXTENSION",

  27. message: "What is the file extension?",

  28. choices: [".rb", ".js", ".php", ".css"],

  29. filter: function(val) {

  30. return val.split(".")[1];

  31. }

  32. }

  33. ];

  34. return inquirer.prompt(questions);

  35. };

  36. const createFile = (filename, extension) => {

  37. const filePath ={filename}.${extension}“

  38. shell.touch(filePath);

  39. return filePath;

  40. };

  41. const success = filepath => {

  42. console.log(

  43. chalk.white.bgGreen.bold(Done! File created at ${filepath})

  44. );

  45. };

  46. const run = async () => {

  47. // show script introduction

  48. init();

  49. // ask questions

  50. const answers = await askQuestions();

  51. const { FILENAME, EXTENSION } = answers;

  52. // create the file

  53. const filePath = createFile(FILENAME, EXTENSION);

  54. // show success message

  55. success(filePath);

  56. };

  57. run();

使用這個(gè)腳本

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

  1. {

  2. "name": "creator",

  3. "version": "1.0.0",

  4. "description": "",

  5. "main": "index.js",

  6. "scripts": {

  7. "test": "echo \"Error: no test specified\" && exit 1",

  8. "start": "node index.js"

  9. },

  10. "author": "",

  11. "license": "ISC",

  12. "dependencies": {

  13. "chalk": "^2.4.1",

  14. "figlet": "^1.2.0",

  15. "inquirer": "^6.0.0",

  16. "shelljs": "^0.8.2"

  17. },

  18. "bin": {

  19. "creator": "./index.js"

  20. }

  21. }

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

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

  1. /usr/bin/creator -> /usr/lib/node_modules/creator/index.js

  2. /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í)行$ creator命令來調(diào)用。

關(guān)于“Linux系統(tǒng)中怎么使用Node.js構(gòu)建根據(jù)詢問創(chuàng)建文件的命令行工具”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對(duì)“Linux系統(tǒng)中怎么使用Node.js構(gòu)建根據(jù)詢問創(chuàng)建文件的命令行工具”知識(shí)都有一定的了解,大家如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI