Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

用commander.js开发自己的命令行工具(一) #23

Open
damoclesX opened this issue Feb 1, 2016 · 0 comments
Open

用commander.js开发自己的命令行工具(一) #23

damoclesX opened this issue Feb 1, 2016 · 0 comments

Comments

@damoclesX
Copy link
Owner

平时使用gulp较多,项目中除了改动gulpfile.js比较多,剩下的就是在命令行中执行命令了,感觉很方便(也感觉很高端,:))。平时项目总会做一些重复的工作,比如新建文件夹,copy需要用的js,css之类的通用文件,于是想着写个命令行工具,来完成这些工作。

github上面一番搜索,找到commander.js,感觉不错,再google到commander的两位主要贡献者的文章:
http://tj.github.io/commander.js/
http://blog.tsnrose.com/2015-01-15-intro-to-commander.js-zh-cn.html

对commander.js的用法有了基本的认识,开始动工:

1.初始化package.json,安装commander依赖

npm init
npm i commander -S

2.新建bin目录,在目录中新建index.js。创建了个create命令,命令执行的操作这里简单输出,创建成功.
第一行参看
https://cnodejs.org/topic/50f18a44df9e9fcc5824ada5

#!/usr/bin/env node 
var program = require('commander');

program
    .version('0.0.1')
    .option('-t, --type', '脚手架类型');

program
    .command('create')
    .description('创建')
    .action(() => {
        console.log('创建成功');
    });
program.parse(process.argv);

3.修改package.json,新增bin字段,安装到全局后可以使用tinman这个命令了

"bin":{
    "tinman":"bin/index.js"
}

4.发布到npm,这个就不说明了

基本结构弄好,剩下便是根据需求创建相应命令,执行相应操作,修改index.js.容我想想再写。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant