CLI has analyzed

Command Line Interface is a tool or application to realize some functions by running some codes through the Command Line, such as VUE-CLI, webpack and so on used in the process of front-end development. Using them can reduce some low-level repetitive labor in development, or standardize the development workflow. Improve development efficiency.

Functional goals

  • 1. Newman Create XXX: command line initialization projects, one-click generation of application templates;
  • 2. Newman Refresh: Automatic route generation;
  • 3. Newman publish “./dist” : upload front-end package in dist directory to Ali cloud OSS;

(Of course, other files can also be uploaded, which requires opening ali Cloud OSS service)

Development steps

1. Initialize the project

npm init -y
Copy the code

2. Set the bin import file in package.json

/ / package. Json {" name ":" Newman - cli ", "version" : "1.0.1", "description" : ""," main ":" index. Js ", "scripts" : {" test ": "echo \"Error: no test specified\" && exit 1" }, "bin": { "newman": "./bin/index.js" }, "keywords": [], "author": "" and" license ", "ISC", "dependencies" : {" commander ":" ^ 4.0.1 ", "download - git - repo" : "^ 3.0.2", "handlebars" : "^ 4.5.3 ora", ""," ^ 4.0.3 "}} / /. / bin/index, js #! /usr/bin/env node // The first line prompts the operating system to run the file with nodeCopy the code

3. Run NPM link

Connects the current application entry to the specified system location to make it available globally

4. Business function development

The contents of./bin/index.js are as follows. You can use commander to invoke different operations using different commands. Git clone github.com/Amy-Tong126…

  • Create a project:

You can download the library code from Github via download-git-repo.

  • Route generation function:

It is to read the components under the page folder under the current SRC, and then according to the routing template file, through handlebars library to generate routing configuration file.

  • Upload and publish functions:

Is through ali-OSS SDK to upload local files;

#! /usr/bin/env node
const program = require("commander");
const { create, refresh, publish } = require(".. /lib/api");

program.version(require(".. /package").version)
console.log(process.argv)
// Create the project
program
    .command("create <name>")
    .description("create your project")
    .action(name= > {
        create(name);
    })
// Refresh automatically updated routes
program
    .command('refresh')
    .description('refresh routers... ')
    .action(refresh)
// Upload to oss
program
    .command('publish <filePath>')
    .description('upload assets to CDN and git commit && push')
    .action((filePath) = > {
        console.log("Upload package");
        publish(filePath)
    });

program.parse(process.argv);
Copy the code

5. Publish to NPM

  • 1. Reset the address of taobao mirror before;
  • 2. Login NPM
  • 3. Publish
  • 4. Reset the Taobao image

Note that you need to register your NPM account and confirm by email before Posting.

#!/usr/NPM config set registry= HTTP://registry.npmjs.org
echo 'Please perform line login related operations :'NPM login # echo"-------publishing-------"NPM publish # config set registry= HTTPS:/ / registry.npm.taobao.org # set to taobao mirror echo "issued to complete"
exit
Copy the code

conclusion

Inspiration comes from a CLI tool I came into contact with in recent business, which can create projects and publish projects with one click. At the same time, I have recently created a low version of THE CLI by sorting out nodeJS learning materials. I would like to record it and hope it can be improved in the future.