Skip to content

Commit a553a61

Browse files
committed
Merge pull request #66 from ipfs/bugfix/paths
Fix default IPFS exec path when used via Electron
2 parents d94b962 + 60c7b0b commit a553a61

File tree

3 files changed

+72
-33
lines changed

3 files changed

+72
-33
lines changed

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ module.exports = {
5858
done(null, node)
5959
})
6060
} else {
61-
node.init(err => {
61+
node.init((err) => {
6262
done(err, node)
6363
})
6464
}

lib/node.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ const shutdown = require('shutdown')
1010
const path = require('path')
1111
const join = path.join
1212

13-
const ipfsDefaultPath = path.join(process.cwd(), 'node_modules/go-ipfs-dep/go-ipfs/ipfs')
13+
const rootPath = process.versions['electron'] ? process.resourcesPath + '/app/' : process.cwd()
14+
const ipfsDefaultPath = path.join(rootPath, 'node_modules/go-ipfs-dep/go-ipfs/ipfs')
1415
const GRACE_PERIOD = 7500 // amount of ms to wait before sigkill
1516

1617
function configureNode (node, conf, done) {
@@ -55,7 +56,9 @@ module.exports = class Node {
5556
let result = ''
5657
run(this.exec, args, envArg)
5758
.on('error', done)
58-
.on('data', data => result += data)
59+
.on('data', (data) => {
60+
result += data
61+
})
5962
.on('end', () => done(null, result.trim()))
6063
}
6164

@@ -75,9 +78,11 @@ module.exports = class Node {
7578

7679
run(this.exec, ['init', '-b', keySize], {env: this.env})
7780
.on('error', done)
78-
.on('data', data => buf += data)
81+
.on('data', (data) => {
82+
buf += data
83+
})
7984
.on('end', () => {
80-
configureNode(this, this.opts, err => {
85+
configureNode(this, this.opts, (err) => {
8186
if (err) return done(err)
8287
this.clean = false
8388
this.initialized = true
@@ -93,7 +98,7 @@ module.exports = class Node {
9398
// cleanup tmp files
9499
shutdown (done) {
95100
if (!this.clean && this.disposable) {
96-
rimraf(this.path, err => {
101+
rimraf(this.path, (err) => {
97102
if (err) throw err
98103
done()
99104
})
@@ -105,7 +110,7 @@ module.exports = class Node {
105110
if (err) return done(err)
106111

107112
this.subprocess = run(this.exec, ['daemon'], {env: this.env})
108-
.on('error', err => {
113+
.on('error', (err) => {
109114
if ((err + '').match('daemon is running')) {
110115
// we're good
111116
done(null, ipfs(conf.Addresses.API))
@@ -115,7 +120,7 @@ module.exports = class Node {
115120
done(err)
116121
}
117122
})
118-
.on('data', data => {
123+
.on('data', (data) => {
119124
const match = (data + '').trim().match(/API server listening on (.*)/)
120125
if (match) {
121126
this.apiAddr = match[1]
@@ -164,7 +169,7 @@ module.exports = class Node {
164169
setConfig (key, value, done) {
165170
run(this.exec, ['config', key, value, '--json'], {env: this.env})
166171
.on('error', done)
167-
.on('data', data => {})
172+
.on('data', (data) => {})
168173
.on('end', () => done())
169174
}
170175

test/test.js

Lines changed: 58 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,39 @@ const path = require('path')
1111
// this comment is used by mocha, do not delete
1212
/*global describe, before, it*/
1313

14+
describe('ipfs executable path', function () {
15+
this.timeout(2000)
16+
let Node
17+
18+
it('has the correct path when used via Electron', () => {
19+
process.versions['electron'] = '0.0.0-test' // Electron sets its version to the array --> we know that we're using Electron
20+
process.resourcesPath = '/test/path/one/more' // Path to the Electron app, set by Electron
21+
22+
// Force reload of the module (pathing is handled globally in ../lib/node.js)
23+
delete require.cache[require.resolve('../lib/node.js')]
24+
Node = require('../lib/node.js')
25+
26+
var node = new Node()
27+
assert.equal(node.exec, path.join(process.resourcesPath, '/app', 'node_modules/go-ipfs-dep/go-ipfs/ipfs'))
28+
})
29+
30+
it('has the correct path when used via Node.js', () => {
31+
delete process.versions['electron']
32+
delete process.resourcesPath
33+
34+
// Force reload of the module (pathing is handled globally in ../lib/node.js)
35+
delete require.cache[require.resolve('../lib/node.js')]
36+
Node = require('../lib/node.js')
37+
38+
var node = new Node()
39+
assert.equal(node.exec, path.join(process.cwd(), 'node_modules/go-ipfs-dep/go-ipfs/ipfs'))
40+
})
41+
})
42+
1443
describe('disposable node with local api', function () {
1544
this.timeout(20000)
1645
let ipfs
17-
before(done => {
46+
before((done) => {
1847
ipfsd.disposable((err, node) => {
1948
if (err) throw err
2049
node.startDaemon((err, ignore) => {
@@ -32,7 +61,7 @@ describe('disposable node with local api', function () {
3261

3362
let store, retrieve
3463

35-
before(done => {
64+
before((done) => {
3665
const blorb = Buffer('blorb')
3766
ipfs.block.put(blorb, (err, res) => {
3867
if (err) throw err
@@ -42,7 +71,9 @@ describe('disposable node with local api', function () {
4271
if (err) throw err
4372
let buf = ''
4473
res
45-
.on('data', data => buf += data)
74+
.on('data', (data) => {
75+
buf += data
76+
})
4677
.on('end', () => {
4778
retrieve = buf
4879
done()
@@ -61,7 +92,7 @@ describe('disposable node with local api', function () {
6192
describe('disposableApi node', function () {
6293
this.timeout(20000)
6394
let ipfs
64-
before(done => {
95+
before((done) => {
6596
ipfsd.disposableApi((err, api) => {
6697
if (err) throw err
6798
ipfs = api
@@ -78,7 +109,7 @@ describe('disposableApi node', function () {
78109

79110
let store, retrieve
80111

81-
before(done => {
112+
before((done) => {
82113
const blorb = Buffer('blorb')
83114
ipfs.block.put(blorb, (err, res) => {
84115
if (err) throw err
@@ -88,7 +119,9 @@ describe('disposableApi node', function () {
88119
if (err) throw err
89120
let buf = ''
90121
res
91-
.on('data', data => buf += data)
122+
.on('data', (data) => {
123+
buf += data
124+
})
92125
.on('end', () => {
93126
retrieve = buf
94127
done()
@@ -109,7 +142,7 @@ describe('starting and stopping', function () {
109142
let node
110143

111144
describe('init', () => {
112-
before(done => {
145+
before((done) => {
113146
ipfsd.disposable((err, res) => {
114147
if (err) throw err
115148
node = res
@@ -130,7 +163,7 @@ describe('starting and stopping', function () {
130163

131164
describe('starting', () => {
132165
let ipfs
133-
before(done => {
166+
before((done) => {
134167
node.startDaemon((err, res) => {
135168
if (err) throw err
136169

@@ -139,7 +172,7 @@ describe('starting and stopping', function () {
139172

140173
// actually running?
141174
run('kill', ['-0', pid])
142-
.on(err, err => { throw err })
175+
.on(err, (err) => { throw err })
143176
.on('end', () => { done() })
144177
})
145178
})
@@ -151,8 +184,8 @@ describe('starting and stopping', function () {
151184

152185
let stopped = false
153186
describe('stopping', () => {
154-
before(done => {
155-
node.stopDaemon(err => {
187+
before((done) => {
188+
node.stopDaemon((err) => {
156189
if (err) throw err
157190
stopped = true
158191
})
@@ -178,7 +211,7 @@ describe('setting up and initializing a local node', () => {
178211
const testpath1 = '/tmp/ipfstestpath1'
179212

180213
describe('cleanup', () => {
181-
before(done => {
214+
before((done) => {
182215
rimraf(testpath1, done)
183216
})
184217

@@ -189,7 +222,7 @@ describe('setting up and initializing a local node', () => {
189222

190223
describe('setup', () => {
191224
let node
192-
before(done => {
225+
before((done) => {
193226
ipfsd.local(testpath1, (err, res) => {
194227
if (err) throw err
195228
node = res
@@ -208,8 +241,8 @@ describe('setting up and initializing a local node', () => {
208241
describe('initialize', function () {
209242
this.timeout(10000)
210243

211-
before(done => {
212-
node.init(err => {
244+
before((done) => {
245+
node.init((err) => {
213246
if (err) throw err
214247
done()
215248
})
@@ -235,7 +268,7 @@ describe('change config values of a disposable node', function () {
235268

236269
let ipfsNode
237270

238-
before(done => {
271+
before((done) => {
239272
ipfsd.disposable((err, node) => {
240273
if (err) {
241274
throw err
@@ -245,7 +278,7 @@ describe('change config values of a disposable node', function () {
245278
})
246279
})
247280

248-
it('Should return a config value', done => {
281+
it('Should return a config value', (done) => {
249282
ipfsNode.getConfig('Bootstrap', (err, config) => {
250283
if (err) {
251284
throw err
@@ -255,8 +288,8 @@ describe('change config values of a disposable node', function () {
255288
})
256289
})
257290

258-
it('Should set a config value', done => {
259-
ipfsNode.setConfig('Bootstrap', null, err => {
291+
it('Should set a config value', (done) => {
292+
ipfsNode.setConfig('Bootstrap', null, (err) => {
260293
if (err) {
261294
throw err
262295
}
@@ -273,7 +306,7 @@ describe('change config values of a disposable node', function () {
273306
})
274307

275308
describe('external ipfs binaray', () => {
276-
it('allows passing via $IPFS_EXEC', done => {
309+
it('allows passing via $IPFS_EXEC', (done) => {
277310
process.env.IPFS_EXEC = '/some/path'
278311
ipfsd.local((err, node) => {
279312
if (err) throw err
@@ -287,7 +320,7 @@ describe('external ipfs binaray', () => {
287320
})
288321

289322
describe('version', () => {
290-
it('prints the version', done => {
323+
it('prints the version', (done) => {
291324
ipfsd.version((err, version) => {
292325
if (err) throw err
293326

@@ -302,7 +335,7 @@ describe('ipfs-api version', function () {
302335

303336
let ipfs
304337

305-
before(done => {
338+
before((done) => {
306339
ipfsd.disposable((err, node) => {
307340
if (err) throw err
308341
node.startDaemon((err, ignore) => {
@@ -313,13 +346,14 @@ describe('ipfs-api version', function () {
313346
})
314347
})
315348

316-
it('uses the correct ipfs-api', done => {
349+
// NOTE: if you change ../lib/node.js, the hash will need to be changed
350+
it('uses the correct ipfs-api', (done) => {
317351
ipfs.add(path.join(__dirname, '../lib'), { recursive: true }, (err, res) => {
318352
if (err) throw err
319353

320354
const added = res[res.length - 1]
321355
assert(added)
322-
assert.equal(added.Hash, 'QmWab9Js2ueyo739mUdLxv45u6YkEgd3LmzXKn4SQjcFuq')
356+
assert.equal(added.Hash, 'QmTioWzyNf4ybt6RDYCxqWBGYBfDqFWCoNwRKF89xgUvgF')
323357
done()
324358
})
325359
})

0 commit comments

Comments
 (0)