Skip to content

Commit 0561798

Browse files
richardschneiderdaviddias
authored andcommitted
feat: more windows interop (#171)
* test: appveyor * fix: executable have '.exe' on windows * fix: lint errors
1 parent 9017e66 commit 0561798

File tree

2 files changed

+25
-18
lines changed

2 files changed

+25
-18
lines changed

src/daemon.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ const shutdown = require('shutdown')
99
const path = require('path')
1010
const join = path.join
1111
const once = require('once')
12+
const os = require('os')
13+
const isWindows = os.platform() === 'win32'
1214

1315
const exec = require('./exec')
1416

@@ -28,19 +30,19 @@ function findIpfsExecutable () {
2830
if (appRoot.includes(`.asar${path.sep}`)) {
2931
appRoot = appRoot.replace(`.asar${path.sep}`, `.asar.unpacked${path.sep}`)
3032
}
31-
const depPath = path.join('go-ipfs-dep', 'go-ipfs', 'ipfs')
33+
const appName = isWindows ? 'ipfs.exe' : 'ipfs'
34+
const depPath = path.join('go-ipfs-dep', 'go-ipfs', appName)
3235
const npm3Path = path.join(appRoot, '../', depPath)
3336
const npm2Path = path.join(appRoot, 'node_modules', depPath)
3437

35-
let npmPath
36-
try {
37-
fs.statSync(npm3Path)
38-
npmPath = npm3Path
39-
} catch (e) {
40-
npmPath = npm2Path
38+
if (fs.existsSync(npm3Path)) {
39+
return npm3Path
40+
}
41+
if (fs.existsSync(npm2Path)) {
42+
return npm2Path
4143
}
4244

43-
return npmPath
45+
throw new Error('Cannot find the IPFS executable')
4446
}
4547

4648
function setConfigValue (node, key, value, callback) {

test/npm-installs.spec.js

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,45 +10,50 @@ const fs = require('fs')
1010
const rimraf = require('rimraf')
1111
const mkdirp = require('mkdirp')
1212
const path = require('path')
13+
const os = require('os')
14+
const isWindows = os.platform() === 'win32'
1315

1416
describe('ipfs executable path', () => {
17+
const tmp = os.tmpdir()
18+
const appName = isWindows ? 'ipfs.exe' : 'ipfs'
19+
1520
it('has the correct path when installed with npm3', (done) => {
16-
process.env.testpath = '/tmp/ipfsd-ctl-test/node_modules/ipfsd-ctl/lib' // fake __dirname
17-
let npm3Path = '/tmp/ipfsd-ctl-test/node_modules/go-ipfs-dep/go-ipfs'
21+
process.env.testpath = path.join(tmp, 'ipfsd-ctl-test/node_modules/ipfsd-ctl/lib') // fake __dirname
22+
let npm3Path = path.join(tmp, 'ipfsd-ctl-test/node_modules/go-ipfs-dep/go-ipfs')
1823

1924
mkdirp(npm3Path, (err) => {
2025
expect(err).to.not.exist()
2126

22-
fs.writeFileSync(path.join(npm3Path, 'ipfs'))
27+
fs.writeFileSync(path.join(npm3Path, appName))
2328
delete require.cache[require.resolve('../src/daemon.js')]
2429
const Daemon = require('../src/daemon.js')
2530

2631
const node = new Daemon()
2732
expect(node.exec)
28-
.to.eql(path.normalize('/tmp/ipfsd-ctl-test/node_modules/go-ipfs-dep/go-ipfs/ipfs'))
29-
rimraf('/tmp/ipfsd-ctl-test', done)
33+
.to.eql(path.join(tmp, `ipfsd-ctl-test/node_modules/go-ipfs-dep/go-ipfs/${appName}`))
34+
rimraf(path.join(tmp, 'ipfsd-ctl-test'), done)
3035
})
3136
})
3237

3338
it('has the correct path when installed with npm2', (done) => {
34-
process.env.testpath = '/tmp/ipfsd-ctl-test/node_modules/ipfsd-ctl/lib' // fake __dirname
39+
process.env.testpath = path.join(tmp, 'ipfsd-ctl-test/node_modules/ipfsd-ctl/lib') // fake __dirname
3540

36-
let npm2Path = '/tmp/ipfsd-ctl-test/node_modules/ipfsd-ctl/node_modules/go-ipfs-dep/go-ipfs'
41+
let npm2Path = path.join(tmp, 'ipfsd-ctl-test/node_modules/ipfsd-ctl/node_modules/go-ipfs-dep/go-ipfs')
3742

3843
mkdirp(npm2Path, (err) => {
3944
expect(err).to.not.exist()
4045

41-
fs.writeFileSync(path.join(npm2Path, 'ipfs'))
46+
fs.writeFileSync(path.join(npm2Path, appName))
4247
delete require.cache[require.resolve('../src/daemon.js')]
4348
const Daemon = require('../src/daemon.js')
4449

4550
const node = new Daemon()
4651

4752
expect(node.exec)
4853
.to.eql(
49-
path.normalize('/tmp/ipfsd-ctl-test/node_modules/ipfsd-ctl/node_modules/go-ipfs-dep/go-ipfs/ipfs')
54+
path.join(tmp, `ipfsd-ctl-test/node_modules/ipfsd-ctl/node_modules/go-ipfs-dep/go-ipfs/${appName}`)
5055
)
51-
rimraf('/tmp/ipfsd-ctl-test', done)
56+
rimraf(path.join(tmp, 'ipfsd-ctl-test'), done)
5257
})
5358
})
5459
})

0 commit comments

Comments
 (0)