From b825239c82abaa3bb4309c15b5da8e750ce171c7 Mon Sep 17 00:00:00 2001 From: David Dias Date: Thu, 7 Apr 2016 21:20:51 -0400 Subject: [PATCH] buuunch of stuff --- .gitignore | 2 +- LICENSE | 2 +- README.md | 56 +++++++++----------------------------- bin/ipfs | 5 ---- filesize.js | 20 -------------- index.js | 2 -- install.js | 75 --------------------------------------------------- package.json | 11 +++++--- publish.sh | 39 --------------------------- src/bin.js | 2 ++ src/index.js | 48 +++++++++++++++++++++++++++++++++ test/index.js | 18 ++++++------- update.js | 56 -------------------------------------- 13 files changed, 79 insertions(+), 257 deletions(-) delete mode 100755 bin/ipfs delete mode 100755 filesize.js delete mode 100644 index.js delete mode 100644 install.js delete mode 100755 publish.sh create mode 100644 src/bin.js create mode 100644 src/index.js delete mode 100755 update.js diff --git a/.gitignore b/.gitignore index 3efd630..41339df 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ -tmp node_modules +go-ipfs diff --git a/LICENSE b/LICENSE index c7386b3..9ce9744 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) 2014 Juan Batiz-Benet +Copyright (c) 2016 Juan Batiz-Benet Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index d5d113a..74e6da7 100644 --- a/README.md +++ b/README.md @@ -1,56 +1,24 @@ -# install go-ipfs from npm +Install go-ipfs from npm as a dependency of your project +======================================================== -[![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](http://ipn.io) [![](https://img.shields.io/badge/project-IPFS-blue.svg?style=flat-square)](http://ipfs.io/) [![](https://img.shields.io/badge/freenode-%23ipfs-blue.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23ipfs) [![Dependency Status](https://david-dm.org/ipfs/npm-go-ipfs.svg?style=flat-square)](https://david-dm.org/ipfs/npm-go-ipfs) -Install the latest [go-ipfs](https://github.com/ipfs/go-ipfs/) binary. - -``` -npm install -g go-ipfs -``` - -Installs from [https://gobuilder.me/github.com/ipfs/go-ipfs/cmd/ipfs](https://gobuilder.me/github.com/ipfs/go-ipfs/cmd/ipfs). +[![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](http://ipn.io) +[![](https://img.shields.io/badge/project-IPFS-blue.svg?style=flat-square)](http://ipfs.io/) +[![](https://img.shields.io/badge/freenode-%23ipfs-blue.svg?style=flat-square)](http://webchat.freenode.net/?channels=%23ipfs) +[![Dependency Status](https://david-dm.org/ipfs/npm-go-ipfs.svg?style=flat-square)](https://david-dm.org/ipfs/npm-go-ipfs) +> Install the latest [go-ipfs](https://github.com/ipfs/go-ipfs/) binary from [http://dist.ipfs.io](http://dist.ipfs.io) -## Usage +# Installation -```sh -> npm install -g go-ipfs -> ipfs version -ipfs version 0.3.11 - -> npm install go-ipfs -> node_modules/.bin/ipfs -ipfs version 0.3.11 +``` +npm install go-ipfs --save ``` 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). -Warning: this module uses the _latest_ version of ipfs. If there is a strong need to vendor an older version, let us know. We care about versions very much :( but for a number of reasons, this is easier for us all right now. - +Note: this module uses the _latest_ version of ipfs. If there is a strong need to vendor an older version, let us know. We care about versions very much, but for a number of reasons, this is easier for us all right now. ## Development -**Warning**: the file `bin/ipfs` is a placeholder, when downloading stuff, it gets replaced. so if you run `node install.js` it will then be dirty in the git repo. **Do not commit this file**, as then you would be commiting a big binary and publishing it to npm. (**TODO: add a pre-commit or pre-publish hook that warns about this**) - -### Publish a new version - -You should be able to just run `./publish.sh` for example: - -```sh -> ./publish.sh -usage ./publish.sh -publish a version of go-ipfs to npm - -> ./publish.sh 0.3.11 -``` - -This will: - -- check the version is indeed a tag in https://github.com/ipfs/go-ipfs -- check the size of `bin/ipfs` is right (must be the checked in file) -- update the version numbers in `package.json` and `README.md` -- `git commit` the changes -- push to https://github.com/ipfs/npm-go-ipfs -- publish to `go-ipfs@$version` to https://npmjs.com/package/go-ipfs - -Open an issue in the repo if you run into trouble. +**Warning**: The binary gets put in the `go-ipfs` folder inside the module folder. diff --git a/bin/ipfs b/bin/ipfs deleted file mode 100755 index c6b714f..0000000 --- a/bin/ipfs +++ /dev/null @@ -1,5 +0,0 @@ -#!/bin/sh -echo "something went wrong installing ipfs." -echo "this file should've been overwritten" -echo "try again?" -exit 1 diff --git a/filesize.js b/filesize.js deleted file mode 100755 index 8a79519..0000000 --- a/filesize.js +++ /dev/null @@ -1,20 +0,0 @@ -#!/usr/bin/env node -// portable file size -if (process.argv.length != 3) { - process.stdout.write("usage: ./filesize.js \n") - process.stdout.write("return filesize of (portably!)\n") - process.exit(0) -} - -function die(err) { - process.stderr.write("error: " + err) - process.exit(1) -} - -var fs = require('fs') -var path = process.argv[2] -fs.stat(path, function(err, stat) { - if (err) return die(err) - - process.stdout.write("" + stat.size + "\n") -}) diff --git a/index.js b/index.js deleted file mode 100644 index f86f788..0000000 --- a/index.js +++ /dev/null @@ -1,2 +0,0 @@ -var path = require('path') -module.exports = path.join(__dirname, "bin", "ipfs") diff --git a/install.js b/install.js deleted file mode 100644 index 2a92be0..0000000 --- a/install.js +++ /dev/null @@ -1,75 +0,0 @@ -#!/usr/bin/env node - -var fs = require('fs') -var path = require('path') -var mkdirp = require('mkdirp'); -var nugget = require('nugget') -var goenv = require('go-platform') -var extract = require('extract-zip') -var version = require('./package.json').version -var ipfs = require('./') - -checkPlatform(goenv) // make sure we can do this. - -// hacky hack hack to work around unpublishability -version = version.replace(/-[0-9]+/, "") - -var filename = 'ipfs_v'+ version +'_' + goenv.GOOS + '-' + goenv.GOARCH + '.zip' -var url = 'https://gobuilder.me/get/github.com/ipfs/go-ipfs/cmd/ipfs/' + filename - -var bin = path.dirname(ipfs) -var tmp = path.join(__dirname, 'tmp') -var installPath = path.join(bin, 'ipfs') -var zipfile = path.join(tmp, filename) - -// mk tmp dir -mkdirp(tmp, function(err) { - if (err) onerror(err) - - // download binary - nugget(url, {target: filename, dir: tmp, resume: true, verbose: true}, function (err) { - if (err) return onerror(err) - - // extract zip - extract(zipfile, {dir: tmp}, function (err) { - if (err) return onerror(err) - - // move ipfs binary into place. - fs.rename(path.join(tmp, "ipfs", "ipfs"), installPath, function(err) { - if (err) return onerror(err) - - // remove zip from disk - fs.unlink(zipfile, function(err) { - if (err) return onerror(err) - - }) - }) - }) - }) -}) - -function onerror (err) { - if (err) throw err -} - -function checkPlatform(goenv) { - switch (goenv.GOOS) { - case "darwin": - case "linux": - case "freebsd": - break - - default: - throw new Error("no binary available for os:" + goenv.GOOS) - } - - switch (goenv.GOARCH) { - case "amd64": - case "386": - case "arm": - break - - default: - throw new Error("no binary available for arch: " + goenv.GOARCH) - } -} diff --git a/package.json b/package.json index 3798e34..d0f4552 100644 --- a/package.json +++ b/package.json @@ -2,9 +2,9 @@ "name": "go-ipfs", "version": "0.3.11", "description": "Install the latest go-ipfs binary", - "main": "index.js", + "main": "src/index.js", "scripts": { - "install": "node install.js", + "install": "node src/bin.js", "test": "tape test/*.js | tap-spec" }, "bin": { @@ -15,10 +15,13 @@ "url": "git+https://github.com/ipfs/npm-go-ipfs.git" }, "dependencies": { - "extract-zip": "^1.0.3", "go-platform": "^1.0.0", + "gunzip-maybe": "^1.3.1", "mkdirp": "^0.5.0", - "nugget": "^1.2.0" + "nugget": "^1.2.0", + "request": "^2.70.0", + "tar-fs": "^1.12.0", + "tar.gz": "^1.0.3" }, "keywords": [ "ipfs", diff --git a/publish.sh b/publish.sh deleted file mode 100755 index 283eb71..0000000 --- a/publish.sh +++ /dev/null @@ -1,39 +0,0 @@ -#!/bin/sh - -USAGE="usage $0 \npublish a version of go-ipfs to npm" -if [ "$#" -ne 1 ]; then - echo $USAGE - exit 0 -fi -# arguments: -version=$1 - -die() { - echo >&2 "error: $@" - exit 1 -} - -# check the version is indeed a tag in go-ipfs -repo="https://github.com/ipfs/go-ipfs" -echo "--> checking version is tag in $repo" -git ls-remote -t $repo | egrep "refs/tags/v$version$" >/dev/null 2>&1 || - die "$version not a tag in $repo" - -# check the bin/ipfs file is less than one MB. -echo "--> check the bin/ipfs size" -binsize=$(./filesize.js bin/ipfs) -if [ "$binsize" -gt "4096" ]; then - die "bin directory larger than expected. -did you change bin/ipfs? try: git checkout bin/ipfs" -fi - -echo "--> updating versions in repo" -./update.js "$version" - -echo "\n--> publishing to git/github" -git add README.md package.json -git commit -m "v$version" -git push origin master - -echo "\n--> publishing go-ipfs@$version to npm" -npm publish diff --git a/src/bin.js b/src/bin.js new file mode 100644 index 0000000..a3d7817 --- /dev/null +++ b/src/bin.js @@ -0,0 +1,2 @@ +#!/usr/bin/env node +require('./')(function () {}) diff --git a/src/index.js b/src/index.js new file mode 100644 index 0000000..2eb2f4e --- /dev/null +++ b/src/index.js @@ -0,0 +1,48 @@ +var path = require('path') +var goenv = require('go-platform') +var version = require('./../package.json').version +var request = require('request') +var gunzip = require('gunzip-maybe') +var tarFS = require('tar-fs') + +module.exports = function (callback) { + checkPlatform(goenv) // make sure we can do this. + + // hacky hack hack to work around unpublishability + version = version.replace(/-[0-9]+/, '') + + var filename = 'ipfs_v' + version + '_' + goenv.GOOS + '-' + goenv.GOARCH + '.tar.gz' + var url = 'http://dist.ipfs.io/go-ipfs/v' + version + '/go-' + filename + + var installPath = path.resolve(__dirname, '..') + + request + .get(url) + .pipe(gunzip()) + .pipe( + tarFS + .extract(installPath) + .on('finish', callback) + ) + + function checkPlatform (goenv) { + switch (goenv.GOOS) { + case 'darwin': + case 'linux': + case 'freebsd': + break + default: + throw new Error('no binary available for os:' + goenv.GOOS) + } + + switch (goenv.GOARCH) { + case 'amd64': + case '386': + case 'arm': + break + + default: + throw new Error('no binary available for arch: ' + goenv.GOARCH) + } + } +} diff --git a/test/index.js b/test/index.js index a61ed04..aac946b 100644 --- a/test/index.js +++ b/test/index.js @@ -1,16 +1,14 @@ var test = require('tape') var fs = require('fs') -var cp = require('child_process') -var ipfs = require('../') +var download = require('../src') +var path = require('path') -test('ensure ipfs bin path exists', function(t) { - t.plan(4) - fs.stat(ipfs, function(err, stats) { - t.error(err, 'ipfs bin should stat witout error') - cp.exec([ipfs,'version'].join(' '), function(err, stdout, stderr) { - t.error(err, 'ipfs runs without error') - t.true(stdout.indexOf('ipfs version') >= 0, 'ipfs version retreived') - t.false(stderr, 'no stderr output') +test('Ensure ipfs gets downloaded', function (t) { + t.plan(2) + download(function () { + fs.stat(path.resolve(__dirname, '../go-ipfs'), function (err, stats) { + t.error(err, 'ipfs bin should stat without error') + t.ok(stats, 'ipfs was downloaded') }) }) }) diff --git a/update.js b/update.js deleted file mode 100755 index 0945772..0000000 --- a/update.js +++ /dev/null @@ -1,56 +0,0 @@ -#!/usr/bin/env node - -var fs = require('fs') - -// portable file size -if (process.argv.length != 3) { - process.stdout.write("usage: ./update.js \n") - process.stdout.write("update module to \n") - process.exit(0) -} - -var version = process.argv[2] - -var pFind = /^ "version": "[^"]+",$/m -var pRep = ' "version": "'+ version +'",' -fsReplace("package.json", pFind, pRep, function(err) { - dieOnErr(err) - console.log("updated package.json") -}) - -var rFind = /ipfs version [0-9\.]+/g -var rRep = "ipfs version " + version -fsReplace("README.md", rFind, rRep, function(err) { - dieOnErr(err) - console.log("updated README.md") -}) - -function die(err) { - process.stderr.write(err + "\n") - process.exit(1) -} - -function dieOnErr(err) { - if (err) die(err) -} - -function fsReplace(path, find, replace, cb) { - fs.readFile(path, function(err, contents) { - if (err) { - err = new Error("failed to read " + path + ": " + err) - return cb(err) - } - - contents = contents.toString() - var nc = contents.replace(find, replace) - - fs.writeFile(path, nc, function(err) { - if (err) { - err = new Error("failed to write " + path + ": " + err) - return cb(err) - } - - cb(null) // success. - }) - }) -}