#!/usr/bin/env node
'use strict';

var _lodash = require('lodash');

var _lodash2 = _interopRequireDefault(_lodash);

var _raven = require('raven');

var _raven2 = _interopRequireDefault(_raven);

var _commander = require('commander');

var _commander2 = _interopRequireDefault(_commander);

var _program = require('./program');

var _program2 = _interopRequireDefault(_program);

var _commands = require('./commands');

var _commands2 = _interopRequireDefault(_commands);

var _context = require('./utils/context');

var _context2 = _interopRequireDefault(_context);

var _validateCommands = require('./utils/validate-commands');

var _validateCommands2 = _interopRequireDefault(_validateCommands);

var _raven3 = require('./utils/raven');

var _raven4 = _interopRequireDefault(_raven3);

var _session = require('./utils/session');

var _session2 = _interopRequireDefault(_session);

var _debug = require('./utils/debug');

var _debug2 = _interopRequireDefault(_debug);

var _package = require('../package.json');

var _package2 = _interopRequireDefault(_package);

var _printTools = require('./utils/print-tools');

var _analytics = require('./utils/analytics');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }

const { debug } = (0, _debug2.default)('main-cli');
(0, _raven4.default)();

const commandDebug = options => {
  const cmd = _lodash2.default.find(options, option => option instanceof _commander2.default.Command);
  debug(`Command: ${cmd.parent.rawArgs.slice(2).join(' ')}`);
};

const trackAndDebug = (options, additionalParams) => {
  commandDebug(options);
  (0, _analytics.trackCommand)(options, additionalParams);
};

debug(_package2.default.version);
_raven2.default.context(() => {
  _context2.default.session.load().then(() => {
    _program2.default.version(_package2.default.version);

    _program2.default.command('init').group('Basics').description('Start a Syncano project in the current directory').option('-i, --instance <name>', 'Instance you want to use for your project. If not provided, an Instance will be created').action((() => {
      var _ref = _asyncToGenerator(function* (...options) {
        trackAndDebug(options);
        _session2.default.notAlreadyInitialized();
        new _commands2.default.Init(_context2.default).run(options);
      });

      return function () {
        return _ref.apply(this, arguments);
      };
    })());

    _program2.default.command('attach').group('Basics').description('Attach project to the chosen Instance').option('-i, --instance <name>', 'Instance you want to use for your project').action((() => {
      var _ref2 = _asyncToGenerator(function* (...options) {
        trackAndDebug(options);
        _session2.default.isAuthenticated();
        if (options[0].instance) {
          yield _session2.default.checkConnection(options[0].instance);
        }
        _session2.default.hasProject();
        (0, _printTools.echo)();
        new _commands2.default.Attach(_context2.default).run(options);
      });

      return function () {
        return _ref2.apply(this, arguments);
      };
    })());

    _program2.default.command('login').group('Basics').description('Login to your account').action((...options) => {
      trackAndDebug(options);
      new _commands2.default.Login(_context2.default).run(options);
    });

    _program2.default.command('logout').group('Basics').description('Logout from your current account').action((...options) => {
      trackAndDebug(options);
      new _commands2.default.Logout(_context2.default).run(options);
    });

    _program2.default.command('hot [socket_name]').group('Project').description('Hot deploy to make your project continuously synced to the Syncano cloud').action((() => {
      var _ref3 = _asyncToGenerator(function* (...options) {
        trackAndDebug(options);
        _session2.default.isAuthenticated();
        _session2.default.hasProject();
        yield _session2.default.checkConnection();

        const optionsToRun = options;
        optionsToRun[1].hot = true;
        optionsToRun[1].trace = true;
        (0, _printTools.echo)();
        new _commands2.default.SocketSync(_context2.default).run(optionsToRun);
      });

      return function () {
        return _ref3.apply(this, arguments);
      };
    })());

    _program2.default.command('deploy [socket_name]').group('Project').description('Synchronize your project to Syncano').option('--hot', 'Enable Hot deploy').option('-b, --bail', 'Bail after first deploy failure').option('-t, --trace', 'Turn on showing traces').action((() => {
      var _ref4 = _asyncToGenerator(function* (...options) {
        trackAndDebug(options);
        _session2.default.isAuthenticated();
        _session2.default.hasProject();
        yield _session2.default.checkConnection();
        (0, _printTools.echo)();
        new _commands2.default.SocketSync(_context2.default).run(options);
      });

      return function () {
        return _ref4.apply(this, arguments);
      };
    })());

    _program2.default.command('call <socket_name>/<endpoint>').group('Project').description("Call Socket's endpoint").option('-b, --body', 'Print only body of the response').action((() => {
      var _ref5 = _asyncToGenerator(function* (...options) {
        trackAndDebug(options);
        _session2.default.isAuthenticated();
        _session2.default.hasProject();
        yield _session2.default.checkConnection();
        new _commands2.default.SocketEndpointCall(_context2.default).run(options);
      });

      return function () {
        return _ref5.apply(this, arguments);
      };
    })());

    _program2.default.command('list [socket_name]').group('Sockets').description('List the installed Sockets').option('-f, --full', 'Print the detailed information (including parameters and response)').option('-d, --with-deps', 'Print also Sockets which are dependencies of other Sockets').action((() => {
      var _ref6 = _asyncToGenerator(function* (...options) {
        trackAndDebug(options);
        _session2.default.isAuthenticated();
        _session2.default.hasProject();
        yield _session2.default.checkConnection();
        new _commands2.default.SocketList(_context2.default).run(options);
      });

      return function () {
        return _ref6.apply(this, arguments);
      };
    })());

    _program2.default.command('add <socket_name>').group('Sockets').description('Add a Socket as a dependency of your project or local Socket').option('-s, --socket <socket>', 'Name of the Socket').action((() => {
      var _ref7 = _asyncToGenerator(function* (...options) {
        const [name] = options;
        trackAndDebug(options, { socketName: name });
        _session2.default.isAuthenticated();
        _session2.default.hasProject();
        yield _session2.default.checkConnection();
        (0, _printTools.echo)();
        new _commands2.default.SocketInstall(_context2.default).run(options);
      });

      return function () {
        return _ref7.apply(this, arguments);
      };
    })());

    _program2.default.command('remove <socket_name>').group('Sockets').description('Remove a Socket from your project').action((() => {
      var _ref8 = _asyncToGenerator(function* (...options) {
        const [name] = options;
        trackAndDebug(options, { socketName: name });
        _session2.default.isAuthenticated();
        _session2.default.hasProject();
        yield _session2.default.checkConnection();
        (0, _printTools.echo)();
        new _commands2.default.SocketUninstall(_context2.default).run(options);
      });

      return function () {
        return _ref8.apply(this, arguments);
      };
    })());

    _program2.default.command('create <socket_name>').group('Sockets').description('Create a new Socket for your project').action((...options) => {
      const [name] = options;
      trackAndDebug(options, { socketName: name });
      new _commands2.default.SocketCreate(_context2.default).run(options);
    });

    _program2.default.command('config <socket_name>').group('Sockets').description('Configure a given Socket').action((() => {
      var _ref9 = _asyncToGenerator(function* (...options) {
        const [name] = options;
        trackAndDebug(options, { name });
        _session2.default.isAuthenticated();
        _session2.default.hasProject();
        yield _session2.default.checkConnection();
        new _commands2.default.SocketConfig(_context2.default).run(options);
      });

      return function () {
        return _ref9.apply(this, arguments);
      };
    })());

    _program2.default.command('config-set <socket_name> <option_name> <value>').group('Sockets').description('Configure a config option of a given Socket').action((() => {
      var _ref10 = _asyncToGenerator(function* (...options) {
        trackAndDebug(options);
        _session2.default.isAuthenticated();
        _session2.default.hasProject();
        yield _session2.default.checkConnection();
        new _commands2.default.SocketConfigSet(_context2.default).run(options);
      });

      return function () {
        return _ref10.apply(this, arguments);
      };
    })());

    _program2.default.command('config-show <socket_name>').group('Sockets').description('Show config options of a Socket').action((() => {
      var _ref11 = _asyncToGenerator(function* (...options) {
        trackAndDebug(options);
        _session2.default.isAuthenticated();
        _session2.default.hasProject();
        yield _session2.default.checkConnection();
        (0, _printTools.echo)();
        new _commands2.default.SocketConfigShow(_context2.default).run(options);
      });

      return function () {
        return _ref11.apply(this, arguments);
      };
    })());

    _program2.default.command('search [keyword]').group('Registry').description('Search for a specific Socket in the Sockets Registry').option('-l, --long', 'Display full descriptions').action((...options) => {
      const [keyword] = options;
      trackAndDebug(options, { keyword });
      if (!keyword) {
        return _program2.default.outputHelp();
      }
      (0, _printTools.echo)();
      new _commands2.default.SocketSearch(_context2.default).run(options);
    });

    _program2.default.command('submit <socket_name>').group('Registry').description('Submit a Socket to Socket Registry').option('-b, --bump <release type>', 'Bump version of the socket (major, premajor, minor, preminor, patch, prepatch, or prerelease)').action((() => {
      var _ref12 = _asyncToGenerator(function* (...options) {
        const [name] = options;
        trackAndDebug(options, { socketName: name });
        new _commands2.default.SocketSubmit(_context2.default).run(options);
      });

      return function () {
        return _ref12.apply(this, arguments);
      };
    })());

    _program2.default.command('publish <socket_name>').group('Registry').description('Publish a Socket in a Socket Registry').option('-v, --version <socket version>', 'Version of the Socket you want to publish').action((() => {
      var _ref13 = _asyncToGenerator(function* (...options) {
        const [name] = options;
        trackAndDebug(options, { socketName: name });
        new _commands2.default.SocketPublish(_context2.default).run(options);
      });

      return function () {
        return _ref13.apply(this, arguments);
      };
    })());

    _program2.default.command('trace [socket_name]').group('Project').description('Trace Socket calls').action((() => {
      var _ref14 = _asyncToGenerator(function* (...options) {
        trackAndDebug(options);
        _session2.default.isAuthenticated();
        _session2.default.hasProject();
        yield _session2.default.checkConnection();
        (0, _printTools.echo)();
        new _commands2.default.SocketTrace(_context2.default).run(options);
      });

      return function () {
        return _ref14.apply(this, arguments);
      };
    })());

    _program2.default.command('hosting', 'Manage your web assets and host them on Syncano').on('*', commandsArr => (0, _validateCommands2.default)(commandsArr));

    _context2.default.session.loadPlugins(_program2.default, _context2.default);
    _program2.default.parse(process.argv);

    if (!process.argv.slice(2).length) {
      _program2.default.outputHelp();
    }
  });
});
//# sourceMappingURL=cli.js.map