"use strict";

var _path = _interopRequireDefault(require("path"));

var _fs = _interopRequireDefault(require("fs"));

var _child_process = _interopRequireDefault(require("child_process"));

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

function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }

function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }

// Install the granax (Tor Control Protocol library) package on demand.
//
// Installing granax downloads the Tor Browser Bundle, which results in about 250mb of
// extra storage space, which we want to avoid if the user isn't using the Tor onion feature.
// Prefer the npm executable is in the same dir as our nodejs executable,
// fallback to searching in $PATH otherwise.
var npmExe = _path["default"].join(_path["default"].dirname(process.argv[0]), 'npm');

_fs["default"].existsSync(npmExe) || (npmExe = 'npm');

module.exports = /*#__PURE__*/function () {
  var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(_) {
    var installer;
    return regeneratorRuntime.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            _context.prev = 0;
            return _context.abrupt("return", require('@deadcanaries/granax'));

          case 4:
            _context.prev = 4;
            _context.t0 = _context["catch"](0);

          case 6:
            console.log('\nTor Hidden Service enabled (via --onion) for the first time, downloading the Tor Bundle...' + '\nThe Spark .onion server will start automatically when its ready.' + '\n\nIn the meanwhile, you can access the local HTTP server directly.\n'); // trigger an "npm install" for granax-dep/package.json

            installer = _child_process["default"].spawn(npmExe, ['install'], {
              cwd: __dirname,
              stdio: 'inherit'
            });
            _context.next = 10;
            return new Promise(function (resolve, reject) {
              installer.on('error', reject);
              installer.on('close', function (code) {
                return code == 0 ? resolve() : reject(new Error('Tor installation failed, exited with code ' + code));
              });
            });

          case 10:
            return _context.abrupt("return", require('@deadcanaries/granax'));

          case 11:
          case "end":
            return _context.stop();
        }
      }
    }, _callee, null, [[0, 4]]);
  }));

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