本篇内容介绍了“怎么实现一个Node.js-CLI开发工具”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
创新互联建站,专注为中小企业提供官网建设、营销型网站制作、响应式网站开发、展示型成都网站建设、成都做网站等服务,帮助中小企业通过网站体现价值、有效益。帮助企业快速建站、解决网站建设与网站营销推广问题。
初始化项目
首先,我们要打开CLI界面,用npm初始化一个项目:
npm init
package.json
{ "name": "mycli", "version": "1.0.0", "description": "A cli-demo", "main": "index.js", "author": "xmanlin", "license": "MIT"}里面一些用不上都已经去掉。
自定义命令
相信很多小伙伴在使用 npm 初始化项目的时候,或者在用 Vue CLI 搭建项目的时候,会发现它们有一个共同点——都有自己个性化的命令,感觉有点酷炫。那么我们怎么才能实现自己的命令呢,很简单,在 package.json 添加 bin :
{ "name": "mycli", "version": "1.0.0", "description": "A cli-demo", "main": "index.js", "bin": {"mycli": "./index.js"
}, "author": "xmanlin", "license": "MIT"}package.json中的 bin 的作用就是可以让设置的 mycli 成为一个可执行命令,而命令所执行的文件就是后面的 index.js ,这些都可以根据自己的想法来定。接着我们要继续对 index.js 进行修改:
index.js
#!/usr/bin/env nodeconsole.log("执行成功")这里的第一行很重要,这里表明 index.js 是 node 可执行文件。
设置完成后,就可以全局安装我们的CLI工具了:
npm install -g
+ mycli@1.0.0added 1 package from 1 contributor in 0.12s
可以看见全局安装成功,最后我们试试我们的命令:
mycli
输出:
执行成功
交互式命令行
刚刚小伙伴们在进行命令行操作的时候,使用了不同选项的命令,例如 npm init 、 npm install -g ,其中包含一些交互式的,比如在初始化项目时, npm init 提供一些输入问答形式的命令。接下来,我们就来为自己的CLI工具添加这些类似的命令。
我们可以依赖两个库进行我们的开发:commander.js和Inquirer.js
这两个库的具体用法,这里就过多的介绍,小伙伴们可以点击上面名字的链接去熟悉一下,花不了太多时间,实际用起来也不难,后面那个没有中文Readme,但是不妨碍大家会搜索呀~对不对。
定义选项
我们首先来实现类似于 npm -v、node -v这样类似的命令选项。
首先安装commander.js:
npm install commander
然后引入 commander.js 并对 index.js 进行修改
#!/usr/bin/env nodeconst { program } = require('commander');// 字符串分割为数组的方法function strToArr(value, preValue){return value.split(',')
}// cli版本program.version(require('./package').version, '-v, --version', 'cli的最新版本');// 设置选项program
.option('-d, --debug', '调试一下')
.option('-l, --list ', '把字符串分割为数组', strToArr)
.action((options, command) => {// 进行逻辑处理if(options.debug) {console.log("调试成功")
}if(options.list !== undefined) {console.log(options.list)
}
});// 处理命令行输入的参数program.parse(process.argv);我们来试试刚刚设置的命令选项:
mycli -d
输出:
调试成功
输入:
mycli -l 1,2,3
输出:
[ '1', '2', '3' ]
在commander.js里面已经给我们定义好了 --help 选项:
mycli -h
输出:
Usage: index [options]
Options:
-v, --version cli的最新版本
-d, --debug 调试一下
-l, --list 把字符串分割为数组
-h, --help display help for command
利用 --help 选项,我们可以很清楚的了解到 mycli 中已有多少命令。
设置子命令
在项目开发中,我们有时会用到类似于 npm run xxx 这样的命令,其中run就相当于npm的子命令。这里我们也可以给mycli设置类似的子命令:
const { program } = require('commander');
...// 创建文件命令行program
.command('create ')
.description('创建一个文件')
.action((filename) => {console.log(filename)
})
...// 处理命令行输入的参数program.parse(process.argv);command('create ') 就是创建了一个 mycli 的 create 子命令,后面跟了一个必填参数。
输入:
mycli create file
输出
file
子命令创建成功。
利用命令行创建项目文件
我们现在能够定义选项,设置命令了。接下来我们就可以实际做点东西,利用命令行来创建项目的文件。
简单的设计一个流程:

创建模板文件
我们先来创建一个templates文件夹,然后在里面写几个常见的模板文件,这里的模板文件运用到了模板字符串,结构如下:
![模板文件]()
reactClass.js
module.exports = function (className) {return `import * as React from 'react';export class ${className} extends React.Component{constructor(props){super(props);this.state = {}
}componentDidMount(){
}render() {return ()
}
}`
}vueTemplate.js
module.exports = function () {
return `
基本
文件
流程
错误
SQL
调试
- 请求信息 : 2026-05-23 18:37:11 HTTP/1.1 GET : /article/gdcoec.html
- 运行时间 : 0.0432s ( Load:0.0011s Init:0.0006s Exec:0.0295s Template:0.0121s )
- 吞吐率 : 23.15req/s
- 内存开销 : 504.91 kb
- 查询信息 : 12 queries 5 writes
- 文件加载 : 36
- 缓存信息 : 0 gets 0 writes
- 配置加载 : 130
- 会话信息 : SESSION_ID=ic0sqbia78dr6ahjhnmpnghj64
- /home/wwwroot/bluegullmediac/wwwroot/index.php ( 1.09 KB )
- /home/wwwroot/bluegullmediac/wwwroot/ThinkPHP/ThinkPHP.php ( 4.61 KB )
- /home/wwwroot/bluegullmediac/wwwroot/ThinkPHP/Library/Think/Think.class.php ( 12.26 KB )
- /home/wwwroot/bluegullmediac/wwwroot/ThinkPHP/Library/Think/Storage.class.php ( 1.37 KB )
- /home/wwwroot/bluegullmediac/wwwroot/ThinkPHP/Library/Think/Storage/Driver/File.class.php ( 3.52 KB )
- /home/wwwroot/bluegullmediac/wwwroot/ThinkPHP/Mode/common.php ( 2.82 KB )
- /home/wwwroot/bluegullmediac/wwwroot/ThinkPHP/Common/functions.php ( 53.56 KB )
- /home/wwwroot/bluegullmediac/wwwroot/ThinkPHP/Library/Think/Hook.class.php ( 4.01 KB )
- /home/wwwroot/bluegullmediac/wwwroot/ThinkPHP/Library/Think/App.class.php ( 13.49 KB )
- /home/wwwroot/bluegullmediac/wwwroot/ThinkPHP/Library/Think/Dispatcher.class.php ( 14.79 KB )
- /home/wwwroot/bluegullmediac/wwwroot/ThinkPHP/Library/Think/Route.class.php ( 13.36 KB )
- /home/wwwroot/bluegullmediac/wwwroot/ThinkPHP/Library/Think/Controller.class.php ( 11.23 KB )
- /home/wwwroot/bluegullmediac/wwwroot/ThinkPHP/Library/Think/View.class.php ( 7.59 KB )
- /home/wwwroot/bluegullmediac/wwwroot/ThinkPHP/Library/Behavior/BuildLiteBehavior.class.php ( 3.68 KB )
- /home/wwwroot/bluegullmediac/wwwroot/ThinkPHP/Library/Behavior/ParseTemplateBehavior.class.php ( 3.88 KB )
- /home/wwwroot/bluegullmediac/wwwroot/ThinkPHP/Library/Behavior/ContentReplaceBehavior.class.php ( 1.91 KB )
- /home/wwwroot/bluegullmediac/wwwroot/ThinkPHP/Conf/convention.php ( 11.15 KB )
- /home/wwwroot/bluegullmediac/wwwroot/App/Common/Conf/config.php ( 2.16 KB )
- /home/wwwroot/bluegullmediac/wwwroot/ThinkPHP/Lang/zh-cn.php ( 2.55 KB )
- /home/wwwroot/bluegullmediac/wwwroot/ThinkPHP/Conf/debug.php ( 1.49 KB )
- /home/wwwroot/bluegullmediac/wwwroot/App/Home/Conf/config.php ( 0.31 KB )
- /home/wwwroot/bluegullmediac/wwwroot/App/Home/Common/function.php ( 3.33 KB )
- /home/wwwroot/bluegullmediac/wwwroot/ThinkPHP/Library/Behavior/ReadHtmlCacheBehavior.class.php ( 5.62 KB )
- /home/wwwroot/bluegullmediac/wwwroot/App/Home/Controller/ArticleController.class.php ( 6.02 KB )
- /home/wwwroot/bluegullmediac/wwwroot/App/Home/Controller/CommController.class.php ( 1.60 KB )
- /home/wwwroot/bluegullmediac/wwwroot/ThinkPHP/Library/Think/Model.class.php ( 60.11 KB )
- /home/wwwroot/bluegullmediac/wwwroot/ThinkPHP/Library/Think/Db.class.php ( 32.43 KB )
- /home/wwwroot/bluegullmediac/wwwroot/ThinkPHP/Library/Think/Db/Driver/Pdo.class.php ( 16.74 KB )
- /home/wwwroot/bluegullmediac/wwwroot/ThinkPHP/Library/Think/Cache.class.php ( 3.83 KB )
- /home/wwwroot/bluegullmediac/wwwroot/ThinkPHP/Library/Think/Cache/Driver/File.class.php ( 5.87 KB )
- /home/wwwroot/bluegullmediac/wwwroot/ThinkPHP/Library/Think/Template.class.php ( 28.16 KB )
- /home/wwwroot/bluegullmediac/wwwroot/ThinkPHP/Library/Think/Template/TagLib/Cx.class.php ( 22.40 KB )
- /home/wwwroot/bluegullmediac/wwwroot/ThinkPHP/Library/Think/Template/TagLib.class.php ( 9.16 KB )
- /home/wwwroot/bluegullmediac/wwwroot/App/Runtime/Cache/Home/7540f392f42b28b481b30614275e4e55.php ( 18.20 KB )
- /home/wwwroot/bluegullmediac/wwwroot/ThinkPHP/Library/Behavior/WriteHtmlCacheBehavior.class.php ( 0.97 KB )
- /home/wwwroot/bluegullmediac/wwwroot/ThinkPHP/Library/Behavior/ShowPageTraceBehavior.class.php ( 5.24 KB )
- [ app_init ] --START--
- Run Behavior\BuildLiteBehavior [ RunTime:0.000009s ]
- [ app_init ] --END-- [ RunTime:0.000042s ]
- [ app_begin ] --START--
- Run Behavior\ReadHtmlCacheBehavior [ RunTime:0.000059s ]
- [ app_begin ] --END-- [ RunTime:0.000084s ]
- [ view_parse ] --START--
- [ template_filter ] --START--
- Run Behavior\ContentReplaceBehavior [ RunTime:0.000113s ]
- [ template_filter ] --END-- [ RunTime:0.000157s ]
- Run Behavior\ParseTemplateBehavior [ RunTime:0.009370s ]
- [ view_parse ] --END-- [ RunTime:0.009415s ]
- [ view_filter ] --START--
- Run Behavior\WriteHtmlCacheBehavior [ RunTime:0.000123s ]
- [ view_filter ] --END-- [ RunTime:0.000143s ]
- [ app_end ] --START--
- 1064:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') LIMIT 1' at line 1
[ SQL语句 ] : SELECT `id`,`pid`,`navname` FROM `cx_nav` WHERE ( id= ) LIMIT 1
- 1064:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') LIMIT 1' at line 1
[ SQL语句 ] : SELECT `id`,`navname` FROM `cx_nav` WHERE ( id= ) LIMIT 1
- 1064:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1
[ SQL语句 ] : SELECT `id`,`navname` FROM `cx_nav` WHERE ( pid= )
- [8] Undefined index: pid /home/wwwroot/bluegullmediac/wwwroot/App/Home/Controller/ArticleController.class.php 第 47 行.
- [8] Undefined index: db_host /home/wwwroot/bluegullmediac/wwwroot/ThinkPHP/Library/Think/Db.class.php 第 120 行.
- [8] Undefined index: db_port /home/wwwroot/bluegullmediac/wwwroot/ThinkPHP/Library/Think/Db.class.php 第 121 行.
- [8] Undefined index: db_name /home/wwwroot/bluegullmediac/wwwroot/ThinkPHP/Library/Think/Db.class.php 第 122 行.

0.0432s
