编写并发布npm包

Posted by yuk on 2016-05-10

想把自己实现的在koa框架下用canvas绘制的验证码库发布到npm社区。于是翻阅资料动手。
最终地址 https://www.npmjs.com/package/koa-canvas-captcha
github地址 https://github.com/yukrain/koa-canvas-captcha

创建repository

现在github新建一个仓库,用来托管代码,方便讨论提交bug。开源的魅力。

1
$ git clone https://github.com/yukrain/koa-canvas-captcha.git

在得到一个只有readme的空项目, 接下来开始初始化项目

初始化package.json

依葫芦画瓢,执行命令npm init系统会提示你输入所需的信息,不想输入的直接输入Enter可以跳过。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
$ cd koa-canvas-captcha
$ npm init

This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sane 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: (node) koa-canvas-captcha
version: (0.0.0) 0.1.0
description: A captcha library for koa framework
entry point: (index.js)
test command: make test
git repository: https://github.com/yukrain/koa-canvas-captcha.git
keywords: node koa coptcha canvas
author: yuk
license: (BSD-2-Clause) MIT

输入完成之后,如果没有问题直接输入yes就可以了。
生成package.json文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
{
"name": "koa-canvas-captcha",
"version": "0.1.4",
"description": "A captcha library for koa framework",
"main": "index.js",
"scripts": {
"test": "mocha --harmony test.js"
},
"repository": {
"type": "git",
"url": "https://github.com/yukrain/koa-canvas-captcha.git"
},
"keywords": [
"canvas",
"coptcha",
"koa",
"node"
],
"author": "yuk",
"license": "MIT",
"bugs": {
"url": "https://github.com/yukrain/koa-canvas-captcha/issues"
},
"homepage": "https://github.com/yukrain/koa-canvas-captcha",
"dependencies": {
},
"devDependencies": { }
}

核心代码

省略,具体查看https://github.com/yukrain/koa-canvas-captcha/blob/master/index.js
注意:项目依赖canvas@1.1.6 其他版本会有各种bug.

1
2
3
4
5
npm install canvas@1.1.6
```

### 配置npm账户
现在www.npmjs.org 上注册一个账号,并加入本地配置

$ npm adduser
Username: your name
Password: your password
Email: yourmail@gmail.com

1
2

成功之后可以查询当前登录用户:

$ npm whoami

1
### 发布模块

$ @koa-canvas-captcha$ npm publish –tag 0.1.0

  • koa-canvas-captcha@0.1.0
    1
    2
    ### 语义化版本号
    npm社区版本号规则采用的是semver(语义化版本),主要规则如下:

全选复制放进笔记版本格式:主版号.次版号.修订号,版号递增规则如下:
主版号:当你做了不相容的 API 修改,
次版号:当你做了向下相容的功能性新增,
修订号:当你做了向下相容的问题修正。
先行版号及版本编译资讯可以加到「主版号.次版号.修订号」的后面,作为延伸。

1
2
3
至此 已经成功发布npm包了!!!!
### 试试安装自己的包!
在其他项目中引入试试吧

$ npm install koa-canvas-captcha –save

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
### 持续集成
看到别人的项目有个绿色标志,感觉酷炫.
该怎么做呢?
目前Github已经整合了持续集成服务travis,我们只需要在项目中添加`.travis.yml`文件,在下一次push之后,travis就会定时执行npm test来测试你的项目,并且会在测试失败的时候通知到你,你也可以把项目当前的状态显示在README.md中,让人一目了然,比如
[![Build Status](https://travis-ci.org/yukrain/koa-canvas-captcha.svg?branch=master)](https://travis-ci.org/yukrain/koa-canvas-captcha)

#### 授权travis.org
使用github账户登录https://travis-ci.org
点击Account > yukrain/koa-canvas-captcha 旁的开关打开

#### 添加并配置.travis.yml
因为需要依赖canvas, 一部分配置直接从node-canvas项目中找到
```
language: node_js
node_js:
- "0.12"
addons:
apt:
sources:
- ubuntu-toolchain-r-test
packages:
- libcairo2-dev
- libjpeg8-dev
- libpango1.0-dev
- libgif-dev
- build-essential
- g++-4.8
env:
- CXX=g++-4.8
sudo: false
script: "npm test"
```
#### 提交代码
以后每一次提交代码都会自动触发测试

### 编写测试用例
因这个库小到只有一个文件,测试也就只有 test.js了。
先安装
```
$ npm install mocha --save-dev
$ npm install chai --save-dev
```
因为库需要es6支持,需要另一个库
```
$ npm install mocha-generators --save-dev
```
使用

require(‘mocha-generators’).install();

1
具体省略,查看`https://github.com/yukrain/koa-canvas-captcha/blob/master/test.js`

npm test

1
2
3
4
5
6
7
8
9
10
11
12
13

### 最终文件目录
```
├── .npmignore //记录哪些文件不需要被发布到npmjs.org
├── .travis.yml //是持续集成服务travis的描述文件
├── LICENSE.txt
├── README.md //此module的描述和使用方法
├── font
│   └── captchaFont.ttf //验证码字体
├── index.js //因为项目简单,只有一个文件
├── node_modules
├── package.json
└── test.js //测试文件