diff --git a/package.json b/package.json index cbccdcf396..ac8bbc83a7 100644 --- a/package.json +++ b/package.json @@ -79,9 +79,11 @@ "ipfs-multipart": "^0.1.0", "ipfs-repo": "^0.5.0", "joi": "^8.0.2", + "libp2p-ipfs": "^0.1.0", "lodash.get": "^4.0.0", "lodash.set": "^4.0.0", "peer-id": "^0.6.0", + "peer-info": "^0.6.0", "ronin": "^0.3.11", "temp": "^0.8.3" } diff --git a/src/cli/commands/bootstrap/add.js b/src/cli/commands/bootstrap/add.js index f891438534..80625d1dd9 100644 --- a/src/cli/commands/bootstrap/add.js +++ b/src/cli/commands/bootstrap/add.js @@ -1,9 +1,9 @@ 'use strict' const Command = require('ronin').Command -const IPFS = require('../../../ipfs-core') const debug = require('debug') const log = debug('cli:bootstrap') +const utils = require('../../utils') log.error = debug('cli:bootstrap:error') module.exports = Command.extend({ @@ -12,9 +12,15 @@ module.exports = Command.extend({ options: {}, run: (multiaddr) => { - var node = new IPFS() - node.bootstrap.add(multiaddr, (err, list) => { - if (err) { return log.error(err) } + utils.getIPFS((err, ipfs) => { + if (err) { + throw err + } + ipfs.bootstrap.add(multiaddr, (err, list) => { + if (err) { + return log.error(err) + } + }) }) } }) diff --git a/src/cli/commands/bootstrap/list.js b/src/cli/commands/bootstrap/list.js index 9ca2f1e238..a6b1478b49 100644 --- a/src/cli/commands/bootstrap/list.js +++ b/src/cli/commands/bootstrap/list.js @@ -1,7 +1,7 @@ 'use strict' const Command = require('ronin').Command -const IPFS = require('../../../ipfs-core') +const utils = require('../../utils') const debug = require('debug') const log = debug('cli:bootstrap') log.error = debug('cli:bootstrap:error') @@ -11,12 +11,18 @@ module.exports = Command.extend({ options: {}, - run: (name) => { - var node = new IPFS() - node.bootstrap.list((err, list) => { - if (err) { return log.error(err) } - list.forEach((node) => { - console.log(node) + run: () => { + utils.getIPFS((err, ipfs) => { + if (err) { + throw err + } + ipfs.bootstrap.list((err, list) => { + if (err) { + throw err + } + list.forEach((node) => { + console.log(node) + }) }) }) } diff --git a/src/cli/commands/bootstrap/rm.js b/src/cli/commands/bootstrap/rm.js index 06bd38e935..c8a41665cf 100644 --- a/src/cli/commands/bootstrap/rm.js +++ b/src/cli/commands/bootstrap/rm.js @@ -1,10 +1,10 @@ 'use strict' const Command = require('ronin').Command -const IPFS = require('../../../ipfs-core') const debug = require('debug') const log = debug('cli:bootstrap') log.error = debug('cli:bootstrap:error') +const utils = require('../../utils') module.exports = Command.extend({ desc: 'Removes peers from the bootstrap list', @@ -12,9 +12,15 @@ module.exports = Command.extend({ options: {}, run: (multiaddr) => { - var node = new IPFS() - node.bootstrap.rm(multiaddr, (err, list) => { - if (err) { return log.error(err) } + utils.getIPFS((err, ipfs) => { + if (err) { + throw err + } + ipfs.bootstrap.rm(multiaddr, (err, list) => { + if (err) { + return log.error(err) + } + }) }) } }) diff --git a/src/cli/commands/cat.js b/src/cli/commands/cat.js index deedf71f28..4e829e0c8c 100644 --- a/src/cli/commands/cat.js +++ b/src/cli/commands/cat.js @@ -1,7 +1,7 @@ -var Command = require('ronin').Command +const Command = require('ronin').Command module.exports = Command.extend({ desc: '', - run: function (name) {} + run: function () {} }) diff --git a/src/cli/commands/config.js b/src/cli/commands/config.js index 71b8e042a4..86c4527aea 100644 --- a/src/cli/commands/config.js +++ b/src/cli/commands/config.js @@ -1,12 +1,10 @@ -'use strict' - const Command = require('ronin').Command -const IPFS = require('../../ipfs-core') const debug = require('debug') const get = require('lodash.get') const set = require('lodash.set') const log = debug('cli:config') log.error = debug('cli:config:error') +const utils = require('../utils') module.exports = Command.extend({ desc: 'Get and set IPFS config values', @@ -27,48 +25,52 @@ module.exports = Command.extend({ throw new Error("argument 'key' is required") } - var node = new IPFS() - - if (!value) { - // Get the value of a given key + utils.getIPFS((err, ipfs) => { + if (err) { + throw err + } - node.config.show((err, config) => { - if (err) { - log.error(err) - throw new Error('failed to read the config') - } + if (!value) { + // Get the value of a given key - const value = get(config, key) - console.log(value) - }) - } else { - // Set the new value of a given key + ipfs.config.show((err, config) => { + if (err) { + log.error(err) + throw new Error('failed to read the config') + } - if (bool) { - value = (value === 'true') - } else if (json) { - try { - value = JSON.parse(value) - } catch (err) { - log.error(err) - throw new Error('invalid JSON provided') - } - } + const value = get(config, key) + console.log(value) + }) + } else { + // Set the new value of a given key - node.config.show((err, originalConfig) => { - if (err) { - log.error(err) - throw new Error('failed to read the config') + if (bool) { + value = (value === 'true') + } else if (json) { + try { + value = JSON.parse(value) + } catch (err) { + log.error(err) + throw new Error('invalid JSON provided') + } } - const updatedConfig = set(originalConfig, key, value) - node.config.replace(updatedConfig, (err) => { + ipfs.config.show((err, originalConfig) => { if (err) { log.error(err) - throw new Error('failed to save the config') + throw new Error('failed to read the config') } + + const updatedConfig = set(originalConfig, key, value) + ipfs.config.replace(updatedConfig, (err) => { + if (err) { + log.error(err) + throw new Error('failed to save the config') + } + }) }) - }) - } + } + }) } }) diff --git a/src/cli/commands/config/edit.js b/src/cli/commands/config/edit.js index fd7e5f1d98..2770cecdc5 100644 --- a/src/cli/commands/config/edit.js +++ b/src/cli/commands/config/edit.js @@ -5,104 +5,108 @@ const spawn = require('child_process').spawn const fs = require('fs') const temp = require('temp') const async = require('async') -const IPFS = require('../../../ipfs-core') const debug = require('debug') const log = debug('cli:config') log.error = debug('cli:config:error') +const utils = require('../../utils') module.exports = Command.extend({ desc: 'Opens the config file for editing in $EDITOR', run: (name) => { - const node = new IPFS() - const editor = process.env.EDITOR - - if (!editor) { - throw new Error('ENV variable $EDITOR not set') - } + utils.getIPFS((err, ipfs) => { + if (err) { + throw err + } + const editor = process.env.EDITOR - function getConfig (next) { - node.config.show((err, config) => { - if (err) { - log.error(err) - next(new Error('failed to get the config')) - } + if (!editor) { + throw new Error('ENV variable $EDITOR not set') + } - next(null, config) - }) - } + function getConfig (next) { + ipfs.config.show((err, config) => { + if (err) { + log.error(err) + next(new Error('failed to get the config')) + } - function saveTempConfig (config, next) { - temp.open('ipfs-config', (err, info) => { - if (err) { - log.error(err) - next(new Error('failed to open the config')) - } + next(null, config) + }) + } - fs.write(info.fd, JSON.stringify(config, null, 2)) - fs.close(info.fd, (err) => { + function saveTempConfig (config, next) { + temp.open('ipfs-config', (err, info) => { if (err) { log.error(err) next(new Error('failed to open the config')) } + + fs.write(info.fd, JSON.stringify(config, null, 2)) + fs.close(info.fd, (err) => { + if (err) { + log.error(err) + next(new Error('failed to open the config')) + } + }) + + next(null, info.path) }) + } - next(null, info.path) - }) - } + function openEditor (path, next) { + const child = spawn(editor, [path], { + stdio: 'inherit' + }) - function openEditor (path, next) { - const child = spawn(editor, [path], { - stdio: 'inherit' - }) + child.on('exit', (err, code) => { + if (err) { + log.error(err) + throw new Error('error on the editor') + } - child.on('exit', (err, code) => { - if (err) { - log.error(err) - throw new Error('error on the editor') - } + next(null, path) + }) + } - next(null, path) - }) - } + function readTempConfig (path, next) { + fs.readFile(path, 'utf8', (err, data) => { + if (err) { + log.error(err) + next(new Error('failed to get the updated config')) + } - function readTempConfig (path, next) { - fs.readFile(path, 'utf8', (err, data) => { - if (err) { - log.error(err) - next(new Error('failed to get the updated config')) - } + try { + next(null, JSON.parse(data)) + } catch (err) { + log.error(err) + next(new Error(`failed to parse the updated config "${err.message}"`)) + } + }) + } - try { - next(null, JSON.parse(data)) - } catch (err) { - log.error(err) - next(new Error(`failed to parse the updated config "${err.message}"`)) - } - }) - } + function saveConfig (config, next) { + ipfs.config.replace(config, (err) => { + if (err) { + log.error(err) + next(new Error('failed to save the config')) + } + + next() + }) + } - function saveConfig (config, next) { - node.config.replace(config, (err) => { + async.waterfall([ + getConfig, + saveTempConfig, + openEditor, + readTempConfig, + saveConfig + ], (err) => { if (err) { - log.error(err) - next(new Error('failed to save the config')) + throw err } - - next() }) - } - - async.waterfall([ - getConfig, - saveTempConfig, - openEditor, - readTempConfig, - saveConfig - ], (err) => { - if (err) { - throw err - } }) } }) diff --git a/src/cli/commands/config/replace.js b/src/cli/commands/config/replace.js index 0279714acb..87a68014ad 100644 --- a/src/cli/commands/config/replace.js +++ b/src/cli/commands/config/replace.js @@ -1,11 +1,9 @@ -'use strict' - const Command = require('ronin').Command -const IPFS = require('../../../ipfs-core') const debug = require('debug') const path = require('path') const log = debug('cli:config') log.error = debug('cli:config:error') +const utils = require('utils') module.exports = Command.extend({ desc: 'Replaces the config with ', @@ -13,13 +11,19 @@ module.exports = Command.extend({ options: {}, run: (configPath) => { - var node = new IPFS() - var config = require(path.resolve(process.cwd(), configPath)) + utils.getIPFS((err, ipfs) => { + if (err) { + throw err + } + const config = require(path.resolve(process.cwd(), configPath)) - node.config.replace(config, (err, version) => { - if (err) { return log.error(err) } + ipfs.config.replace(config, (err, version) => { + if (err) { + throw err + } - console.log(version) + console.log(version) + }) }) } }) diff --git a/src/cli/commands/config/show.js b/src/cli/commands/config/show.js index 27425530b9..444bd9f71a 100644 --- a/src/cli/commands/config/show.js +++ b/src/cli/commands/config/show.js @@ -1,10 +1,8 @@ -'use strict' - const Command = require('ronin').Command -const IPFS = require('../../../ipfs-core') const debug = require('debug') const log = debug('cli:config') log.error = debug('cli:config:error') +const utils = require('../../utils') module.exports = Command.extend({ desc: 'Outputs the content of the config file', @@ -12,11 +10,17 @@ module.exports = Command.extend({ options: {}, run: () => { - var node = new IPFS() - node.config.show((err, config) => { - if (err) { return log.error(err) } + utils.getIPFS((err, ipfs) => { + if (err) { + throw err + } + ipfs.config.show((err, config) => { + if (err) { + throw err + } - console.log(JSON.stringify(config, null, 4)) + console.log(JSON.stringify(config, null, 4)) + }) }) } }) diff --git a/src/cli/commands/dns.js b/src/cli/commands/dns.js index af4020b384..5d59b3e92d 100644 --- a/src/cli/commands/dns.js +++ b/src/cli/commands/dns.js @@ -1,10 +1,8 @@ -'use strict' - const Command = require('ronin').Command module.exports = Command.extend({ desc: '', - run: (name) => { + run: () => { } }) diff --git a/src/cli/commands/get.js b/src/cli/commands/get.js index 947d8e4b96..104a4bccc7 100644 --- a/src/cli/commands/get.js +++ b/src/cli/commands/get.js @@ -3,6 +3,5 @@ const Command = require('ronin').Command module.exports = Command.extend({ desc: '', - run: (name) => { - } + run: () => {} }) diff --git a/src/cli/commands/id.js b/src/cli/commands/id.js index c273446f9c..24f32d3d8c 100644 --- a/src/cli/commands/id.js +++ b/src/cli/commands/id.js @@ -15,13 +15,17 @@ module.exports = Command.extend({ }, run: (name) => { - var ipfs = utils.getIPFS() - - ipfs.id((err, id) => { + utils.getIPFS((err, ipfs) => { if (err) { - return log.error(err) + throw err } - console.log(id) + + ipfs.id((err, id) => { + if (err) { + return log.error(err) + } + console.log(id) + }) }) } }) diff --git a/src/cli/commands/init.js b/src/cli/commands/init.js index 8b3b7af9fb..fc85528eb2 100644 --- a/src/cli/commands/init.js +++ b/src/cli/commands/init.js @@ -1,5 +1,3 @@ -'use strict' - const Command = require('ronin').Command module.exports = Command.extend({ @@ -24,6 +22,5 @@ module.exports = Command.extend({ } }, - run: (name) => { - } + run: () => {} }) diff --git a/src/cli/commands/ls.js b/src/cli/commands/ls.js index af4020b384..104a4bccc7 100644 --- a/src/cli/commands/ls.js +++ b/src/cli/commands/ls.js @@ -1,10 +1,7 @@ -'use strict' - const Command = require('ronin').Command module.exports = Command.extend({ desc: '', - run: (name) => { - } + run: () => {} }) diff --git a/src/cli/commands/object/data.js b/src/cli/commands/object/data.js index 98a9e32e73..1591993c42 100644 --- a/src/cli/commands/object/data.js +++ b/src/cli/commands/object/data.js @@ -1,5 +1,3 @@ -'use strict' - const Command = require('ronin').Command const utils = require('../../utils') const bs58 = require('bs58') @@ -17,25 +15,28 @@ module.exports = Command.extend({ throw new Error("Argument 'key' is required") } - var ipfs = utils.getIPFS() - - const mh = utils.isDaemonOn() - ? key - : new Buffer(bs58.decode(key)) - - ipfs.object.data(mh, (err, data) => { + utils.getIPFS((err, ipfs) => { if (err) { - log.error(err) throw err } - - if (data instanceof Buffer) { - console.log(data.toString()) - return - } - - // js-ipfs-api output (http stream) - data.pipe(process.stdout) + const mh = utils.isDaemonOn() + ? key + : new Buffer(bs58.decode(key)) + + ipfs.object.data(mh, (err, data) => { + if (err) { + log.error(err) + throw err + } + + if (data instanceof Buffer) { + console.log(data.toString()) + return + } + + // js-ipfs-api output (http stream) + data.pipe(process.stdout) + }) }) } }) diff --git a/src/cli/commands/object/get.js b/src/cli/commands/object/get.js index d22e6f1502..66ecbefd8e 100644 --- a/src/cli/commands/object/get.js +++ b/src/cli/commands/object/get.js @@ -1,5 +1,3 @@ -'use strict' - const Command = require('ronin').Command const utils = require('../../utils') const bs58 = require('bs58') @@ -17,34 +15,37 @@ module.exports = Command.extend({ throw new Error("Argument 'key' is required") } - var ipfs = utils.getIPFS() + utils.getIPFS((err, ipfs) => { + if (err) { + throw err + } + if (utils.isDaemonOn()) { + return ipfs.object.get(key, (err, obj) => { + if (err) { + log.error(err) + throw err + } + + console.log(JSON.stringify(obj)) + }) + } - if (utils.isDaemonOn()) { - return ipfs.object.get(key, (err, obj) => { + const mh = new Buffer(bs58.decode(key)) + ipfs.object.get(mh, (err, obj) => { if (err) { log.error(err) throw err } - console.log(JSON.stringify(obj)) + console.log(JSON.stringify({ + Links: obj.links.map((link) => ({ + Name: link.name, + Hash: bs58.encode(link.hash).toString(), + Size: link.size + })), + Data: obj.data.toString() + })) }) - } - - const mh = new Buffer(bs58.decode(key)) - ipfs.object.get(mh, (err, obj) => { - if (err) { - log.error(err) - throw err - } - - console.log(JSON.stringify({ - Links: obj.links.map((link) => ({ - Name: link.name, - Hash: bs58.encode(link.hash).toString(), - Size: link.size - })), - Data: obj.data.toString() - })) }) } }) diff --git a/src/cli/commands/object/links.js b/src/cli/commands/object/links.js index d2973a04c4..7cb20a26f8 100644 --- a/src/cli/commands/object/links.js +++ b/src/cli/commands/object/links.js @@ -1,5 +1,3 @@ -'use strict' - const Command = require('ronin').Command const utils = require('../../utils') const bs58 = require('bs58') @@ -17,27 +15,30 @@ module.exports = Command.extend({ throw new Error("Argument 'key' is required") } - var ipfs = utils.getIPFS() - - const mh = utils.isDaemonOn() - ? key - : new Buffer(bs58.decode(key)) - - ipfs.object.links(mh, (err, links) => { + utils.getIPFS((err, ipfs) => { if (err) { - log.error(err) throw err } - - if (links.Links) { // js-ipfs-api output - links.Links.forEach((link) => { - console.log(link.Hash, link.Size, link.Name) + const mh = utils.isDaemonOn() + ? key + : new Buffer(bs58.decode(key)) + + ipfs.object.links(mh, (err, links) => { + if (err) { + log.error(err) + throw err + } + + if (links.Links) { // js-ipfs-api output + links.Links.forEach((link) => { + console.log(link.Hash, link.Size, link.Name) + }) + return + } + + links.forEach((link) => { + console.log(bs58.encode(link.hash).toString(), link.size, link.name) }) - return - } - - links.forEach((link) => { - console.log(bs58.encode(link.hash).toString(), link.size, link.name) }) }) } diff --git a/src/cli/commands/object/new.js b/src/cli/commands/object/new.js index e6191550e1..a6df1069a1 100644 --- a/src/cli/commands/object/new.js +++ b/src/cli/commands/object/new.js @@ -1,5 +1,3 @@ -'use strict' - const Command = require('ronin').Command const utils = require('../../utils') const bs58 = require('bs58') @@ -13,20 +11,23 @@ module.exports = Command.extend({ options: {}, run: (template) => { - var ipfs = utils.getIPFS() - - ipfs.object.new(template, (err, obj) => { + utils.getIPFS((err, ipfs) => { if (err) { - log.error(err) throw err } + ipfs.object.new(template, (err, obj) => { + if (err) { + log.error(err) + throw err + } - if (typeof obj.Hash === 'string') { // js-ipfs-api output - console.log(obj.Hash) - return - } + if (typeof obj.Hash === 'string') { // js-ipfs-api output + console.log(obj.Hash) + return + } - console.log(bs58.encode(obj.Hash).toString()) + console.log(bs58.encode(obj.Hash).toString()) + }) }) } }) diff --git a/src/cli/commands/object/put.js b/src/cli/commands/object/put.js index 0608016cf3..792d16fd66 100644 --- a/src/cli/commands/object/put.js +++ b/src/cli/commands/object/put.js @@ -1,5 +1,3 @@ -'use strict' - const Command = require('ronin').Command const utils = require('../../utils') const bs58 = require('bs58') @@ -29,28 +27,31 @@ function parseJSONBuffer (buf) { } function parseAndAddNode (buf) { - var ipfs = utils.getIPFS() + utils.getIPFS((err, ipfs) => { + if (err) { + throw err + } + if (utils.isDaemonOn()) { + return ipfs.object.put(buf, 'json', (err, obj) => { + if (err) { + log.error(err) + throw err + } + + console.log('added', obj.Hash) + }) + } - if (utils.isDaemonOn()) { - return ipfs.object.put(buf, 'json', (err, obj) => { + const parsed = parseJSONBuffer(buf) + const dagNode = new DAGNode(parsed.data, parsed.links) + ipfs.object.put(dagNode, (err, obj) => { if (err) { log.error(err) throw err } - console.log('added', obj.Hash) + console.log('added', bs58.encode(dagNode.multihash()).toString()) }) - } - - const parsed = parseJSONBuffer(buf) - const dagNode = new DAGNode(parsed.data, parsed.links) - ipfs.object.put(dagNode, (err, obj) => { - if (err) { - log.error(err) - throw err - } - - console.log('added', bs58.encode(dagNode.multihash()).toString()) }) } diff --git a/src/cli/commands/object/stat.js b/src/cli/commands/object/stat.js index 7056eb0a9c..f0adfebe33 100644 --- a/src/cli/commands/object/stat.js +++ b/src/cli/commands/object/stat.js @@ -17,22 +17,25 @@ module.exports = Command.extend({ throw new Error("Argument 'key' is required") } - var ipfs = utils.getIPFS() - - const mh = utils.isDaemonOn() - ? key - : new Buffer(bs58.decode(key)) - - ipfs.object.stat(mh, (err, stats) => { + utils.getIPFS((err, ipfs) => { if (err) { - log.error(err) throw err } + const mh = utils.isDaemonOn() + ? key + : new Buffer(bs58.decode(key)) + + ipfs.object.stat(mh, (err, stats) => { + if (err) { + log.error(err) + throw err + } - delete stats.Hash // only for js-ipfs-api output + delete stats.Hash // only for js-ipfs-api output - Object.keys(stats).forEach((key) => { - console.log(`${key}: ${stats[key]}`) + Object.keys(stats).forEach((key) => { + console.log(`${key}: ${stats[key]}`) + }) }) }) } diff --git a/src/cli/commands/ping.js b/src/cli/commands/ping.js index af4020b384..104a4bccc7 100644 --- a/src/cli/commands/ping.js +++ b/src/cli/commands/ping.js @@ -1,10 +1,7 @@ -'use strict' - const Command = require('ronin').Command module.exports = Command.extend({ desc: '', - run: (name) => { - } + run: () => {} }) diff --git a/src/cli/commands/repo/gc.js b/src/cli/commands/repo/gc.js index 6900dc7727..2228495248 100644 --- a/src/cli/commands/repo/gc.js +++ b/src/cli/commands/repo/gc.js @@ -1,11 +1,9 @@ -var Command = require('ronin').Command -// var IPFS = require('../../ipfs-core') +const Command = require('ronin').Command module.exports = Command.extend({ desc: '', - options: { - }, + options: {}, - run: function (name) {} + run: function () {} }) diff --git a/src/cli/commands/repo/init.js b/src/cli/commands/repo/init.js index 6900dc7727..2228495248 100644 --- a/src/cli/commands/repo/init.js +++ b/src/cli/commands/repo/init.js @@ -1,11 +1,9 @@ -var Command = require('ronin').Command -// var IPFS = require('../../ipfs-core') +const Command = require('ronin').Command module.exports = Command.extend({ desc: '', - options: { - }, + options: {}, - run: function (name) {} + run: function () {} }) diff --git a/src/cli/commands/repo/version.js b/src/cli/commands/repo/version.js index f3800e0e09..8ce4b4fe8b 100644 --- a/src/cli/commands/repo/version.js +++ b/src/cli/commands/repo/version.js @@ -1,19 +1,22 @@ -var Command = require('ronin').Command -var IPFS = require('../../../ipfs-core') +const Command = require('ronin').Command +const utils = require('../../utils') module.exports = Command.extend({ desc: 'Shows IPFS repo version information', - options: { - }, + options: {}, - run: function (name) { - var node = new IPFS() - node.repo.version(function (err, version) { + run: function () { + utils.getIPFS((err, ipfs) => { if (err) { - return console.error(err) + throw err } - console.log(version) + ipfs.repo.version(function (err, version) { + if (err) { + return console.error(err) + } + console.log(version) + }) }) } }) diff --git a/src/cli/commands/resolve.js b/src/cli/commands/resolve.js index af4020b384..104a4bccc7 100644 --- a/src/cli/commands/resolve.js +++ b/src/cli/commands/resolve.js @@ -1,10 +1,7 @@ -'use strict' - const Command = require('ronin').Command module.exports = Command.extend({ desc: '', - run: (name) => { - } + run: () => {} }) diff --git a/src/cli/commands/version.js b/src/cli/commands/version.js index 09375cf4b0..a206a12ce8 100644 --- a/src/cli/commands/version.js +++ b/src/cli/commands/version.js @@ -1,5 +1,3 @@ -'use strict' - const Command = require('ronin').Command const utils = require('../utils') const debug = require('debug') @@ -25,17 +23,21 @@ module.exports = Command.extend({ } }, - run: (name) => { - var ipfs = utils.getIPFS() - ipfs.version((err, version) => { - if (err) { return log.error(err) } - - if (typeof version === 'object') { // js-ipfs-api output - console.log('ipfs version', version.Version) - return + run: () => { + utils.getIPFS((err, ipfs) => { + if (err) { + throw err } + ipfs.version((err, version) => { + if (err) { return log.error(err) } + + if (typeof version === 'object') { // js-ipfs-api output + console.log('ipfs version', version.Version) + return + } - console.log('ipfs version', version) + console.log('ipfs version', version) + }) }) } }) diff --git a/src/cli/utils.js b/src/cli/utils.js index 7a5be59c7e..94756d6221 100644 --- a/src/cli/utils.js +++ b/src/cli/utils.js @@ -33,10 +33,14 @@ function getAPICtl () { return APIctl(apiAddr.toString()) } -exports.getIPFS = () => { +exports.getIPFS = (callback) => { if (!isDaemonOn()) { - return new IPFS() + const ipfs = new IPFS() + ipfs.load(() => { + callback(null, ipfs) + }) + return } - return getAPICtl() + callback(null, getAPICtl()) } diff --git a/src/http-api/index.js b/src/http-api/index.js index fce4526485..de961a5902 100644 --- a/src/http-api/index.js +++ b/src/http-api/index.js @@ -12,51 +12,54 @@ exports = module.exports exports.start = (callback) => { const ipfs = exports.ipfs = new IPFS() - - const repoPath = process.env.IPFS_PATH || os.homedir() + '/.ipfs' - try { - fs.statSync(repoPath + '/api') - console.log('This repo is currently being used by another daemon') - process.exit(1) - } catch (err) { - fs.writeFileSync(repoPath + '/api', 'api is on by js-ipfs', {flag: 'w+'}) - } - - ipfs.config.show((err, config) => { - if (err) { - return callback(err) + ipfs.load(() => { + const repoPath = process.env.IPFS_PATH || os.homedir() + '/.ipfs' + try { + fs.statSync(repoPath + '/api') + console.log('This repo is currently being used by another daemon') + process.exit(1) + } catch (err) { + fs.writeFileSync(repoPath + '/api', 'api is on by js-ipfs', {flag: 'w+'}) } - // TODO: set up cors correctly, following config - var server = exports.server = new Hapi.Server({ - connections: { - routes: { - cors: true - } + ipfs.config.show((err, config) => { + if (err) { + return callback(err) } - }) - const api = config.Addresses.API.split('/') - const gateway = config.Addresses.Gateway.split('/') - // for the CLI to know the where abouts of the API - fs.writeFileSync(repoPath + '/api', config.Addresses.API) + // TODO: set up cors correctly, following config + var server = exports.server = new Hapi.Server({ + connections: { + routes: { + cors: true + } + } + }) + const api = config.Addresses.API.split('/') + const gateway = config.Addresses.Gateway.split('/') - // select which connection with server.select(