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

Commit 708f24e

Browse files
olizillaAlan Shaw
authored and
Alan Shaw
committed
fix: use go-ipfs.version from parent package.json (#34)
* fix: use go-ipfs.version from parent package.json fixes #33 * chore: add pkg-conf dep * fix: use version from fixture and other stories - fix all the things @alanshaw spotted License: MIT Signed-off-by: Oli Evans <[email protected]>
1 parent 363d027 commit 708f24e

File tree

5 files changed

+80
-22
lines changed

5 files changed

+80
-22
lines changed

README.md

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
Install go-ipfs from npm as a dependency of your project
2-
========================================================
1+
# go-ipfs-dep
32

3+
> Download [go-ipfs](https://github.com/ipfs/go-ipfs/) to your node_modules.
44
55
[![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](http://ipn.io)
66
[![](https://img.shields.io/badge/project-IPFS-blue.svg?style=flat-square)](http://ipfs.io/)
77
[![](https://img.shields.io/badge/freenode-%23ipfs-blue.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23ipfs)
88
[![Travis CI](https://flat.badgen.net/travis/ipfs/npm-go-ipfs-dep)](https://travis-ci.com/ipfs/npm-go-ipfs-dep)
99
[![Dependency Status](https://david-dm.org/ipfs/npm-go-ipfs.svg?style=flat-square)](https://david-dm.org/ipfs/npm-go-ipfs)
1010

11-
> Install the latest [go-ipfs](https://github.com/ipfs/go-ipfs/) binary from [http://dist.ipfs.io](http://dist.ipfs.io)
1211

1312
# Installation
1413

@@ -18,13 +17,15 @@ npm install go-ipfs-dep --save
1817

1918
See [IPFS getting-started](http://ipfs.io/docs/getting-started). If anything goes wrong, try using: [http://ipfs.io/docs/install](http://ipfs.io/docs/install).
2019

21-
## Development
20+
## Usage
21+
22+
This module downloads `go-ipfs` binaries from https://dist.ipfs.io into your project.
2223

23-
**Warning**: The binary gets put in the `go-ipfs` folder inside the module folder.
24+
By default it will download the go-ipfs version that matches the npm version of this module. So depending on `go-ipfs[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`.
2425

25-
### Which go-ipfs version this package downloads?
26+
### Overriding the go-ipfs version
2627

27-
Can be specified in `package.json` with a field `go-ipfs.version`, eg:
28+
You can override the version of go-ipfs that gets downloaded by adding by adding a `go-ipfs.version` field to your `package.json`
2829

2930
```json
3031
"go-ipfs": {
@@ -33,7 +34,8 @@ Can be specified in `package.json` with a field `go-ipfs.version`, eg:
3334
```
3435

3536
### Using local IPFS daemon as the package download url
36-
Can be specified in `package.json` with a field `go-ipfs.version`, eg:
37+
38+
The url to download the binaries from can be specified by adding a field `go-ipfs.distUrl` field to your `package.json`, eg:
3739

3840
```json
3941
"go-ipfs": {
@@ -66,6 +68,10 @@ node src/bin.js <version> <platform> <architecture> <install directory>
6668
node src/bin.js v0.4.3 linux amd64 ./go-ipfs
6769
```
6870

71+
## Development
72+
73+
**Note**: The binary gets put in the `go-ipfs` folder inside the module folder.
74+
6975
## Deployment
7076

7177
### Prerelease

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"go-platform": "^1.0.0",
2121
"gunzip-maybe": "^1.4.1",
2222
"node-fetch": "^2.3.0",
23+
"pkg-conf": "^3.1.0",
2324
"tar-fs": "^2.0.0",
2425
"unzip-stream": "^0.3.0"
2526
},
@@ -35,7 +36,8 @@
3536
"homepage": "https://github.com/ipfs/npm-go-ipfs-dep",
3637
"devDependencies": {
3738
"aegir": "^18.2.0",
38-
"rimraf": "^2.6.2",
39+
"execa": "^1.0.0",
40+
"rimraf": "^2.6.3",
3941
"standard": "^12.0.1",
4042
"tap-spec": "^5.0.0",
4143
"tape": "^4.9.1"
@@ -49,6 +51,7 @@
4951
5052
"Harlan T Wood <[email protected]>",
5153
"Henrique Dias <[email protected]>",
54+
"Oli Evans <[email protected]>",
5255
"Scott Hardy <[email protected]>",
5356
"Stephen Whitmore <[email protected]>",
5457

src/index.js

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const path = require('path')
2525
const tarFS = require('tar-fs')
2626
const unzip = require('unzip-stream')
2727
const fetch = require('node-fetch')
28+
const pkgConf = require('pkg-conf')
2829
const pkg = require('./../package.json')
2930

3031
function unpack ({ url, installPath, stream }) {
@@ -56,21 +57,18 @@ async function download ({ installPath, url }) {
5657
}
5758

5859
function cleanArguments (version, platform, arch, installPath) {
59-
const goIpfsInfo = pkg['go-ipfs']
60-
61-
const goIpfsVersion = (goIpfsInfo && goIpfsInfo.version)
62-
? pkg['go-ipfs'].version
63-
: 'v' + pkg.version.replace(/-[0-9]+/, '')
64-
65-
const distUrl = (goIpfsInfo && goIpfsInfo.distUrl)
66-
? pkg['go-ipfs'].distUrl
67-
: 'https://dist.ipfs.io'
68-
60+
const conf = pkgConf.sync('go-ipfs', {
61+
cwd: path.join(process.cwd(), '..'),
62+
defaults: {
63+
version: 'v' + pkg.version.replace(/-[0-9]+/, ''),
64+
distUrl: 'https://dist.ipfs.io'
65+
}
66+
})
6967
return {
70-
version: process.env.TARGET_VERSION || version || goIpfsVersion,
68+
version: process.env.TARGET_VERSION || version || conf.version,
7169
platform: process.env.TARGET_OS || platform || goenv.GOOS,
7270
arch: process.env.TARGET_ARCH || arch || goenv.GOARCH,
73-
distUrl: process.env.GO_IPFS_DIST_URL || distUrl,
71+
distUrl: process.env.GO_IPFS_DIST_URL || conf.distUrl,
7472
installPath: installPath ? path.resolve(installPath) : process.cwd()
7573
}
7674
}
@@ -105,7 +103,7 @@ async function getDownloadURL ({ version, platform, arch, distUrl }) {
105103
}
106104

107105
module.exports = async function () {
108-
const args = cleanArguments(...arguments)
106+
const args = await cleanArguments(...arguments)
109107
const url = await getDownloadURL(args)
110108

111109
process.stdout.write(`Downloading ${url}\n`)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "example",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"author": "",
7+
"license": "ISC",
8+
"go-ipfs": {
9+
"version": "v0.4.21-rc3"
10+
}
11+
}

test/install.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
const fs = require('fs')
2+
const path = require('path')
3+
const test = require('tape')
4+
const execa = require('execa')
5+
const rimraf = require('rimraf')
6+
7+
/*
8+
Test that go-ipfs is downloaded during npm install.
9+
- package up the current source code with `npm pack`
10+
- install the tarball into the example project
11+
- ensure that the "go-ipfs.version" prop in the package.json is used
12+
*/
13+
14+
const testVersion = require('./fixture/example-project/package.json')['go-ipfs'].version
15+
let tarballName = null
16+
17+
function packTarball () {
18+
const parentDir = path.join(__dirname, '..')
19+
const res = execa.sync('npm', ['pack', parentDir], {
20+
cwd: __dirname
21+
})
22+
tarballName = res.stdout
23+
return tarballName
24+
}
25+
26+
test.onFinish(() => {
27+
fs.unlinkSync(path.join(__dirname, tarballName))
28+
rimraf.sync(path.join('fixture', 'example-project', 'node_modules'))
29+
})
30+
31+
test('Ensure go-ipfs.version defined in parent package.json is used', (t) => {
32+
const tarballName = packTarball()
33+
// from `example-project`, install the module
34+
const res = execa.sync('npm', ['install', '--no-save', path.join('..', '..', tarballName)], {
35+
cwd: path.join(__dirname, 'fixture', 'example-project')
36+
})
37+
const msg = `Downloading https://dist.ipfs.io/go-ipfs/${testVersion}`
38+
t.ok(res.stdout.includes(msg), msg)
39+
t.end()
40+
})

0 commit comments

Comments
 (0)