var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(exports,"__esModule",{value:true});exports.listWebOSTargets=exports.buildWebOSProject=exports.runWebOS=exports.configureWebOSProject=exports.launchWebOSimulator=void 0;var _regenerator=_interopRequireDefault(require("@babel/runtime/regenerator"));var _slicedToArray2=_interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _path=_interopRequireDefault(require("path"));
var _fs=_interopRequireDefault(require("fs"));
var _chalk=_interopRequireDefault(require("chalk"));
var _semver=_interopRequireDefault(require("semver"));
var _inquirer=_interopRequireDefault(require("inquirer"));
var _exec=require("../../systemTools/exec");
var _common=require("../../common");













var _projectParser=require("../../projectTools/projectParser");
var _constants=require("../../constants");







var _fileutils=require("../../systemTools/fileutils");
var _web=require("../web");

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

var launchWebOSimulator=function launchWebOSimulator(c){
(0,_common.logTask)('launchWebOSimulator');

var ePath=_path.default.join(c.files.private.config.sdks.WEBOS_SDK,"Emulator/v4.0.0/LG_webOS_TV_Emulator"+(isRunningOnWindows?'.exe':'_RCU.app'));

if(!_fs.default.existsSync(ePath)){
return Promise.reject("Can't find emulator at path: "+ePath);
}

return(0,_exec.executeAsync)(c,_exec.openCommand+" "+ePath,{detached:true});
};exports.launchWebOSimulator=launchWebOSimulator;

var parseDevices=function parseDevices(c,devicesResponse){
var linesArray=devicesResponse.split('\n').slice(2).map(function(line){return line.trim();}).filter(function(line){return line!=='';});
return Promise.all(linesArray.map(function _callee(line){var _line$split$map$filte,_line$split$map$filte2,name,device,connection,profile,deviceInfo;return _regenerator.default.async(function _callee$(_context){while(1){switch(_context.prev=_context.next){case 0:_line$split$map$filte=
line.split(' ').map(function(word){return word.trim();}).filter(function(word){return word!=='';}),_line$split$map$filte2=(0,_slicedToArray2.default)(_line$split$map$filte,4),name=_line$split$map$filte2[0],device=_line$split$map$filte2[1],connection=_line$split$map$filte2[2],profile=_line$split$map$filte2[3];
deviceInfo='';_context.prev=2;_context.next=5;return _regenerator.default.awrap(

(0,_exec.execCLI)(c,_constants.CLI_WEBOS_ARES_DEVICE_INFO,"-d "+name,{silent:true,timeout:10000}));case 5:deviceInfo=_context.sent;_context.next=11;break;case 8:_context.prev=8;_context.t0=_context["catch"](2);

deviceInfo=_context.t0;case 11:return _context.abrupt("return",


{
name:name,
device:device,
connection:connection,
profile:profile,
isDevice:!device.includes('127.0.0.1'),
active:!deviceInfo.includes('ERR!')});case 12:case"end":return _context.stop();}}},null,null,[[2,8]]);}));


};

var installAndLaunchApp=function installAndLaunchApp(c,target,appPath,tId){var maxErrorLength;return _regenerator.default.async(function installAndLaunchApp$(_context2){while(1){switch(_context2.prev=_context2.next){case 0:
maxErrorLength=c.program.maxErrorLength;_context2.next=3;return _regenerator.default.awrap(

(0,_exec.execCLI)(c,_constants.CLI_WEBOS_ARES_INSTALL,"--device "+target+" "+appPath));case 3:_context2.next=5;return _regenerator.default.awrap(
(0,_exec.execCLI)(c,_constants.CLI_WEBOS_ARES_LAUNCH,"--device "+target+" "+tId));case 5:case"end":return _context2.stop();}}});};


var buildDeviceChoices=function buildDeviceChoices(devices){return devices.map(function(device){return{
key:device.name,name:device.name+" - "+device.device,value:device.name};});};


var listWebOSTargets=function listWebOSTargets(c){var maxErrorLength,devicesResponse,devices,deviceArray;return _regenerator.default.async(function listWebOSTargets$(_context3){while(1){switch(_context3.prev=_context3.next){case 0:
maxErrorLength=c.program.maxErrorLength;_context3.next=3;return _regenerator.default.awrap(
(0,_exec.execCLI)(c,_constants.CLI_WEBOS_ARES_DEVICE_INFO,'-D'));case 3:devicesResponse=_context3.sent;_context3.next=6;return _regenerator.default.awrap(
parseDevices(c,devicesResponse));case 6:devices=_context3.sent;

deviceArray=devices.map(function(device,i){return"-["+(i+1)+"] "+device.name+" | "+device.device;});
console.log(deviceArray.join('\n'));return _context3.abrupt("return",
true);case 10:case"end":return _context3.stop();}}});};exports.listWebOSTargets=listWebOSTargets;


var waitForEmulatorToBeReady=function waitForEmulatorToBeReady(c){var maxErrorLength,devicesResponse,devices,emulator;return _regenerator.default.async(function waitForEmulatorToBeReady$(_context4){while(1){switch(_context4.prev=_context4.next){case 0:
maxErrorLength=c.program.maxErrorLength;_context4.next=3;return _regenerator.default.awrap(
(0,_exec.execCLI)(c,_constants.CLI_WEBOS_ARES_DEVICE_INFO,'-D'));case 3:devicesResponse=_context4.sent;_context4.next=6;return _regenerator.default.awrap(
parseDevices(c,devicesResponse));case 6:devices=_context4.sent;
emulator=devices.filter(function(d){return!d.isDevice;})[0];if(
emulator){_context4.next=10;break;}throw new Error('No WebOS emulator configured');case 10:return _context4.abrupt("return",

(0,_common.waitForEmulator)(c,_constants.CLI_WEBOS_ARES_DEVICE_INFO,"-d "+emulator.name,function(res){return res.includes('modelName');}));case 11:case"end":return _context4.stop();}}});};


var runWebOS=function runWebOS(c,platform,target){var _c$program,device,hosted,maxErrorLength,isHosted,tDir,tOut,tSim,configFilePath,cnfg,tId,appPath,devicesResponse,devices,activeDevices,actualDevices,response,newDeviceResponse,dev,actualDev,newDevice,tv,choices,_response;return _regenerator.default.async(function runWebOS$(_context5){while(1){switch(_context5.prev=_context5.next){case 0:
(0,_common.logTask)("runWebOS:"+platform+":"+target);_c$program=

c.program,device=_c$program.device,hosted=_c$program.hosted,maxErrorLength=_c$program.maxErrorLength;

isHosted=hosted||!(0,_common.getConfigProp)(c,platform,'bundleAssets');

tDir=_path.default.join((0,_common.getAppFolder)(c,platform),'public');
tOut=_path.default.join((0,_common.getAppFolder)(c,platform),'output');
tSim=c.program.target||'emulator';
configFilePath=_path.default.join((0,_common.getAppFolder)(c,platform),'public/appinfo.json');

(0,_common.logTask)("runWebOS:"+platform+":"+target+":"+isHosted,_chalk.default.grey);

cnfg=JSON.parse(_fs.default.readFileSync(configFilePath,'utf-8'));
tId=cnfg.id;
appPath=_path.default.join(tOut,tId+"_"+cnfg.version+"_all.ipk");_context5.t0=


!isHosted;if(!_context5.t0){_context5.next=15;break;}_context5.next=15;return _regenerator.default.awrap((0,_web.buildWeb)(c,platform));case 15:_context5.next=17;return _regenerator.default.awrap(
(0,_exec.execCLI)(c,_constants.CLI_WEBOS_ARES_PACKAGE,"-o "+tOut+" "+tDir+" -n"));case 17:_context5.next=19;return _regenerator.default.awrap(


(0,_exec.execCLI)(c,_constants.CLI_WEBOS_ARES_DEVICE_INFO,'-D'));case 19:devicesResponse=_context5.sent;_context5.next=22;return _regenerator.default.awrap(
parseDevices(c,devicesResponse));case 22:devices=_context5.sent;
activeDevices=devices.filter(function(d){return d.active;});if(!

device){_context5.next=62;break;}

actualDevices=devices.filter(function(d){return d.isDevice;});if(

actualDevices.length){_context5.next=54;break;}_context5.next=29;return _regenerator.default.awrap(

_inquirer.default.prompt([{
type:'confirm',
name:'setupDevice',
message:'Looks like you want to deploy on a device but have none configured. Do you want to configure one?',
default:false}]));case 29:response=_context5.sent;if(!


response.setupDevice){_context5.next=52;break;}

(0,_common.logInfo)('Please follow the instructions from http://webostv.developer.lge.com/develop/app-test/#installDevModeApp on how to setup the TV and the connection with the PC. Then follow the onscreen prompts\n');_context5.next=34;return _regenerator.default.awrap(
(0,_exec.execCLI)(c,_constants.CLI_WEBOS_ARES_SETUP_DEVICE,'',{stdio:'inherit',silent:true}));case 34:_context5.next=36;return _regenerator.default.awrap(

(0,_exec.execCLI)(c,_constants.CLI_WEBOS_ARES_DEVICE_INFO,'-D'));case 36:newDeviceResponse=_context5.sent;_context5.next=39;return _regenerator.default.awrap(
parseDevices(c,newDeviceResponse));case 39:dev=_context5.sent;
actualDev=dev.filter(function(d){return d.isDevice;});if(!(

actualDev.length>0)){_context5.next=52;break;}
newDevice=actualDev[0];

(0,_common.logInfo)('Please enter the `Passphrase` from the TV\'s Developer Mode app');_context5.next=46;return _regenerator.default.awrap(
(0,_exec.execCLI)(c,_constants.CLI_WEBOS_ARES_NOVACOM,"--device "+newDevice.name+" --getkey",{stdio:'inherit',maxErrorLength:maxErrorLength}));case 46:_context5.next=48;return _regenerator.default.awrap(
(0,_exec.execCLI)(c,_constants.CLI_WEBOS_ARES_INSTALL,"--device "+newDevice.name+" "+appPath));case 48:_context5.next=50;return _regenerator.default.awrap(
(0,_exec.execCLI)(c,_constants.CLI_WEBOS_ARES_LAUNCH,"--device "+newDevice.name+" "+tId));case 50:_context5.next=52;break;case 52:_context5.next=60;break;case 54:if(!(





actualDevices.length===1)){_context5.next=60;break;}
tv=actualDevices[0];_context5.next=58;return _regenerator.default.awrap(
(0,_exec.execCLI)(c,_constants.CLI_WEBOS_ARES_INSTALL,"--device "+tv.name+" "+appPath));case 58:_context5.next=60;return _regenerator.default.awrap(
(0,_exec.execCLI)(c,_constants.CLI_WEBOS_ARES_LAUNCH,"--device "+tv.name+" "+tId));case 60:_context5.next=88;break;case 62:if(

c.program.target){_context5.next=86;break;}if(!(

activeDevices.length===1)){_context5.next=68;break;}_context5.next=66;return _regenerator.default.awrap(

installAndLaunchApp(c,devices[0].name,appPath,tId));case 66:_context5.next=84;break;case 68:if(!(
activeDevices.length>1)){_context5.next=78;break;}

choices=buildDeviceChoices(devices);_context5.next=72;return _regenerator.default.awrap(
_inquirer.default.prompt([{
name:'chosenDevice',
type:'list',
message:'What device would you like to start the app?',
choices:choices}]));case 72:_response=_context5.sent;if(!

_response.chosenDevice){_context5.next=76;break;}_context5.next=76;return _regenerator.default.awrap(
installAndLaunchApp(c,_response.chosenDevice,appPath,tId));case 76:_context5.next=84;break;case 78:_context5.next=80;return _regenerator.default.awrap(


launchWebOSimulator(c));case 80:_context5.next=82;return _regenerator.default.awrap(
waitForEmulatorToBeReady(c));case 82:_context5.next=84;return _regenerator.default.awrap(
installAndLaunchApp(c,tSim,appPath,tId));case 84:_context5.next=88;break;case 86:_context5.next=88;return _regenerator.default.awrap(



installAndLaunchApp(c,c.program.target,appPath,tId));case 88:case"end":return _context5.stop();}}});};exports.runWebOS=runWebOS;




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

var tDir=_path.default.join((0,_common.getAppFolder)(c,platform),'public');
var tOut=_path.default.join((0,_common.getAppFolder)(c,platform),'output');var
maxErrorLength=c.program.maxErrorLength;

(0,_web.buildWeb)(c,platform).
then(function(){return(0,_exec.execCLI)(c,_constants.CLI_WEBOS_ARES_PACKAGE,"-o "+tOut+" "+tDir+" -n");}).
then(function(){
(0,_common.logSuccess)("Your IPK package is located in "+_chalk.default.white(tOut)+" .");
return resolve();
}).
catch(reject);
});};exports.buildWebOSProject=buildWebOSProject;

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

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

(0,_projectParser.copyAssetsFolder)(c,platform).
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.configureWebOSProject=configureWebOSProject;

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

var appFolder=(0,_common.getAppFolder)(c,platform);

var configFile='public/appinfo.json';
(0,_common.writeCleanFile)(_path.default.join((0,_common.getAppTemplateFolder)(c,platform),configFile),_path.default.join(appFolder,configFile),[
{pattern:'{{APPLICATION_ID}}',override:(0,_common.getAppId)(c,platform)},
{pattern:'{{APP_TITLE}}',override:(0,_common.getAppTitle)(c,platform)},
{pattern:'{{APP_VERSION}}',override:_semver.default.coerce((0,_common.getAppVersion)(c,platform))}]);


resolve();
});};