Skip to content
This repository was archived by the owner on May 25, 2021. It is now read-only.

Commit c48958d

Browse files
authored
feat: add .path and .path.silent functions to detect binary (#40)
After installation if you wish to specify a path to the installed binary, it's useful for this module to be able to tell the world where it put the binary, so this PR adds `.path` and `.path.silent()` functions to do that. This way we can use the installed module without having to rely on any external context or platform. fixes #25
1 parent 7465f17 commit c48958d

File tree

4 files changed

+96
-8
lines changed

4 files changed

+96
-8
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,22 @@ This module downloads `go-ipfs` binaries from https://dist.ipfs.io into your pro
2323

2424
By default it will download the go-ipfs version that matches the npm version of this module. So depending on `[email protected]` will install `go-ipfs v0.4.19` for your current system architecture, in to your project at `node_modules/go-ipfs-dep/go-ipfs/ipfs`.
2525

26+
After downloading you can find out the path of the installed binary by calling the `path` function exported by this module:
27+
28+
```javascript
29+
const { path } = require('go-ipfs-dep')
30+
31+
console.info('go-ipfs is installed at', path())
32+
```
33+
34+
An error will be thrown if the path to the binary cannot be resolved - if you do not wish this to happen, call `path.silent()`:
35+
36+
```javascript
37+
const { path: silent } = require('go-ipfs-dep')
38+
39+
console.info('go-ipfs may installed at', silent())
40+
```
41+
2642
### Overriding the go-ipfs version
2743

2844
You can override the version of go-ipfs that gets downloaded by adding by adding a `go-ipfs.version` field to your `package.json`

src/bin.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ const success = (output) => {
1616
process.exit(0)
1717
}
1818

19+
const existingPath = download.path.silent()
20+
21+
if (existingPath) {
22+
process.stdout.write(`Detected existing binary at ${existingPath}\n`)
23+
process.stdout.write(`Skipping download\n`)
24+
process.exit(0)
25+
}
26+
1927
// First param is the target version
2028
// Second param is the target platform
2129
// Third param is the target architecture

src/index.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const unzip = require('unzip-stream')
2727
const fetch = require('node-fetch')
2828
const pkgConf = require('pkg-conf')
2929
const pkg = require('./../package.json')
30+
const fs = require('fs')
3031

3132
function unpack ({ url, installPath, stream }) {
3233
return new Promise((resolve, reject) => {
@@ -115,3 +116,26 @@ module.exports = async function () {
115116
installPath: path.join(args.installPath, 'go-ipfs') + path.sep
116117
}
117118
}
119+
120+
module.exports.path = function () {
121+
const paths = [
122+
path.resolve(path.join(__dirname, '..', 'go-ipfs', 'ipfs')),
123+
path.resolve(path.join(__dirname, '..', 'go-ipfs', 'ipfs.exe'))
124+
]
125+
126+
for (const bin of paths) {
127+
if (fs.existsSync(bin)) {
128+
return bin
129+
}
130+
}
131+
132+
throw new Error('go-ipfs binary not found, it may not be installed or an error may have occured during installation')
133+
}
134+
135+
module.exports.path.silent = function () {
136+
try {
137+
return module.exports.path()
138+
} catch (err) {
139+
// ignore
140+
}
141+
}

test/index.js

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ function download (version, platform, arch, callback) {
3939
}
4040

4141
test('Ensure ipfs gets downloaded (current version and platform)', (t) => {
42-
t.plan(5)
42+
t.plan(7)
4343
const dir = path.resolve(__dirname, '../go-ipfs')
4444
rimraf.sync(dir)
4545

@@ -52,12 +52,17 @@ test('Ensure ipfs gets downloaded (current version and platform)', (t) => {
5252
fs.stat(dir, (err, stats) => {
5353
t.error(err, 'go-ipfs should stat without error')
5454
t.ok(stats, 'go-ipfs was downloaded')
55+
// Check detected path
56+
fs.stat(Download.path(), (err3, stats3) => {
57+
t.error(err3, 'detected binary path should stat without error')
58+
t.ok(stats3.mode, 'downloaded binary was detected')
59+
})
5560
})
5661
})
5762
})
5863

5964
test('Ensure Windows version gets downloaded', (t) => {
60-
t.plan(7)
65+
t.plan(9)
6166
const dir = path.resolve(__dirname, '../go-ipfs')
6267
rimraf.sync(dir)
6368
download(version, 'windows', (err, res) => {
@@ -72,13 +77,18 @@ test('Ensure Windows version gets downloaded', (t) => {
7277
fs.stat(path.join(dir, 'ipfs.exe'), (err2, stats2) => {
7378
t.error(err2, 'windows bin should stat without error')
7479
t.ok(stats2, 'windows bin was downloaded')
80+
// Check detected path
81+
fs.stat(Download.path(), (err3, stats3) => {
82+
t.error(err3, 'detected binary path should stat without error')
83+
t.ok(stats3.mode, 'downloaded binary was detected')
84+
})
7585
})
7686
})
7787
})
7888
})
7989

8090
test('Ensure Linux version gets downloaded', (t) => {
81-
t.plan(7)
91+
t.plan(9)
8292
const dir = path.resolve(__dirname, '../go-ipfs')
8393
rimraf.sync(dir)
8494
download(version, 'linux', (err, res) => {
@@ -93,13 +103,18 @@ test('Ensure Linux version gets downloaded', (t) => {
93103
fs.stat(path.join(dir, 'ipfs'), (err2, stats2) => {
94104
t.error(err2, 'linux bin should stat without error')
95105
t.ok(stats2, 'linux bin was downloaded')
106+
// Check detected path
107+
fs.stat(Download.path(), (err3, stats3) => {
108+
t.error(err3, 'detected binary path should stat without error')
109+
t.ok(stats3.mode, 'downloaded binary was detected')
110+
})
96111
})
97112
})
98113
})
99114
})
100115

101116
test('Ensure OSX version gets downloaded', (t) => {
102-
t.plan(7)
117+
t.plan(9)
103118
const dir = path.resolve(__dirname, '../go-ipfs')
104119
rimraf.sync(dir)
105120
download(version, 'darwin', (err, res) => {
@@ -114,13 +129,18 @@ test('Ensure OSX version gets downloaded', (t) => {
114129
fs.stat(path.join(dir, 'ipfs'), (err2, stats2) => {
115130
t.error(err2, 'OSX bin should stat without error')
116131
t.ok(stats2, 'OSX bin was downloaded')
132+
// Check detected path
133+
fs.stat(Download.path(), (err3, stats3) => {
134+
t.error(err3, 'detected binary path should stat without error')
135+
t.ok(stats3.mode, 'downloaded binary was detected')
136+
})
117137
})
118138
})
119139
})
120140
})
121141

122142
test('Ensure TARGET_OS, TARGET_VERSION and TARGET_ARCH version gets downloaded', (t) => {
123-
t.plan(7)
143+
t.plan(9)
124144
const dir = path.resolve(__dirname, '../go-ipfs')
125145
rimraf.sync(dir)
126146
process.env.TARGET_OS = 'windows'
@@ -142,9 +162,14 @@ test('Ensure TARGET_OS, TARGET_VERSION and TARGET_ARCH version gets downloaded',
142162
fs.stat(path.join(dir, 'ipfs.exe'), (err2, stats2) => {
143163
t.error(err2, 'windows bin should stat without error')
144164
t.ok(stats2, 'windows bin was downloaded')
145-
delete process.env.TARGET_OS
146-
delete process.env.TARGET_VERSION
147-
delete process.env.TARGET_ARCH
165+
// Check detected path
166+
fs.stat(Download.path(), (err3, stats3) => {
167+
t.error(err3, 'detected binary path should stat without error')
168+
t.ok(stats3.mode, 'downloaded binary was detected')
169+
delete process.env.TARGET_OS
170+
delete process.env.TARGET_VERSION
171+
delete process.env.TARGET_ARCH
172+
})
148173
})
149174
})
150175
})
@@ -171,3 +196,18 @@ test('Returns an error when dist url is 404', (t) => {
171196
delete process.env.GO_IPFS_DIST_URL
172197
})
173198
})
199+
200+
test('Path returns undefined when no binary has been downloaded', (t) => {
201+
t.plan(1)
202+
const dir = path.resolve(__dirname, '../go-ipfs')
203+
rimraf.sync(dir)
204+
t.ok(Download.path.silent() === undefined, 'Path is undefined before installation')
205+
})
206+
207+
test('Path returns undefined when no binary has been downloaded', (t) => {
208+
t.plan(1)
209+
const dir = path.resolve(__dirname, '../go-ipfs')
210+
rimraf.sync(dir)
211+
212+
t.throws(Download.path, /not found/, 'Path throws if binary is not installed')
213+
})

0 commit comments

Comments
 (0)