Home Web Front-end JS Tutorial How to implement Node.js scaffolding (detailed tutorial)

How to implement Node.js scaffolding (detailed tutorial)

Jun 22, 2018 pm 05:38 PM

This article mainly introduces how to implement a simple Node.js scaffolding. The editor thinks it is quite good. Now I will share it with you and give you a reference. Let’s follow the editor and take a look

Reason

At work, it is necessary to develop a scaffolding to provide relevant development conveniences 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

  1. Develop a simple scaffolding that can be provided to users for installation.

  2. can output relevant prompts.

  3. Perform reading and writing operations on user files.

  4. Use shell scripts in scaffolding.

Steps

Developing scaffolding

The initial process of scaffolding development is similar to that of ordinary front-end projects Same, an entry file command.js and a configuration file package.json are required.

Different from other configuration files, you need to add the following item to the package.json file:

{
 ...,
 "bin": {
    "cm-cli": "command.js"
  }
}
Copy after login

After adding this item to the configuration file, you only need to add it to the root of the configuration file. Execute the npm link command in the directory, and then 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

Able 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);
}
Copy after login

The specific display is as follows:

command

This method can be added on the command line An order. After the user executes this command, he can execute the logic in the callback. The specific usage is as follows:

commander
  .command(&#39;init <extensionId>&#39;)
  .description(&#39;init extension project&#39;)
  .action((extensionId) => {
    console.log(`init Extension Project "${extensionId}"`);
    // todo something you need
  });
Copy after login

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 download the template based on the relevant scaffolding configuration file read locally.

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(&#39;init <extensionId>&#39;)
  .description(&#39;init extension project&#39;)
  .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(&#39;init success&#39;);
          return;
        }

        console.error(&#39;init error&#39;);
      });
  });
Copy after login
We can quickly use Shell scripts to create folders and download file templates.

Summary

If you want scaffolding to be executed quickly on the terminal, you can add relevant fields to the package.json configuration file.

The scaffolding needs to be able to read relevant terminal input, and can use the commander library for rapid development.

Scaffolding needs to be able to execute Shell scripts, and you can use the node-cmd library to quickly implement the requirements.

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

How to make a cone using JS canvas

##Summary of JS sorting algorithm

About the loop mechanism of js events (detailed tutorial)

How to use javascript to implement QQ space photo album display

How to customize PC WeChat scan code login

Details about the use of jade template engine

About JS Hill sorting Algorithm (detailed tutorial)

The above is the detailed content of How to implement Node.js scaffolding (detailed tutorial). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What should I do if I encounter garbled code printing for front-end thermal paper receipts? What should I do if I encounter garbled code printing for front-end thermal paper receipts? Apr 04, 2025 pm 02:42 PM

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

Who gets paid more Python or JavaScript? Who gets paid more Python or JavaScript? Apr 04, 2025 am 12:09 AM

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

Demystifying JavaScript: What It Does and Why It Matters Demystifying JavaScript: What It Does and Why It Matters Apr 09, 2025 am 12:07 AM

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

How to merge array elements with the same ID into one object using JavaScript? How to merge array elements with the same ID into one object using JavaScript? Apr 04, 2025 pm 05:09 PM

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

How to achieve parallax scrolling and element animation effects, like Shiseido's official website?
or:
How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? How to achieve parallax scrolling and element animation effects, like Shiseido's official website? or: How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? Apr 04, 2025 pm 05:36 PM

Discussion on the realization of parallax scrolling and element animation effects in this article will explore how to achieve similar to Shiseido official website (https://www.shiseido.co.jp/sb/wonderland/)...

The difference in console.log output result: Why are the two calls different? The difference in console.log output result: Why are the two calls different? Apr 04, 2025 pm 05:12 PM

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...

Is JavaScript hard to learn? Is JavaScript hard to learn? Apr 03, 2025 am 12:20 AM

Learning JavaScript is not difficult, but it is challenging. 1) Understand basic concepts such as variables, data types, functions, etc. 2) Master asynchronous programming and implement it through event loops. 3) Use DOM operations and Promise to handle asynchronous requests. 4) Avoid common mistakes and use debugging techniques. 5) Optimize performance and follow best practices.

How to implement panel drag and drop adjustment function similar to VSCode in front-end development? How to implement panel drag and drop adjustment function similar to VSCode in front-end development? Apr 04, 2025 pm 02:06 PM

Explore the implementation of panel drag and drop adjustment function similar to VSCode in the front-end. In front-end development, how to implement VSCode similar to VSCode...

See all articles