diff --git a/src/core/index.js b/src/core/index.js index fc4c54d6e5..37b4222cac 100644 --- a/src/core/index.js +++ b/src/core/index.js @@ -8,6 +8,7 @@ const debug = require('debug') const defaultRepo = require('./default-repo') const components = require('./components') +const utils = require('./utils') class IPFS { constructor (configOpts) { @@ -62,6 +63,7 @@ class IPFS { this.bitswap = components.bitswap(this) this.ping = components.ping(this) this.pubsub = components.pubsub(this) + this.repoExists = utils.repoExists(this) if (configOpts.EXPERIMENTAL.pubsub) { this.log('EXPERIMENTAL pubsub is enabled') diff --git a/src/core/utils.js b/src/core/utils.js index 6278eba9ed..72f0212002 100644 --- a/src/core/utils.js +++ b/src/core/utils.js @@ -14,4 +14,16 @@ exports.ifRepoExists = (repo, cb) => { }) } +exports.repoExists = (self) => { + return (cb) => { + self._repo.exists((err, exists) => { + if (err) { + return cb(err) + } + + cb(err, exists) + }) + } +} + exports.OFFLINE_ERROR = new Error('This command must be run in online mode. Try running \'ipfs daemon\' first.') diff --git a/test/core/init.spec.js b/test/core/init.spec.js index 16b382eba9..a012fc0d4b 100644 --- a/test/core/init.spec.js +++ b/test/core/init.spec.js @@ -83,4 +83,24 @@ describe('init', () => { }) }) }) + + it('checks if existing repo exists', (done) => { + ipfs.init({ bits: 1024, emptyRepo: true }, (err) => { + expect(err).to.not.exist + + ipfs.repoExists((err, exists) => { + expect(err).to.not.exist + expect(exists).to.be.true + done() + }) + }) + }) + + it('checks if nonexistant repo exists', (done) => { + ipfs.repoExists((err, exists) => { + expect(err).to.not.exist + expect(exists).to.be.false + done() + }) + }) })