var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(exports,"__esModule",{value:true});exports.checkAndMigrateProject=void 0;var _fs=_interopRequireDefault(require("fs"));
var _path=_interopRequireDefault(require("path"));
var _chalk=_interopRequireDefault(require("chalk"));

var _prompt=require("../systemTools/prompt");
var _logger=require("../systemTools/logger");
var _fileutils=require("../systemTools/fileutils");
var _configParser=require("../configTools/configParser");
var _cleaner=require("../systemTools/cleaner");
var _projectParser=require("./projectParser");

var checkAndMigrateProject=function checkAndMigrateProject(c){return new Promise(function(resolve,reject){
(0,_logger.logTask)('checkAndMigrateProject');
var prjDir=c.paths.project.dir;


var paths={
project:prjDir,
globalConfig:_path.default.join(c.paths.GLOBAL_RNV_DIR,'config.json'),



config:_path.default.join(prjDir,'rnv-config.json'),
configNew:_path.default.join(prjDir,'renative.json'),
package:_path.default.join(prjDir,'package.json'),
plugins:_path.default.join(prjDir,'projectConfig/plugins.json'),
permissions:_path.default.join(prjDir,'projectConfig/permissions.json'),
appConfigs:_path.default.join(prjDir,'appConfigs')};


try{
paths.appConfigDirs=(0,_configParser.listAppConfigsFoldersSync)(c).map(function(v){return prjDir+"/appConfigs/"+v+"/config.json";});
}catch(e){
(0,_logger.logWarning)(e);
}

if(_fs.default.existsSync(paths.config)){
(0,_prompt.askQuestion)('Your project has been created with previous version of ReNative. Do you want to migrate it to new format? Backing up project is recommended! To Proceed type: (y)').
then(function(v){
if(v==='y'){
c.program.reset=true;
_migrateProject(c,paths).
then(function(){return _migrateProjectSoft(c,paths);}).
then(function(){return(0,_cleaner.cleanProjectModules)(c);}).
then(function(){return(0,_projectParser.configureNodeModules)(c);}).
then(function(){return resolve();}).
catch(function(e){return reject(e);});
return;
}
return resolve();
}).
catch(function(e){return reject(e);});
}else{
_migrateProjectSoft(c,paths).then(function(){return resolve();}).catch(function(e){return reject(e);});
}
});};exports.checkAndMigrateProject=checkAndMigrateProject;

var PATH_PROPS=[
{oldKey:'globalConfigFolder',newKey:'globalConfigDir'},
{oldKey:'appConfigsFolder',newKey:'appConfigsDir'},
{oldKey:'platformTemplatesFolder',newKey:'platformTemplatesDir'},
{oldKey:'entryFolder',newKey:'entryDir'},
{oldKey:'platformAssetsFolder',newKey:'platformAssetsDir'},
{oldKey:'platformBuildsFolder',newKey:'platformBuildsDir'},
{oldKey:'projectConfigFolder',newKey:'projectConfigDir'}];


var _migrateProjectSoft=function _migrateProjectSoft(c,paths){return new Promise(function(resolve,reject){
(0,_logger.logTask)('_migrateProjectSoft');
var files;
try{var _files$configNew,_files$configNew2,_files$configNew3;
var requiresSave=false;
files={
configNew:(0,_fileutils.readObjectSync)(paths.configNew)};



if((_files$configNew=files.configNew)==null?void 0:_files$configNew.paths){
PATH_PROPS.forEach(function(v){
if(files.configNew.paths[v.oldKey]){
(0,_logger.logWarning)("You use old key "+_chalk.default.white(v.oldKey)+" instead of new one: "+_chalk.default.white(v.newKey)+". ReNative will try to fix it for you!");
files.configNew.paths[v.newKey]=files.configNew.paths[v.oldKey];
delete files.configNew.paths[v.oldKey];
requiresSave=true;
}
});
}

if((_files$configNew2=files.configNew)==null?void 0:_files$configNew2.android){
(0,_logger.logWarning)('Found legacy object "android" at root. ReNative will try to fix it for you!');
files.configNew.platforms=files.configNew.platforms||{};

files.configNew.platforms.android=(0,_fileutils.mergeObjects)(files.configNew.platforms.android||{},files.configNew.android);
if(files.configNew.platforms.androidtv){
files.configNew.platforms.androidtv=(0,_fileutils.mergeObjects)(files.configNew.platforms.androidtv||{},files.configNew.android);
}
if(files.configNew.platforms.androidwear){
files.configNew.platforms.androidwear=(0,_fileutils.mergeObjects)(files.configNew.platforms.androidwear||{},files.configNew.android);
}
delete files.configNew.android;
requiresSave=true;
}

if((_files$configNew3=files.configNew)==null?void 0:_files$configNew3.ios){
(0,_logger.logWarning)('Found legacy object "ios" at root. ReNative will try to fix it for you!');
files.configNew.platforms=files.configNew.platforms||{};
files.configNew.platforms.ios=(0,_fileutils.mergeObjects)(files.configNew.platforms.ios||{},files.configNew.ios);
if(files.configNew.platforms.tvos){
files.configNew.platforms.tvos=(0,_fileutils.mergeObjects)(files.configNew.platforms.tvos||{},files.configNew.ios);
}
delete files.configNew.ios;
requiresSave=true;
}

if(_fs.default.existsSync(paths.permissions)){
(0,_logger.logWarning)("Found legacy object "+_chalk.default.red(paths.permissions)+". this should be migrated to "+_chalk.default.green('./renative.json'));
}

if(_fs.default.existsSync(paths.plugins)){
(0,_logger.logWarning)("Found legacy object "+_chalk.default.red(paths.plugins)+". this should be migrated to "+_chalk.default.green('./renative.json'));
}

if(requiresSave)(0,_fileutils.writeObjectSync)(paths.configNew,files.configNew);



}catch(e){
(0,_logger.logError)("Migration not successfull. "+e);
}

resolve();
});};

var _migrateFile=function _migrateFile(oldPath,newPath){
if(!_fs.default.existsSync(newPath)){
if(_fs.default.existsSync(oldPath)){
(0,_logger.logWarning)("Found old app config at "+_chalk.default.white(oldPath)+". will copy to "+_chalk.default.white(newPath));
}
(0,_fileutils.copyFileSync)(oldPath,newPath);
}
};

var _migrateProject=function _migrateProject(c,paths){return new Promise(function(resolve,reject){
(0,_logger.logTask)('MIGRATION STARTED');

if(!_fs.default.existsSync(c.paths.private.config)){
if(_fs.default.existsSync(paths.globalConfig)){
(0,_fileutils.copyFileSync)(paths.globalConfig,c.paths.private.config);
}
}

var files={
config:(0,_fileutils.readObjectSync)(paths.config),
package:(0,_fileutils.readObjectSync)(paths.package),
plugins:(0,_fileutils.readObjectSync)(paths.plugins),
permissions:(0,_fileutils.readObjectSync)(paths.permissions)};


(0,_logger.logDebug)("paths to migrate: \n "+paths);

var newConfig={};

if(files.package){
newConfig.projectName=files.package.name;
}

if(files.config){
newConfig.defaults={};

if(files.config.defaultProjectConfigs){
newConfig.defaults=(0,_fileutils.mergeObjects)(newConfig.defaults,files.config.defaultProjectConfigs);
}
newConfig.currentTemplate=newConfig.defaults.template||'renative-template-hello-world';

newConfig.templates={};

if(newConfig.defaults.template){
newConfig.templates[newConfig.defaults.template]={
version:c.files.rnv.package.version};

}

delete newConfig.defaults.template;

newConfig.paths={};
PATH_PROPS.forEach(function(v){
if(files.config[v.oldKey]){
newConfig.paths[v.newKey]=files.config[v.oldKey];
}
});
newConfig.paths.appConfigDirs=[newConfig.paths.appConfigDir];
delete newConfig.paths.appConfigDir;

if(files.config.defaultPorts){
newConfig.defaults.ports=files.config.defaultPorts;
}

if(files.config.crypto){
newConfig.crypto=files.config.crypto;
}
}

if(!newConfig.platforms)newConfig.platforms={};

if(files.plugins){
newConfig.plugins=files.plugins.plugins;

if(files.plugins.android){
newConfig.platforms.android=files.plugins.android;
}
if(files.plugins.ios){
newConfig.platforms.ios=files.plugins.ios;
}
}

if(files.permissions){
newConfig.permissions=files.permissions.permissions;
}

var pathsToDelete=[
paths.config,
paths.plugins,
paths.permissions].
concat(paths.appConfigDirs);

paths.appConfigDirs.forEach(function(v){
if(_fs.default.existsSync(v)){
(0,_fileutils.copyFileSync)(v,v.replace('/config.json','/renative.json'));
}
});

(0,_fileutils.writeObjectSync)(c.paths.project.config,newConfig);

(0,_logger.logDebug)("Paths to delete, "+pathsToDelete.join('\n'));

(0,_fileutils.removeFilesSync)(pathsToDelete);

(0,_logger.logSuccess)('Migration Complete!');

resolve();
});};