1. Everything happens for a reason

Because I built a set of mobile terminal scaffolding before and used it in many projects and landed it, many colleagues wanted to use this set of scaffolding in the later projects to save trouble, but I could not copy the project to them. So I emptied all the project contents from my scaffold, leaving only the basic scaffold contents and created a package to put in the NPM for them to pull.

2. Customize a command

The first thing you need to know is why NPM packages can be executed by typing commands on the command line. In fact, the so-called NPM package is a Node.js module package. Json file, which contains the entry file for the module. By default, it should be index.js. I prefer to change it to bin.js

{" name ":" sg - vue - cli ", "version" : "1.1.2", "description" : "mobile cli superGuid", "main" : "bin. Js", "bin" : { "superVueCli": "bin.js" }, "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "author": "Visupervi", "license": "ISC", "dependencies": {"chalk": "latest", "commander": "^6.0.0", "download-git-repo": "^3.0.2", "ora": "^5.0.0"}}Copy the code

You also need your own custom commands, so add a bin field that defines the name of the command followed by the file specifying the command execution

Bin. Js as follows

#! /usr/bin/env node

/ * * *@Author: visupervi
 * @Date: but * 2020-08-18@return:
 * @Description Scaffold file creation */


const program = require('commander');// CMD console interaction
const ora = require('ora');/ / the progress bar
const chalk = require('chalk');// Color the prompt text
const download = require('download-git-repo');// Pull the Github project
const fs = require('fs');


// Prompt style
const success = chalk.blueBright;
const error = chalk.bold.red;


const templateUrl = 'direct:https://gitee.com/visupervi/superGuild-vue-cli.git';// Github project address


program.version('1.1.2')
  .option('-i, init [name]'.'Initialize the project')
  .parse(process.argv);


// Pull the project code

console.log("program.init",program.init);
if (program.init && typeof program.init === "string") {
  const spinner = ora('Pulling template... ').start();// Start the progress bar
  download(templateUrl, program.init, {clone: true}, function (err) {
    if(! err) { spinner.succeed(success('Pull successful'));
      // Change the name and version number in package.json
      changePackage()
    } else {
      console.log(err);
      spinner.fail('Pull failed'); }}); }else {
  console.error(error('Please enter the directory name after init'));
}


// Replace the name field in the template package.json file
const changePackage = () = > {
  fs.readFile(`${process.cwd()}/${program.init}/package.json`.(err, data) = > {
    if (err) throw err;
    let _data = JSON.parse(data.toString());
    _data.name = program.init;
    _data.version = '1.0.0';
    let str = JSON.stringify(_data, null.4);
    fs.writeFile(`${process.cwd()}/${program.init}/package.json`, str, function (err) {
      if (err) throwerr; })}); };Copy the code

Release 3.

First login NPM, NPM login and then NPM publish

Use 4.

Installation: NPM I superguid-vue-cli create project: superguid-vue-cli -I test Install dependencies based on scaffolding: yarn install/yarn install Run: yarn run dev / npm run dev