preface

In the development process, especially when a new project is started, it is often necessary to re-configure various kinds of code, and even copy the old code. To solve this situation, there is the CLI. For example, the VUE-CLI of Vue and create-react-app of React are commonly used. The main purpose of such tools is to free up hands and speed up project development.

My scene

In this article, I’ll show you how I can speed up the development of a project by writing a CLI based on the tedious scenarios within our team.

The most common backend management system in a development project is the backend management system, and if you look closely, you’ll see that most backend management systems are pretty much the same.

In terms of our team’s internal background management system, we need to perform the following operations every time we open a new project:

  • Build project: Copy the previous project to change, or create a new project, and then copy the files as required
  • When adding pages, you need to create a lot of files (views, routes, Vuex)

And every page is pretty much the same:

  • Tables of data (search, paging, other operations, etc.)
  • The form

Implementation approach

Based on the above scenario, the solution I came up with was to write a CLI tool that could implement the following functions with a single command:

  • Initialize the project
  • Add or delete page

The CLI alone is not enough, of course, but is also used with project templates.

However, be careful that the scaffolding tool is decoupled from the project template, as discussed below.

Project template

Create a GitHub organization to host project templates. A template should contain the following:

  1. meta.json

    Customize some questions for the project, such as:

    • The project name
    • Project description
  2. The template folder

    Store project templates and generate projects according to the questions answered by users during initialization.

  3. The generator folder

    When executing CLI commands, they are called by the CLI and used to define problems and template file information.

CLI

Main commands:

  1. Init:

    Initialize the project, which queries the list of project templates, initializes based on the template selected by the user (downloading the project template locally), and generates a template.json record of the name of the current project template for future use

  2. Add, delete,

    This command must be used in the generated project. Based on the template information in template.json, the system finds the method in the generator directory and executes the method.

Write in the last

Each team faces a different scenario, so if you want to write your own CLI tool, I hope this article gives you some ideas.

If you are interested, please refer to my CLI implementation: fa-cli and project template: fa-web-template

Thanks for watching.