var Command = require('ronin').Command
var fs = require('fs')
var ipfsAPI = require('ipfs-api')
var path = require('path')

module.exports = Command.extend({
  desc: 'Publish your project',

  run: function (name) {
    try {
      var configPath = path.resolve(process.cwd() + '/ipscend.json')
      fs.statSync(configPath)
      publish()
    } catch (err) {
      console.log('Project must be initiated first, run `ipscend init`')
    }

    function publish () {
      var config = JSON.parse(fs.readFileSync(configPath))
      // TODO check if daemon is running
      var ipfs = ipfsAPI('localhost', '5001')

      ipfs.add(config.path, { recursive: true, 'stream-channels': false }, function (err, res) {
        if (err || !res) {
          return console.error('err', err)
        }

        var version = {
          hash: res[res.length - 2].Hash,
          timestamp: new Date()
        }
        console.log(version)

        console.log('published', config.path, version.hash)

        config.versions.push(version)

        var fd = fs.openSync(configPath, 'w')
        fs.writeSync(fd, JSON.stringify(config, null, '  '), 0, 'utf-8')
      })
    }
  }
})

var Command = require('ronin').Command
var fs = require('fs')
var ipfsAPI = require('ipfs-api')
var path = require('path')

module.exports = Command.extend({
  desc: 'Publish your project',

  run: function (name) {
    try {
      var configPath = path.resolve(process.cwd() + '/ipscend.json')
      fs.statSync(configPath)
      publish()
    } catch (err) {
      console.log('Project must be initiated first, run `ipscend init`')
    }

    function publish () {
      var config = JSON.parse(fs.readFileSync(configPath))
      // TODO check if daemon is running
      var ipfs = ipfsAPI('localhost', '5001')

      ipfs.add(config.path, { recursive: true, 'stream-channels': false }, function (err, res) {
        if (err || !res) {
          return console.error('err', err)
        }

        var version = {
          hash: res[res.length - 1].Hash,
          timestamp: new Date()
        }
        console.log(version)

        console.log('published', config.path, version.hash)

        config.versions.push(version)

        var fd = fs.openSync(configPath, 'w')
        fs.writeSync(fd, JSON.stringify(config, null, '  '), 0, 'utf-8')
      })
    }
  }
})

Revert "use the second-from-root object returned when publishing"