這篇文章給大家分享的是有關(guān)node如何通過npm寫一個cli命令行工具的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
創(chuàng)新互聯(lián)成立于2013年,我們提供高端重慶網(wǎng)站建設(shè)公司、網(wǎng)站制作、成都網(wǎng)站設(shè)計、網(wǎng)站定制、成都營銷網(wǎng)站建設(shè)、小程序開發(fā)、微信公眾號開發(fā)、成都網(wǎng)站推廣服務(wù),提供專業(yè)營銷思路、內(nèi)容策劃、視覺設(shè)計、程序開發(fā)來完成項目落地,為辦公空間設(shè)計企業(yè)提供源源不斷的流量和訂單咨詢。
怎么做一個npm命令行插件。
注冊npm賬戶
發(fā)布npm插件,首先肯定要有個npm帳號了,過程就不啰嗦了,走你。
npm官網(wǎng)
有了賬號后,我們通過npm init 生成一個package配置文件,填寫一些你的信息,然后就可以開始寫邏輯代碼了。
編寫命令入口
首先看一下項目結(jié)構(gòu)
. ├── bin //命令配置 ├── README.md //說明文檔 ├── index.js //主入口 ├── src //功能文件 ├── package.json //包信息 └── test //測試用例
實例命令代碼都是寫在bin目錄下,我們現(xiàn)在配置文件package文件中啟用命令,添加一個配置項bin
"bin": { "xu": "./bin/xu.js" },
然后安裝一個依賴,TJ大神寫的commander插件,
npm i commander --save
有了這個工具我們可以很方便的編寫命令代碼
xu.js
#!/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可以在系統(tǒng)的PATH目錄中查找。同時,env還規(guī)定一些系統(tǒng)環(huán)境變量。 這種寫法主要是為了讓你的程序在不同的系統(tǒng)上都能適用。
在這一步,你可以簡單測試你自己的npm插件
$ node ./bin/xu.js >>> 輸出一些插件usage。help信息
關(guān)于commander,大家可以去作者的Github先學(xué)習(xí)了解,這里不對參數(shù)講解。
xu-generate.js
#!/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); });返回一個函數(shù),這個函數(shù)就是我們定義這個命令需要做什么事。
編寫功能函數(shù)
./src/generate.js
這個文件就定義了當(dāng)我們輸入
$ 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文件導(dǎo)出了一個run函數(shù)給 xu-generate.js調(diào)用,我們通過參數(shù)拿到了用戶輸入的type,name,然后就可以根據(jù)type通過node fs模塊(這里用了一個依賴,不過原理還是fs)操作把template文件復(fù)制了一份到你的項目中。
到這,我們就已經(jīng)完成了一個命令的開發(fā),這個命令可以快速生成項目的模版文件。
本地測試
npm包開發(fā)不像web開發(fā),可以直接在瀏覽器看,實例目錄下建立一個test文件,再 node test 就可以測試我們的邏輯。如果有一些功能需要在發(fā)布后才能測,npm 有個 link命令 可以連接你本地的模塊,當(dāng)然你也可以發(fā)布后 自己安裝插件測試,就跟平時引入一個插件一樣。
發(fā)布npm包
首先在項目根目錄執(zhí)行npm登陸
$ npm login $ npm publish
如果這里有個報錯,可能是你使用了cnpm地址,需要把npm倉庫設(shè)置回來
$ npm config set registry https://registry.npmjs.org/
然后,更新更新npm包,版本號需要大于上一次
感謝各位的閱讀!關(guān)于“node如何通過npm寫一個cli命令行工具”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
網(wǎng)頁標(biāo)題:node如何通過npm寫一個cli命令行工具
當(dāng)前鏈接:http://www.chinadenli.net/article6/ieocig.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站排名、網(wǎng)站設(shè)計、標(biāo)簽優(yōu)化、定制開發(fā)、微信小程序、企業(yè)建站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)