Skip to content
This repository was archived by the owner on Mar 10, 2020. It is now read-only.
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 840298f

Browse files
committedNov 23, 2015
merge latest master
2 parents 85942b9 + 863b201 commit 840298f

29 files changed

+900
-498
lines changed
 

‎CONTRIBUTING.md

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Contributing
2+
3+
## Setup
4+
5+
You should have [node.js], [npm] and [gulp] installed.
6+
7+
## Linting
8+
9+
Linting is done using [eslint] and the rules are based on [standard]
10+
11+
```bash
12+
$ gulp lint
13+
```
14+
15+
## Tests
16+
17+
Tests in node
18+
19+
```bash
20+
$ gulp test:node
21+
```
22+
23+
Tests in the browser
24+
25+
```bash
26+
$ gulp test:browser
27+
```
28+
29+
## Building browser version
30+
31+
```bash
32+
$ gulp build
33+
```
34+
35+
## Releases
36+
37+
The `release` task will
38+
39+
1. Run a build
40+
2. Commit the build
41+
3. Bump the version in `package.json`
42+
4. Commit the version change
43+
5. Create a git tag
44+
6. Run `git push` to `upstream/master` (You can change this with `--remote my-remote`)
45+
46+
```bash
47+
# Major release
48+
$ gulp release --major
49+
# Minor relase
50+
$ gulp release --minor
51+
# Patch release
52+
$ gulp release
53+
```
54+
55+
[node.js]: https://nodejs.org/
56+
[npm]: http://npmjs.org/
57+
[gulp]: http://gulpjs.com/
58+
[eslint]: http://eslint.org/
59+
[standard]: https://github.com/feross/standard

‎dist/ipfsapi.min.js

+15-15
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎dist/ipfsapi.js ‎dist/main

+363-231
Large diffs are not rendered by default.

‎package.json

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ipfs-api",
3-
"version": "2.9.3",
3+
"version": "2.9.7",
44
"description": "A client library for the IPFS API",
55
"main": "src/index.js",
66
"dependencies": {
@@ -9,6 +9,7 @@
99
"multipart-stream": "^2.0.0",
1010
"ndjson": "^1.4.3",
1111
"qs": "^6.0.0",
12+
"require-dir": "^0.3.0",
1213
"vinyl": "^1.1.0",
1314
"vinyl-fs-browser": "^2.1.1-1",
1415
"vinyl-multipart-stream": "^1.2.6",
@@ -32,10 +33,14 @@
3233
"eslint-config-standard": "^4.4.0",
3334
"eslint-plugin-standard": "^1.3.1",
3435
"gulp": "^3.9.0",
36+
"gulp-bump": "^1.0.0",
3537
"gulp-eslint": "^1.0.0",
38+
"gulp-filter": "^3.0.1",
39+
"gulp-git": "^1.6.0",
3640
"gulp-load-plugins": "^1.0.0",
3741
"gulp-mocha": "^2.1.3",
3842
"gulp-size": "^2.0.0",
43+
"gulp-tag-version": "^1.3.0",
3944
"gulp-util": "^3.0.7",
4045
"https-browserify": "0.0.1",
4146
"ipfsd-ctl": "^0.6.1",
@@ -50,9 +55,9 @@
5055
"mocha": "^2.3.3",
5156
"pre-commit": "^1.0.6",
5257
"raw-loader": "^0.5.1",
53-
"require-dir": "^0.3.0",
5458
"rimraf": "^2.4.3",
5559
"run-sequence": "^1.1.4",
60+
"semver": "^5.1.0",
5661
"stream-equal": "^0.1.7",
5762
"stream-http": "^2.0.2",
5863
"uglify-js": "^2.4.24",

‎src/api/add.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const Wreck = require('wreck')
2+
3+
module.exports = send => {
4+
return function add (files, opts, cb) {
5+
if (typeof (opts) === 'function' && cb === undefined) {
6+
cb = opts
7+
opts = {}
8+
}
9+
10+
if (typeof files === 'string' && files.startsWith('http')) {
11+
Wreck.request('GET', files, null, (err, res) => {
12+
if (err) return cb(err)
13+
14+
send('add', null, opts, res, cb)
15+
})
16+
17+
return
18+
}
19+
20+
send('add', null, opts, files, cb)
21+
}
22+
}

‎src/api/block.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'use strict'
2+
3+
const argCommand = require('../cmd-helpers').argCommand
4+
5+
module.exports = send => {
6+
return {
7+
get: argCommand(send, 'block/get'),
8+
put (file, cb) {
9+
if (Array.isArray(file)) {
10+
return cb(null, new Error('block.put() only accepts 1 file'))
11+
}
12+
return send('block/put', null, null, file, cb)
13+
}
14+
}
15+
}

‎src/api/cat.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict'
2+
3+
const argCommand = require('../cmd-helpers').argCommand
4+
5+
module.exports = send => {
6+
return argCommand(send, 'cat')
7+
}

‎src/api/commands.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict'
2+
3+
const command = require('../cmd-helpers').command
4+
5+
module.exports = send => {
6+
return command(send, 'commands')
7+
}

‎src/api/config.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict'
2+
3+
const argCommand = require('../cmd-helpers').argCommand
4+
5+
module.exports = send => {
6+
return {
7+
get: argCommand(send, 'config'),
8+
set (key, value, opts, cb) {
9+
if (typeof (opts) === 'function') {
10+
cb = opts
11+
opts = {}
12+
}
13+
return send('config', [key, value], opts, null, cb)
14+
},
15+
show (cb) {
16+
return send('config/show', null, null, null, true, cb)
17+
},
18+
replace (file, cb) {
19+
return send('config/replace', null, null, file, cb)
20+
}
21+
}
22+
}

‎src/api/dht.js

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
'use strict'
2+
3+
const argCommand = require('../cmd-helpers').argCommand
4+
5+
module.exports = send => {
6+
return {
7+
findprovs: argCommand(send, 'dht/findprovs'),
8+
get (key, opts, cb) {
9+
if (typeof (opts) === 'function' && !cb) {
10+
cb = opts
11+
opts = null
12+
}
13+
14+
return send('dht/get', key, opts, null, (err, res) => {
15+
if (err) return cb(err)
16+
if (!res) return cb(new Error('empty response'))
17+
if (res.length === 0) return cb(new Error('no value returned for key'))
18+
19+
// Inconsistent return values in the browser vs node
20+
if (Array.isArray(res)) {
21+
res = res[0]
22+
}
23+
24+
if (res.Type === 5) {
25+
cb(null, res.Extra)
26+
} else {
27+
cb(res)
28+
}
29+
})
30+
},
31+
put (key, value, opts, cb) {
32+
if (typeof (opts) === 'function' && !cb) {
33+
cb = opts
34+
opts = null
35+
}
36+
37+
return send('dht/put', [key, value], opts, null, cb)
38+
}
39+
}
40+
}

‎src/api/diag.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'use strict'
2+
3+
const command = require('../cmd-helpers').command
4+
5+
module.exports = send => {
6+
return {
7+
net: command(send, 'diag/net'),
8+
sys: command(send, 'diag/sys')
9+
}
10+
}

‎src/api/files.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
'use strict'
2+
3+
const argCommand = require('../cmd-helpers').argCommand
4+
5+
module.exports = send => {
6+
return {
7+
cp: argCommand(send, 'files/cp'),
8+
ls: argCommand(send, 'files/ls'),
9+
mkdir: argCommand(send, 'files/mkdir'),
10+
stat: argCommand(send, 'files/stat'),
11+
rm: function (path, opts, cb) {
12+
return send('files/rm', path, opts, null, cb)
13+
},
14+
read: argCommand(send, 'files/read'),
15+
write: function (pathDst, files, opts, cb) {
16+
if (typeof (opts) === 'function' && cb === undefined) {
17+
cb = opts
18+
opts = {}
19+
}
20+
21+
return send('files/write', pathDst, opts, files, cb)
22+
},
23+
mv: argCommand(send, 'files/mv')
24+
}
25+
}

‎src/api/id.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'use strict'
2+
3+
module.exports = send => {
4+
return function id (id, cb) {
5+
if (typeof id === 'function') {
6+
cb = id
7+
id = null
8+
}
9+
return send('id', id, null, null, cb)
10+
}
11+
}

‎src/api/log.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
'use strict'
2+
3+
const ndjson = require('ndjson')
4+
5+
module.exports = send => {
6+
return {
7+
tail (cb) {
8+
send('log/tail', null, {}, null, false, (err, res) => {
9+
if (err) return cb(err)
10+
cb(null, res.pipe(ndjson.parse()))
11+
})
12+
}
13+
}
14+
}

‎src/api/ls.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict'
2+
3+
const argCommand = require('../cmd-helpers').argCommand
4+
5+
module.exports = send => {
6+
return argCommand(send, 'ls')
7+
}

‎src/api/mount.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'use strict'
2+
3+
module.exports = send => {
4+
return function mount (ipfs, ipns, cb) {
5+
if (typeof ipfs === 'function') {
6+
cb = ipfs
7+
ipfs = null
8+
} else if (typeof ipns === 'function') {
9+
cb = ipns
10+
ipns = null
11+
}
12+
const opts = {}
13+
if (ipfs) opts.f = ipfs
14+
if (ipns) opts.n = ipns
15+
return send('mount', null, opts, null, cb)
16+
}
17+
}

‎src/api/name.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'use strict'
2+
3+
const argCommand = require('../cmd-helpers').argCommand
4+
5+
module.exports = send => {
6+
return {
7+
publish: argCommand(send, 'name/publish'),
8+
resolve: argCommand(send, 'name/resolve')
9+
}
10+
}

‎src/api/object.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'use strict'
2+
3+
const argCommand = require('../cmd-helpers').argCommand
4+
5+
module.exports = send => {
6+
return {
7+
get: argCommand(send, 'object/get'),
8+
put (file, encoding, cb) {
9+
if (typeof encoding === 'function') {
10+
return cb(null, new Error("Must specify an object encoding ('json' or 'protobuf')"))
11+
}
12+
return send('object/put', encoding, null, file, cb)
13+
},
14+
data: argCommand(send, 'object/data'),
15+
stat: argCommand(send, 'object/stat'),
16+
links: argCommand(send, 'object/links'),
17+
patch (file, opts, cb) {
18+
return send('object/patch', [file].concat(opts), null, null, cb)
19+
}
20+
}
21+
}

‎src/api/pin.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'use strict'
2+
3+
module.exports = send => {
4+
return {
5+
add (hash, opts, cb) {
6+
if (typeof opts === 'function') {
7+
cb = opts
8+
opts = null
9+
}
10+
11+
send('pin/add', hash, opts, null, cb)
12+
},
13+
remove (hash, opts, cb) {
14+
if (typeof opts === 'function') {
15+
cb = opts
16+
opts = null
17+
}
18+
19+
send('pin/rm', hash, opts, null, cb)
20+
},
21+
list (type, cb) {
22+
if (typeof type === 'function') {
23+
cb = type
24+
type = null
25+
}
26+
let opts = null
27+
if (type) opts = { type: type }
28+
return send('pin/ls', null, opts, null, cb)
29+
}
30+
}
31+
}

‎src/api/ping.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'use strict'
2+
3+
module.exports = send => {
4+
return function ping (id, cb) {
5+
return send('ping', id, { n: 1 }, null, function (err, res) {
6+
if (err) return cb(err, null)
7+
cb(null, res[1])
8+
})
9+
}
10+
}

‎src/api/refs.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'use strict'
2+
3+
const cmds = require('../cmd-helpers')
4+
5+
module.exports = send => {
6+
const refs = cmds.argCommand(send, 'refs')
7+
refs.local = cmds.command(send, 'refs/local')
8+
9+
return refs
10+
}

‎src/api/swarm.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'use strict'
2+
3+
const cmds = require('../cmd-helpers')
4+
5+
module.exports = send => {
6+
return {
7+
peers: cmds.command(send, 'swarm/peers'),
8+
connect: cmds.argCommand(send, 'swarm/connect')
9+
}
10+
}

‎src/api/update.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'use strict'
2+
3+
const command = require('../cmd-helpers').command
4+
5+
module.exports = send => {
6+
return {
7+
apply: command(send, 'update'),
8+
check: command(send, 'update/check'),
9+
log: command(send, 'update/log')
10+
}
11+
}

‎src/api/version.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict'
2+
3+
const command = require('../cmd-helpers').command
4+
5+
module.exports = send => {
6+
return command(send, 'version')
7+
}

‎src/cmd-helpers.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'use strict'
2+
3+
exports.command = function command (send, name) {
4+
return (opts, cb) => {
5+
if (typeof (opts) === 'function') {
6+
cb = opts
7+
opts = {}
8+
}
9+
return send(name, null, opts, null, cb)
10+
}
11+
}
12+
13+
exports.argCommand = function argCommand (send, name) {
14+
return (arg, opts, cb) => {
15+
if (typeof (opts) === 'function') {
16+
cb = opts
17+
opts = {}
18+
}
19+
return send(name, arg, opts, null, cb)
20+
}
21+
}

‎src/index.js

+6-249
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,16 @@
11
'use strict'
22

33
const multiaddr = require('multiaddr')
4+
5+
const loadCommands = require('./load-commands')
46
const getConfig = require('./config')
57
const getRequestAPI = require('./request-api')
6-
const Wreck = require('wreck')
7-
const ndjson = require('ndjson')
88

99
exports = module.exports = IpfsAPI
1010

1111
function IpfsAPI (host_or_multiaddr, port, opts) {
12-
const self = this
1312
const config = getConfig()
1413

15-
if (!(self instanceof IpfsAPI)) {
16-
return new IpfsAPI(host_or_multiaddr, port, opts)
17-
}
18-
1914
try {
2015
const maddr = multiaddr(host_or_multiaddr).nodeAddress()
2116
config.host = maddr.address
@@ -44,247 +39,9 @@ function IpfsAPI (host_or_multiaddr, port, opts) {
4439
}
4540

4641
const requestAPI = getRequestAPI(config)
42+
const cmds = loadCommands(requestAPI)
43+
cmds.send = requestAPI
44+
cmds.Buffer = Buffer
4745

48-
// -- Internal
49-
50-
function command (name) {
51-
return (opts, cb) => {
52-
if (typeof (opts) === 'function') {
53-
cb = opts
54-
opts = {}
55-
}
56-
return requestAPI(name, null, opts, null, cb)
57-
}
58-
}
59-
60-
function argCommand (name) {
61-
return (arg, opts, cb) => {
62-
if (typeof (opts) === 'function') {
63-
cb = opts
64-
opts = {}
65-
}
66-
return requestAPI(name, arg, opts, null, cb)
67-
}
68-
}
69-
70-
// -- Interface
71-
72-
self.send = requestAPI
73-
74-
self.add = (files, opts, cb) => {
75-
if (typeof (opts) === 'function' && cb === undefined) {
76-
cb = opts
77-
opts = {}
78-
}
79-
80-
if (typeof files === 'string' && files.startsWith('http')) {
81-
Wreck.request('GET', files, null, (err, res) => {
82-
if (err) return cb(err)
83-
84-
requestAPI('add', null, opts, res, cb)
85-
})
86-
87-
return
88-
}
89-
90-
requestAPI('add', null, opts, files, cb)
91-
}
92-
93-
self.cat = argCommand('cat')
94-
self.ls = argCommand('ls')
95-
96-
self.config = {
97-
get: argCommand('config'),
98-
set (key, value, opts, cb) {
99-
if (typeof (opts) === 'function') {
100-
cb = opts
101-
opts = {}
102-
}
103-
return requestAPI('config', [key, value], opts, null, cb)
104-
},
105-
show (cb) {
106-
return requestAPI('config/show', null, null, null, true, cb)
107-
},
108-
replace (file, cb) {
109-
return requestAPI('config/replace', null, null, file, cb)
110-
}
111-
}
112-
113-
self.update = {
114-
apply: command('update'),
115-
check: command('update/check'),
116-
log: command('update/log')
117-
}
118-
119-
self.version = command('version')
120-
self.commands = command('commands')
121-
122-
self.mount = (ipfs, ipns, cb) => {
123-
if (typeof ipfs === 'function') {
124-
cb = ipfs
125-
ipfs = null
126-
} else if (typeof ipns === 'function') {
127-
cb = ipns
128-
ipns = null
129-
}
130-
const opts = {}
131-
if (ipfs) opts.f = ipfs
132-
if (ipns) opts.n = ipns
133-
return requestAPI('mount', null, opts, null, cb)
134-
}
135-
136-
self.diag = {
137-
net: command('diag/net'),
138-
sys: command('diag/sys')
139-
}
140-
141-
self.block = {
142-
get: argCommand('block/get'),
143-
put (file, cb) {
144-
if (Array.isArray(file)) {
145-
return cb(null, new Error('block.put() only accepts 1 file'))
146-
}
147-
return requestAPI('block/put', null, null, file, cb)
148-
}
149-
}
150-
151-
self.object = {
152-
get: argCommand('object/get'),
153-
put (file, encoding, cb) {
154-
if (typeof encoding === 'function') {
155-
return cb(null, new Error("Must specify an object encoding ('json' or 'protobuf')"))
156-
}
157-
return requestAPI('object/put', encoding, null, file, cb)
158-
},
159-
data: argCommand('object/data'),
160-
stat: argCommand('object/stat'),
161-
links: argCommand('object/links'),
162-
patch (file, opts, cb) {
163-
return requestAPI('object/patch', [file].concat(opts), null, null, cb)
164-
}
165-
}
166-
167-
self.swarm = {
168-
peers: command('swarm/peers'),
169-
connect: argCommand('swarm/connect')
170-
}
171-
172-
self.ping = (id, cb) => {
173-
return requestAPI('ping', id, { n: 1 }, null, function (err, res) {
174-
if (err) return cb(err, null)
175-
cb(null, res[1])
176-
})
177-
}
178-
179-
self.id = (id, cb) => {
180-
if (typeof id === 'function') {
181-
cb = id
182-
id = null
183-
}
184-
return requestAPI('id', id, null, null, cb)
185-
}
186-
187-
self.pin = {
188-
add (hash, opts, cb) {
189-
if (typeof opts === 'function') {
190-
cb = opts
191-
opts = null
192-
}
193-
194-
requestAPI('pin/add', hash, opts, null, cb)
195-
},
196-
remove (hash, opts, cb) {
197-
if (typeof opts === 'function') {
198-
cb = opts
199-
opts = null
200-
}
201-
202-
requestAPI('pin/rm', hash, opts, null, cb)
203-
},
204-
list (type, cb) {
205-
if (typeof type === 'function') {
206-
cb = type
207-
type = null
208-
}
209-
let opts = null
210-
if (type) opts = { type: type }
211-
return requestAPI('pin/ls', null, opts, null, cb)
212-
}
213-
}
214-
215-
self.log = {
216-
tail (cb) {
217-
requestAPI('log/tail', null, {}, null, false, (err, res) => {
218-
if (err) return cb(err)
219-
cb(null, res.pipe(ndjson.parse()))
220-
})
221-
}
222-
}
223-
224-
self.name = {
225-
publish: argCommand('name/publish'),
226-
resolve: argCommand('name/resolve')
227-
}
228-
229-
self.Buffer = Buffer
230-
231-
self.refs = argCommand('refs')
232-
self.refs.local = command('refs/local')
233-
234-
self.dht = {
235-
findprovs: argCommand('dht/findprovs'),
236-
237-
get (key, opts, cb) {
238-
if (typeof (opts) === 'function' && !cb) {
239-
cb = opts
240-
opts = null
241-
}
242-
243-
return requestAPI('dht/get', key, opts, null, (err, res) => {
244-
if (err) return cb(err)
245-
if (!res) return cb(new Error('empty response'))
246-
if (res.length === 0) return cb(new Error('no value returned for key'))
247-
248-
// Inconsistent return values in the browser vs node
249-
if (Array.isArray(res)) {
250-
res = res[0]
251-
}
252-
253-
if (res.Type === 5) {
254-
cb(null, res.Extra)
255-
} else {
256-
cb(res)
257-
}
258-
})
259-
},
260-
261-
put (key, value, opts, cb) {
262-
if (typeof (opts) === 'function' && !cb) {
263-
cb = opts
264-
opts = null
265-
}
266-
267-
return requestAPI('dht/put', [key, value], opts, null, cb)
268-
}
269-
}
270-
271-
self.files = {
272-
cp: argCommand('files/cp'),
273-
ls: argCommand('files/ls'),
274-
mkdir: argCommand('files/mkdir'),
275-
stat: argCommand('files/stat'),
276-
rm: function (path, opts, cb) {
277-
return requestAPI('files/rm', path, opts, null, cb)
278-
},
279-
read: argCommand('files/read'),
280-
write: function (pathDst, files, opts, cb) {
281-
if (typeof (opts) === 'function' && cb === undefined) {
282-
cb = opts
283-
opts = {}
284-
}
285-
286-
return requestAPI('files/write', pathDst, opts, files, cb)
287-
},
288-
mv: argCommand('files/mv')
289-
}
46+
return cmds
29047
}

‎src/load-commands.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
'use strict'
2+
3+
const isNode = !global.window
4+
5+
function requireDir () {
6+
if (isNode) return require('require-dir')('./api')
7+
8+
// Webpack specific require of a directory
9+
const req = require.context('./api', false, /\.js$/)
10+
11+
const files = {}
12+
req.keys().forEach(key => {
13+
const name = key
14+
.replace(/^\.\//, '')
15+
.replace(/\.js$/, '')
16+
files[name] = req(key)
17+
})
18+
19+
return files
20+
}
21+
22+
function loadCommands (send) {
23+
const files = requireDir()
24+
const cmds = {}
25+
26+
Object.keys(files).forEach(file => {
27+
cmds[file] = files[file](send)
28+
})
29+
30+
return cmds
31+
}
32+
33+
module.exports = loadCommands

‎tasks/config.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ const shared = {
4040
net: '{}',
4141
fs: '{}',
4242
tls: '{}',
43-
console: '{}'
43+
console: '{}',
44+
'require-dir': '{}'
4445
}
4546
}
4647

‎tasks/release.js

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
'use strict'
2+
3+
const gulp = require('gulp')
4+
const $ = require('gulp-load-plugins')()
5+
const runSequence = require('run-sequence')
6+
const semver = require('semver')
7+
const fs = require('fs')
8+
const exec = require('child_process').exec
9+
10+
function getCurrentVersion () {
11+
return JSON.parse(fs.readFileSync('./package.json', 'utf8')).version
12+
}
13+
14+
function npmPublish (done) {
15+
exec('npm publish', (err, stdout, stderr) => {
16+
if (err) throw err
17+
$.util.log('Published to npm')
18+
})
19+
}
20+
21+
function fail (msg) {
22+
$.util.log($.util.colors.red(msg))
23+
process.exit(1)
24+
}
25+
26+
function getType () {
27+
if ($.util.env.major) return 'major'
28+
if ($.util.env.minor) return 'minor'
29+
30+
return 'patch'
31+
}
32+
33+
gulp.task('release:build', ['build'], () => {
34+
return gulp.src('./dist')
35+
.pipe($.git.add())
36+
.pipe($.git.commit('chore: build', {args: '-n'}))
37+
})
38+
39+
gulp.task('release:bump', () => {
40+
const type = getType()
41+
const newVersion = semver.inc(getCurrentVersion(), type)
42+
43+
return gulp.src('./package.json')
44+
.pipe($.bump({version: newVersion}))
45+
.pipe(gulp.dest('./'))
46+
.pipe($.git.add())
47+
.pipe($.git.commit(`chore: release version v${newVersion}`, {args: '-n'}))
48+
.pipe($.filter('package.json'))
49+
.pipe($.tagVersion())
50+
})
51+
52+
gulp.task('release:push', done => {
53+
const remote = $.util.remote || 'origin'
54+
55+
$.git.push(remote, 'master', {args: '--tags'}, err => {
56+
if (err) return fail(err.message)
57+
58+
$.util.log(`Pushed to git ${remote}:master`)
59+
done()
60+
})
61+
})
62+
63+
gulp.task('release:publish', done => {
64+
$.git.status({args: '-s'}, (err, stdout) => {
65+
if (err) return fail(err.message)
66+
67+
const isDirty = stdout.trim().length > 0
68+
69+
if (isDirty) {
70+
return fail('Dirt workspace, cannot push to npm')
71+
}
72+
73+
npmPublish(done)
74+
})
75+
})
76+
77+
gulp.task('release', done => {
78+
runSequence(
79+
'lint',
80+
'test',
81+
'release:build',
82+
'release:bump',
83+
'release:push',
84+
'release:publish',
85+
done
86+
)
87+
})

0 commit comments

Comments
 (0)
This repository has been archived.