Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Node.js in Linux system to build a command line tool to create files according to query

2025-03-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces how to use Node.js in the Linux system to build a command line tool based on inquiry to create files, the content is detailed and easy to understand, the operation is simple and fast, and it has a certain reference value. I believe you will have something to gain after reading this article on how to use Node.js to build a command line tool to create files based on inquiry in the Linux system. Let's take a look.

First, create a new npm [2] package (NPM is the JavaScript package manager).

Mkdir my-scriptcd my-scriptnpm initNPM will ask some questions. Then we need to install some packages.

Npm install-save chalk figlet inquirer shelljs

This is the bag we need:

◈ Chalk: correctly set the character style of the terminal ◈ Figlet: a program for making large letters using ordinary characters ◈ Inquirer: a collection of universal interactive command line user interfaces ◈ ShellJS:Node.js version of the portable Unix Shell command line tool

Create an index.js file

Now we are going to create an index.js file using the following.

#! / usr/bin/env node

"

Const inquirer = require ("inquirer")

Const chalk = require ("chalk")

Const figlet = require ("figlet")

Const shell = require ("shelljs")

Planning command line tools

It's always great to plan before we write any code we need for command-line tools. The command line tool does only one thing: create a file.

It will ask two questions: what is the file name and what is the file suffix? Then create a file and display a success message that contains the path to the created file.

/ / index.js

"

Const run = async () = > {

/ / showscript introduction

/ / ask questions

/ / create the file

/ / show success message

}

"

Run ()

The first function is just an introduction to the script. Let's use chalk and figlet to finish it.

Const init = () = > {

Console.log (

Chalk.green (

Figlet.textSync ("Node JS CLI", {

Font: "Ghost"

HorizontalLayout: "default"

VerticalLayout: "default"

})

)

);

}

"

Const run = async () = > {

/ / showscript introduction

Init ()

"

/ / ask questions

/ / create the file

/ / show success message

}

"

Run ()

Then, let's write a function to ask the question.

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 () = > {

/ / showscript introduction

Init ()

"

/ / ask questions

Const answers = await askQuestions ()

Const {FILENAME, EXTENSION} = answers

"

/ / create the file

/ / show success message

}

Note that the constants FILENAME and EXTENSIONS come from the inquirer package.

The next step is to create the file.

Const createFile = (filename, extension) = > {

Const filePath = {filename}. ${extension} "

Shell.touch (filePath)

Return filePath

}

"

/ /...

"

Const run = async () = > {

/ / showscript introduction

Init ()

"

/ / ask questions

Const answers = await askQuestions ()

Const {FILENAME, EXTENSION} = answers

"

/ / create the file

Const filePath = createFile (FILENAME, EXTENSION)

"

/ / show success message

}

Finally, importantly, we will show the success information and the file path.

Const success = (filepath) = > {

Console.log (

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

);

}

"

/ /...

"

Const run = async () = > {

/ / showscript 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)

}

Let's test the script by running node index.js, which is what we got:

Complete code

The following code is the complete code:

#! / 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 = {filename}. ${extension} "

Shell.touch (filePath)

Return filePath

}

"

Const success = filepath = > {

Console.log (

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

);

}

"

Const run = async () = > {

/ / showscript 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 ()

Use this script

To execute this script elsewhere, add a bin section to your package.json file and execute 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"

}

}

Execute npm link so that this script can be called anywhere.

This is what happens when you run this command.

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

/ usr/lib/node_modules/creator-> / home/hugo/code/creator

This connects the index.js as an executable. This is entirely possible because the first line of the CLI script is #! / usr/bin/env node.

Now we can invoke it by executing the $creator command.

This is the end of the article on "how to use Node.js to build a command-line tool for creating files based on inquiry in a Linux system". Thank you for reading! I believe that everyone has a certain understanding of "how to use Node.js to build a command line tool to create documents according to inquiry in the Linux system". If you want to learn more, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report