"use strict";Object.defineProperty(exports, "__esModule", { value: true });exports.taskRnvWorkspaceConfigure = exports["default"] = void 0;var _path = _interopRequireDefault(require("path"));
var _constants = require("../../core/constants");
var _fileutils = require("../../core/systemManager/fileutils");






var _logger = require("../../core/systemManager/logger");function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { "default": obj };}function ownKeys(object, enumerableOnly) {var keys = Object.keys(object);if (Object.getOwnPropertySymbols) {var symbols = Object.getOwnPropertySymbols(object);enumerableOnly && (symbols = symbols.filter(function (sym) {return Object.getOwnPropertyDescriptor(object, sym).enumerable;})), keys.push.apply(keys, symbols);}return keys;}function _objectSpread(target) {for (var i = 1; i < arguments.length; i++) {var source = null != arguments[i] ? arguments[i] : {};i % 2 ? ownKeys(Object(source), !0).forEach(function (key) {_defineProperty(target, key, source[key]);}) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) {Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));});}return target;}function _defineProperty(obj, key, value) {if (key in obj) {Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true });} else {obj[key] = value;}return 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);});};}







var taskRnvWorkspaceConfigure = /*#__PURE__*/function () {var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(c) {var oldGlobalConfigPath, _c$files$workspace$co, defaultConfig, newConfig;return regeneratorRuntime.wrap(function _callee$(_context) {while (1) {switch (_context.prev = _context.next) {case 0:
            (0, _logger.logTask)('taskRnvWorkspaceConfigure');

            // Check globalConfig Dir
            if ((0, _fileutils.fsExistsSync)(c.paths.workspace.dir)) {
              (0, _logger.logDebug)("".concat(c.paths.workspace.dir, " folder exists!"));
            } else {
              (0, _logger.logInfo)("".concat(
              c.paths.workspace.dir, " folder missing! Creating one for you..."));

              (0, _fileutils.mkdirSync)(c.paths.workspace.dir);
            }

            // Check globalConfig
            if ((0, _fileutils.fsExistsSync)(c.paths.workspace.config)) {
              (0, _logger.logDebug)("".concat(
              c.paths.workspace.dir, "/").concat(_constants.RENATIVE_CONFIG_NAME, " file exists!"));

            } else {
              oldGlobalConfigPath = _path["default"].join(
              c.paths.workspace.dir,
              'config.json');

              if ((0, _fileutils.fsExistsSync)(oldGlobalConfigPath)) {
                (0, _logger.logWarning)(
                'Found old version of your config. will copy it to new renative.json config');

                (0, _fileutils.copyFileSync)(oldGlobalConfigPath, c.paths.workspace.config);
              } else {
                (0, _logger.logInfo)("".concat(

                c.paths.workspace.dir, "/").concat(
                _constants.RENATIVE_CONFIG_NAME, " file missing! Creating one for you..."));

                (0, _fileutils.copyFileSync)(
                _path["default"].join(
                c.paths.rnv.dir,
                'coreTemplateFiles',
                'global-config-template.json'),

                c.paths.workspace.config);

              }
            }

            if ((0, _fileutils.fsExistsSync)(c.paths.workspace.config)) {
              c.files.workspace.config = JSON.parse(
              (0, _fileutils.fsReadFileSync)(c.paths.workspace.config).toString());


              if ((_c$files$workspace$co = c.files.workspace.config) !== null && _c$files$workspace$co !== void 0 && _c$files$workspace$co.appConfigsPath) {
                if (!(0, _fileutils.fsExistsSync)(c.files.workspace.config.appConfigsPath)) {
                  (0, _logger.logWarning)("Your custom global appConfig is pointing to ".concat(
                  (0, _logger.chalk)().white(
                  c.files.workspace.config.appConfigsPath), " which doesn't exist! Make sure you create one in that location"));


                } else {
                  (0, _logger.logInfo)("Found custom appConfing location pointing to ".concat(
                  (0, _logger.chalk)().white(
                  c.files.workspace.config.appConfigsPath), ". ReNativewill now swith to that location!"));


                  c.paths.project.appConfigsDir = c.files.workspace.config.appConfigsPath;
                }
              }

              // Check config sanity
              if (c.files.workspace.config.defaultTargets === undefined) {
                (0, _logger.logWarning)("You're missing defaultTargets in your config ".concat(
                (0, _logger.chalk)().white(
                c.paths.workspace.config), ". Let's add them!"));


                defaultConfig = JSON.parse(
                (0, _fileutils.fsReadFileSync)(
                _path["default"].join(
                c.paths.rnv.dir,
                'coreTemplateFiles',
                'global-config-template.json')).

                toString());

                newConfig = _objectSpread(_objectSpread({},
                c.files.workspace.config), {}, {
                  defaultTargets: defaultConfig.defaultTargets });

                (0, _fileutils.fsWriteFileSync)(
                c.paths.workspace.config,
                JSON.stringify(newConfig, null, 2));

              }
            }return _context.abrupt("return",

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


{
  description: '',
  fn: taskRnvWorkspaceConfigure,
  task: 'workspace configure',
  params: _constants.PARAMS.withBase(),
  platforms: [],
  skipPlatforms: true,
  isGlobalScope: true };exports["default"] = _default;
//# sourceMappingURL=task.rnv.workspace.configure.js.map