Home>Article>Web Front-end> Node practical development of a mycli command line tool
This article will take you step by step to understand anodeactual combat, and talk about how to make a mycli command line tool/scaffolding based on node. I hope it will be helpful to everyone!
First make sure there is anode.jsenvironment on your computer
Execute the following code on the command line to initialize apackage.json
file
npm init -y
At this time, an error will definitely be reported when the command line executesmycli
.
Configure custom commands
package.json
Addbin
field, associatedmycli
command
"bin": { "mycli": "./test.js" },
/ test.js
Fileconsole.log("mycli命令执行成功");
install
installation command, but the project has not yet been published to npm, so usenpm link## for the time being. #Command, associate the
myclicommand to the global world.
mycli, no more errors will be reported.
Script configuration
test.js file:console.log("mycli命令执行成功");Then execute
mycli, an error pop-up window will appear
myclicommand, it is equivalent to Let the computer execute this file, but the computer system
cannot directly execute the js file. This requires us to add a configuration to the first line of the script code to specify thenode.js## on the computer. #Program to execute this js script file.
Due to changing the execution environment, you need to delete the previously linked files. The file location may be#!/usr/bin/env node
. Findmycli
and delete it. , and then executenpm link
again.Now execute
on the console, and you can see that the console prints correctly.
Chalk
Installnpm install chalk@4.1.2 -S
const chalk = require("chalk"); // chalk // const hello = chalk.red("hello"); // const hello = chalk.blue.bgRed("hello"); // const hello = chalk.blue.bgYellow("hello"); const hello = chalk.rgb(200, 200, 200).bgRgb(0, 200, 3)("hello"); console.log(hello);
Installation
npm install ora@4.0.3 -S
const ora = require("ora"); // ora const spinner = ora({ text: "安装中..." }); spinner.start(); setTimeout(() => { // spinner.stop(); spinner.succeed("安装成功"); // console.log("安装成功"); }, 2000)
stop
succeed
##commander
Commands commonly used in development, such asvue -V
git --versionvue Create
and other commands, if you want to implement such commands, you need to use thecommander
library.The-V
etc. after the command used can be understood as the parameters of the command. Then we need to obtain these parameters and judge by Different parameters are used to handle different events.In the
node
environment, you can get this parameter through
. Thecommanderlibrary helps us encapsulate some methods without us having to judge the instructions carried by the user's input.Installation
npm install commander@8.2.0 -S
const commander = require("commander"); // ... commander.parse(process.argv); // 放在后面
, let’s test it below:
mycli --help
provides a method to set the version number
commander.version("1.0.0");
version number.Custom command method
commander.option(command name, description, callback function)
Some of the functions written above are configured to the
--init
commander.option("--init", "this is init", () => { // chalk // const hello = chalk.red("hello"); // const hello = chalk.blue.bgRed("hello"); // const hello = chalk.blue.bgYellow("hello"); const hello = chalk.rgb(200, 200, 200).bgRgb(0, 200, 3)("hello"); console.log(hello); // ora const spinner = ora({ text: "安装中..." }); spinner.start(); setTimeout(() => { // spinner.stop(); spinner.succeed("安装成功"); // console.log("安装成功"); }, 1000) })
Now execute the
mycli --initcommander.option("--number", "log a number", (num) => { console.log(num); })
表示必传的参数,
[参数名]
表示非必传的参数。控制台输入mycli --number 100
回车,可以看到会输出100
。
自定义命令方法
commander.command("create").action((projectName)=>{ console.log(projectName); })
执行mycli create xx
回车,控制台可以看到 输出了xx
。
查看帮助
执行mycli --help
,可以看到我们刚才配置的指令和命令都出现在了帮助列表里。
inquirer
npm install inquirer -S
prompt
提问的方法inquirer.prompt([ { type: "input", name: "username", message: "请输入用户名:" } ]).then((answer)=>{ })
type
表示问题的类型,取值可能是:input
,number
,password
,editor
等。
answer
是{username: 输入的值}
input
const inquirer = require("inquirer"); commander.command("add user").action(() => { inquirer.prompt([ { type: "input", name: "username", message: "请输入用户名:" } ]).then((answer) => { console.log(answer); }) })
confirm
commander.command("testcon").action(() => { inquirer.prompt([ { type: "confirm", name: "age", message: "是否大于18岁?" } ]).then((answer) => { console.log(answer); }) })
输入y
或n
来进行判断。
list
commander.command("testlist").action(() => { inquirer.prompt([ { type: "list", name: "lib", message: "选择用到的框架:", choices: [ "vue2", "vue3", "react", "svelte", ] } ]).then((answer) => { console.log(answer); }) })
执行mycli testlist
命令:
download-git-repo是一个拉取代码的工具。
安装
npm install download-git-repo@3.0.2 -S
const downgit = require("download-git-repo"); downgit("github:kongcodes/vue3-vant", downUrl, { clone: false }, function (err) { console.log(err) })
downgit
方法里面的第一个参数理解为在github上面下载kongcodes用户的vue3-vant
项目模板。第二个参数downUrl
为要将模板下载到什么目录下。第三个参数clone
是否要用git clone
下载。第四个参数 为下载完成执行的一些事情。
command
方法使用commander.command("create").action((projectName) => { const spinner = ora({ text: "正在下载https://github.com/kongcodes/vue3-vant..." }); spinner.start(); fs.mkdirSync(`./${projectName}`); const downUrl = `${process.cwd()}\\${projectName}`; downgit("github:kongcodes/vue3-vant", downUrl, { clone: false }, function (err) { if (err) throw err; spinner.stop(); console.log(chalk.green("downgit success")); }) })
执行mycli create pro
回车,会在当前目录下创建pro
目录,下载vue3-vant
模板到这个目录里。
https://github.com/kongcodes/mycli
更多node相关知识,请访问:nodejs 教程!
The above is the detailed content of Node practical development of a mycli command line tool. For more information, please follow other related articles on the PHP Chinese website!