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:
download-git-repo
The 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.ora
You can understand Text Loading, DynamicThe original link.handlebars
Aid, a plug-in that enables you to understand the separation of data and viewsThe original link.figlet
Make your log cool? ! ☛The original link.clear
Clear the CMD panel.chalk
Example: chalk. Blue (‘Hello world! ‘); So you see how it works.open
Literally.
Start the configuration
- configuration
package.json
file
"bin": {
"base": "./bin/base.js"
}
Copy the code
Create instructions and execute files
- 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
- 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:
- 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.
- 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
- 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