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

var _raven = require('raven');

var _raven2 = _interopRequireDefault(_raven);

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 _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)();

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

    _program2.default.command('init').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) {
        (0, _analytics.trackCommand)(options);
        session.notAlreadyInitialized();
        new _commands2.default.Init(_context2.default).run(options);
      });

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

    _program2.default.command('login').description('Login to your account').action((...options) => {
      (0, _analytics.trackCommand)(options);
      new _commands2.default.Login(_context2.default).run(options);
    });

    _program2.default.command('logout').description('Logout from your current Instance').action((...options) => {
      (0, _analytics.trackCommand)(options);
      new _commands2.default.Logout(_context2.default).run(options);
    });

    _program2.default.command('sync').description('Synchronize your local configuration with the connected Instance').action((() => {
      var _ref2 = _asyncToGenerator(function* (...options) {
        (0, _analytics.trackCommand)(options);
        session.isAuthenticated();
        session.hasProject();
        yield session.checkConnection();
        new _commands2.default.Sync(_context2.default).run(options);
      });

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

    _program2.default.command('attach').description('Attach project to the choosen Instance').option('-i, --instance <name>', 'Instance you want to use for your project').action((() => {
      var _ref3 = _asyncToGenerator(function* (...options) {
        (0, _analytics.trackCommand)(options);
        if (options[0].instance) {
          yield session.checkConnection(options[0].instance);
        }
        (0, _printTools.echo)();
        new _commands2.default.Attach(_context2.default).run(options);
      });

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

    _program2.default.command('list [name]').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 _ref4 = _asyncToGenerator(function* (...options) {
        (0, _analytics.trackCommand)(options);
        session.hasProject();
        yield session.checkConnection();
        new _commands2.default.SocketList(_context2.default).run(options);
      });

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

    _program2.default.command('call <name>/<endpoint>').description("Call socket's endpoint").option('-b, --body', 'Print only body of the response').action((() => {
      var _ref5 = _asyncToGenerator(function* (...options) {
        (0, _analytics.trackCommand)(options);
        session.hasProject();
        yield session.checkConnection();
        new _commands2.default.SocketEndpointCall(_context2.default).run(options);
      });

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

    _program2.default.command('deploy [name]').description('Synchronize a given Socket').option('--hot', 'Enable Hot deploy').option('-b, --bail', 'Bail after first deploy failure').option('-t, --with-traces', 'Turn on showing traces').action((() => {
      var _ref6 = _asyncToGenerator(function* (...options) {
        (0, _analytics.trackCommand)(options);
        session.hasProject();
        yield session.checkConnection();
        (0, _printTools.echo)();
        new _commands2.default.SocketSync(_context2.default).run(options);
      });

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

    _program2.default.command('config <name>').description('Configure a given Socket').action((() => {
      var _ref7 = _asyncToGenerator(function* (...options) {
        const [name] = options;
        (0, _analytics.trackCommand)(options, { name });
        session.hasProject();
        yield session.checkConnection();
        new _commands2.default.SocketConfig(_context2.default).run(options);
      });

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

    _program2.default.command('config-set <socket> <option_name> <value>').description('Configure a config option of a given Socket').action((() => {
      var _ref8 = _asyncToGenerator(function* (...options) {
        (0, _analytics.trackCommand)(options);
        session.hasProject();
        yield session.checkConnection();
        new _commands2.default.SocketConfigSet(_context2.default).run(options);
      });

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

    _program2.default.command('config-show <socket>').description('Show config options of a Socket').action((() => {
      var _ref9 = _asyncToGenerator(function* (...options) {
        (0, _analytics.trackCommand)(options);
        session.hasProject();
        yield session.checkConnection();
        new _commands2.default.SocketConfigShow(_context2.default).run(options);
      });

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

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

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

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

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

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

    _program2.default.command('add <name>').description('Add a Socket as a dependency of your project').action((() => {
      var _ref12 = _asyncToGenerator(function* (...options) {
        const [name] = options;
        (0, _analytics.trackCommand)(options, { socketName: name });
        session.hasProject();
        yield session.checkConnection();
        (0, _printTools.echo)();
        new _commands2.default.SocketInstall(_context2.default).run(options);
      });

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

    _program2.default.command('remove <name>').description('Remove a Socket from your project').action((() => {
      var _ref13 = _asyncToGenerator(function* (...options) {
        const [name] = options;
        (0, _analytics.trackCommand)(options, { socketName: name });
        session.hasProject();
        yield session.checkConnection();
        new _commands2.default.SocketUninstall(_context2.default).run(options);
      });

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

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

    _program2.default.command('add-dep <name> <dependency>').description('Add dependency of the Socket').action((...options) => {
      (0, _analytics.trackCommand)(options);
      new _commands2.default.SocketAddDep(_context2.default).run(options);
    });

    _program2.default.command('trace [socket]').description('Trace Socket calls').action((() => {
      var _ref14 = _asyncToGenerator(function* (...options) {
        (0, _analytics.trackCommand)(options);
        session.hasProject();
        yield session.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