diff --git a/packages/sdk-android/src/runner.ts b/packages/sdk-android/src/runner.ts index 8d51d7658..6159223ae 100644 --- a/packages/sdk-android/src/runner.ts +++ b/packages/sdk-android/src/runner.ts @@ -120,7 +120,7 @@ export const getAndroidDeviceToRunOn = async () => { choices, }); if (chosenTarget) { - // update defaultTarget in .rnv/renative.json + // update defaultTarget if (!target) { await updateDefaultTargets(c, chosenTarget); } diff --git a/packages/sdk-apple/src/__tests__/runner.test.ts b/packages/sdk-apple/src/__tests__/runner.test.ts index ec8c99c40..8c284e3b7 100644 --- a/packages/sdk-apple/src/__tests__/runner.test.ts +++ b/packages/sdk-apple/src/__tests__/runner.test.ts @@ -2,6 +2,7 @@ import { inquirerPrompt, getContext, createRnvContext, logSuccess } from '@rnv/c import type { PromptParams } from '@rnv/core'; import { getIosDeviceToRunOn } from '../runner'; import { getAppleDevices } from '../deviceManager'; +import { updateDefaultTargets } from '@rnv/sdk-utils'; const simJson = [ { @@ -33,6 +34,7 @@ const devicesJson = [ ]; jest.mock('@rnv/core'); +jest.mock('@rnv/sdk-utils'); jest.mock('../deviceManager'); jest.mock('chalk', () => ({ bold: { @@ -97,22 +99,17 @@ describe('getIosDeviceToRunOn', () => { [name as string]: true, }; } - if (type === 'list') { - // Testing the addition of global/project value should be handled in another UT - if (choices?.includes("Don't update")) { - const choiceIndex = choices.findIndex((c) => c === "Don't update"); - return { - [name as string]: - (choices![choiceIndex] as { name: string; value: any }).value || choices![choiceIndex], - }; - } // By default first value returned (aka the first simulator from the list in this case) return { [name as string]: (choices![0] as { name: string; value: any }).value || choices![0], }; } }); + jest.mocked(updateDefaultTargets).mockImplementation(async (ctx, currentTarget) => { + if (!ctx.platform) return; + ctx.runtime.target = currentTarget; + }); // WHEN const deviceArgs = await getIosDeviceToRunOn(ctx); //THEN @@ -139,14 +136,19 @@ describe('getIosDeviceToRunOn', () => { ctx.files.workspace.config = {}; ctx.runtime.target = 'iPhone 14'; jest.mocked(getAppleDevices).mockResolvedValueOnce(simJson); - jest.mocked(inquirerPrompt).mockImplementation(async ({ type, name, choices }: PromptParams) => { - if (type === 'list' && choices?.includes('Update global default target for platform ios')) { - return { [name as string]: 'Update global default target for platform ios' }; - } + jest.mocked(inquirerPrompt).mockImplementation(async ({ name, choices }: PromptParams) => { return { [name as string]: (choices![0] as { name: string; value: any }).value || choices![0], }; }); + jest.mocked(updateDefaultTargets).mockImplementation(async (ctx, currentTarget) => { + if (!ctx.platform) return; + const configGlobal = ctx.files.workspace.config || {}; + configGlobal.defaultTargets = configGlobal.defaultTargets || {}; + configGlobal.defaultTargets[ctx.platform] = currentTarget; + ctx.files.workspace.config = configGlobal; + ctx.runtime.target = currentTarget; + }); // WHEN const deviceArgs = await getIosDeviceToRunOn(ctx); // THEN diff --git a/packages/sdk-apple/src/runner.ts b/packages/sdk-apple/src/runner.ts index 9788fa626..ef4e97d9b 100644 --- a/packages/sdk-apple/src/runner.ts +++ b/packages/sdk-apple/src/runner.ts @@ -45,7 +45,7 @@ import { import { registerDevice } from './fastlane'; import { Context, getContext } from './getContext'; import { parsePrivacyManifest } from './privacyManifestParser'; -import { getAppId } from '@rnv/sdk-utils'; +import { getAppId, updateDefaultTargets } from '@rnv/sdk-utils'; export const packageBundleForXcode = () => { return packageReactNativeIOS(); @@ -176,43 +176,7 @@ export const getIosDeviceToRunOn = async (c: Context) => { })), }); desiredSim = currentTarget; - const localOverridden = !!c.files.project.configLocal?.defaultTargets?.[c.platform]; - - const actionLocalUpdate = `Update ${chalk().green('project')} default target for platform ${c.platform}`; - const actionGlobalUpdate = `Update ${chalk().green('global')}${ - localOverridden ? ` and ${chalk().green('project')}` : '' - } default target for platform ${c.platform}`; - const actionNoUpdate = "Don't update"; - - const { chosenAction } = await inquirerPrompt({ - message: 'What to do next?', - type: 'list', - name: 'chosenAction', - choices: [actionLocalUpdate, actionGlobalUpdate, actionNoUpdate], - warningMessage: `Your default target for platform ${c.platform} is set to ${c.runtime.target}.`, - }); - - c.runtime.target = currentTarget.name; - - if (chosenAction === actionLocalUpdate || (chosenAction === actionGlobalUpdate && localOverridden)) { - const configLocal = c.files.project.configLocal || {}; - if (!configLocal.defaultTargets) configLocal.defaultTargets = {}; - configLocal.defaultTargets[c.platform] = currentTarget.name; - - c.files.project.configLocal = configLocal; - writeFileSync(c.paths.project.configLocal, configLocal); - } - - if (chosenAction === actionGlobalUpdate) { - const configGlobal = c.files.workspace.config; - if (configGlobal) { - if (!configGlobal.defaultTargets) configGlobal.defaultTargets = {}; - configGlobal.defaultTargets[c.platform] = currentTarget.name; - - c.files.workspace.config = configGlobal; - writeFileSync(c.paths.workspace.config, configGlobal); - } - } + await updateDefaultTargets(c, currentTarget.name); } if (!desiredSim?.isDevice) { const target = c.runtime.target?.replace(/(\s+)/g, '\\$1'); diff --git a/packages/sdk-tizen/src/deviceManager.ts b/packages/sdk-tizen/src/deviceManager.ts index 99a30e715..26c00b86b 100644 --- a/packages/sdk-tizen/src/deviceManager.ts +++ b/packages/sdk-tizen/src/deviceManager.ts @@ -73,7 +73,11 @@ const formatXMLObject = ( return {}; }; -export const launchTizenTarget = async (name: string | true, hideDevices?: boolean): Promise => { +export const launchTizenTarget = async ( + name: string | true, + hideDevices?: boolean, + updateDefault = false +): Promise => { const c = getContext(); logDefault(`launchTizenTarget:${name}`); if (name === true) { @@ -122,11 +126,16 @@ export const launchTizenTarget = async (name: string | true, hideDevices?: boole return new Promise(() => logInfo('Device is launched.')); } try { - await executeAsync( - `${c.cli[CLI_TIZEN_EMULATOR]} launch --name ${name}`, - ExecOptionsPresets.SPINNER_FULL_ERROR_SUMMARY - ); - return true; + if (updateDefault) { + await runTizenSimOrDevice(); + return true; + } else { + await executeAsync( + `${c.cli[CLI_TIZEN_EMULATOR]} launch --name ${name}`, + ExecOptionsPresets.SPINNER_FULL_ERROR_SUMMARY + ); + return true; + } } catch (e) { if (typeof e === 'string') { if (e.includes(ERROR_MSG.UNKNOWN_VM)) { @@ -432,7 +441,7 @@ export const runTizenSimOrDevice = async () => { if (!tId) return Promise.reject(`Tizen platform requires "id" filed in platforms.tizen`); const askForEmulator = async () => { if (!target) { - launchTizenTarget(true); + launchTizenTarget(true, undefined, true); return; } const { startEmulator } = await inquirerPrompt({ diff --git a/packages/sdk-utils/src/target.ts b/packages/sdk-utils/src/target.ts index c4a2acb57..14c9a8bac 100644 --- a/packages/sdk-utils/src/target.ts +++ b/packages/sdk-utils/src/target.ts @@ -1,4 +1,4 @@ -import { RnvContext, getContext, inquirerPrompt, logInfo, writeFileSync } from '@rnv/core'; +import { RnvContext, chalk, getContext, inquirerPrompt, writeFileSync } from '@rnv/core'; export const getTargetWithOptionalPrompt = async () => { const ctx = getContext(); @@ -32,28 +32,45 @@ export const getTargetWithOptionalPrompt = async () => { return target; }; -export const updateDefaultTargets = async (c: RnvContext, selectedTarget: string) => { +export const updateDefaultTargets = async (c: RnvContext, currentTarget: string) => { + if (!c.platform) return; + const localOverridden = !!c.files.project.configLocal?.defaultTargets?.[c.platform]; const defaultTarget = c.runtime.target; - const { confirm } = await inquirerPrompt({ - type: 'confirm', - name: 'confirm', - message: `Your default target for platform ${c.platform} is ${ + const actionLocalUpdate = `Update ${chalk().green('project')} default target for platform ${c.platform}`; + const actionGlobalUpdate = `Update ${chalk().green('global')}${ + localOverridden ? ` and ${chalk().green('project')}` : '' + } default target for platform ${c.platform}`; + const actionNoUpdate = "Don't update"; + + const { chosenAction } = await inquirerPrompt({ + message: 'What to do next?', + type: 'list', + name: 'chosenAction', + choices: [actionLocalUpdate, actionGlobalUpdate, actionNoUpdate], + warningMessage: `Your default target for platform ${c.platform} is ${ !defaultTarget ? 'not defined' : `set to ${defaultTarget}` - }. Do you want to ${!defaultTarget ? 'set' : `update `} it to ${selectedTarget} `, + }. `, }); - if (!confirm) return; - const workspaceConfig = c.files.workspace.config; + c.runtime.target = currentTarget; + + if (chosenAction === actionLocalUpdate || (chosenAction === actionGlobalUpdate && localOverridden)) { + const configLocal = c.files.project.configLocal || {}; + if (!configLocal.defaultTargets) configLocal.defaultTargets = {}; + configLocal.defaultTargets[c.platform] = currentTarget; - if (workspaceConfig && c.platform) { - if (!workspaceConfig.defaultTargets) workspaceConfig.defaultTargets = {}; + c.files.project.configLocal = configLocal; + writeFileSync(c.paths.project.configLocal, JSON.stringify(configLocal, null, 2)); + } - workspaceConfig.defaultTargets[c.platform] = selectedTarget; + if (chosenAction === actionGlobalUpdate) { + const configGlobal = c.files.workspace.config; + if (configGlobal) { + if (!configGlobal.defaultTargets) configGlobal.defaultTargets = {}; + configGlobal.defaultTargets[c.platform] = currentTarget; - c.files.workspace.config = workspaceConfig; - writeFileSync(c.paths.workspace.config, workspaceConfig); + c.files.workspace.config = configGlobal; + writeFileSync(c.paths.workspace.config, JSON.stringify(configGlobal, null, 2)); + } } - logInfo( - `Your default target for platform ${c.platform} has been updated successfully in ${c.paths.workspace.config}` - ); };