最近想把自己寫的angular module(Github - sibevin/angular-tau-utils)包成package,這樣除了可以方便安裝,程式碼也比較好管理。其實一開始還真不知道要怎麼開始(繞口令…),於是就找了網路上的別人的package(Github - chieffancypants/angular-loading-bar)來偷學(open source的好處),以下是不負責筆記。
對我來說,我希望可以盡可能簡化打包package的流程,所以想要做到:
我規劃的目錄結構如下,除了bower.json, package.json與Gruntfile.js這些設定檔名字不能亂取之外,其它目錄名稱其實可以隨便取,只要在設定Gruntfile位置指對就好了。
基本上能自動生成的目錄與檔案都不應該commit到repo中,不過因為要包成package,build的目錄還是要commit。
首先我知道bower這個package management,所以自然會想包成bower package,但是在看bower的文件發現還要處理另一個npm的package。這就讓我有點困惑,這兩個到底差在哪?我又要包成哪一個?查了一下G大(bower npm),果然也有很多人跟我有同樣的問題(SO1, SO2),簡單來說:
當然不是從零開始,至少要把node js與bower裝好。這些準備好之後,接下來的步驟會先建立package root folder, bower.json與package.json這兩個檔案。
一開始先建立一個folder用來放package所有的內容(也就是目錄結構中提到的檔案與目錄),我們把這個目錄稱作package root folder,所有的操作都會在這個目錄下執行。
$ mkdir angular-tau-utils
$ cd angular-tau-utils
將source code放在src/,測試檔則是放在test/。這兩個目錄的檔案與資料夾要怎麼放也是隨個人喜好,不過如果是angular相關的程式碼,我還是習慣會將controller, service, directive等這些類別分開放置,比較清楚也方便管理。
angular-tau-utils/
├── src
│ ├── angular_tau_utils.coffee
│ ├── tau-switcher
│ │ └── services
│ │ ├── bool_switcher.coffee
│ │ ├── cycle_switcher.coffee
│ │ └── tab_switcher.coffee
│ └── tau_switcher.js.coffee
└── test
├── angualr_tau_utils.coffee
├── tau-switcher
│ └── services
│ ├── bool_switcher.coffee
│ ├── cycle_switcher.coffee
│ └── tab_switcher.coffee
└── tau_switcher.coffee
建立bower.json其實很簡單,只要執行bower init
就可以了,它會問你一些問題,填完bower.json就會幫你生好了,真方便啊。(詳細使用方式可以參考bower - Creating packages這份文件)
$ bower init
? name: angular-tau-utils
? version: 1.0.0
? description: Utilitis for AngularJS app
? main file: build/angular-tau-utils.js
? what types of modules does this package expose?: globals
? keywords: angular angularjs tau tau-utils utility
? authors: Sibevin Wang
? license: MIT
? homepage: https://github.com/sibevin/angular-tau-utils
? set currently installed components as dependencies?: No
? add commonly ignored files to ignore list?: Yes
? would you like to mark this package as private which prevents it from being accidentally published to the registry?: No
{
name: 'angular-tau-utils',
version: '1.0.0',
authors: [
'Sibevin Wang'
],
description: 'Utilitis for AngularJS app',
main: 'build/angular-tau-utils.js',
moduleType: [
'globals'
],
keywords: [
'angular',
'angularjs',
'tau',
'tau-utils',
'utility'
],
license: 'MIT',
homepage: 'https://github.com/sibevin/angular-tau-utils',
ignore: [
'**/.*',
'node_modules',
'bower_components',
'test',
'tests'
]
}
? Looks good?: Yes
bower.json的設定值可以參考bower.json specification,要注意的是某些設定值有一定的規定與建議,所以別亂取啊。其中幾個重要的設定這邊說明一下
之前提到我們還要另外準備npm package的設定檔,也就是package.json。建立package.json跟bower.json一樣簡單,只要執行npm init
就可以了,它同樣會問你一些問題,填完package.json就會幫你生好了,一樣方便啊。
$ 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 --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: (test) angular-tau-utils
version: (0.0.0) 1.0.0
description: Utilitis for AngularJS app
entry point: (index.js) build/angular-tau-utils.js
test command: grunt test
git repository: git@github.com:sibevin/angular-tau-utils.git
keywords: angular angularjs tau tau-utils utility
author: Sibevin Wang
license: (ISC) MIT
About to write to /Users/wangkaito/workspace/angular/dev/test/package.json:
{
"name": "angular-tau-utils",
"version": "1.0.0",
"description": "Utilitis for AngularJS app",
"main": "build/angular-tau-utils.js",
"scripts": {
"test": "grunt test"
},
"repository": {
"type": "git",
"url": "git@github.com:sibevin/angular-tau-utils.git"
},
"keywords": [
"angular",
"angularjs",
"tau",
"tau-utils",
"utility"
],
"author": "Sibevin Wang",
"license": "MIT",
"bugs": {
"url": "https://github.com/sibevin/angular-tau-utils/issues"
},
"homepage": "https://github.com/sibevin/angular-tau-utils"
}
Is this ok? (yes)
package.json的設定值可以參考package.json,內容跟bower.json大同小異,所以就不再贅述。
目前我們已經建立好bower與npm package的設定檔,會先建立這兩個檔案是因為在接下來的流程都會用到。下一篇會開始處理package dependence,並使用grunt建立自動化流程。