Describes how to implement a simple Node. js scaffold, and describes node. js scaffolding.
Cause
In the work, a scaffold needs to be developed to provide users with convenient development.
Target Audience
Developers who have a certain understanding of front-end and Node operations and learn about the scaffolding development process or need to implement a scaffold by themselves.
Target
- Develop a simple scaffold that can be provided to users for installation.
- Provides related prompts.
- Read and Write user files.
- Use Shell scripts in scaffolding.
Procedure
Development scaffolding
The initial process of scaffolding development is the same as that of common front-end projects. An entry file command. js and the configuration file package. json are required.
Unlike other configuration files, you need to add one in the package. json file:
{ ..., "bin": { "cm-cli": "command.js" }}
After this option is added to the configuration file, you only need to execute the npm link command in the root directory of the configuration file to view the loaded cm-cli scaffold using the cm-cli -- help Command.
If you release your scaffolding, you can use it globally after other users use the command npm install-g cm-cli.
Prompt users
In the comments and command prompts, we need to use the commander package and use npm install commander to install it. (If NPM version is earlier than 5, you must add the -- save parameter to ensure that the package. json configuration file is updated ).
Commander is a powerful function that provides command line input and Parameter Parsing. If necessary, read the relevant library documentation. Here I will introduce the two most used methods.
Option
You can initialize custom parameter objects, set keywords and descriptions, and set parameters for reading user input. The 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 details are as follows:
Command
This method can add a command in the command line. After you execute this command, you can execute the logic in the callback. The 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 results are as follows:
Read and Write User Files
Through the above steps, we have been able to complete a simple scaffold. Below, we need to read the user configuration and generate some template files for the user.
Read files
Now we need to read the user's cm-cli.json profile for some configuration.
We can use the fs file module of Node. js to read the file progress. because there are not many difficulties, we can skip this step.
Write File Template
We store the template file on CDN in advance, and then download the template based on the scaffolding configuration file read locally.
Note: The Path read from scaffolding is the current path used by the user, so there is no way to store the template file in scaffolding for reading.
We can use a library such as request to help us download files and simplify the operation steps. Run npm install request to install the SDK.
Note: When writing a file, we recommend that you first determine whether the file exists and then overwrite it.
Use Shell scripts
According to the API functions provided by Node. js, some people prefer to use Shell scripts for file operations. Fortunately, we can also introduce node-cmd in our scaffolding to enable support for Shell scripts. Run npm install node-cmd to install the tool.
Example:
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 download file templates.
Summary
To quickly execute scaffolding on the terminal, you can add related fields to the package. json configuration file.
Scaffolding needs to be able to read related terminal input, and can be quickly developed using the commander library.
Scaffolding needs to be able to execute Shell scripts. You can use the node-cmd library to quickly implement the requirements.
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.