See how to achieve a simple node.js scaffolder

At work, a scaffolding is required to provide relevant developments to relevant users.

Suitable for the crowd

A certain understanding of the front end, Node operation, and the developer of a scaffolding process or requires a scaffolding.

Develop a simple scaffolding and can be provided to the user.

    It is possible to output a relevant prompt.
  1. Read and write the user file.
  2. Use the shell script in a scaffold.

Development Scaffolding

Scaffolding is the starting process and ordinary The front end project is the same, requiring an entry file command.js and profile package.json. Unlike other configuration files, you need to add one below Package.json file:

{…, “bin” : {“CM-CLI”: “Command.js”}}

After the configuration file adds this, only the NPM is only required in the root directory of the configuration file. The link command can use the cm-cli –help command to view the loaded CM-CLI scaffold.
  If you release your scaffold, you can use your scaffolding in the global use of the command NPM Install -G CM-CLI.  
Tips for the user

In prompting the comments and commands, we need to use Commander package, using npm install commander can be installed. (NPM version is less than 5 if you need to add parameters to ensure –save update package.json configuration file).

commander is to provide a user command line input and power parameters parsed. There is a need to read the relevant documentation library. Here I introduce two of the most used methods.

can be customized initialization parameter object, keywords and description provided, but also can set the read user input parameters. Used 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 ( {console.log ( ‘aaa’);} if (commander.bbb) {console.log ( ‘bbb’);} if (commander.ccc) {console.log ( ‘ccc’, commander.ccc );}

specifically shown below:

详解如何实现一个简单的Node.js脚手架 the method can increase a command line. After this command the user can execute callback logic. Used as follows:

Commander .command (‘init

‘) .description (‘init extension project’) .Action ((extensionID) => {console.log (`Init Extension Project” $ {extensionid} “`); // Todo Something YOU NEED});

The specific display is as follows:
   Read and write the user file  
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 file


Now we need to read the user’s CM-CLI.JSON configuration file for some configuration.

We can use node.js’s FS file module to read the file progress, because there is not much difficult point here, thus slightly.

Write file template

We store the template file in a cDN, and then template according to the relevant scaffolding configuration file read by the local. download.
Note: The path read in the scaffolding is the current path for the user, so there is no way to store the template file in the scaffolding.

We can use such a library such as Request to help us download, simplify the steps. Perform the NPM Install Request to install.

Note: It is recommended to determine if the file exists and then overwritten when the file is written.

Use shell scripts

and NODE.JS provides the API function, some people prefer to use the shell script to perform file operations. Fortunately, we can also introduce Node-cmd in our scaffold to enable support for the shell script. Enter NPM Install Node-CMD to install.

Specific examples:

Commander .command (‘init

‘) .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 touch tmp / source-file / index.css curl -o tmp / build-file / server.js Https:// curl -o tmp / build-file / router.js curl -o tmp / build-file / package.json HTTPS: // filename = package.json cp tmp / source-file / * static / $ {extensionid} cp tmp / build-file / * ./ rm -fr tmp npm install`/ 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 the shell script to create and file templates for folders.


Scaffolding wants to execute quickly at the terminal, can add relevant fields in the package.json configuration file.
Scaffolding needs to be able to read related terminal inputs, you can use the Commander library to quickly develop.
 Scaffolding needs to be able to perform a shell script, you can use the Node-CMD library to quickly implement demand.    The above is all the content of this article, I hope to help everyone, I hope everyone will support Tumi Clouds. 
© Copyright Notice
Just support it if you like
comment Grab the couch

Please log in to comment