Self_cli_520 fully understand scaffolding CLI implementation

The scaffolding is customized based on NPM such as Commander and Download-Git-repo.

The main dependencies involved

  • Commander Command line parsing
  • Download-git-repo Downloads the project
  • Ora load loading
  • Chalk text style
  • Inquirer gets command line interaction
  • HandleBars compiles file packages

Video to understand

First self_cli_520 scaffolding | | write scaffolding scaffolding involves package

Implementation function (command)

  • Self_cli_520 – h | — help to see help
  • Self_cli_520 | — – V version to check the version number
  • Self_cli_520 list Displays all templates
  • Self_cli_520 init Initializes the item

Installation steps

npm install self_cli_520

The effect

1. self_cli_520 --version

2. self_cli_520 list

3. self_cli_520 --help

4. self_cli_520 init my_template

Code implementation

All of the following code

// Command line parsing
let program = require('commander');
// Download the project
let download = require('download-git-repo')
/ / load loading
let ora = require('ora')
// Text style
let chalk = require('chalk')
// Get command line interaction
let inquirer = require('inquirer')
// Compile the package
let handleBars = require('handlebars')
// Manipulate the package
let fs = require('fs')
let spinner = ora()

// Obtain the CLI version
let {version} = require('./package.json')
program
    .version(version)

// Scaffold corresponding template
let templates = [
  {key: 'Visual backend System Front End'.value: 'http://github.com:FrontWalker2000/self-vue-sys#master'},
  {key: 'Visual backend System Back End'.value: 'http://github.com:FrontWalker2000/node-sys#master'},
  {key: 'Custom Function libraries/plug-ins'.value: 'http://github.com:FrontWalker2000/self-function#master'},]// Select project template download
let selectTemplate = async (templateName) => {
  let res = await inquirer.prompt([
    {
      type: 'list'.name: 'choice'.message: 'Please select the project to initialize :'.default: 'http://github.com:FrontWalker2000/self-vue-sys#master'.choices: templates
    }
  ])
  // Get the template selected by the user
  let {choice} = res
  / / loading condition
  spinner.start('Pulling project template')
  downloadTemplate(choice, templateName)
}
// Download the template
let downloadTemplate = (url, project) = > {
  download(url, project, {clone: true}, async (err) => {
    if (err) {
      spinner.fail()
      return console.log(err)
    }
    spinner.succeed('Template downloaded successfully, waiting for project initialization! ')
    // Initialize package.json command line interaction my
    let answers = await inquirer.prompt([
      {
        type: 'input'.message: 'Please set project name :'.name: 'name'.default: "self-vue-sys" / / the default value
      },
      {
        type: 'input'.message: 'Please set project author :'.name: 'author'.default: "yangjialin" / / the default value
      },
      {
        type: 'input'.message: 'Please set project description :'.name: 'description'.default: "vue simple of sys" / / the default value
      },
      {
        type: 'confirm'.message: 'Please set the project to private :'.name: 'private'.default: true / / the default value}])const path = `${project}/package.json`
    / / read package. Json
    let packCont = fs.readFileSync(path, "utf8")
    let compileCont = handleBars.compile(packCont)(answers)
    // Write again
    fs.writeFileSync(path, compileCont)
    spinner.succeed(chalk.green('Initialization template is complete, thank you for your hard work! '))})}// Initialize the command line (self_cli_520 init my_template)
program
    .command('init <project>')
    .description('Select project Template')
    .action((project) = > {
      // Download the template
      selectTemplate(project)
    })

// Initialize the command line (self_cli_520 init my_template)
program
    .command('list')
    .description('View all available templates')
    .action(() = > {
      for (let key in templates) {
        console.log(templates[key])
      }
    })

program
    .command(The '*')
    .description('Cannot find corresponding command')
    .action(() = > {
      spinner.fail(chalk.red('Cannot find corresponding command! '))})// Parse the command line
program.parse(process.argv)
Copy the code

Published to the NPM

- NPM login login - username enter the username - password enter the password - NPM publish publishCopy the code

Note: Pay attention to the package name and version number before release.

Making the address

Self_cli_520 scaffolding implementation

Write in the last

No difficulty, the most or more to see, more involved.