var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(exports,"__esModule",{value:true});exports.cleanPlaformAssets=exports.upgradeProjectDependencies=exports.copyBuildsFolder=exports.copyAssetsFolder=exports.parseFonts=exports.copyRuntimeAssets=exports.checkAndCreateGitignore=exports.checkAndCreateProjectPackage=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 _configParser=require("../configManager/configParser");
var _prompt=require("../../cli/prompt");


var checkAndCreateProjectPackage=function checkAndCreateProjectPackage(c){return new Promise(function(resolve){
(0,_logger.logTask)('checkAndCreateProjectPackage');

if(!(0,_fileutils.fsExistsSync)(c.paths.project.package)){var _c$files$project$conf,_c$files$project$conf2,_c$files$project$conf3;
(0,_logger.logInfo)("Your "+
c.paths.project.package+" is missing. CREATING...DONE");


var packageName=c.files.project.config.projectName||
c.paths.project.dir.split('/').pop();
var version=((_c$files$project$conf=c.files.project.config.defaults)==null?void 0:(_c$files$project$conf2=_c$files$project$conf.package)==null?void 0:_c$files$project$conf2.version)||'0.1.0';
var templateName=((_c$files$project$conf3=c.files.project.config.defaults)==null?void 0:_c$files$project$conf3.template)||
'renative-template-hello-world';
var rnvVersion=c.files.rnv.package.version;

var pkgJson={};
pkgJson.name=packageName;
pkgJson.version=version;
pkgJson.dependencies={
renative:rnvVersion};

pkgJson.devDependencies={
rnv:rnvVersion};

pkgJson.devDependencies[templateName]=rnvVersion;
var pkgJsonStringClean=JSON.stringify(pkgJson,null,2);
(0,_fileutils.fsWriteFileSync)(c.paths.project.package,pkgJsonStringClean);
}

(0,_configParser.loadFile)(c.files.project,c.paths.project,'package');

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

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,'supportFiles/gitignore-template'),
ignrPath);

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


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 supportFiles=_path.default.resolve(c.paths.rnv.dir,'supportFiles');
(0,_fileutils.copyFileSync)(
_path.default.resolve(supportFiles,'fontManager.js'),
_path.default.resolve(
c.paths.project.assets.dir,
'runtime',
'fontManager.js'));


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


(0,_fileutils.copyFileSync)(
_path.default.resolve(supportFiles,'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 ASSET_PATH_ALIASES={
android:'app/src/main',
androidtv:'app/src/main',
androidwear:'app/src/main',
ios:'RNVApp',
tvos:'RNVAppTVOS',
tizen:'',
tizenmobile:'',
tizenwatch:'',
webos:'',
kaios:'',
firefoxtv:'',
firefoxos:'',
windows:'',
macos:'',
web:'',
chromecast:''};


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

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


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



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

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



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


destPath=_path.default.join(
(0,_common.getPlatformProjectDir)(c,platform),
ASSET_PATH_ALIASES[platform]);


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 _context2.stop();}}},null,null,null,Promise);};exports.copyAssetsFolder=copyAssetsFolder;


var generateDefaultAssets=function generateDefaultAssets(c,platform,sourcePath){var confirmAssets,_await$inquirerPrompt,confirm;return _regenerator.default.async(function generateDefaultAssets$(_context3){while(1){switch(_context3.prev=_context3.next){case 0:
(0,_logger.logTask)('generateDefaultAssets');
confirmAssets=true;if(!(
c.program.ci!==true)){_context3.next=8;break;}_context3.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=_context3.sent;confirm=_await$inquirerPrompt.confirm;


confirmAssets=confirm;case 8:


if(confirmAssets){
(0,_fileutils.copyFolderContentsRecursiveSync)(
_path.default.join(c.paths.rnv.dir,"projectTemplate/assets/"+platform),
sourcePath);

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


var copyBuildsFolder=function copyBuildsFolder(c,platform){return new Promise(function(resolve){
(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(_constants.WEB_HOSTED_PLATFORMS.includes(platform)){
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_DEV_DEPS=[
'rnv',
'@rnv/engine-rn',
'@rnv/engine-rn-next',
'@rnv/engine-rn-web',
'@rnv/engine-rn-electron'];


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


var upgradeProjectDependencies=function upgradeProjectDependencies(c,version){var _c$files$project$pack,_c$files$project$pack2;
(0,_logger.logTask)('upgradeProjectDependencies');





var devDependencies=(_c$files$project$pack=c.files.project.package)==null?void 0:_c$files$project$pack.devDependencies;

SYNCED_DEV_DEPS.forEach(function(dep){
if(devDependencies==null?void 0:devDependencies[dep]){
devDependencies[dep]=version;
}
});
SYNCED_TEMPLATES.forEach(function(templ){var _c$files$project$conf4,_c$files$project$conf5,_c$files$project$conf6;
if(devDependencies==null?void 0:devDependencies[templ]){
devDependencies[templ]=version;
}
if((_c$files$project$conf4=c.files.project.config)==null?void 0:(_c$files$project$conf5=_c$files$project$conf4.templates)==null?void 0:(_c$files$project$conf6=_c$files$project$conf5[templ])==null?void 0:_c$files$project$conf6.version){
c.files.project.config.templates[templ].version=version;
}
});


var dependencies=(_c$files$project$pack2=c.files.project.package)==null?void 0:_c$files$project$pack2.dependencies;
if(dependencies==null?void 0:dependencies.renative){
dependencies.renative=version;
}

(0,_fileutils.writeFileSync)(c.paths.project.package,c.files.project.package);

c._requiresNpmInstall=true;

(0,_fileutils.writeFileSync)(c.paths.project.config,c.files.project.config);
};exports.upgradeProjectDependencies=upgradeProjectDependencies;

var cleanPlaformAssets=function cleanPlaformAssets(c){return _regenerator.default.async(function cleanPlaformAssets$(_context4){while(1){switch(_context4.prev=_context4.next){case 0:
(0,_logger.logTask)('cleanPlaformAssets');_context4.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 _context4.abrupt("return",
true);case 5:case"end":return _context4.stop();}}},null,null,null,Promise);};exports.cleanPlaformAssets=cleanPlaformAssets;
//# sourceMappingURL=projectParser.js.map