Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
fix(core): consistent repo.exists checks
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire committed May 5, 2016
1 parent 036eaf6 commit 3d1e6b0
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 12 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
"ipfs-data-importing": "^0.3.3",
"ipfs-merkle-dag": "^0.5.0",
"ipfs-multipart": "^0.1.0",
"ipfs-repo": "^0.7.1",
"ipfs-repo": "^0.8.0",
"joi": "^8.0.2",
"libp2p-ipfs": "^0.3.3",
"lodash.get": "^4.2.1",
Expand Down
26 changes: 18 additions & 8 deletions src/core/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict'

const defaultRepo = require('./default-repo')
const BlockService = require('ipfs-block-service')
const Block = require('ipfs-block')
const mDAG = require('ipfs-merkle-dag')
Expand All @@ -11,10 +10,13 @@ const PeerInfo = require('peer-info')
const multiaddr = require('multiaddr')
const importer = require('ipfs-data-importing').import
const libp2p = require('libp2p-ipfs')
const init = require('./init')
const IPFSRepo = require('ipfs-repo')
const PeerBook = require('peer-book')

const init = require('./init')
const defaultRepo = require('./default-repo')
const utils = require('./utils')

exports = module.exports = IPFS

function IPFS (repo) {
Expand All @@ -33,7 +35,7 @@ function IPFS (repo) {
const peerInfoBook = new PeerBook()

this.load = (callback) => {
repo.exists((err, exists) => {
utils.ifRepoExists(repo, (err) => {
if (err) {
throw err
}
Expand All @@ -58,11 +60,15 @@ function IPFS (repo) {
opts = {}
}

repo.exists((err, exists) => {
if (err) { return callback(err) }
utils.ifRepoExists(repo, (err) => {
if (err) {
return callback(err)
}

repo.config.get((err, config) => {
if (err) { return callback(err) }
if (err) {
return callback(err)
}

callback(null, config.Version.Current)
})
Expand Down Expand Up @@ -100,8 +106,12 @@ function IPFS (repo) {
callback = opts
opts = {}
}
repo.exists((err, res) => {
if (err) { return callback(err) }

utils.ifRepoExists(repo, (err, res) => {
if (err) {
return callback(err)
}

repo.version.get(callback)
})
},
Expand Down
9 changes: 6 additions & 3 deletions src/core/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@ module.exports = (repo, opts, callback) => {
var config = require('../init-files/default-config.json')

// Verify repo does not yet exist.
repo.exists((err, res) => {
if (err) { return callback(err) }
if (res === true) {
repo.exists((err, exists) => {
if (err) {
return callback(err)
}

if (exists === true) {
return callback(new Error('repo already exists'))
}

Expand Down
15 changes: 15 additions & 0 deletions src/core/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict'

exports.ifRepoExists = (repo, cb) => {
repo.exists((err, exists) => {
if (err) {
return cb(err)
}

if (exists === false) {
return cb(new Error('no ipfs repo found'))
}

cb()
})
}

0 comments on commit 3d1e6b0

Please sign in to comment.