diff --git a/packages/aragon-cli/src/commands/ipfs.js b/packages/aragon-cli/src/commands/ipfs.js index bec8d155a..60799574e 100644 --- a/packages/aragon-cli/src/commands/ipfs.js +++ b/packages/aragon-cli/src/commands/ipfs.js @@ -57,12 +57,17 @@ exports.task = ({ apmOptions }) => { exports.handler = function ({ reporter, apm: apmOptions }) { const task = exports.task({ apmOptions }) - task.run().then(ctx => { - if (ctx.started) { - reporter.info('IPFS daemon is now running. Stopping this process will stop IPFS') - } else { - reporter.warning('Didnt start IPFS, port busy') - process.exit() - } - }) + task.run() + .then(ctx => { + if (ctx.started) { + reporter.info('IPFS daemon is now running. Stopping this process will stop IPFS') + } else { + reporter.warning('Didnt start IPFS, port busy') + process.exit() + } + }) + .catch(error => { + reporter.error(error.message) + process.exit() + }) } diff --git a/packages/aragon-cli/src/helpers/ipfs-daemon.js b/packages/aragon-cli/src/helpers/ipfs-daemon.js index 7e038a6a1..2437d8f95 100644 --- a/packages/aragon-cli/src/helpers/ipfs-daemon.js +++ b/packages/aragon-cli/src/helpers/ipfs-daemon.js @@ -9,19 +9,39 @@ const ipfsBin = getNPMBinary('go-ipfs', 'bin/ipfs') const ensureIPFSInitialized = async () => { if (!fs.existsSync(path.join(os.homedir(), '.ipfs'))) { + // We could use 'ipfs daemon --init' when https://github.com/ipfs/go-ipfs/issues/3913 is solved await execa(ipfsBin, ['init']) } } -const startIPFSDaemon = () => ( - new Promise(async (resolve) => { +const startIPFSDaemon = () => { + const IPFS_START_TIMEOUT = 7500 // 7.5s for timeout, may need to be tweaked + + let startOutput = '' + + // We add a timeout as starting + const timeout = new Promise((resolve, reject) => { + setTimeout(() => { + reject(new Error(`Starting IPFS timed out:\n${startOutput}`)) + }, IPFS_START_TIMEOUT) + }) + + const start = new Promise(async (resolve, reject) => { await ensureIPFSInitialized() - const ipfsProc = execa(ipfsBin, ['daemon']) + const ipfsProc = execa(ipfsBin, ['daemon', '--migrate']) + ipfsProc.stdout.on('data', (data) => { + startOutput = `${startOutput}${data.toString()}\n` if (data.toString().includes('Daemon is ready')) resolve() }) + + ipfsProc.stderr.on('data', (data) => { + reject(new Error(`Starting IPFS failed: ${data.toString()}`)) + }) }) -) + + return Promise.race([start, timeout]) +} let ipfsNode