"use strict";

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

function _chalk() {
  const data = _interopRequireDefault(require("chalk"));

  _chalk = function () {
    return data;
  };

  return data;
}

function _fp() {
  const data = _interopRequireDefault(require("lodash/fp"));

  _fp = function () {
    return data;
  };

  return data;
}

function _xdl() {
  const data = require("@expo/xdl");

  _xdl = function () {
    return data;
  };

  return data;
}

function _CommandError() {
  const data = _interopRequireDefault(require("../CommandError"));

  _CommandError = function () {
    return data;
  };

  return data;
}

function _log() {
  const data = _interopRequireDefault(require("../log"));

  _log = function () {
    return data;
  };

  return data;
}

function _urlOpts() {
  const data = _interopRequireDefault(require("../urlOpts"));

  _urlOpts = function () {
    return data;
  };

  return data;
}

function _printRunInstructionsAsync() {
  const data = _interopRequireDefault(require("../printRunInstructionsAsync"));

  _printRunInstructionsAsync = function () {
    return data;
  };

  return data;
}

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

const logArtifactUrl = platform => async (projectDir, options) => {
  if (options.publicUrl && !_xdl().UrlUtils.isHttps(options.publicUrl)) {
    throw new (_CommandError().default)('INVALID_PUBLIC_URL', '--public-url must be a valid HTTPS URL.');
  }

  const res = await _xdl().Project.buildAsync(projectDir, {
    current: false,
    mode: 'status',
    ...(options.publicUrl ? {
      publicUrl: options.publicUrl
    } : {})
  });

  const url = _fp().default.compose(_fp().default.get(['artifacts', 'url']), _fp().default.head, _fp().default.filter(job => platform && job.platform === platform), _fp().default.getOr([], 'jobs'))(res);

  if (url) {
    _log().default.nested(url);
  } else {
    throw new Error(`No ${platform} binary file found. Use "expo build:${platform}" to create one.`);
  }
};

async function getWebAppUrlAsync(projectDir) {
  const webAppUrl = await _xdl().UrlUtils.constructWebAppUrlAsync(projectDir);

  if (!webAppUrl) {
    throw new (_CommandError().default)('NOT_RUNNING', `Expo web server is not running. Please start it with \`expo start:web\`.`);
  }

  return webAppUrl;
}

async function action(projectDir, options) {
  await _urlOpts().default.optsAsync(projectDir, options);

  if ((await _xdl().Project.currentStatus(projectDir)) !== 'running') {
    throw new (_CommandError().default)('NOT_RUNNING', `Project is not running. Please start it with \`expo start\`.`);
  }

  const url = options.web ? await getWebAppUrlAsync(projectDir) : await _xdl().UrlUtils.constructManifestUrlAsync(projectDir);

  _log().default.newLine();

  _urlOpts().default.printQRCode(url);

  (0, _log().default)('Your URL is\n\n' + _chalk().default.underline(url) + '\n');

  _log().default.raw(url);

  if (!options.web) {
    await (0, _printRunInstructionsAsync().default)();
    await _urlOpts().default.handleMobileOptsAsync(projectDir, options);
  }
}

function _default(program) {
  program.command('url [project-dir]').alias('u').option('-w, --web', 'Return the URL of the web app').description('Displays the URL you can use to view your project in Expo').urlOpts().allowOffline().asyncActionProjectDir(action,
  /* skipProjectValidation: */
  true,
  /* skipAuthCheck: */
  true);
  program.command('url:ipa [project-dir]').option('--public-url <url>', 'The URL of an externally hosted manifest (for self-hosted apps)').description('Displays the standalone iOS binary URL you can use to download your app binary').asyncActionProjectDir(logArtifactUrl('ios'), true);
  program.command('url:apk [project-dir]').option('--public-url <url>', 'The URL of an externally hosted manifest (for self-hosted apps)').description('Displays the standalone Android binary URL you can use to download your app binary').asyncActionProjectDir(logArtifactUrl('android'), true);
}
//# sourceMappingURL=../__sourcemaps__/commands/url.js.map