var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(exports,"__esModule",{value:true});exports.exportElectron=exports.buildElectron=exports.runElectron=exports.configureElectronProject=void 0;var _regenerator=_interopRequireDefault(require("@babel/runtime/regenerator"));var _path=_interopRequireDefault(require("path"));
var _child_process=require("child_process");
var _platformManager=require("../core/platformManager");
var _exec=require("../core/systemManager/exec");
var _fileutils=require("../core/systemManager/fileutils");







var _common=require("../core/common");




















var _resolve=require("../core/resolve");
var _logger=require("../core/systemManager/logger");







var _projectParser=require("../core/projectManager/projectParser");



var _constants=require("../core/constants");


var _sdkWebpack=require("../sdk-webpack");


var configureElectronProject=function configureElectronProject(c){var platform,bundleAssets;return _regenerator.default.async(function configureElectronProject$(_context){while(1){switch(_context.prev=_context.next){case 0:
(0,_logger.logTask)('configureElectronProject');

platform=c.platform;

c.runtime.platformBuildsProjectPath=""+(0,_common.getPlatformProjectDir)(c);
bundleAssets=(0,_common.getConfigProp)(c,platform,'bundleAssets')===true;_context.next=6;return _regenerator.default.awrap(


(0,_projectParser.copyAssetsFolder)(
c,
platform,
platform===_constants.MACOS?_generateICNS:null));case 6:_context.next=8;return _regenerator.default.awrap(


(0,_sdkWebpack.configureCoreWebProject)(c,bundleAssets?_constants.RNV_PROJECT_DIR_NAME:_constants.RNV_SERVER_DIR_NAME));case 8:_context.next=10;return _regenerator.default.awrap(

configureProject(c));case 10:return _context.abrupt("return",
(0,_projectParser.copyBuildsFolder)(c,platform));case 11:case"end":return _context.stop();}}},null,null,null,Promise);};exports.configureElectronProject=configureElectronProject;

var merge=require('deepmerge');

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

if(!(0,_platformManager.isPlatformActive)(c,platform,resolve))return;

var platformProjectDir=(0,_common.getPlatformProjectDir)(c);
var platformBuildDir=(0,_common.getPlatformBuildDir)(c);
var templateFolder=(0,_common.getTemplateProjectDir)(c);
var bundleAssets=(0,_common.getConfigProp)(c,platform,'bundleAssets')===true;
var electronConfigPath=_path.default.join(platformBuildDir,'electronConfig.json');
var packagePath=_path.default.join(platformProjectDir,'package.json');
var appId=(0,_common.getAppId)(c,platform);

if(!(0,_fileutils.fsExistsSync)(packagePath)){
(0,_logger.logWarning)("Looks like your "+
(0,_logger.chalk)().white(
platform)+" platformBuild is misconfigured!. let's repair it.");


(0,_platformManager.createPlatformBuild)(c,platform).
then(function(){return configureElectronProject(c,platform);}).
then(function(){return resolve(c);}).
catch(function(e){return reject(e);});
return;
}

var pkgJson=_path.default.join(templateFolder,'package.json');
var packageJson=(0,_fileutils.readObjectSync)(pkgJson);

packageJson.name=c.runtime.appId+"-"+platform;
packageJson.productName=""+(0,_common.getAppTitle)(c,platform);
packageJson.version=""+(0,_common.getAppVersion)(c,platform);
packageJson.description=""+(0,_common.getAppDescription)(c,platform);
packageJson.author=(0,_common.getAppAuthor)(c,platform);
packageJson.license=""+(0,_common.getAppLicense)(c,platform);
packageJson.main='./main.js';

(0,_fileutils.writeFileSync)(packagePath,packageJson);

var browserWindow={
width:1200,
height:800,
webPreferences:{nodeIntegration:true}};

var browserWindowExt=(0,_common.getConfigProp)(c,platform,'BrowserWindow');
if(browserWindowExt){
browserWindow=merge(browserWindow,browserWindowExt);
}
var browserWindowStr=JSON.stringify(browserWindow,null,2);


if(bundleAssets){
var injects=[
{
pattern:'{{PLUGIN_INJECT_BROWSER_WINDOW}}',
override:browserWindowStr}];



(0,_common.addSystemInjects)(c,injects);

(0,_fileutils.writeCleanFile)(
_path.default.join(platformBuildDir,'main.prod.js'),
_path.default.join(platformProjectDir,'main.js'),
injects,null,c);

}else{
var _injects=[
{
pattern:'{{DEV_SERVER}}',
override:"http://"+c.runtime.localhost+":"+c.runtime.port},

{
pattern:'{{PLUGIN_INJECT_BROWSER_WINDOW}}',
override:browserWindowStr}];



(0,_common.addSystemInjects)(c,_injects);

(0,_fileutils.writeCleanFile)(
_path.default.join(platformBuildDir,'main.dev.js'),
_path.default.join(platformProjectDir,'main.js'),
_injects,null,c);

}

var macConfig={};
if(platform===_constants.MACOS){
macConfig.mac={
entitlements:_path.default.join(platformProjectDir,'entitlements.mac.plist'),
entitlementsInherit:_path.default.join(
platformProjectDir,
'entitlements.mac.plist'),

hardenedRuntime:true};

macConfig.mas={
entitlements:_path.default.join(platformProjectDir,'entitlements.mas.plist'),
entitlementsInherit:_path.default.join(
platformProjectDir,
'entitlements.mas.inherit.plist'),

provisioningProfile:_path.default.join(
platformProjectDir,
'embedded.provisionprofile'),

hardenedRuntime:false};

}

var electronConfig=merge(
{
appId:appId,
directories:{
app:platformProjectDir,
buildResources:_path.default.join(platformProjectDir,'resources'),
output:_path.default.join(platformBuildDir,'build/release')},

files:['!build/release']},

macConfig);


var electronConfigExt=(0,_common.getConfigProp)(c,platform,'electronConfig');

if(electronConfigExt){
electronConfig=merge(electronConfig,electronConfigExt);
}
(0,_fileutils.writeFileSync)(electronConfigPath,electronConfig);

resolve();
});};

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

(0,_sdkWebpack.buildWeb)(c));case 3:return _context2.abrupt("return",
true);case 4:case"end":return _context2.stop();}}},null,null,null,Promise);};exports.buildElectron=buildElectron;


var exportElectron=function exportElectron(c){var platformBuildDir,buildPath;return _regenerator.default.async(function exportElectron$(_context3){while(1){switch(_context3.prev=_context3.next){case 0:
(0,_logger.logTask)('exportElectron');

platformBuildDir=platformBuildDir(c);
buildPath=_path.default.join(platformBuildDir,'build');if(!

(0,_fileutils.fsExistsSync)(buildPath)){_context3.next=7;break;}
(0,_logger.logInfo)("exportElectron: removing old build "+buildPath);_context3.next=7;return _regenerator.default.awrap(
(0,_fileutils.removeDirs)([buildPath]));case 7:_context3.next=9;return _regenerator.default.awrap(


(0,_exec.executeAsync)(
c,"npx electron-builder --config "+
_path.default.join(
platformBuildDir,
'electronConfig.json')));case 9:



(0,_logger.logSuccess)("Your Exported App is located in "+
(0,_logger.chalk)().cyan(
_path.default.join(platformBuildDir,'build/release'))+" .");case 10:case"end":return _context3.stop();}}},null,null,null,Promise);};exports.exportElectron=exportElectron;




var runElectron=function runElectron(c){var platform,port,bundleAssets,isPortActive,resetCompleted;return _regenerator.default.async(function runElectron$(_context4){while(1){switch(_context4.prev=_context4.next){case 0:
(0,_logger.logTask)('runElectron');

platform=c.platform;
port=c.runtime.port;


bundleAssets=(0,_common.getConfigProp)(c,platform,'bundleAssets')===true;if(!

bundleAssets){_context4.next=11;break;}_context4.next=7;return _regenerator.default.awrap(
buildElectron(c));case 7:_context4.next=9;return _regenerator.default.awrap(
_runElectronSimulator(c));case 9:_context4.next=32;break;case 11:_context4.next=13;return _regenerator.default.awrap(

(0,_common.checkPortInUse)(c,platform,port));case 13:isPortActive=_context4.sent;if(
isPortActive){_context4.next=21;break;}
(0,_logger.logInfo)("Looks like your "+
(0,_logger.chalk)().white(
platform)+" devServer at port "+
(0,_logger.chalk)().white(
port)+" is not running. Starting it up for you...");


(0,_sdkWebpack.waitForWebpack)(c).
then(function(){return _runElectronSimulator(c);}).
catch(_logger.logError);_context4.next=19;return _regenerator.default.awrap(

(0,_sdkWebpack.runWebpackServer)(c));case 19:_context4.next=32;break;case 21:_context4.next=23;return _regenerator.default.awrap(

(0,_common.confirmActiveBundler)(c));case 23:resetCompleted=_context4.sent;if(!
resetCompleted){_context4.next=30;break;}
(0,_sdkWebpack.waitForWebpack)(c).
then(function(){return _runElectronSimulator(c);}).
catch(_logger.logError);_context4.next=28;return _regenerator.default.awrap(

(0,_sdkWebpack.runWebpackServer)(c));case 28:_context4.next=32;break;case 30:_context4.next=32;return _regenerator.default.awrap(

_runElectronSimulator(c));case 32:case"end":return _context4.stop();}}},null,null,null,Promise);};exports.runElectron=runElectron;





var _runElectronSimulator=function _runElectronSimulator(c){var elc,child;return _regenerator.default.async(function _runElectronSimulator$(_context5){while(1){switch(_context5.prev=_context5.next){case 0:
(0,_logger.logTask)("_runElectronSimulator:"+c.platform);

elc=(0,_resolve.doResolve)('electron')+"/cli.js";

child=(0,_child_process.spawn)('node',[elc,_path.default.join((0,_common.getPlatformProjectDir)(c),'/main.js')],{
detached:true,
env:process.env,
stdio:'inherit'}).

on('close',function(code){return process.exit(code);}).
on('error',function(spawnError){return(0,_logger.logError)(spawnError);});

child.unref();case 4:case"end":return _context5.stop();}}},null,null,null,Promise);};


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

var source;

if(c.paths.appConfig.dirs){
c.paths.appConfig.dirs.forEach(function(v){
var pf=_path.default.join(v,"assets/"+platform+"/AppIcon.iconset");
if((0,_fileutils.fsExistsSync)(pf)){
source=pf;
}
});
}else if(c.paths.appConfig.dir){
source=_path.default.join(
c.paths.appConfig.dir,"assets/"+
platform+"/AppIcon.iconset");

}

var dest=_path.default.join(
(0,_common.getPlatformProjectDir)(c),
'resources/icon.icns');


if(!source){
(0,_logger.logWarning)("You are missing AppIcon.iconset in "+
(0,_logger.chalk)().white(
c.paths.appConfig.dir)+". icon.icns will not be generated!");


resolve();
return;
}

if(!(0,_fileutils.fsExistsSync)(source)){
(0,_logger.logWarning)("Your app config is missing "+
(0,_logger.chalk)().white(
source)+". icon.icns will not be generated!");


resolve();
return;
}

(0,_fileutils.mkdirSync)(_path.default.join((0,_common.getPlatformProjectDir)(c),'resources'));

var p=['--convert','icns',source,'--output',dest];
try{
(0,_exec.executeAsync)(c,"iconutil "+p.join(' '));
resolve();
}catch(e){
reject(e);
}
});};
//# sourceMappingURL=index.js.map