if (process.type === 'browser') { require('source-map-support').install(); }var log = require('common/utils/logger').debugLogger(__filename); var logError = require('common/utils/logger').errorLogger(__filename, false); var logFatal = require('common/utils/logger').errorLogger(__filename, true);'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _stringify = require('babel-runtime/core-js/json/stringify');

var _stringify2 = _interopRequireDefault(_stringify);

var _regenerator = require('babel-runtime/regenerator');

var _regenerator2 = _interopRequireDefault(_regenerator);

var _promise = require('babel-runtime/core-js/promise');

var _promise2 = _interopRequireDefault(_promise);

var _asyncToGenerator2 = require('babel-runtime/helpers/asyncToGenerator');

var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2);

var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _createClass2 = require('babel-runtime/helpers/createClass');

var _createClass3 = _interopRequireDefault(_createClass2);

var _electron = require('electron');

var _child_process = require('child_process');

var _child_process2 = _interopRequireDefault(_child_process);

var _path = require('path');

var _path2 = _interopRequireDefault(_path);

var _del = require('del');

var _del2 = _interopRequireDefault(_del);

var _autoLauncher = require('browser/components/auto-launcher');

var _autoLauncher2 = _interopRequireDefault(_autoLauncher);

var _filePaths = require('common/utils/file-paths');

var _filePaths2 = _interopRequireDefault(_filePaths);

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

var SquirrelEvents = function () {
  function SquirrelEvents() {
    (0, _classCallCheck3.default)(this, SquirrelEvents);
  }

  (0, _createClass3.default)(SquirrelEvents, [{
    key: 'check',
    value: function check(options) {
      if (options.squirrelInstall) {
        log('creating shortcuts');
        this.spawnSquirrel('--createShortcut', this.getShortcutExeName()).then(this.exitApp);
        return true;
      }

      if (options.squirrelUpdated || options.squirrelObsolete) {
        setTimeout(this.exitApp);
        return true;
      }

      if (options.squirrelUninstall) {
        this.teardown().then(this.exitApp);
        return true;
      }

      return false;
    }
  }, {
    key: 'getShortcutExeName',
    value: function getShortcutExeName() {
      return _path2.default.basename(_electron.app.getPath('exe'));
    }
  }, {
    key: 'exitApp',
    value: function exitApp() {
      var exitCode = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;

      _electron.app.exit(exitCode);
    }

    /**
     * Spawn Squirrel's Update.exe with the given arguments.
     */

  }, {
    key: 'spawnSquirrel',
    value: function () {
      var _ref = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee() {
        for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
          args[_key] = arguments[_key];
        }

        var squirrelExec, child;
        return _regenerator2.default.wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                squirrelExec = _filePaths2.default.getSquirrelUpdateExePath();

                log('spawning', squirrelExec, args);

                child = _child_process2.default.spawn(squirrelExec, args, { detached: true });
                _context.next = 5;
                return new _promise2.default(function (resolve, reject) {
                  child.on('close', function (code) {
                    if (code) {
                      logError(new Error(squirrelExec + ' exited with code ' + code));
                    }
                    resolve(code || 0);
                  });
                });

              case 5:
                return _context.abrupt('return', _context.sent);

              case 6:
              case 'end':
                return _context.stop();
            }
          }
        }, _callee, this);
      }));

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

      return spawnSquirrel;
    }()
  }, {
    key: 'teardown',
    value: function () {
      var _ref2 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee2() {
        return _regenerator2.default.wrap(function _callee2$(_context2) {
          while (1) {
            switch (_context2.prev = _context2.next) {
              case 0:
                _context2.prev = 0;
                _context2.next = 3;
                return this.teardownShortcuts();

              case 3:
                _context2.next = 8;
                break;

              case 5:
                _context2.prev = 5;
                _context2.t0 = _context2['catch'](0);
                logError(_context2.t0, true);

              case 8:
                _context2.prev = 8;
                _context2.next = 11;
                return this.teardownAutoLauncherRegKey();

              case 11:
                _context2.next = 16;
                break;

              case 13:
                _context2.prev = 13;
                _context2.t1 = _context2['catch'](8);
                logError(_context2.t1, true);

              case 16:
                _context2.prev = 16;
                _context2.next = 19;
                return this.teardownLeftoverUserData();

              case 19:
                _context2.next = 24;
                break;

              case 21:
                _context2.prev = 21;
                _context2.t2 = _context2['catch'](16);
                logError(_context2.t2, true);

              case 24:
                log('teardown finished');

              case 25:
              case 'end':
                return _context2.stop();
            }
          }
        }, _callee2, this, [[0, 5], [8, 13], [16, 21]]);
      }));

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

      return teardown;
    }()
  }, {
    key: 'teardownShortcuts',
    value: function () {
      var _ref3 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee3() {
        return _regenerator2.default.wrap(function _callee3$(_context3) {
          while (1) {
            switch (_context3.prev = _context3.next) {
              case 0:
                log('removing shortcuts');
                _context3.next = 3;
                return this.spawnSquirrel('--removeShortcut', this.getShortcutExeName());

              case 3:
              case 'end':
                return _context3.stop();
            }
          }
        }, _callee3, this);
      }));

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

      return teardownShortcuts;
    }()
  }, {
    key: 'teardownAutoLauncherRegKey',
    value: function () {
      var _ref4 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee4() {
        return _regenerator2.default.wrap(function _callee4$(_context4) {
          while (1) {
            switch (_context4.prev = _context4.next) {
              case 0:
                log('removing reg keys');
                _context4.next = 3;
                return new _autoLauncher2.default().disable();

              case 3:
              case 'end':
                return _context4.stop();
            }
          }
        }, _callee4, this);
      }));

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

      return teardownAutoLauncherRegKey;
    }()
  }, {
    key: 'teardownLeftoverUserData',
    value: function () {
      var _ref5 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee5() {
        var userDataPath;
        return _regenerator2.default.wrap(function _callee5$(_context5) {
          while (1) {
            switch (_context5.prev = _context5.next) {
              case 0:
                userDataPath = _electron.app.getPath('userData');

                log('removing user data folder', userDataPath);
                _context5.next = 4;
                return (0, _del2.default)(_path2.default.join(userDataPath, '**'), { force: true }).then(function (files) {
                  return log('deleted', (0, _stringify2.default)(files));
                });

              case 4:
              case 'end':
                return _context5.stop();
            }
          }
        }, _callee5, this);
      }));

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

      return teardownLeftoverUserData;
    }()
  }]);
  return SquirrelEvents;
}();

exports.default = new SquirrelEvents();
//# sourceMappingURL=data:application/json;charset=utf8;base64, 
