"use strict";Object.defineProperty(exports, "__esModule", { value: true });exports.taskRnvProjectUpgrade = exports["default"] = void 0;var _path = _interopRequireDefault(require("path"));
var _fs = _interopRequireDefault(require("fs"));
var _constants = require("../../core/constants");
var _logger = require("../../core/systemManager/logger");
var _projectManager = require("../../core/projectManager");
var _taskManager = require("../../core/taskManager");
var _npmUtils = require("../../core/systemManager/npmUtils");
var _pluginManager = require("../../core/pluginManager");
var _fileutils = require("../../core/systemManager/fileutils");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 taskRnvProjectUpgrade = /*#__PURE__*/function () {var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(c, parentTask, originTask) {var upgradedPaths, selectedVersion, packagesPath, _selectedVersion, dirs;return regeneratorRuntime.wrap(function _callee$(_context) {while (1) {switch (_context.prev = _context.next) {case 0:
            (0, _logger.logTask)('taskRnvProjectUpgrade');
            upgradedPaths = [];if (!
            (0, _fileutils.fsExistsSync)(c.paths.project.config)) {_context.next = 13;break;}_context.next = 5;return (
              (0, _taskManager.executeTask)(c, _constants.TASK_PROJECT_CONFIGURE, _constants.TASK_PROJECT_UPGRADE, originTask));case 5:_context.next = 7;return (
              (0, _npmUtils.listAndSelectNpmVersion)(c, 'rnv'));case 7:selectedVersion = _context.sent;

            upgradedPaths.push.apply(upgradedPaths, _toConsumableArray((0, _projectManager.upgradeProjectDependencies)(c, selectedVersion)));_context.next = 11;return (

              (0, _pluginManager.installPackageDependenciesAndPlugins)(c));case 11:_context.next = 22;break;case 13:

            (0, _logger.logInfo)('Your are running rnv upgrade outside of renative project');
            packagesPath = _path["default"].join(c.paths.project.dir, 'packages');if (!(
            (0, _fileutils.fsExistsSync)(c.paths.project["package"]) && (0, _fileutils.fsExistsSync)(packagesPath))) {_context.next = 22;break;}_context.next = 18;return (
              (0, _npmUtils.listAndSelectNpmVersion)(c, 'rnv'));case 18:_selectedVersion = _context.sent;

            upgradedPaths.push.apply(upgradedPaths, _toConsumableArray((0, _projectManager.upgradeDependencies)(
            c.files.project["package"],
            c.paths.project["package"],
            null, null,
            _selectedVersion)));


            dirs = _fs["default"].readdirSync(packagesPath);

            dirs.forEach(function (dir) {
              var dirPath = _path["default"].join(packagesPath, dir);
              if (_fs["default"].statSync(dirPath).isDirectory()) {
                var pkgPath = _path["default"].join(dirPath, 'package.json');
                var rnvPath = _path["default"].join(dirPath, 'renative.json');
                var pkgFile;
                var rnvFile;
                if ((0, _fileutils.fsExistsSync)(pkgPath)) {
                  pkgFile = (0, _fileutils.readObjectSync)(pkgPath);
                }

                if ((0, _fileutils.fsExistsSync)(rnvPath)) {
                  rnvFile = (0, _fileutils.readObjectSync)(rnvPath);
                }
                upgradedPaths.push.apply(upgradedPaths, _toConsumableArray((0, _projectManager.upgradeDependencies)(
                pkgFile,
                pkgPath,
                rnvFile,
                rnvPath,
                _selectedVersion)));

              }
            });case 22:



            (0, _logger.logToSummary)("Upgraded following files:\n".concat(upgradedPaths.join('\n')), true);return _context.abrupt("return",


            true);case 24:case "end":return _context.stop();}}}, _callee);}));return function taskRnvProjectUpgrade(_x, _x2, _x3) {return _ref.apply(this, arguments);};}();exports.taskRnvProjectUpgrade = taskRnvProjectUpgrade;var _default =


{
  description: 'Upgrade or downgrade RNV dependencies in your ReNative project',
  fn: taskRnvProjectUpgrade,
  task: _constants.TASK_PROJECT_UPGRADE,
  params: _constants.PARAMS.withBase(),
  platforms: [],
  skipAppConfig: true,
  skipPlatforms: true,
  isGlobalScope: true };exports["default"] = _default;
//# sourceMappingURL=task.rnv.project.upgrade.js.map