From d4fbb1a3149daf88961a910c455b3894ef0755b4 Mon Sep 17 00:00:00 2001 From: ElenaDiachenko Date: Wed, 25 Sep 2024 15:52:44 +0300 Subject: [PATCH] fix default target setting for webos + select it to run if available --- packages/sdk-webos/src/deviceManager.ts | 76 ++++++++++++++++++------- 1 file changed, 54 insertions(+), 22 deletions(-) diff --git a/packages/sdk-webos/src/deviceManager.ts b/packages/sdk-webos/src/deviceManager.ts index c2e5a5312..85bf59ff3 100644 --- a/packages/sdk-webos/src/deviceManager.ts +++ b/packages/sdk-webos/src/deviceManager.ts @@ -21,6 +21,7 @@ import { getAppFolder, getContext, logError, + writeFileSync, } from '@rnv/core'; import { WebosDevice } from './types'; import { @@ -130,7 +131,7 @@ const parseDevices = (c: RnvContext, devicesResponse: string): Promise { logDefault('launchAppOnSimulator'); - + const defaultTarget = c.runtime.target; const webosSdkPath = getRealPath(c.buildConfig?.sdks?.WEBOS_SDK); if (!webosSdkPath) { @@ -158,34 +159,66 @@ const launchAppOnSimulator = async (c: RnvContext, appPath: string) => { if (!availableEmulatorVersions.length) { return Promise.reject(`Can't find simulator at path: ${simulatorDirPath}`); } - - let selectedOption; - if (availableEmulatorVersions.length > 1) { - ({ selectedOption } = await inquirerPrompt({ - name: 'selectedOption', - type: 'list', - choices: availableEmulatorVersions, - warningMessage: `Found several installed simulators. Choose which one to use:`, - })); + let selectedSimulator; + if (defaultTarget && availableEmulatorVersions.includes(defaultTarget)) { + selectedSimulator = defaultTarget; + logInfo(`Found default simulator ${chalk().bold.white(selectedSimulator)} in ${simulatorDirPath}`); } else { - selectedOption = availableEmulatorVersions[0]; - logInfo(`Found simulator ${selectedOption} at path: ${simulatorDirPath}`); + if (availableEmulatorVersions.length > 1) { + ({ selectedSimulator } = await inquirerPrompt({ + name: 'selectedSimulator', + type: 'list', + choices: availableEmulatorVersions, + message: `Found several installed simulators. Choose which one to use:`, + })); + } else { + selectedSimulator = availableEmulatorVersions[0]; + logInfo(`Found simulator ${chalk().bold.white(selectedSimulator)} in ${simulatorDirPath}`); + } + await _updateDefaultTargets(c, selectedSimulator); } const regex = /\d+(\.\d+)?/g; - const version = selectedOption.match(regex)[0]; + const version = selectedSimulator.match(regex)[0]; if (c.isSystemMac) { logInfo( - `If you encounter damaged simulator error, run this command line: xattr -c ${simulatorDirPath}/${selectedOption}/${selectedOption}.app` + `If you encounter damaged simulator error, run this command line: xattr -c ${simulatorDirPath}/${selectedSimulator}/${selectedSimulator}.app` ); } await execCLI(CLI_WEBOS_ARES_LAUNCH, `-s ${version} "${appPath}"`); logInfo( - `Launched app on webOS TV simulator ${selectedOption}. If you do not see the app opening please close the simulator and try again.` + `Launched app on webOS TV simulator ${chalk().bold.white( + selectedSimulator + )}. If you do not see the app opening please close the simulator and try again.` ); }; +const _updateDefaultTargets = async (c: RnvContext, selectedSimulator: string) => { + const defaultTarget = c.runtime.target; + const { confirm } = await inquirerPrompt({ + type: 'confirm', + name: 'confirm', + message: `Your default target for platform ${c.platform} is ${ + !defaultTarget ? 'not defined' : `set to ${defaultTarget}` + }. Do you want to ${!defaultTarget ? 'set' : `update `} it to ${selectedSimulator} `, + }); + if (!confirm) return; + + const workspaceConfig = c.files.workspace.config; + + if (workspaceConfig && c.platform) { + if (!workspaceConfig.defaultTargets) workspaceConfig.defaultTargets = {}; + + workspaceConfig.defaultTargets[c.platform] = selectedSimulator; + + c.files.workspace.config = workspaceConfig; + writeFileSync(c.paths.workspace.config, workspaceConfig); + } + logInfo( + `Your default target for platform ${c.platform} has been updated successfully in ${c.paths.workspace.config}` + ); +}; // Used for actual devices const installAndLaunchApp = async (target: string, appPath: string, tId: string) => { try { @@ -339,14 +372,13 @@ export const runWebosSimOrDevice = async () => { } } } else { - const target_name = - devices.find((device) => { - return device.device.includes(c.program.opts().target) || device.name.includes(c.program.opts().target); - })?.name; + const target_name = devices.find((device) => { + return device.device.includes(c.program.opts().target) || device.name.includes(c.program.opts().target); + })?.name; - if(!target_name){ - return Promise.reject(`Target ${c.program.opts().target} doesn't exist.`); - } + if (!target_name) { + return Promise.reject(`Target ${c.program.opts().target} doesn't exist.`); + } return installAndLaunchApp(target_name, appPath, tId); } };