var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(exports,"__esModule",{value:true});exports.runElectronDevServer=exports.exportElectron=exports.buildElectron=exports.runElectron=exports.configureElectronProject=void 0;var _regenerator=_interopRequireDefault(require("@babel/runtime/regenerator"));var _path=_interopRequireDefault(require("path"));
var _fs=_interopRequireDefault(require("fs"));
var _chalk=_interopRequireDefault(require("chalk"));
var _child_process=require("child_process");
var _=require("..");
var _exec=require("../../systemTools/exec");
var _common=require("../../common");



























var _projectParser=require("../../projectTools/projectParser");
var _constants=require("../../constants");
var _web=require("../web");
var _fileutils=require("../../systemTools/fileutils");




var isRunningOnWindows=process.platform==='win32';

var configureElectronProject=function configureElectronProject(c,platform){return new Promise(function(resolve,reject){
(0,_common.logTask)("configureElectronProject:"+platform);

(0,_projectParser.copyAssetsFolder)(c,platform,platform===_constants.MACOS?_generateICNS:null).
then(function(){return(0,_projectParser.copyBuildsFolder)(c,platform);}).
then(function(){return configureProject(c,platform);}).
then(function(){return resolve();}).
catch(function(e){return reject(e);});
});};exports.configureElectronProject=configureElectronProject;

var configureProject=function configureProject(c,platform){return new Promise(function(resolve,reject){
(0,_common.logTask)("configureProject:"+platform);

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

var appFolder=(0,_common.getAppFolder)(c,platform);
var templateFolder=(0,_common.getAppTemplateFolder)(c,platform);
var bundleIsDev=(0,_common.getConfigProp)(c,platform,'bundleIsDev')===true;
var bundleAssets=(0,_common.getConfigProp)(c,platform,'bundleAssets')===true;
var electronConfigPath=_path.default.join(appFolder,'electronConfig.json');
var packagePath=_path.default.join(appFolder,'package.json');
var appId=(0,_common.getAppId)(c,platform);

if(!_fs.default.existsSync(packagePath)){
(0,_common.logWarning)("Looks like your "+_chalk.default.white(platform)+" platformBuild is misconfigured!. let's repair it.");
(0,_.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=(0,_common.getAppConfigId)(c,platform)+"-"+platform;
packageJson.productName=(0,_common.getAppTitle)(c,platform)+" - "+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.writeObjectSync)(packagePath,packageJson);

if(bundleAssets){
(0,_fileutils.copyFileSync)(_path.default.join(templateFolder,'_privateConfig','main.js'),_path.default.join(appFolder,'main.js'));
(0,_fileutils.copyFileSync)(
_path.default.join(templateFolder,'_privateConfig','webpack.config.js'),
_path.default.join(appFolder,'webpack.config.js'));

}else{
var ip=isRunningOnWindows?'127.0.0.1':'0.0.0.0';
(0,_common.writeCleanFile)(_path.default.join(templateFolder,'_privateConfig','main.dev.js'),_path.default.join(appFolder,'main.js'),[
{pattern:'{{DEV_SERVER}}',override:"http://"+ip+":"+c.platformDefaults[platform].defaultPort}]);

(0,_fileutils.copyFileSync)(
_path.default.join(templateFolder,'_privateConfig','webpack.config.dev.js'),
_path.default.join(appFolder,'webpack.config.js'));

}

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


(0,_fileutils.writeObjectSync)(electronConfigPath,electronConfig);


resolve();
});};

var buildElectron=function buildElectron(c,platform){return new Promise(function(resolve,reject){
(0,_common.logTask)("buildElectron:"+platform);

var appFolder=(0,_common.getAppFolder)(c,platform);
(0,_web.buildWeb)(c,platform).
then(function(){return resolve();}).
catch(function(e){return reject(e);});
});};exports.buildElectron=buildElectron;

var exportElectron=function exportElectron(c,platform){return new Promise(function(resolve,reject){
(0,_common.logTask)("exportElectron:"+platform);var
maxErrorLength=c.program.maxErrorLength;

var appFolder=(0,_common.getAppFolder)(c,platform);
(0,_exec.executeAsync)(c,"npx electron-builder --config "+_path.default.join(appFolder,'electronConfig.json')).
then(function(){
(0,_common.logSuccess)("Your Exported App is located in "+_chalk.default.white(_path.default.join(appFolder,'build/release'))+" .");
resolve();
}).
catch(function(e){return reject(e);});
});};exports.exportElectron=exportElectron;

var runElectron=function runElectron(c,platform,port){var bundleIsDev,bundleAssets,isPortActive;return _regenerator.default.async(function runElectron$(_context){while(1){switch(_context.prev=_context.next){case 0:
(0,_common.logTask)("runElectron:"+platform);

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

bundleAssets){_context.next=10;break;}_context.next=6;return _regenerator.default.awrap(
buildElectron(c,platform,bundleIsDev));case 6:_context.next=8;return _regenerator.default.awrap(
_runElectronSimulator(c,platform));case 8:_context.next=23;break;case 10:_context.next=12;return _regenerator.default.awrap(

(0,_common.checkPortInUse)(c,platform,port));case 12:isPortActive=_context.sent;if(
isPortActive){_context.next=20;break;}
(0,_common.logInfo)("Looks like your "+
_chalk.default.white(platform)+" devServer at port "+_chalk.default.white(
port)+" is not running. Starting it up for you...");


(0,_common.waitForWebpack)(c,port).
then(function(){return _runElectronSimulator(c,platform);}).
catch(_common.logError);_context.next=18;return _regenerator.default.awrap(

runElectronDevServer(c,platform,port));case 18:_context.next=23;break;case 20:

(0,_common.logInfo)("Looks like your "+
_chalk.default.white(platform)+" devServer at port "+_chalk.default.white(
port)+" is already running. ReNative will use it!");_context.next=23;return _regenerator.default.awrap(


_runElectronSimulator(c,platform));case 23:case"end":return _context.stop();}}});};exports.runElectron=runElectron;




var _runElectronSimulator=function _runElectronSimulator(c,platform){return new Promise(function(resolve,reject){
(0,_common.logTask)("_runElectronSimulator:"+platform);
var appFolder=(0,_common.getAppFolder)(c,platform);
var elc=(0,_common.resolveNodeModulePath)(c,'electron/cli.js');

var child=(0,_child_process.spawn)('node',[elc,appFolder],{
detached:true,
env:process.env,
stdio:'inherit'}).

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

child.unref();
resolve();
});};

var runElectronDevServer=function runElectronDevServer(c,platform,port){return new Promise(function(resolve,reject){
(0,_common.logTask)("runElectronDevServer:"+platform);

var appFolder=(0,_common.getAppFolder)(c,platform);
var templateFolder=(0,_common.getAppTemplateFolder)(c,platform);
(0,_fileutils.copyFileSync)(
_path.default.join(templateFolder,'_privateConfig','webpack.config.dev.js'),
_path.default.join(appFolder,'webpack.config.js'));


(0,_web.runWebDevServer)(c,platform,port).
then(function(){return resolve();}).
catch(function(e){return reject(e);});
});};exports.runElectronDevServer=runElectronDevServer;

var _generateICNS=function _generateICNS(c,platform){return new Promise(function(resolve,reject){
(0,_common.logTask)("_generateICNS:"+platform);var
maxErrorLength=c.program.maxErrorLength;

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(_fs.default.existsSync(pf)){
source=pf;
}
});
}else{
source=_path.default.join(v,"assets/"+platform+"/AppIcon.iconset");
}

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

if(!_fs.default.existsSync(source)){
(0,_common.logWarning)("Your app config is missing "+_chalk.default.white(source)+". icon.icns will not be generated!");
resolve();
return;
}

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

var p=[
'--convert',
'icns',
source,
'--output',
dest];

try{
(0,_exec.executeAsync)(c,"iconutil "+p.join(' '));
resolve();
}catch(e){
reject(e);
}
});};