Initialize a folder (for example, file name: base-cli)

npm init
Copy the code

Installing a plug-in

npm i commander download-git-repo ora handlebars figlet clear chalk open -s
Copy the code

Plug-in introduction:

  1. download-git-repoThe function is to download and extract a Git repository (e.g. GitHub, GitLab, Bitbucket) from an incoming connection. More details are found in thisThe original link.
  2. oraYou can understand Text Loading, DynamicThe original link.
  3. handlebarsAid, a plug-in that enables you to understand the separation of data and viewsThe original link.
  4. figletMake your log cool? ! ☛The original link.
  5. clearClear the CMD panel.
  6. chalkExample: chalk. Blue (‘Hello world! ‘); So you see how it works.
  7. openLiterally.

Start the configuration

  1. configurationpackage.jsonfile
"bin": {
  "base": "./bin/base.js"
 }
Copy the code

Create instructions and execute files

  1. Create directive (Node directive)
const program = require("commander");
// Commander is inspired by Ruby, which provides powerful functionality for user command line input and parameter parsing to help simplify command-line development.
program.version(require(".. /package.json").version);

program
  .command("init <name>") // The directive created here is base init XXX
  .description("init project") // Here is the description of the directive
  .action(require(".. /lib/init")); // This is where the execution file is located

program.parse(process.argv);
Copy the code
  1. Start configuring the init file
const { promisify } = require("util");
const figlet = promisify(require("figlet"));
const clear = require("clear");
const chalk = require("chalk");
const log = (content) = > console.log(chalk.green(content));
module.exports = async (name) => {
  clear();
  const data = await figlet("base cli");
  log(data);
};
Copy the code

Copy the above code into init.js and run the following command. Once you’re done, you know what you’ve done.

// This command is actually a connection reference, linking the NPM module to the corresponding running project
npm link
// Start executing the base.js command.
base init project
Copy the code

After execution, the result is shown as follows:

  1. Formally configure the init file
const { promisify } = require("util");

const clone = async (urlRepo, desc) => {
  // Download the git plug-in
  const download = promisify(require("download-git-repo"));
  const ora = require("ora");
  const process = ora('Downloading...${urlRepo}`);
  process.start();
  await download(urlRepo, desc);
  process.succeed();
};

const spawnFn = async(... args) => {/* child_process.spawn(command[, args][, options]) -command: command to run -args: string parameter list. - options: indicates the options where CWD can specify the current working directory of the child process. Detached The detached process can be set to true or false. If this is true, the child process will run independently of the parent process and can continue executing after the parent process finishes. Stdio is used to configure pipes between child and parent processes */
  const { spawn } = require("child_process");
  return new Promise((resolve) = > {
    constproc = spawn(... args); proc.stdout.pipe(process.stdout); proc.stderr.pipe(process.stderr); proc.on("close".() = > {
      resolve();
    });
  });
};

module.exports = async (name) => {
  log(name);
  clear();
  log("Start creating project, pull Git project");
  await clone("github:moshubai/node-master", name);
  log("Install dependency");
  Child_process. Spawn (command[, args][, options])
  await spawnFn("yarn"["install"] and {cwd: `. /${name}` });
};
Copy the code

!!!!!!!!! Await spawn (‘yarn’, [‘install’], {CWD: /{name} ‘}) ‘this code will cause a BUG on Windows because it is not sure which file to execute. There are two executable files, so it is difficult to select. `await spawnFn(process.platform === ‘win32’ ? ‘yarn. CMD’ : ‘yarn, [‘ install’] and {CWD: `. / {name}}). Mac ignored, here a W alpaca a pentium.

  1. Once the dependencies are installed, it’s time to start running automatically.
log(Installation complete);
const open = require("open");
open("http://localhost:8080");
await spawnFn("yarn"["serve"] and {cwd: `. /${name}` });
Copy the code
  1. End, as shown in figure:

conclusion

Learning Node, can not only contact deeper language, but also convenient handwriting of various commands soSO, more importantly, as a front-end, in terms of performance optimization is very important, not just write, you have to make your code 100 meters faster.

The source code to place