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 _semver=_interopRequireDefault(require("semver"));
var _inquirer=_interopRequireDefault(require("inquirer"));
var _fileutils=require("../core/systemManager/fileutils");
var _exec=require("../core/systemManager/exec");
var _common=require("../core/common");

















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

var _targetManager=require("../core/targetManager");
var _platformManager=require("../core/platformManager");
var _logger=require("../core/systemManager/logger");







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



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










var _utils=require("../core/utils");

var launchWebOSimulator=function launchWebOSimulator(c){var _c$buildConfig,_c$buildConfig$sdks;
(0,_logger.logTask)('launchWebOSimulator');

var ePath=_path.default.join(
(0,_fileutils.getRealPath)(c,(_c$buildConfig=c.buildConfig)==null?void 0:(_c$buildConfig$sdks=_c$buildConfig.sdks)==null?void 0:_c$buildConfig$sdks.WEBOS_SDK),"Emulator/v4.0.0/LG_webOS_TV_Emulator"+(

_utils.isSystemWin?'.exe':'_RCU.app'));



if(!(0,_fileutils.fsExistsSync)(ePath)){
return Promise.reject("Can't find emulator at path: "+ePath);
}
if(_utils.isSystemWin){return(0,_exec.executeAsync)(c,ePath,{detached:true,stdio:'ignore'});}
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:!(0,_utils.isUrlLocalhost)(device),
active:!deviceInfo.includes('ERR!')});case 12:case"end":return _context.stop();}}},null,null,[[2,8]],Promise);}));



};

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

(0,_exec.execCLI)(
c,
_constants.CLI_WEBOS_ARES_INSTALL,"--device "+
target+" "+appPath));case 3:_context2.next=9;break;case 5:_context2.prev=5;_context2.t0=_context2["catch"](0);_context2.next=9;return _regenerator.default.awrap(




(0,_exec.execCLI)(
c,
_constants.CLI_WEBOS_ARES_INSTALL,"--device "+
target+" "+appPath));case 9:





toReturn=true;_context2.next=12;return _regenerator.default.awrap(




(0,_exec.execCLI)(c,_constants.CLI_WEBOS_ARES_LAUNCH,"--device "+target+" "+tId));case 12:return _context2.abrupt("return",
toReturn);case 13:case"end":return _context2.stop();}}},null,null,[[0,5]],Promise);};


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 devicesResponse,devices,deviceArray;return _regenerator.default.async(function listWebOSTargets$(_context3){while(1){switch(_context3.prev=_context3.next){case 0:_context3.next=2;return _regenerator.default.awrap(
(0,_exec.execCLI)(c,_constants.CLI_WEBOS_ARES_DEVICE_INFO,'-D'));case 2:devicesResponse=_context3.sent;_context3.next=5;return _regenerator.default.awrap(
parseDevices(c,devicesResponse));case 5:devices=_context3.sent;

deviceArray=devices.map(
function(device,i){return" ["+(i+1)+"]> "+(0,_logger.chalk)().bold(device.name)+" | "+device.device;});


(0,_logger.logToSummary)("WebOS Targets:\n"+deviceArray.join('\n'));return _context3.abrupt("return",

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


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

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



var _runWebosSimOrDevice=function _runWebosSimOrDevice(c){var device,tDir,tOut,tSim,configFilePath,cnfg,tId,appPath,devicesResponse,devices,activeDevices,actualDevices,response,newDeviceResponse,dev,actualDev,newDevice,tv,choices,_response;return _regenerator.default.async(function _runWebosSimOrDevice$(_context5){while(1){switch(_context5.prev=_context5.next){case 0:
device=c.program.device;

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


cnfg=JSON.parse((0,_fileutils.fsReadFileSync)(configFilePath,'utf-8'));
tId=cnfg.id;
appPath=_path.default.join(tOut,tId+"_"+cnfg.version+"_all.ipk");_context5.next=10;return _regenerator.default.awrap(




(0,_exec.execCLI)(c,_constants.CLI_WEBOS_ARES_PACKAGE,"-o "+tOut+" "+tDir+" -n"));case 10:_context5.next=12;return _regenerator.default.awrap(


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

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

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

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

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



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

(0,_logger.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=27;return _regenerator.default.awrap(

(0,_exec.execCLI)(c,_constants.CLI_WEBOS_ARES_SETUP_DEVICE,'',{
interactive:true}));case 27:_context5.next=29;return _regenerator.default.awrap(


(0,_exec.execCLI)(
c,
_constants.CLI_WEBOS_ARES_DEVICE_INFO,
'-D'));case 29:newDeviceResponse=_context5.sent;_context5.next=32;return _regenerator.default.awrap(

parseDevices(c,newDeviceResponse));case 32:dev=_context5.sent;
actualDev=dev.filter(function(d){return d.isDevice;});if(!(

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

(0,_logger.logInfo)(
"Please enter the `Passphrase` from the TV's Developer Mode app");_context5.next=39;return _regenerator.default.awrap(

(0,_exec.execCLI)(
c,
_constants.CLI_WEBOS_ARES_NOVACOM,"--device "+
newDevice.name+" --getkey",
{stdio:'inherit'}));case 39:return _context5.abrupt("return",

installAndLaunchApp(c,newDevice.name,appPath,tId));case 40:_context5.next=45;break;case 42:if(!(




actualDevices.length===1)){_context5.next=45;break;}
tv=actualDevices[0];return _context5.abrupt("return",
installAndLaunchApp(c,tv.name,appPath,tId));case 45:_context5.next=67;break;case 47:if(

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

activeDevices.length===1)){_context5.next=50;break;}return _context5.abrupt("return",

installAndLaunchApp(c,devices[0].name,appPath,tId));case 50:if(!(

activeDevices.length>1)){_context5.next=59;break;}

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


_response.chosenDevice){_context5.next=57;break;}return _context5.abrupt("return",
installAndLaunchApp(
c,
_response.chosenDevice,
appPath,
tId));case 57:_context5.next=64;break;case 59:_context5.next=61;return _regenerator.default.awrap(



launchWebOSimulator(c));case 61:_context5.next=63;return _regenerator.default.awrap(
waitForEmulatorToBeReady(c));case 63:return _context5.abrupt("return",
installAndLaunchApp(c,tSim,appPath,tId));case 64:_context5.next=67;break;case 66:return _context5.abrupt("return",



installAndLaunchApp(c,c.program.target,appPath,tId));case 67:case"end":return _context5.stop();}}},null,null,null,Promise);};



var runWebOS=function runWebOS(c){var hosted,target,platform,isHosted,isPortActive,resetCompleted,bundleAssets,_isPortActive,isWeinreEnabled,_resetCompleted;return _regenerator.default.async(function runWebOS$(_context6){while(1){switch(_context6.prev=_context6.next){case 0:
hosted=c.program.hosted;
target=c.runtime.target;
platform=c.platform;


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

isHosted){_context6.next=13;break;}_context6.next=7;return _regenerator.default.awrap(
(0,_common.checkPortInUse)(c,platform,c.runtime.port));case 7:isPortActive=_context6.sent;if(!
isPortActive){_context6.next=13;break;}_context6.next=11;return _regenerator.default.awrap(
(0,_common.confirmActiveBundler)(c));case 11:resetCompleted=_context6.sent;
c.runtime.skipActiveServerCheck=!resetCompleted;case 13:



(0,_logger.logTask)('runWebOS',"target:"+target+" hosted:"+!!isHosted);if(!
isHosted){_context6.next=16;break;}return _context6.abrupt("return");case 16:

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

bundleAssets){_context6.next=24;break;}_context6.next=20;return _regenerator.default.awrap(
(0,_sdkWebpack.buildWeb)(c));case 20:_context6.next=22;return _regenerator.default.awrap(
_runWebosSimOrDevice(c));case 22:_context6.next=46;break;case 24:_context6.next=26;return _regenerator.default.awrap(

(0,_common.checkPortInUse)(c,platform,c.runtime.port));case 26:_isPortActive=_context6.sent;
isWeinreEnabled=_constants.WEINRE_ENABLED_PLATFORMS.includes(platform)&&!bundleAssets&&!hosted;if(

_isPortActive){_context6.next=35;break;}
(0,_logger.logInfo)("Your "+
(0,_logger.chalk)().white(
platform)+" devServer at port "+
(0,_logger.chalk)().white(
c.runtime.port)+" is not running. Starting it up for you...");


(0,_sdkWebpack.waitForWebpack)(c).
then(function(){return _runWebosSimOrDevice(c);}).
catch(_logger.logError);_context6.next=33;return _regenerator.default.awrap(
(0,_sdkWebpack.runWebpackServer)(c,isWeinreEnabled));case 33:_context6.next=46;break;case 35:_context6.next=37;return _regenerator.default.awrap(

(0,_common.confirmActiveBundler)(c));case 37:_resetCompleted=_context6.sent;if(!
_resetCompleted){_context6.next=44;break;}
(0,_sdkWebpack.waitForWebpack)(c).
then(function(){return _runWebosSimOrDevice(c);}).
catch(_logger.logError);_context6.next=42;return _regenerator.default.awrap(
(0,_sdkWebpack.runWebpackServer)(c,isWeinreEnabled));case 42:_context6.next=46;break;case 44:_context6.next=46;return _regenerator.default.awrap(

_runWebosSimOrDevice(c));case 46:case"end":return _context6.stop();}}},null,null,null,Promise);};exports.runWebOS=runWebOS;





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

(0,_sdkWebpack.buildWeb)(c));case 3:if(

c.program.hosted){_context7.next=9;break;}
tDir=(0,_common.getPlatformProjectDir)(c);
tOut=_path.default.join((0,_common.getPlatformBuildDir)(c),'output');_context7.next=8;return _regenerator.default.awrap(
(0,_exec.execCLI)(c,_constants.CLI_WEBOS_ARES_PACKAGE,"-o "+tOut+" "+tDir+" -n"));case 8:

(0,_logger.logSuccess)("Your IPK package is located in "+
(0,_logger.chalk)().cyan(tOut)+" .");case 9:case"end":return _context7.stop();}}},null,null,null,Promise);};exports.buildWebOSProject=buildWebOSProject;




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

platform=c.platform;

c.runtime.platformBuildsProjectPath=(0,_common.getPlatformProjectDir)(c);if(

(0,_platformManager.isPlatformActive)(c,platform)){_context8.next=5;break;}return _context8.abrupt("return");case 5:

bundleAssets=(0,_common.getConfigProp)(c,platform,'bundleAssets')===true;_context8.next=8;return _regenerator.default.awrap(

(0,_projectParser.copyAssetsFolder)(c,platform));case 8:_context8.next=10;return _regenerator.default.awrap(
(0,_sdkWebpack.configureCoreWebProject)(c,bundleAssets?_constants.RNV_PROJECT_DIR_NAME:_constants.RNV_SERVER_DIR_NAME));case 10:_context8.next=12;return _regenerator.default.awrap(
configureProject(c));case 12:return _context8.abrupt("return",
(0,_projectParser.copyBuildsFolder)(c,platform));case 13:case"end":return _context8.stop();}}},null,null,null,Promise);};exports.configureWebOSProject=configureWebOSProject;


var configureProject=function configureProject(c){var platform,configFile,injects;return _regenerator.default.async(function configureProject$(_context9){while(1){switch(_context9.prev=_context9.next){case 0:
(0,_logger.logTask)('configureProject');
platform=c.platform;

configFile='appinfo.json';

injects=[
{
pattern:'{{APPLICATION_ID}}',
override:(0,_common.getAppId)(c,platform).toLowerCase()},

{
pattern:'{{APP_TITLE}}',
override:(0,_common.getAppTitle)(c,platform)},

{
pattern:'{{APP_VERSION}}',
override:_semver.default.coerce((0,_common.getAppVersion)(c,platform))}];



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

(0,_fileutils.writeCleanFile)(
_path.default.join((0,_common.getTemplateProjectDir)(c),configFile),
_path.default.join((0,_common.getPlatformProjectDir)(c),configFile),
injects,null,c);return _context9.abrupt("return",


true);case 7:case"end":return _context9.stop();}}},null,null,null,Promise);};
//# sourceMappingURL=index.js.map