How to implement a simple Node.js scaffolding
This article mainly introduces how to implement a simple Node.js scaffolding. I hope everyone will have a better grasp of Node.js.
At work, a scaffolding needs to be developed to provide relevant development convenience to relevant users.
Suitable for people
Have a certain understanding of front-end and Node operations, and are also interested in developers who understand the scaffolding development process or need to implement a scaffolding themselves.
Goal
Develop a simple scaffolding that can be provided to users for installation.
can output relevant prompts.
Perform reading and writing operations on user files.
Use shell scripts in scaffolding.
Steps
Develop the scaffolding
The beginning of the development of the scaffolding The process is the same as that of a normal front-end project, requiring an entry file command.js and a configuration file package.json.
Different from other configuration files, you need to add the following item to the package.json file:
{ ..., "bin": { "cm-cli": "command.js" } }
After adding this item to the configuration file, you only need to execute the npm link command in the root directory of the configuration file, and you can use the cm-cli --help command to view the loaded cm-cli scaffolding.
If you publish your scaffolding, then other users can use your scaffolding globally after using the command npm install -g cm-cli.
Prompt users
In prompting comments and commands, we need to use the commander package, which can be installed using npm install commander. (If the NPM version is lower than 5, you need to add the --save parameter to ensure that the package.json configuration file is updated).
commander is a powerful function that provides user command line input and parameter parsing. If necessary, you can read the relevant library documentation. Here I introduce the two most commonly used methods.
#option
Ability to initialize custom parameter objects, set keywords and descriptions, and also set parameters for reading user input. The specific usage is as follows:
const commander = require('commander'); commander.version('1.0.0') .option('-a, --aaa', 'aaaaa') .option('-b, --bbb', 'bbbbb') .option('-c, --ccc [name]', 'ccccc') .parse(process.argv); if (commander.aaa) { console.log('aaa'); } if (commander.bbb) { console.log('bbb'); } if (commander.ccc) { console.log('ccc', commander.ccc); }
The specific usage is as follows:
command
This method can add a command to the command line. After the user executes this command, he can execute the logic in the callback. The specific usage is as follows:
commander .command('init <extensionId>') .description('init extension project') .action((extensionId) => { console.log(`init Extension Project "${extensionId}"`); // todo something you need });
The specific display effect is as follows:
Read and write user files
Through the above steps, we have been able to complete a simple scaffolding. Next, we need to read the user configuration and generate some template files for the user.
Read file
Now, we need to read the user’s cm-cli.json configuration file to perform some configuration.
We can use the fs file module of Node.js to read the file progress. Since there are not many difficulties here, we will omit it.
Write file template
We store the template file on the CDN in advance, and then proceed with the template based on the relevant scaffolding configuration files read locally. download.
Note: The path read in the scaffold is the current path when the user uses it, so there is no way to store the template file in the scaffold for reading.
We can use libraries such as request to help us download files and simplify the operation steps. Execute npm install request` to install.
Note: When writing a file, it is recommended to first determine whether the file exists before overwriting it.
Using Shell Script
Judging from the API functions provided by Node.js, some people prefer to use Shell scripts to perform file operations. Fortunately, we can also introduce node-cmd into our scaffolding to enable support for shell scripts. Execute npm install node-cmd to install.
Specific examples are as follows:
commander .command('init <extensionId>') .description('init extension project') .action((extensionId) => { id = extensionId; console.log(`init Extension Project "${extensionId}"`); cmd.get( ` mkdir -p static/${extensionId} mkdir tmp mkdir tmp/source-file mkdir tmp/build-file curl -o tmp/source-file/index.js https://xxxxxxxx.com?filename=index.js touch tmp/source-file/index.css curl -o tmp/build-file/server.js https://xxxxxxxx.com?filename=server.js curl -o tmp/build-file/router.js https://xxxxxxxx.com?filename=router.js curl -o tmp/build-file/package.json https://xxxxxxxx.com?filename=package.json cp tmp/source-file/* static/${extensionId} cp tmp/build-file/* ./ rm -fr tmp npm install `, (err, data) => { console.log(data) if (!err) { console.log('init success'); return; } console.error('init error'); }); });
We can quickly use Shell scripts to create folders and files Template download.
If you want the scaffolding to be executed quickly in the terminal, you can add relevant fields to the package.json configuration file.
Scaffolding needs to be able to read relevant terminal input, and can use the commander library for rapid development.
The scaffolding needs to be able to execute Shell scripts, and you can use the node-cmd library to quickly implement the requirements.
The above content is how to implement a simple Node.js scaffolding. I hope it will be helpful to everyone.
Related recommendations:
How to use node.js to write a stream createWriteStream
node.js publish-subscribe mode method
The above is the detailed content of How to implement a simple Node.js scaffolding. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The hard disk serial number is an important identifier of the hard disk and is usually used to uniquely identify the hard disk and identify the hardware. In some cases, we may need to query the hard drive serial number, such as when installing an operating system, finding the correct device driver, or performing hard drive repairs. This article will introduce some simple methods to help you check the hard drive serial number. Method 1: Use Windows Command Prompt to open the command prompt. In Windows system, press Win+R keys, enter "cmd" and press Enter key to open the command

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).
