若是你想写一个npm插件,若是你想经过命令行来简化本身的操做,若是你也是个懒惰的人,那么这篇文章值得一看。vue
po主的上一篇文章介绍了定制本身的模版,但这样po主仍是不知足啊,项目中咱们频繁的须要新建一些页面,逻辑样式等文件,每次都手动new一个,而后复制一些基本代码进去很是的麻烦,因此就有了这篇文章。接下来就让po主为你们一步一步演示怎么作一个npm命令行插件。node
发布npm插件,首先确定要有个npm账号了,过程就不啰嗦了,走你。linux
npm官网web
有了帐号后,咱们经过npm init 生成一个package配置文件,填写一些你的信息,而后就能够开始写逻辑代码了。npm
首先看一下项目结构json
.
├── bin //命令配置
├── README.md //说明文档
├── index.js //主入口
├── src //功能文件
├── package.json //包信息
└── test //测试用例复制代码
实例命令代码都是写在bin目录下,咱们如今配置文件package文件中启用命令,添加一个配置项bin浏览器
"bin": {
"xu": "./bin/xu.js"
},复制代码
而后安装一个依赖,TJ大神写的commander插件,bash
npm i commander --save复制代码
有了这个工具咱们能够很方便的编写命令代码less
#!/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还规定一些系统环境变量。 这种写法主要是为了让你的程序在不一样的系统上都能适用。函数
在这一步,你能够简单测试你本身的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包,版本号须要大于上一次