"use strict";Object.defineProperty(exports, "__esModule", { value: true });exports["default"] = void 0;
var _path = _interopRequireDefault(require("path"));
var _semver = _interopRequireDefault(require("semver"));

var _config = _interopRequireDefault(require("../configManager/config"));
var _exec = require("../systemManager/exec");
var _fileutils = require("../systemManager/fileutils");







var _logger = require("../systemManager/logger");function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { "default": obj };}function _toConsumableArray(arr) {return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread();}function _nonIterableSpread() {throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");}function _unsupportedIterableToArray(o, minLen) {if (!o) return;if (typeof o === "string") return _arrayLikeToArray(o, minLen);var n = Object.prototype.toString.call(o).slice(8, -1);if (n === "Object" && o.constructor) n = o.constructor.name;if (n === "Map" || n === "Set") return Array.from(o);if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);}function _iterableToArray(iter) {if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);}function _arrayWithoutHoles(arr) {if (Array.isArray(arr)) return _arrayLikeToArray(arr);}function _arrayLikeToArray(arr, len) {if (len == null || len > arr.length) len = arr.length;for (var i = 0, arr2 = new Array(len); i < len; i++) {arr2[i] = arr[i];}return arr2;}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);});};}

var bumpVersions = function bumpVersions(version) {
  var _Config$getConfig$pat =


  _config["default"].getConfig().paths,dir = _Config$getConfig$pat.project.dir,pluginTemplates = _Config$getConfig$pat.rnv.pluginTemplates;
  // check for packages to bump
  var packagesDir = _path["default"].join(dir, 'packages');
  if ((0, _fileutils.fsExistsSync)(packagesDir)) {
    var packages = (0, _fileutils.fsReaddirSync)(packagesDir);
    packages.forEach(function (name) {
      var pkgPath = _path["default"].join(packagesDir, name);
      var pkgJsonPath = _path["default"].join(pkgPath, 'package.json');
      if (
      (0, _fileutils.fsLstatSync)(pkgPath).isDirectory() &&
      (0, _fileutils.fsExistsSync)(pkgJsonPath))
      {
        // we found a packaaaage, fist-bumpin' it
        var existingPkgJson = require(pkgJsonPath);
        existingPkgJson.version = version;
        (0, _fileutils.writeFileSync)(pkgJsonPath, existingPkgJson);
      }
    });
    // check if it's our turf and do some extra magic
    var renativePkgPath = _path["default"].join(packagesDir, 'renative');
    if ((0, _fileutils.fsExistsSync)(renativePkgPath)) {
      (0, _fileutils.copyFileSync)(
      _path["default"].join(dir, 'README.md'),
      _path["default"].join(renativePkgPath, 'README.md'));

      (0, _fileutils.updateObjectSync)(pluginTemplates.config, {
        pluginTemplates: {
          renative: {
            version: version } } });



    }
  }
};

var publishAll = function publishAll() {
  var
  dir =
  _config["default"].getConfig().paths.project.dir;
  var packagesDir = _path["default"].join(dir, 'packages');
  if ((0, _fileutils.fsExistsSync)(packagesDir)) {
    var packages = (0, _fileutils.fsReaddirSync)(packagesDir);
    return Promise.all(
    packages.map(function (name) {
      var pkgPath = _path["default"].join(packagesDir, name);
      return (0, _exec.executeAsync)('npm i', { cwd: pkgPath });
    }));

  }
  return true;
};

var taskRnvPkg = /*#__PURE__*/function () {var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee() {var args, firstArg, secondArg;return regeneratorRuntime.wrap(function _callee$(_context) {while (1) {switch (_context.prev = _context.next) {case 0:
            args = _toConsumableArray(_config["default"].getConfig().program.rawArgs);
            args = args.slice(3);

            firstArg = args[0];
            secondArg = args[1];_context.t0 =

            firstArg;_context.next = _context.t0 ===
            'version' ? 7 : _context.t0 ===










            'publish' ? 12 : 13;break;case 7:if (secondArg) {_context.next = 9;break;}return _context.abrupt("return", (0, _logger.logError)('No version specified', false, true));case 9:if (_semver["default"].valid(secondArg)) {_context.next = 11;break;}return _context.abrupt("return", (0, _logger.logError)("Invalid version specified ".concat(secondArg), false, true));case 11:return _context.abrupt("return", bumpVersions(secondArg));case 12:return _context.abrupt("return",
            publishAll());case 13:

            (0, _logger.logError)("Unknown argument ".concat(firstArg), false, true);return _context.abrupt("break", 15);case 15:case "end":return _context.stop();}}}, _callee);}));return function taskRnvPkg() {return _ref.apply(this, arguments);};}();var _default =




taskRnvPkg;exports["default"] = _default;
//# sourceMappingURL=package.js.map