成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

使用Node.js構建交互式命令行工具

開發 前端
當用于構建命令行界面(CLI)時,Node.js 十分有用。在這篇文章中,我將會教你如何使用 Node.js 來構建一個問一些問題并基于回答創建一個文件的命令行工具。

[[254552]]

使用 Node.js 構建一個根據詢問創建文件的命令行工具。

當用于構建命令行界面(CLI)時,Node.js 十分有用。在這篇文章中,我將會教你如何使用 Node.js 來構建一個問一些問題并基于回答創建一個文件的命令行工具。

開始

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

  1. mkdir my-script
  2. cd my-script
  3. npm init

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

  1. npm install --save chalk figlet inquirer shelljs

這是我們需要的包:

  • Chalk:正確設定終端的字符樣式
  • Figlet:使用普通字符制作大字母的程序(LCTT 譯注:使用標準字符,拼湊出圖片)
  • Inquirer:通用交互式命令行用戶界面的集合
  • ShellJS:Node.js 版本的可移植 Unix Shell 命令行工具

創建一個 index.js 文件

現在我們要使用下述內容創建一個 index.js 文件。

  1. #!/usr/bin/env node
  2.  
  3. const inquirer = require("inquirer");
  4. const chalk = require("chalk");
  5. const figlet = require("figlet");
  6. const shell = require("shelljs");

規劃命令行工具

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

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

  1. // index.js
  2.  
  3. const run = async () => {
  4. // show script introduction
  5. // ask questions
  6. // create the file
  7. // show success message
  8. };
  9.  
  10. run();

***個函數只是該腳本的介紹。讓我們使用 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.  
  13. const run = async () => {
  14. // show script introduction
  15. init();
  16.  
  17. // ask questions
  18. // create the file
  19. // show success message
  20. };
  21.  
  22. run();

然后,我們來寫一個函數來問問題。

  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. // ...
  22.  
  23. const run = async () => {
  24. // show script introduction
  25. init();
  26.  
  27. // ask questions
  28. const answers = await askQuestions();
  29. const { FILENAME, EXTENSION } = answers;
  30.  
  31. // create the file
  32. // show success message
  33. };

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

下一步將會創建文件。

  1. const createFile = (filename, extension) => {
  2. const filePath = `${process.cwd()}/${filename}.${extension}`
  3. shell.touch(filePath);
  4. return filePath;
  5. };
  6.  
  7. // ...
  8.  
  9. const run = async () => {
  10. // show script introduction
  11. init();
  12.  
  13. // ask questions
  14. const answers = await askQuestions();
  15. const { FILENAME, EXTENSION } = answers;
  16.  
  17. // create the file
  18. const filePath = createFile(FILENAME, EXTENSION);
  19.  
  20. // show success message
  21. };

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

  1. const success = (filepath) => {
  2. console.log(
  3. chalk.white.bgGreen.bold(`Done! File created at ${filepath}`)
  4. );
  5. };
  6.  
  7. // ...
  8.  
  9. const run = async () => {
  10. // show script introduction
  11. init();
  12.  
  13. // ask questions
  14. const answers = await askQuestions();
  15. const { FILENAME, EXTENSION } = answers;
  16.  
  17. // create the file
  18. const filePath = createFile(FILENAME, EXTENSION);
  19.  
  20. // show success message
  21. success(filePath);
  22. };

來讓我們通過運行 node index.js 來測試這個腳本,這是我們得到的:

完整代碼

下述代碼為完整代碼:

  1. #!/usr/bin/env node
  2.  
  3. const inquirer = require("inquirer");
  4. const chalk = require("chalk");
  5. const figlet = require("figlet");
  6. const shell = require("shelljs");
  7.  
  8. const init = () => {
  9. console.log(
  10. chalk.green(
  11. figlet.textSync("Node JS CLI", {
  12. font: "Ghost",
  13. horizontalLayout: "default",
  14. verticalLayout: "default"
  15. })
  16. )
  17. );
  18. };
  19.  
  20. const askQuestions = () => {
  21. const questions = [
  22. {
  23. name: "FILENAME",
  24. type: "input",
  25. message: "What is the name of the file without extension?"
  26. },
  27. {
  28. type: "list",
  29. name: "EXTENSION",
  30. message: "What is the file extension?",
  31. choices: [".rb", ".js", ".php", ".css"],
  32. filter: function(val) {
  33. return val.split(".")[1];
  34. }
  35. }
  36. ];
  37. return inquirer.prompt(questions);
  38. };
  39.  
  40. const createFile = (filename, extension) => {
  41. const filePath = `${process.cwd()}/${filename}.${extension}`
  42. shell.touch(filePath);
  43. return filePath;
  44. };
  45.  
  46. const success = filepath => {
  47. console.log(
  48. chalk.white.bgGreen.bold(`Done! File created at ${filepath}`)
  49. );
  50. };
  51.  
  52. const run = async () => {
  53. // show script introduction
  54. init();
  55.  
  56. // ask questions
  57. const answers = await askQuestions();
  58. const { FILENAME, EXTENSION } = answers;
  59.  
  60. // create the file
  61. const filePath = createFile(FILENAME, EXTENSION);
  62.  
  63. // show success message
  64. success(filePath);
  65. };
  66.  
  67. run();

使用這個腳本

想要在其它地方執行這個腳本,在你的 package.json 文件中添加一個 bin 部分,并執行 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. }

執行 npm link 使得這個腳本可以在任何地方調用。

這就是是當你運行這個命令時的結果。

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

這會連接 index.js 作為一個可執行文件。這是完全可能的,因為這個 CLI 腳本的***行是 #!/usr/bin/env node

現在我們可以通過執行如下命令來調用。

  1. $ creator

總結

正如你所看到的,Node.js 使得構建一個好的命令行工具變得非常簡單。如果你希望了解更多內容,查看下列包。

  • meow:一個簡單的命令行助手工具
  • yargs:一個命令行參數解析工具
  • pkg:將你的 Node.js 程序包裝在一個可執行文件中。
在評論中留下你關于構建命令行工具的經驗吧! 
責任編輯:龐桂玉 來源: Linux中國
相關推薦

2024-04-26 09:44:39

2021-04-01 13:25:46

Node命令工具

2024-07-25 08:58:16

GradioPython數據應用

2015-07-15 10:32:44

Node.js命令行程序

2025-02-25 10:40:00

圖像生成工具模型

2013-12-11 10:41:00

jQuery插件

2023-06-27 13:46:20

2023-01-10 14:11:26

2018-05-08 08:35:34

LinuxDocker 容器管理器

2023-12-18 15:02:00

PyechartsPython數據可視化工具

2022-08-22 07:26:32

Node.js微服務架構

2023-12-01 07:06:14

Go命令行性能

2016-11-29 12:25:56

Python大數據數據可視化

2023-10-12 16:37:36

模型學習

2019-09-06 14:51:40

Python數據庫腳本語言

2015-07-21 16:23:22

Node.js構建分布式

2022-09-12 15:58:50

node.js微服務Web

2013-03-28 14:54:36

2012-04-18 15:36:33

HTML5Canvas交互式

2023-04-18 15:18:10

點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 九色网址| 91久久久久久久久久久久久 | 中文字幕不卡 | 中文字幕日韩欧美一区二区三区 | 成人三级网址 | 久久不卡区 | 亚洲国产aⅴ成人精品无吗 国产精品永久在线观看 | 国产精品一区二区久久 | 久久不射电影网 | 成人黄色电影在线观看 | 婷婷久久综合 | 亚洲成人一区二区 | 日韩不卡在线 | 亚洲美乳中文字幕 | 免费成人在线网站 | 日韩激情在线 | 久草在线青青草 | 中文字幕动漫成人 | 91免费电影| 欧美一级淫片免费视频黄 | 欧美精品日韩精品 | 亚洲精品日日夜夜 | 欧美一区二区大片 | 亚洲毛片 | 欧美激情国产日韩精品一区18 | 一区二区三区免费观看 | 99久视频| 国偷自产av一区二区三区 | 一级欧美一级日韩片免费观看 | 成人综合视频在线 | 二区在线观看 | 秋霞a级毛片在线看 | 国产在线播 | 日韩www | 午夜视频在线 | 鲁一鲁资源影视 | 国产一二区免费视频 | 97色在线视频| 免费观看一级特黄欧美大片 | 中文字字幕在线中文乱码范文 | 国产精品久久久久久av公交车 |