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



var _prompt = require("../../cli/prompt");
var _runtimeManager = require("../../core/runtimeManager");
var _fileutils = require("../../core/systemManager/fileutils");
var _resolve = require("../../core/systemManager/resolve");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);});};}

var taskRnvAppCreate = /*#__PURE__*/function () {var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(c) {var sourcePath, sourceAppConfigDirPath, tacPath, tacDirsPath, tacDirs, appConfigChoicesObj, appConfigChoices, acDirs, _tacPath, _tacDirsPath, _tacDirs, _yield$inquirerPrompt, sourceAppConfig, destPath, appConfigId, _yield$inquirerPrompt2, confName, confObjPath, confObj, appConfigTitle, _confObj$common, _yield$inquirerPrompt3, confTitle, bundleId, _confObj$common2, _yield$inquirerPrompt4, confId;return regeneratorRuntime.wrap(function _callee$(_context) {while (1) {switch (_context.prev = _context.next) {case 0:
            (0, _logger.logTask)('taskRnvAppCreate');_context.next = 3;return (


              (0, _runtimeManager.configureRuntimeDefaults)(c));case 3:if (!




            c.program.sourceAppConfigID) {_context.next = 8;break;}
            sourceAppConfigDirPath = _path["default"].join(c.paths.project.appConfigsDir, c.program.sourceAppConfigID);

            if ((0, _fileutils.fsExistsSync)(sourceAppConfigDirPath)) {
              sourcePath = sourceAppConfigDirPath;
            }_context.next = 24;break;case 8:if (!
            c.program.ci) {_context.next = 13;break;}
            tacPath = (0, _resolve.doResolve)(c.buildConfig.currentTemplate);
            if ((0, _fileutils.fsExistsSync)(tacPath)) {
              tacDirsPath = _path["default"].join(tacPath, 'appConfigs');
              tacDirs = (0, _fileutils.fsReaddirSync)(tacDirsPath);
              tacDirs.forEach(function (v) {
                if (v !== 'base') {
                  var pth = _path["default"].join(tacDirsPath, v);
                  if ((0, _fileutils.fsLstatSync)(pth).isDirectory()) {
                    sourcePath = pth;
                  }
                }
              });
            }_context.next = 24;break;case 13:

            appConfigChoicesObj = {};
            appConfigChoices = [];
            // Project Configs
            acDirs = (0, _fileutils.fsReaddirSync)(c.paths.project.appConfigsDir);
            acDirs.forEach(function (v) {
              if (v !== 'base') {
                var pth = _path["default"].join(c.paths.project.appConfigsDir, v);
                if ((0, _fileutils.fsLstatSync)(pth).isDirectory()) {
                  var key = "project>".concat(v);
                  appConfigChoices.push(key);
                  appConfigChoicesObj[key] = {
                    path: pth };

                }
              }
            });

            // Template Configs
            _tacPath = (0, _resolve.doResolve)(c.buildConfig.currentTemplate);
            if ((0, _fileutils.fsExistsSync)(_tacPath)) {
              _tacDirsPath = _path["default"].join(_tacPath, 'appConfigs');
              _tacDirs = (0, _fileutils.fsReaddirSync)(_tacDirsPath);
              _tacDirs.forEach(function (v) {
                if (v !== 'base') {
                  var pth = _path["default"].join(_tacDirsPath, v);
                  if ((0, _fileutils.fsLstatSync)(pth).isDirectory()) {
                    var key = "template>".concat(v);
                    appConfigChoices.push(key);
                    appConfigChoicesObj[key] = {
                      path: pth };

                  }
                }
              });
            }_context.next = 21;return (

              (0, _prompt.inquirerPrompt)({
                name: 'sourceAppConfig',
                type: 'list',
                choices: appConfigChoices,
                message: 'Which App config to use as copy source?' }));case 21:_yield$inquirerPrompt = _context.sent;sourceAppConfig = _yield$inquirerPrompt.sourceAppConfig;



            sourcePath = appConfigChoicesObj[sourceAppConfig].path;case 24:if (!




            c.program.appConfigID) {_context.next = 29;break;}
            appConfigId = c.program.appConfigID.toLowerCase();
            destPath = _path["default"].join(c.paths.project.appConfigsDir, appConfigId);_context.next = 35;break;case 29:_context.next = 31;return (

              (0, _prompt.inquirerPrompt)({
                name: 'confName',
                type: 'input',
                message: 'Type name of app config (lowercase)' }));case 31:_yield$inquirerPrompt2 = _context.sent;confName = _yield$inquirerPrompt2.confName;

            appConfigId = confName.toLowerCase();
            destPath = _path["default"].join(c.paths.project.appConfigsDir, appConfigId);case 35:



            (0, _logger.logInfo)('Copying new app config...');_context.next = 38;return (
              (0, _fileutils.copyFolderContentsRecursive)(sourcePath, destPath));case 38:
            (0, _logger.logInfo)('Copying new app config...DONE');

            confObjPath = _path["default"].join(destPath, 'renative.json');
            confObj = (0, _fileutils.readObjectSync)(confObjPath);

            confObj.id = appConfigId;
            confObj.common = confObj.common || {};if (!



            c.program.title) {_context.next = 47;break;}
            appConfigTitle = c.program.title;_context.next = 55;break;case 47:if (!
            c.program.ci) {_context.next = 50;break;}_context.next = 55;break;case 50:_context.next = 52;return (


              (0, _prompt.inquirerPrompt)({
                name: 'confTitle',
                type: 'input',
                "default": ((_confObj$common = confObj.common) === null || _confObj$common === void 0 ? void 0 : _confObj$common.title) || '',
                message: 'Type the title of the app' }));case 52:_yield$inquirerPrompt3 = _context.sent;confTitle = _yield$inquirerPrompt3.confTitle;

            appConfigTitle = confTitle;case 55:

            confObj.common.title = appConfigTitle || confObj.common.title;if (!


            c.program.id) {_context.next = 60;break;}
            bundleId = c.program.id;_context.next = 68;break;case 60:if (!
            c.program.ci) {_context.next = 63;break;}_context.next = 68;break;case 63:_context.next = 65;return (


              (0, _prompt.inquirerPrompt)({
                name: 'confId',
                type: 'input',
                "default": ((_confObj$common2 = confObj.common) === null || _confObj$common2 === void 0 ? void 0 : _confObj$common2.id) || '',
                message: 'Type the id of the app (bundle identifier)' }));case 65:_yield$inquirerPrompt4 = _context.sent;confId = _yield$inquirerPrompt4.confId;

            bundleId = confId;case 68:

            confObj.common.id = bundleId || confObj.common.id;


            (0, _fileutils.writeFileSync)(confObjPath, confObj);return _context.abrupt("return",

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


{
  description: 'Create new app config',
  fn: taskRnvAppCreate,
  task: _constants.TASK_APP_CREATE,
  params: _constants.PARAMS.withBase(),
  skipPlatforms: true,
  platforms: [] };exports["default"] = _default;
//# sourceMappingURL=task.rnv.app.create.js.map