若是你想寫一個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包,版本號須要大於上一次