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 runWebOS=function runWebOS(c){var _c$program,device,hosted,target,platform,isHosted,tDir,tOut,tSim,configFilePath,cnfg,tId,appPath,isPortActive,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:_c$program=
c.program,device=_c$program.device,hosted=_c$program.hosted;
target=c.runtime.target;
platform=c.platform;


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

(0,_logger.logTask)('runWebOS',"target:"+target+" hosted:"+!!isHosted);

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


(0,_logger.logTask)("runWebOS:"+target+":"+isHosted,(0,_logger.chalk)().grey);

cnfg=JSON.parse((0,_fileutils.fsReadFileSync)(configFilePath,'utf-8'));
tId=cnfg.id;
appPath=_path.default.join(tOut,tId+"_"+cnfg.version+"_all.ipk");if(!

isHosted){_context5.next=21;break;}_context5.next=16;return _regenerator.default.awrap(
(0,_common.checkPortInUse)(c,platform,c.runtime.port));case 16:isPortActive=_context5.sent;if(!
isPortActive){_context5.next=21;break;}_context5.next=20;return _regenerator.default.awrap(
(0,_common.confirmActiveBundler)(c));case 20:
c.runtime.skipActiveServerCheck=true;case 21:_context5.t0=




!isHosted;if(!_context5.t0){_context5.next=25;break;}_context5.next=25;return _regenerator.default.awrap((0,_sdkWebpack.buildWeb)(c));case 25:_context5.next=27;return _regenerator.default.awrap(
(0,_exec.execCLI)(c,_constants.CLI_WEBOS_ARES_PACKAGE,"-o "+tOut+" "+tDir+" -n"));case 27:_context5.next=29;return _regenerator.default.awrap(


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

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

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

actualDevices.length){_context5.next=59;break;}_context5.next=39;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 39:response=_context5.sent;if(!



response.setupDevice){_context5.next=57;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=44;return _regenerator.default.awrap(

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


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

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

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

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

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

installAndLaunchApp(c,newDevice.name,appPath,tId));case 57:_context5.next=62;break;case 59:if(!(




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

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

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

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

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

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


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



launchWebOSimulator(c));case 78:_context5.next=80;return _regenerator.default.awrap(
waitForEmulatorToBeReady(c));case 80:return _context5.abrupt("return",
installAndLaunchApp(c,tSim,appPath,tId));case 81:_context5.next=84;break;case 83:return _context5.abrupt("return",



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



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

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

c.program.hosted){_context6.next=9;break;}
tDir=_path.default.join((0,_common.getAppFolder)(c),'public');
tOut=_path.default.join((0,_common.getAppFolder)(c),'output');_context6.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 _context6.stop();}}},null,null,null,Promise);};exports.buildWebOSProject=buildWebOSProject;




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

platform=c.platform;

c.runtime.platformBuildsProjectPath=""+(0,_common.getAppFolder)(c);if(

(0,_platformManager.isPlatformActive)(c,platform)){_context7.next=5;break;}return _context7.abrupt("return");case 5:_context7.next=7;return _regenerator.default.awrap(

(0,_projectParser.copyAssetsFolder)(c,platform));case 7:_context7.next=9;return _regenerator.default.awrap(
(0,_sdkWebpack.configureCoreWebProject)(c,platform));case 9:_context7.next=11;return _regenerator.default.awrap(
configureProject(c));case 11:return _context7.abrupt("return",
(0,_projectParser.copyBuildsFolder)(c,platform));case 12:case"end":return _context7.stop();}}},null,null,null,Promise);};exports.configureWebOSProject=configureWebOSProject;


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

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

var configFile='public/appinfo.json';

var 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.getAppTemplateFolder)(c,platform),configFile),
_path.default.join(appFolder,configFile),
injects,null,c);


resolve();
});};
//# sourceMappingURL=index.js.map