千家信息网

Linux系统中怎么使用Node.js构建根据询问创建文件的命令行工具

发表于:2025-02-02 作者:千家信息网编辑
千家信息网最后更新 2025年02月02日,这篇文章主要介绍了Linux系统中怎么使用Node.js构建根据询问创建文件的命令行工具的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇Linux系统中怎么使用Node.j
千家信息网最后更新 2025年02月02日Linux系统中怎么使用Node.js构建根据询问创建文件的命令行工具

这篇文章主要介绍了Linux系统中怎么使用Node.js构建根据询问创建文件的命令行工具的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇Linux系统中怎么使用Node.js构建根据询问创建文件的命令行工具文章都会有所收获,下面我们一起来看看吧。

首先,创建一个新的 npm[2] 包(NPM 是 JavaScript 包管理器)。

mkdir my-scriptcd my-scriptnpm 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. // showscript 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. // showscript 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. // showscript 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 ={filename}.${extension}"

  3. shell.touch(filePath);

  4. return filePath;

  5. };

  6. "

  7. // ...

  8. "

  9. const run = async () => {

  10. // showscript 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. // showscript 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 ={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. // showscript 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。

现在我们可以通过执行$ creator命令来调用。

关于"Linux系统中怎么使用Node.js构建根据询问创建文件的命令行工具"这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对"Linux系统中怎么使用Node.js构建根据询问创建文件的命令行工具"知识都有一定的了解,大家如果还想学习更多知识,欢迎关注行业资讯频道。

0