If you are interested, you can send their accumulation function library or tool on NPM, and then install to your computer terminals, so that it can use the cool, I took some of the items that are commonly used before scaffolding template On the company’s internal network NPM, this article is to introduce how to publish, and set up a NPM toolkit.

The process for publishing NPM packages

Take private storage as an example

  1. Registry: NPM config set @vipabc:registry http://npm.registry.*****.com/

  2. NPM login –registry=http://npm.registry.*****.com.cn/ –scope= @itutorGroup (follow the instructions later) or: npm adduser –registry=http://npm.registry.*****.com.cn/ –always-auth

  3. NPM publish (package.json: “name”: “@*****/templateName”)

  4. NPM unpublish [<@scope>/]< PKG >[@

    ] //

Usage:

Here I take the CRM-Li written by myself as an example

  1. If it is the Intranet NPM of the company, switch the source first
  2. npm i crm-cli -g
  3. Check the versioncrm --version / V
  4. View all commandscrm -help / h
  5. View the list of templatescrm list
  6. Initialization is performed based on template Acrm init a project-name

An NPM scaffold template

rendering

Core Node code section

The explanation is in the notes section

#! /usr/bin/env node
// Javascript scripts executed using node command-line tools must have #! At the top. The/usr/bin/env node statement

const program = require('commander') // node command-line processing tool 🔧
const handlebars = require('handlebars') // The template engine handles strings
const inquirer = require('inquirer') // Use this module to interact with the command line
const fs = require('fs') // The fs module built into Node.js is the file system module, responsible for reading and writing files
const ora = require('ora') // THE ORA package is used to display loading effects, similar to the loading effects of the front-end page
const chalk = require('chalk') // Use to change the string in the console to the style
const logSymbols = require('log-symbols')
const download = require('download-git-repo') // Download the git repository code at 🔧
const templates = require('./config')

// 1. Obtain the user input command
program.version('1.0.1')

program
  .command('init <template> <project>')
  .description('Initialize the project template')
  .action(function (templateName, projectName) {
    /** * Download a template based on the template name to a local directory
    const spinner = ora('Downloading templates... ')
    spinner.start()

    /** * the first parameter: storage address * second parameter: download path */
    const { downloadUrl } = templates[templateName]
    download(downloadUrl, projectName, { clone: true }, (err) = > {
      if (err) {
        spinner.fail('Failed to download template') // Download failed
        console.log(logSymbols.error, chalk.red('Failed to initialize template' + err))
        return false
      }

      spinner.succeed('Template download successful') // Download success message

      /** * Read the package.json file from the project * use the wizard to collect the user input * Use the template engine to parse the user input data into the package.json file * Parsed. Write the parsed results back to package.json */
      inquirer
        .prompt([
          {
            type: 'input'.name: 'name'.message: 'Please enter project name :'}, {type: 'input'.name: 'description'.message: 'Please enter project profile :'}, {type: 'input'.name: 'author'.message: 'Please enter author name :',
          },
        ])
        .then((answers) = > {
          // Parse and replace the collected user input data into package.json file
          const packagePath = `${projectName}/package.json`
          const packageContent = fs.readFileSync(packagePath, 'utf8')
          const packageResult = handlebars.compile(packageContent)(answers)
          fs.writeFileSync(packagePath, packageResult)
          console.log(logSymbols.success, chalk.yellow('Template initialization successful'))
        })
    })
  })

program
  .command('list')
  .description('View all available templates')
  .action(() = > {
    // Download the corresponding template based on the template name and name it projectName
    console.log('Template List:')
    for (let key in templates) {
      console.log(`${templates[key].name} ${templates[key].description}`)
    }
  })

program.parse(process.argv)

Copy the code