var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(exports,"__esModule",{value:true});exports.cleanPlaformAssets=exports.upgradeDependencies=exports.upgradeProjectDependencies=exports.versionCheck=exports.copyBuildsFolder=exports.copyAssetsFolder=exports.parseFonts=exports.copyRuntimeAssets=exports.checkAndCreateBabelConfig=exports.checkAndCreateGitignore=void 0;var _toConsumableArray2=_interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));var _regenerator=_interopRequireDefault(require("@babel/runtime/regenerator"));var _path=_interopRequireDefault(require("path"));
var _constants=require("../constants");
var _common=require("../common");








var _fileutils=require("../systemManager/fileutils");










var _platformManager=require("../platformManager");
var _logger=require("../systemManager/logger");
var _pluginManager=require("../pluginManager");
var _prompt=require("../../cli/prompt");
var _engineManager=require("../engineManager");


var checkAndCreateGitignore=function checkAndCreateGitignore(c){var ignrPath;return _regenerator.default.async(function checkAndCreateGitignore$(_context){while(1){switch(_context.prev=_context.next){case 0:
(0,_logger.logTask)('checkAndCreateGitignore');
ignrPath=_path.default.join(c.paths.project.dir,'.gitignore');
if(!(0,_fileutils.fsExistsSync)(ignrPath)){
(0,_logger.logInfo)(
'Your .gitignore is missing. CREATING...DONE');


(0,_fileutils.copyFileSync)(
_path.default.join(c.paths.rnv.dir,'coreTemplateFiles/gitignore-template'),
ignrPath);

}return _context.abrupt("return",
true);case 4:case"end":return _context.stop();}}},null,null,null,Promise);};exports.checkAndCreateGitignore=checkAndCreateGitignore;


var checkAndCreateBabelConfig=function checkAndCreateBabelConfig(c){return _regenerator.default.async(function checkAndCreateBabelConfig$(_context2){while(1){switch(_context2.prev=_context2.next){case 0:
(0,_logger.logTask)('checkAndCreateBabelConfig');if(

c.paths.project.configExists){_context2.next=3;break;}return _context2.abrupt("return",false);case 3:


(0,_logger.logDebug)('configureProject:check babel config');
if(!(0,_fileutils.fsExistsSync)(c.paths.project.babelConfig)){
(0,_logger.logInfo)("Your babel config file "+
(0,_logger.chalk)().white(
c.paths.project.babelConfig)+" is missing! CREATING...DONE");


(0,_fileutils.copyFileSync)(
_path.default.join(c.paths.rnv.projectTemplate.dir,_constants.RN_BABEL_CONFIG_NAME),
c.paths.project.babelConfig);

}return _context2.abrupt("return",

true);case 6:case"end":return _context2.stop();}}},null,null,null,Promise);};exports.checkAndCreateBabelConfig=checkAndCreateBabelConfig;



var copyRuntimeAssets=function copyRuntimeAssets(c){return new Promise(function(resolve,reject){var _c$buildConfig;
(0,_logger.logTask)('copyRuntimeAssets');

var destPath=_path.default.join(c.paths.project.assets.dir,'runtime');


if(c.paths.appConfig.dirs){
c.paths.appConfig.dirs.forEach(function(v){
var sourcePath=_path.default.join(v,'assets/runtime');
(0,_fileutils.copyFolderContentsRecursiveSync)(sourcePath,destPath);
});
}else{
var sourcePath=_path.default.join(
c.paths.appConfig.dir,
'assets/runtime');

(0,_fileutils.copyFolderContentsRecursiveSync)(sourcePath,destPath);
}

if(!((_c$buildConfig=c.buildConfig)==null?void 0:_c$buildConfig.common)){
(0,_logger.logDebug)('BUILD_CONFIG',c.buildConfig);
reject("Your "+
(0,_logger.chalk)().white(
c.paths.appConfig.config)+" is missconfigured. (Maybe you have older version?). Missing "+
(0,_logger.chalk)().white(
'{ common: {} }')+" object at root");



return;
}



var fontsObj='export default [';

var duplicateFontCheck=[];
parseFonts(c,function(font,dir){
if(font.includes('.ttf')||font.includes('.otf')||font.includes('.woff')){
var key=font.split('.')[0];
var includedFonts=(0,_common.getConfigProp)(
c,
c.platform,
'includedFonts');

if(includedFonts){
if(
includedFonts.includes('*')||
includedFonts.includes(key))
{
if(font&&!duplicateFontCheck.includes(font)){
duplicateFontCheck.push(font);
var fontSource=_path.default.join(dir,font).replace(/\\/g,'\\\\');
if((0,_fileutils.fsExistsSync)(fontSource)){




fontsObj+="{\n                              fontFamily: '"+
key+"',\n                              file: require('"+
fontSource+"'),\n                          },";

}else{
(0,_logger.logWarning)("Font "+
(0,_logger.chalk)().white(
fontSource)+" doesn't exist! Skipping.");


}
}
}
}
}
});

fontsObj+='];';
if(!(0,_fileutils.fsExistsSync)(c.paths.project.assets.runtimeDir)){
(0,_fileutils.mkdirSync)(c.paths.project.assets.runtimeDir);
}
var fontJsPath=_path.default.join(
c.paths.project.assets.dir,
'runtime',
'fonts.web.js');

if((0,_fileutils.fsExistsSync)(fontJsPath)){
var existingFileContents=(0,_fileutils.fsReadFileSync)(fontJsPath).toString();

if(existingFileContents!==fontsObj){
(0,_logger.logDebug)('newFontsJsFile');
(0,_fileutils.fsWriteFileSync)(fontJsPath,fontsObj);
}
}else{
(0,_logger.logDebug)('newFontsJsFile');
(0,_fileutils.fsWriteFileSync)(fontJsPath,fontsObj);
}

var coreTemplateFiles=_path.default.resolve(c.paths.rnv.dir,'coreTemplateFiles');
(0,_fileutils.copyFileSync)(
_path.default.resolve(coreTemplateFiles,'fontManager.js'),
_path.default.resolve(
c.paths.project.assets.dir,
'runtime',
'fontManager.js'));


(0,_fileutils.copyFileSync)(
_path.default.resolve(coreTemplateFiles,'fontManager.js'),
_path.default.resolve(
c.paths.project.assets.dir,
'runtime',
'fontManager.server.web.js'));


(0,_fileutils.copyFileSync)(
_path.default.resolve(coreTemplateFiles,'fontManager.web.js'),
_path.default.resolve(
c.paths.project.assets.dir,
'runtime',
'fontManager.web.js'));



resolve();
});};exports.copyRuntimeAssets=copyRuntimeAssets;

var parseFonts=function parseFonts(c,callback){
(0,_logger.logTask)('parseFonts');

if(c.buildConfig){

if((0,_fileutils.fsExistsSync)(c.paths.project.appConfigBase.fontsDir)){
(0,_fileutils.fsReaddirSync)(c.paths.project.appConfigBase.fontsDir).forEach(
function(font){
if(callback){callback(font,c.paths.project.appConfigBase.fontsDir);}
});

}

if(c.paths.appConfig.fontsDirs){
c.paths.appConfig.fontsDirs.forEach(function(v){
if((0,_fileutils.fsExistsSync)(v)){
(0,_fileutils.fsReaddirSync)(v).forEach(function(font){
if(callback)callback(font,v);
});
}
});
}else if((0,_fileutils.fsExistsSync)(c.paths.appConfig.fontsDir)){
(0,_fileutils.fsReaddirSync)(c.paths.appConfig.fontsDir).forEach(function(font){
if(callback)callback(font,c.paths.appConfig.fontsDir);
});
}
}
};exports.parseFonts=parseFonts;

var copyAssetsFolder=function copyAssetsFolder(c,platform,subPath,customFn){var hasAssetFolder,sourcePath,destPath,tsPathsConfig,_sourcePath;return _regenerator.default.async(function copyAssetsFolder$(_context3){while(1){switch(_context3.prev=_context3.next){case 0:
(0,_logger.logTask)('copyAssetsFolder');if(

(0,_platformManager.isPlatformActive)(c,platform)){_context3.next=3;break;}return _context3.abrupt("return");case 3:if(!


c.paths.appConfig.dirs){_context3.next=10;break;}
hasAssetFolder=c.paths.appConfig.dirs.
filter(function(v){return(0,_fileutils.fsExistsSync)(_path.default.join(v,"assets/"+platform));}).length;if(
hasAssetFolder){_context3.next=8;break;}_context3.next=8;return _regenerator.default.awrap(
generateDefaultAssets(
c,
platform,
_path.default.join(c.paths.appConfig.dirs[0],"assets/"+platform)));case 8:_context3.next=14;break;case 10:



sourcePath=_path.default.join(
c.paths.appConfig.dir,"assets/"+
platform);if(

(0,_fileutils.fsExistsSync)(sourcePath)){_context3.next=14;break;}_context3.next=14;return _regenerator.default.awrap(
generateDefaultAssets(c,platform,sourcePath));case 14:if(!



customFn){_context3.next=16;break;}return _context3.abrupt("return",
customFn(c,platform));case 16:


destPath=_path.default.join(
(0,_common.getPlatformProjectDir)(c,platform),
subPath||'');


tsPathsConfig=(0,_common.getTimestampPathsConfig)(c,platform);


if(c.paths.appConfig.dirs){
c.paths.appConfig.dirs.forEach(function(v){
var sourcePath=_path.default.join(v,"assets/"+platform);
(0,_fileutils.copyFolderContentsRecursiveSync)(sourcePath,destPath,true,false,false,null,tsPathsConfig);
});
}else{
_sourcePath=_path.default.join(
c.paths.appConfig.dir,"assets/"+
platform);

(0,_fileutils.copyFolderContentsRecursiveSync)(_sourcePath,destPath,true,false,false,null,tsPathsConfig);
}case 19:case"end":return _context3.stop();}}},null,null,null,Promise);};exports.copyAssetsFolder=copyAssetsFolder;


var generateDefaultAssets=function generateDefaultAssets(c,platform,sourcePath){var confirmAssets,_await$inquirerPrompt,confirm,engine;return _regenerator.default.async(function generateDefaultAssets$(_context4){while(1){switch(_context4.prev=_context4.next){case 0:
(0,_logger.logTask)('generateDefaultAssets');
confirmAssets=true;if(!(
c.program.ci!==true)){_context4.next=8;break;}_context4.next=5;return _regenerator.default.awrap(
(0,_prompt.inquirerPrompt)({
type:'confirm',
message:"It seems you don't have assets configured in "+(0,_logger.chalk)().white(
sourcePath)+" do you want generate default ones?"}));case 5:_await$inquirerPrompt=_context4.sent;confirm=_await$inquirerPrompt.confirm;


confirmAssets=confirm;case 8:


if(confirmAssets){
engine=(0,_engineManager.getEngineRunnerByPlatform)(c,c.platform);
(0,_fileutils.copyFolderContentsRecursiveSync)(
_path.default.join(engine.originalTemplateAssetsDir,platform),
sourcePath);

}case 9:case"end":return _context4.stop();}}},null,null,null,Promise);};


var copyBuildsFolder=function copyBuildsFolder(c,platform){return new Promise(function(resolve){var _c$runtime$currentPla;
(0,_logger.logTask)('copyBuildsFolder');
if(!(0,_platformManager.isPlatformActive)(c,platform,resolve))return;

var destPath=_path.default.join((0,_common.getAppFolder)(c));
var tsPathsConfig=(0,_common.getTimestampPathsConfig)(c,platform);

var configPropsInjects=[];
_constants.INJECTABLE_CONFIG_PROPS.forEach(function(v){
configPropsInjects.push({
pattern:"{{configProps."+v+"}}",
override:(0,_common.getConfigProp)(c,c.platform,v)});

});
c.configPropsInjects=configPropsInjects;
var allInjects=[].concat((0,_toConsumableArray2.default)(c.configPropsInjects),(0,_toConsumableArray2.default)(c.systemPropsInjects),(0,_toConsumableArray2.default)(c.runtimePropsInjects));


var sourcePath1=(0,_common.getBuildsFolder)(
c,
platform,
c.paths.project.appConfigBase.dir);

(0,_fileutils.copyFolderContentsRecursiveSync)(sourcePath1,destPath,true,false,false,allInjects,tsPathsConfig);


var sourcePath1secLegacy=(0,_common.getBuildsFolder)(
c,
platform,
c.paths.workspace.project.appConfigBase.dir_LEGACY);

(0,_fileutils.copyFolderContentsRecursiveSync)(sourcePath1secLegacy,destPath,true,
false,false,allInjects,tsPathsConfig);


var sourcePath1sec=(0,_common.getBuildsFolder)(
c,
platform,
c.paths.workspace.project.appConfigBase.dir);

(0,_fileutils.copyFolderContentsRecursiveSync)(sourcePath1sec,destPath,true,false,false,allInjects,tsPathsConfig);

if((0,_fileutils.fsExistsSync)(sourcePath1secLegacy)){
(0,_logger.logWarning)("Path: "+(0,_logger.chalk)().red(sourcePath1secLegacy)+" is DEPRECATED.\nMove your files to: "+
(0,_logger.chalk)().white(sourcePath1sec)+" instead");
}


if((_c$runtime$currentPla=c.runtime.currentPlatform)==null?void 0:_c$runtime$currentPla.isWebHosted){
var sourcePathShared=_path.default.join(
c.paths.project.appConfigBase.dir,
'builds/_shared');

if((0,_fileutils.fsExistsSync)(sourcePathShared)){
(0,_logger.logWarning)('Folder builds/_shared is DEPRECATED. use builds/<PLATFORM> instead ');
}
(0,_fileutils.copyFolderContentsRecursiveSync)(
sourcePathShared,
(0,_common.getPlatformBuildDir)(c),
true,false,false,allInjects);

}


if(c.paths.appConfig.dirs){
c.paths.appConfig.dirs.forEach(function(v){
var sourceV=(0,_common.getBuildsFolder)(c,platform,v);
(0,_fileutils.copyFolderContentsRecursiveSync)(
sourceV,
destPath,
true,false,false,allInjects,tsPathsConfig);

});
}else{
(0,_fileutils.copyFolderContentsRecursiveSync)(
(0,_common.getBuildsFolder)(c,platform,c.paths.appConfig.dir),
destPath,
true,false,false,allInjects,tsPathsConfig);

}


var sourcePath0sec=(0,_common.getBuildsFolder)(
c,
platform,
c.paths.workspace.appConfig.dir);

(0,_fileutils.copyFolderContentsRecursiveSync)(sourcePath0sec,destPath,true,false,false,allInjects,tsPathsConfig);

(0,_pluginManager.copyTemplatePluginsSync)(c);

resolve();
});};exports.copyBuildsFolder=copyBuildsFolder;

var SYNCED_DEPS=[
'rnv',
'@rnv/engine-rn',
'@rnv/engine-rn-next',
'@rnv/engine-rn-web',
'@rnv/engine-rn-electron',
'renative',
'renative-template-hello-world',
'renative-template-blank'];


var SYNCED_TEMPLATES=[
'renative-template-hello-world',
'renative-template-blank'];


var versionCheck=function versionCheck(c){var _c$files$project,_c$files$project$conf,_c$files$rnv,_c$files$rnv$package,_c$files$project2,_c$files$project2$pac,_c$files$project2$pac2;var recCmd,actionNoUpdate,actionWithUpdate,actionUpgrade,_await$inquirerPrompt2,chosenAction;return _regenerator.default.async(function versionCheck$(_context5){while(1){switch(_context5.prev=_context5.next){case 0:
(0,_logger.logTask)('versionCheck');if(!(

c.runtime.isWrapper||c.runtime.versionCheckCompleted||((_c$files$project=c.files.project)==null?void 0:(_c$files$project$conf=_c$files$project.config)==null?void 0:_c$files$project$conf.skipAutoUpdate))){_context5.next=3;break;}return _context5.abrupt("return",
true);case 3:

c.runtime.rnvVersionRunner=(_c$files$rnv=c.files.rnv)==null?void 0:(_c$files$rnv$package=_c$files$rnv.package)==null?void 0:_c$files$rnv$package.version;
c.runtime.rnvVersionProject=(_c$files$project2=c.files.project)==null?void 0:(_c$files$project2$pac=_c$files$project2.package)==null?void 0:(_c$files$project2$pac2=_c$files$project2$pac.devDependencies)==null?void 0:_c$files$project2$pac2.rnv;
(0,_logger.logTask)("versionCheck:rnvRunner:"+
c.runtime.rnvVersionRunner+",rnvProject:"+
c.runtime.rnvVersionProject,

(0,_logger.chalk)().grey);if(!(

c.runtime.rnvVersionRunner&&c.runtime.rnvVersionProject)){_context5.next=19;break;}if(!(
c.runtime.rnvVersionRunner!==c.runtime.rnvVersionProject)){_context5.next=19;break;}
recCmd=(0,_logger.chalk)().white("$ npx "+(0,_logger.getCurrentCommand)(true));
actionNoUpdate='Continue and skip updating package.json';
actionWithUpdate='Continue and update package.json';
actionUpgrade="Upgrade project to "+
c.runtime.rnvVersionRunner;_context5.next=14;return _regenerator.default.awrap(


(0,_prompt.inquirerPrompt)({
message:'What to do next?',
type:'list',
name:'chosenAction',
choices:[actionNoUpdate,actionWithUpdate,actionUpgrade],
warningMessage:"You are running $rnv v"+(0,_logger.chalk)().red(
c.runtime.rnvVersionRunner)+" against project built with rnv v"+
(0,_logger.chalk)().red(
c.runtime.rnvVersionProject)+". This might result in unexpected behaviour!\nIt is recommended that you run your rnv command with npx prefix: "+


recCmd+" . or manually update your devDependencies.rnv version in your package.json."}));case 14:_await$inquirerPrompt2=_context5.sent;chosenAction=_await$inquirerPrompt2.chosenAction;



c.runtime.versionCheckCompleted=true;

c.runtime.skipPackageUpdate=chosenAction===actionNoUpdate;

if(chosenAction===actionUpgrade){
upgradeProjectDependencies(c,c.runtime.rnvVersionRunner);
}case 19:return _context5.abrupt("return",


true);case 20:case"end":return _context5.stop();}}},null,null,null,Promise);};exports.versionCheck=versionCheck;


var upgradeProjectDependencies=function upgradeProjectDependencies(c,version){
(0,_logger.logTask)('upgradeProjectDependencies');




var result=upgradeDependencies(
c.files.project.package,
c.paths.project.package,
c.files.project.config,
c.paths.project.config,
version);

c._requiresNpmInstall=true;
return result;
};exports.upgradeProjectDependencies=upgradeProjectDependencies;

var upgradeDependencies=function upgradeDependencies(packageFile,packagesPath,configFile,configPath,version){


var result=[];

_fixDeps(packageFile==null?void 0:packageFile.devDependencies,version);
_fixDeps(packageFile==null?void 0:packageFile.dependencies,version);
_fixDeps(packageFile==null?void 0:packageFile.peerDependencies,version);
SYNCED_TEMPLATES.forEach(function(templ){var _configFile$templates,_configFile$templates2;
if(configFile==null?void 0:(_configFile$templates=configFile.templates)==null?void 0:(_configFile$templates2=_configFile$templates[templ])==null?void 0:_configFile$templates2.version){
configFile.templates[templ].version=version;
}
});

if(packageFile){
(0,_fileutils.writeFileSync)(packagesPath,packageFile);
result.push(packagesPath);
}
if(configFile){
(0,_fileutils.writeFileSync)(configPath,configFile);
result.push(configPath);
}
return result;
};exports.upgradeDependencies=upgradeDependencies;

var _fixDeps=function _fixDeps(deps,version){
SYNCED_DEPS.forEach(function(dep){
var d=deps==null?void 0:deps[dep];
if(d){
var prefix=d.match(/~|>|>=|\^|<|<=/)||'';
deps[dep]=""+prefix+version;
}
});
};

var cleanPlaformAssets=function cleanPlaformAssets(c){return _regenerator.default.async(function cleanPlaformAssets$(_context6){while(1){switch(_context6.prev=_context6.next){case 0:
(0,_logger.logTask)('cleanPlaformAssets');_context6.next=3;return _regenerator.default.awrap(

(0,_fileutils.cleanFolder)(c.paths.project.assets.dir));case 3:
(0,_fileutils.mkdirSync)(c.paths.project.assets.runtimeDir);return _context6.abrupt("return",
true);case 5:case"end":return _context6.stop();}}},null,null,null,Promise);};exports.cleanPlaformAssets=cleanPlaformAssets;
//# sourceMappingURL=index.js.map