|
| 1 | +# Develop A Package |
| 2 | + |
| 3 | +--- |
| 4 | + |
| 5 | +You can follow the steps to develop a package using [spm](https://github.com/spmjs/spm). |
| 6 | + |
| 7 | +``` |
| 8 | +$ spm --version |
| 9 | +3.6.0 |
| 10 | +``` |
| 11 | + |
| 12 | +Make sure that [email protected] is installed. |
| 13 | + |
| 14 | +## init |
| 15 | + |
| 16 | +```bash |
| 17 | +$ mkdir now && cd now |
| 18 | +$ spm init |
| 19 | +``` |
| 20 | + |
| 21 | +``` |
| 22 | +Creating a spm package: |
| 23 | +[?] Package name: (now) |
| 24 | +[?] Version: (1.0.0) |
| 25 | +[?] Description: hello world |
| 26 | +[?] Author: pigcan <[email protected]> |
| 27 | +
|
| 28 | +Initialize a spm package Succeccfully! |
| 29 | +``` |
| 30 | + |
| 31 | +Then you have a package named `now`. |
| 32 | + |
| 33 | +## Install dependencies |
| 34 | + |
| 35 | +Install default devDependencies first. |
| 36 | + |
| 37 | +```bash |
| 38 | +$ spm install |
| 39 | +``` |
| 40 | + |
| 41 | +We need [moment](http://momentjs.com) as a dependency which can be found [here](http://spmjs.io/package/moment). |
| 42 | + |
| 43 | +```bash |
| 44 | +$ spm install moment --save |
| 45 | +``` |
| 46 | + |
| 47 | +## Code and Debug |
| 48 | + |
| 49 | +Edit `index.js` as follow, just like nodejs. |
| 50 | + |
| 51 | +```javascript |
| 52 | +// require module in spm.dependencies |
| 53 | +var moment = require('moment'); |
| 54 | + |
| 55 | +// require relative file in you project |
| 56 | +// var util = require('./util'); |
| 57 | + |
| 58 | +var now = moment().format('MMMM Do YYYY, h:mm:ss a'); |
| 59 | +module.exports = now; |
| 60 | +``` |
| 61 | + |
| 62 | +Then edit `examples/index.md`: |
| 63 | + |
| 64 | +<pre> |
| 65 | +# Demo |
| 66 | + |
| 67 | +--- |
| 68 | + |
| 69 | +## Normal usage |
| 70 | + |
| 71 | +````javascript |
| 72 | +var now = require('../index'); |
| 73 | +console.log(now); |
| 74 | +```` |
| 75 | +</pre> |
| 76 | + |
| 77 | + |
| 78 | +Run `spm doc` to start a documentation service at `127.0.0.1:8000` . |
| 79 | + |
| 80 | +```bash |
| 81 | +$ spm doc |
| 82 | +``` |
| 83 | + |
| 84 | +Open [http://127.0.0.1:8000/examples/](http://127.0.0.1:8000/examples/) in browser to see the result. |
| 85 | + |
| 86 | +Except using three ` in Markdown file, you can also use four ` to wrap your code. |
| 87 | + |
| 88 | +It is a special rule that make your code highlighted and would be inserted to document page as a script block so they can be excuted at the same time. That is very useful for debugging your demo and writing a beautiful documentation both. |
| 89 | + |
| 90 | +If you want to insert a iframe in your demo, make your code to `iframe` type. |
| 91 | + |
| 92 | +<pre> |
| 93 | +````iframe:600 |
| 94 | +I am in a iframe of 600px high |
| 95 | +```` |
| 96 | +</pre> |
| 97 | + |
| 98 | + |
| 99 | +> If you don't want to debug your code by `spm doc`, you can try [spm-webpack-server](https://github.com/spmjs/spm-webpack-server) to debug `CommonJS` modules in development. |
| 100 | +
|
| 101 | +## Add Test Case |
| 102 | + |
| 103 | +Edit test file at `tests/now-spec.js`. We introduce a default assert solution [expect.js](http://spmjs.io/package/expect.js). |
| 104 | + |
| 105 | +```javascript |
| 106 | +var expect = require('expect.js'); |
| 107 | +var now = require('../index'); |
| 108 | + |
| 109 | +describe('now', function() { |
| 110 | + |
| 111 | + it('normal usage', function() { |
| 112 | + expect(now).to.be.a('string'); // add this |
| 113 | + }); |
| 114 | + |
| 115 | +}); |
| 116 | +``` |
| 117 | + |
| 118 | +See tests result. |
| 119 | + |
| 120 | +```bash |
| 121 | +$ spm test |
| 122 | +``` |
| 123 | + |
| 124 | +You can also open [http://127.0.0.1:8000/tests/runner.html](http://127.0.0.1:8000/tests/runner.html) in browser. |
| 125 | + |
| 126 | +## Publish |
| 127 | + |
| 128 | +Now you have a great package having wonderful function and complete tests case, you can publish the package to [spmjs.io](http://spmjs.io/). |
| 129 | + |
| 130 | +```bash |
| 131 | +$ spm publish |
| 132 | +``` |
| 133 | + |
| 134 | +You should run `spm login` first to get permission, otherwise it would propmt the authorization problem. |
| 135 | + |
| 136 | +```bash |
| 137 | +$ spm login |
| 138 | +``` |
| 139 | + |
| 140 | +`username` is the name of your github account, and `authkey` can be found at http://spmjs.io/account after signing in. |
| 141 | + |
| 142 | +> The package `now` is published by me, of cause. You should change other name and retry. |
| 143 | +
|
| 144 | +## Documentation |
| 145 | + |
| 146 | +spmjs.io can host your package's documentation. What all you need to do is editing `README.md` and `examples` folder, preview it by `spm doc watch`, then publish it to spmjs.io. |
| 147 | + |
| 148 | +```bash |
| 149 | +$ spm doc publish |
| 150 | +``` |
| 151 | + |
| 152 | +The documentation url is `http://docs.spmjs.io/{{name}}/` for latest version and `http://docs.spmjs.io/{{name}}/{{version}}/` for each versions. |
| 153 | + |
| 154 | +For example, http://docs.spmjs.io/now/. |
| 155 | + |
| 156 | +## Build |
| 157 | + |
| 158 | +```bash |
| 159 | +$ spm build |
| 160 | +``` |
| 161 | + |
| 162 | +This command will build the files indicated by `spm.main` and `spm.output` field to `dist` folder. The `spm.build` would be executed as arguments. |
| 163 | + |
| 164 | +The default build result is a package which could be deployed at cdn. Then you can use it .For example. |
| 165 | + |
| 166 | +**In your html** |
| 167 | +```html |
| 168 | +<script> |
| 169 | + var now = require('now'); |
| 170 | + console.log(now); |
| 171 | +</script> |
| 172 | +``` |
| 173 | +**In your project package.json** |
| 174 | + |
| 175 | +```javascript |
| 176 | +{ |
| 177 | + "name": "Project-Name", |
| 178 | + "spm": { |
| 179 | + "output": ["index.js"], |
| 180 | + "dependencies": { |
| 181 | + "now": "1.0.0" |
| 182 | + } |
| 183 | + }, |
| 184 | + "devDependencies": { |
| 185 | + "spm": "~3.6.0" |
| 186 | + }, |
| 187 | + "scripts": { |
| 188 | + "build": "spm build" |
| 189 | + } |
| 190 | +} |
| 191 | +``` |
| 192 | +## Congratulation |
| 193 | + |
| 194 | +Now you learn how to develop a package using spm, welcome to publish your packages here! |
0 commit comments