diff --git a/src/cli/commands/daemon.js b/src/cli/commands/daemon.js index acbbc5dc00..11fdba83ee 100644 --- a/src/cli/commands/daemon.js +++ b/src/cli/commands/daemon.js @@ -16,6 +16,11 @@ module.exports = { console.log('Initializing daemon...') httpAPI = new HttpAPI(process.env.IPFS_PATH) httpAPI.start((err) => { + if (err.code === 'ENOENT') { + console.log('Error: no ipfs repo found in ' + process.env.IPFS_PATH) + console.log('please run: jsipfs init') + process.exit(1) + } if (err) { throw err } diff --git a/src/http-api/index.js b/src/http-api/index.js index 469f584ef2..9cd866a5cb 100644 --- a/src/http-api/index.js +++ b/src/http-api/index.js @@ -25,11 +25,17 @@ exports = module.exports = function HttpApi (repo) { } this.ipfs = new IPFS(repo) + const repoPath = this.ipfs.repo.path() + + try { + fs.statSync(repoPath) + } catch (err) { + return callback(err) + } console.log('Starting at %s', this.ipfs.repo.path()) this.ipfs.load(() => { - const repoPath = this.ipfs.repo.path() const apiPath = path.join(repoPath, 'api') try { diff --git a/test/cli/test-daemon.js b/test/cli/test-daemon.js new file mode 100644 index 0000000000..e5a844f9a1 --- /dev/null +++ b/test/cli/test-daemon.js @@ -0,0 +1,28 @@ +/* eslint-env mocha */ +'use strict' + +const expect = require('chai').expect +const clean = require('../utils/clean') +const ipfsCmd = require('../utils/ipfs-exec') + +describe('daemon', function () { + let repoPath + let ipfs + + beforeEach(() => { + repoPath = '/tmp/ipfs-test-' + Math.random().toString().substring(2, 8) + ipfs = ipfsCmd(repoPath) + }) + + afterEach(() => { + clean(repoPath) + }) + + it('gives error if user hasn\'t run init before', (done) => { + const expectedError = 'no ipfs repo found in ' + repoPath + ipfs('daemon').catch((err) => { + expect(err.stdout).to.have.string(expectedError) + done() + }) + }) +})