From 0561798f29734deb1b38611ced81fa461e9129bb Mon Sep 17 00:00:00 2001 From: Richard Schneider Date: Mon, 13 Nov 2017 02:05:55 +1300 Subject: [PATCH] feat: more windows interop (#171) * test: appveyor * fix: executable have '.exe' on windows * fix: lint errors --- src/daemon.js | 18 ++++++++++-------- test/npm-installs.spec.js | 25 +++++++++++++++---------- 2 files changed, 25 insertions(+), 18 deletions(-) diff --git a/src/daemon.js b/src/daemon.js index 3e7c5851..d88f5b05 100644 --- a/src/daemon.js +++ b/src/daemon.js @@ -9,6 +9,8 @@ const shutdown = require('shutdown') const path = require('path') const join = path.join const once = require('once') +const os = require('os') +const isWindows = os.platform() === 'win32' const exec = require('./exec') @@ -28,19 +30,19 @@ function findIpfsExecutable () { if (appRoot.includes(`.asar${path.sep}`)) { appRoot = appRoot.replace(`.asar${path.sep}`, `.asar.unpacked${path.sep}`) } - const depPath = path.join('go-ipfs-dep', 'go-ipfs', 'ipfs') + const appName = isWindows ? 'ipfs.exe' : 'ipfs' + const depPath = path.join('go-ipfs-dep', 'go-ipfs', appName) const npm3Path = path.join(appRoot, '../', depPath) const npm2Path = path.join(appRoot, 'node_modules', depPath) - let npmPath - try { - fs.statSync(npm3Path) - npmPath = npm3Path - } catch (e) { - npmPath = npm2Path + if (fs.existsSync(npm3Path)) { + return npm3Path + } + if (fs.existsSync(npm2Path)) { + return npm2Path } - return npmPath + throw new Error('Cannot find the IPFS executable') } function setConfigValue (node, key, value, callback) { diff --git a/test/npm-installs.spec.js b/test/npm-installs.spec.js index 4085087c..090752e2 100644 --- a/test/npm-installs.spec.js +++ b/test/npm-installs.spec.js @@ -10,35 +10,40 @@ const fs = require('fs') const rimraf = require('rimraf') const mkdirp = require('mkdirp') const path = require('path') +const os = require('os') +const isWindows = os.platform() === 'win32' describe('ipfs executable path', () => { + const tmp = os.tmpdir() + const appName = isWindows ? 'ipfs.exe' : 'ipfs' + it('has the correct path when installed with npm3', (done) => { - process.env.testpath = '/tmp/ipfsd-ctl-test/node_modules/ipfsd-ctl/lib' // fake __dirname - let npm3Path = '/tmp/ipfsd-ctl-test/node_modules/go-ipfs-dep/go-ipfs' + process.env.testpath = path.join(tmp, 'ipfsd-ctl-test/node_modules/ipfsd-ctl/lib') // fake __dirname + let npm3Path = path.join(tmp, 'ipfsd-ctl-test/node_modules/go-ipfs-dep/go-ipfs') mkdirp(npm3Path, (err) => { expect(err).to.not.exist() - fs.writeFileSync(path.join(npm3Path, 'ipfs')) + fs.writeFileSync(path.join(npm3Path, appName)) delete require.cache[require.resolve('../src/daemon.js')] const Daemon = require('../src/daemon.js') const node = new Daemon() expect(node.exec) - .to.eql(path.normalize('/tmp/ipfsd-ctl-test/node_modules/go-ipfs-dep/go-ipfs/ipfs')) - rimraf('/tmp/ipfsd-ctl-test', done) + .to.eql(path.join(tmp, `ipfsd-ctl-test/node_modules/go-ipfs-dep/go-ipfs/${appName}`)) + rimraf(path.join(tmp, 'ipfsd-ctl-test'), done) }) }) it('has the correct path when installed with npm2', (done) => { - process.env.testpath = '/tmp/ipfsd-ctl-test/node_modules/ipfsd-ctl/lib' // fake __dirname + process.env.testpath = path.join(tmp, 'ipfsd-ctl-test/node_modules/ipfsd-ctl/lib') // fake __dirname - let npm2Path = '/tmp/ipfsd-ctl-test/node_modules/ipfsd-ctl/node_modules/go-ipfs-dep/go-ipfs' + let npm2Path = path.join(tmp, 'ipfsd-ctl-test/node_modules/ipfsd-ctl/node_modules/go-ipfs-dep/go-ipfs') mkdirp(npm2Path, (err) => { expect(err).to.not.exist() - fs.writeFileSync(path.join(npm2Path, 'ipfs')) + fs.writeFileSync(path.join(npm2Path, appName)) delete require.cache[require.resolve('../src/daemon.js')] const Daemon = require('../src/daemon.js') @@ -46,9 +51,9 @@ describe('ipfs executable path', () => { expect(node.exec) .to.eql( - path.normalize('/tmp/ipfsd-ctl-test/node_modules/ipfsd-ctl/node_modules/go-ipfs-dep/go-ipfs/ipfs') + path.join(tmp, `ipfsd-ctl-test/node_modules/ipfsd-ctl/node_modules/go-ipfs-dep/go-ipfs/${appName}`) ) - rimraf('/tmp/ipfsd-ctl-test', done) + rimraf(path.join(tmp, 'ipfsd-ctl-test'), done) }) }) })