一步步建立本身的npm插件

github建立一個項目,好比建立名爲randomNickname的項目,

這個代碼庫用戶存放npm插件的代碼。javascript

首先在npm官網申請帳號,後面提交到npm官網須要用到。
mkdir npmjs
cd npmjs
npm init
根據提示輸入相應的信息,可用參考以下設置。
ubuntuvimdeMacBook-Pro:randomNichname ubuntuvim$ npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.

See `npm help json` for definitive documentation on these fields
and exactly what they do.

Use `npm install <pkg> --save` afterwards to install a package and
save it as a dependency in the package.json file.

Press ^C at any time to quit.
name: (randomNichname)
Sorry, name can no longer contain capital letters.
name: (randomNichname) randomNichname
Sorry, name can no longer contain capital letters.
name: (randomNichname) y
version: (1.0.0) 0.1.0
description: Get the name of the three kingdoms.
entry point: (index.js) y
test command:
git repository: https://github.com/ubuntuvim/randomNichname.git
keywords: nickname,random
author: ubuntuvim
license: (ISC) ISC
About to write to /Users/ubuntuvim/codes/my-npm-plugins/randomNichname/package.json:

{
  "name": "y",
  "version": "0.1.0",
  "description": "Get the name of the three kingdoms.",
  "main": "y",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/ubuntuvim/randomNichname.git"
  },
  "keywords": [
    "nickname",
    "random"
  ],
  "author": "ubuntuvim",
  "license": "ISC",
  "bugs": {
    "url": "https://github.com/ubuntuvim/randomNichname/issues"
  },
  "homepage": "https://github.com/ubuntuvim/randomNichname#readme"
}


Is this ok? (yes) yes
ubuntuvimdeMacBook-Pro:randomNichname ubuntuvim$

建立完成package.json以後你也能夠根據實際狀況作修改。html

建立npm插件所需的目錄和文件,目錄結構以下:
npmjs
├─┬ lib
│ └── npmjs.js
├─┬ test
│ └── test.js
├── .gitignore
├── .npmignore
├── .travis.yml
├── index.js
├── LICENSE
├── makefile
├── package.json
├── README.md

這些文件的做用是:java

  • lib目錄下存放業務邏輯文件
  • test目錄下存放單元測試用例
  • .npmignore記錄哪些文件不須要被髮布到npmjs.org
  • .travis.yml是持續集成服務travis的描述文件
  • index.js是入口文件
  • makefile方便咱們用make test進行測試
  • README.md是此module的描述和使用方法
編寫插件代碼

主要代碼直接放在index.js。好比本插件是用於獲取隨機的三國人物名稱,代碼以下(詳細代碼請從github下載):node

//  從1099個名字中獲取任意名字
(function () {
  'use strict';
    //  一共1099個
    var arr = [];
    //  獲取隨機名字
    function getNickname() {
        //  取0~1099的隨機數
        var random = Math.floor(Math.random() * 1099);
        if (random >= 1099)
            throw new Error("獲取人名數組下標月結!");
        return arr[random];
    }
    exports.getNickname = getNickname;
  }());
測試
var arr = require('../index');
var s = arr.getNickname();
if (s) {
    console.log(s);
} else {
    throw new Error('The test does not pass...');
}

運行測試。 node ./test/test.jsgit

在LICENSE文件中添加許可信息

許可內容請從github下載。github

編寫使用文檔README.md

插件使用方式shell

  1. 安裝插件 npm install randomNicknamenpm

  2. 使用插件json

var names = require('randomNickname');
var s = names.getNickname();
console.log('獲得的人物名字爲: ' + s);
提交代碼到github

步驟以下:ubuntu

  • git init
  • git remote add origin <git遠程URL>
  • git add *
  • git commit -am '描述信息'
  • git push origin master 若是出現相似以下錯誤
! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'https://github.com/ubuntuvim/randomNickname.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

先更新合併再提交到遠程代碼庫。

  • git pull
  • git merge origin/master
  • git push origin/master
發佈到npmjs官網

步驟以下:

  • npm adduser (輸入你在npmjs官網註冊的帳號和密碼)
  • npm publish .

若是出現錯誤: no_perms Private mode enable, only admin can publish this module 執行下列代碼重置後再執行npm addusernpm publish .

npm config set registry http://registry.npmjs.org

等待完成,看到以下信息說明發布成功。

+ randomnickname@0.1.0

若是你的代碼修改了又須要從新發布到npmjs上,則首先修改package.json裏的版本號,再執行npm publish .便可。

此時你能夠到npmjs的我的中心中查看剛剛發佈的插件。 好比個人我的中心https://www.npmjs.com/~ubuntuvim

參考文獻:

  1. http://segmentfault.com/a/1190000000491188
  2. http://blog.csdn.net/liuxiao723846/article/details/46237289
  3. http://www.lellansin.com/npm-publish-%E5%8F%91%E5%B8%83%E7%A4%BA%E4%BE%8B.html
相關文章
相關標籤/搜索