若是你想写一个npm插件,若是你想经过命令行来简化本身的操做,若是你也是个懒惰的人,那么这篇文章值得一看。前端
po主的上一篇文章介绍了定制本身的模版,但这样po主仍是不知足啊,项目中咱们频繁的须要新建一些页面,逻辑样式等文件,每次都手动new一个,而后复制一些基本代码进去很是的麻烦,因此就有了这篇文章。接下来就让po主为你们一步一步演示怎么作一个npm命令行插件。vue
发布npm插件,首先确定要有个npm账号了,过程就不啰嗦了,走你。node
npm官网linux
有了帐号后,咱们经过npm init 生成一个package配置文件,填写一些你的信息,而后就能够开始写逻辑代码了。git
首先看一下项目结构github
. ├── bin //命令配置 ├── README.md //说明文档 ├── index.js //主入口 ├── src //功能文件 ├── package.json //包信息 └── test //测试用例
实例命令代码都是写在bin目录下,咱们如今配置文件package文件中启用命令,添加一个配置项binweb
"bin": { "xu": "./bin/xu.js" },
而后安装一个依赖,TJ大神写的commander插件,vue-cli
npm i commander --save
有了这个工具咱们能够很方便的编写命令代码npm
#!/usr/bin/env node process.title = 'xu'; require('commander') .version(require('../package').version) .usage('<command> [options]') .command('generate', 'generate file from a template (short-cut alias: "g")') .parse(process.argv) require('./xu-generate'); >>引入
这个文件能够看做是入口文件,第一行代码是必须添加的,脚本用env启动的缘由,是由于脚本解释器在linux中可能被安装于不一样的目录,env能够在系统的PATH目录中查找。同时,env还规定一些系统环境变量。 这种写法主要是为了让你的程序在不一样的系统上都能适用。json
在这一步,你能够简单测试你本身的npm插件
$ node ./bin/xu.js >>> 输出一些插件usage。help信息
关于commander,你们能够去做者的Github先学习了解,这里不对参数讲解。
#!/usr/bin/env node const program = require('commander'); const chalk = require('chalk') const xu = require('../src/generate'); /** * Usage. */ program .command('generate') .description('quick generate your file') .alias('g') .action(function(type, name){ xu.run(type, name); }); program.parse(process.argv);
这就是功能命令,定义了一个generate命令,.alias('g')是该命令的缩写,而后.action(function(type, name){
xu.run(type, name);
});返回一个函数,这个函数就是咱们定义这个命令须要作什么事。
这个文件就定义了当咱们输入
$ xu g
所作的操做了。
/** * Created by xushaoping on 17/10/11. */ const fs = require('fs-extra') const chalk = require('chalk') exports.run = function(type, name) { switch (type) { case 'page': const pageFile = './src/page/' + name + '/' + name + '.vue' const styleFile = './src/page/' + name + '/' + name + '.less' fs.pathExists(pageFile, (err, exists) => { if (exists) { console.log('this file has created') } else { fs.copy('/usr/local/lib/node_modules/vue-xu-generate/src/template/page.vue', pageFile, err => { if (err) return console.error(err) console.log(pageFile + ' has created') }) fs.copy('/usr/local/lib/node_modules/vue-xu-generate/src/template/page.less', styleFile, err => { if (err) return console.error(err) console.log(styleFile + ' has created') }) } }) break; case 'component': const componentFile = './src/components/' + name + '.vue' fs.pathExists(componentFile, (err, exists) => { if (exists) { console.log('this file has created') } else { fs.copy('/usr/local/lib/node_modules/vue-xu-generate/src/template/component.vue', componentFile, err => { if (err) return console.error(err) console.log(componentFile + ' has created') }) } }) break; case 'store': const storeFile = './src/store/modules' + name + '.js' fs.pathExists(storeFile, (err, exists) => { if (exists) { console.log('this file has created') } else { fs.copy('/usr/local/lib/node_modules/vue-xu-generate/src/template/store.js', storeFile, err => { if (err) return console.error(err) console.log(storeFile + ' has created') }) } }) break; default: console.log(chalk.red(`ERROR: uncaught type , you should input like $ xu g page demo` )) console.log() console.log(' Examples:') console.log() console.log(chalk.gray(' # create a new page')) console.log(' $ xu g page product') console.log() console.log(chalk.gray(' # create a new component')) console.log(' $ xu g component product') console.log() console.log(chalk.gray(' # create a new store')) console.log(' $ xu g store product') console.log() break; } };
这里有2个新的依赖,分别是命令输出颜色和一个文件操做的插件,经过npm安装。
$ npm i fs-extra --save $ npm i chalk --save
这个js文件导出了一个run函数给 xu-generate.js调用,咱们经过参数拿到了用户输入的type,name,而后就能够根据type经过node fs模块(这里用了一个依赖,不过原理仍是fs)操做把template文件复制了一份到你的项目中。
到这,咱们就已经完成了一个命令的开发,这个命令能够快速生成项目的模版文件。
npm包开发不像web开发,能够直接在浏览器看,实例目录下创建一个test文件,再 node test 就能够测试咱们的逻辑。若是有一些功能须要在发布后才能测,npm 有个 link命令 能够链接你本地的模块,固然你也能够发布后 本身安装插件测试,就跟平时引入一个插件同样。
首先在项目根目录执行npm登录
$ npm login $ npm publish
若是这里有个报错,多是你使用了cnpm地址,须要把npm仓库设置回来
$ npm config set registry https://registry.npmjs.org/
而后,更新更新npm包,版本号须要大于上一次
至此,一个入门级的npm包就制做完成了。万分感慨,记得刚入门前端的时候看到别人的插件作的真牛,本身只要简单安装一下就能搞得那么漂亮,想搞~可是看到一堆陌生的东西,马上怂了(node环境,东西很是很是多,直接拷个vue-cli看到一对代码,一头雾水。。。大牛请无视)
学习是一个按部就班的过程,大牛写出来的东西,没有必定的基础,和长时间的积累经验,源码是很难学习。非要啃,也行,只是效率感受不如按部就班来的好。
插件已经发布,Github也有完整源码,想学习的同窗能够fork一个本身玩玩,干这一行~随心所动 ,跟着兴趣走,准没错
传送门: npm地址
传送门: github源码
若是以为本文对你有所帮助,就star一下吧~大传送之术! 个人博客Github
前端❤️~越学越感受本身的无知哎~