Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
ElenaDiachenko committed Nov 14, 2024
1 parent e491e5a commit 3818417
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 76 deletions.
2 changes: 1 addition & 1 deletion packages/sdk-android/src/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
28 changes: 15 additions & 13 deletions packages/sdk-apple/src/__tests__/runner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
{
Expand Down Expand Up @@ -33,6 +34,7 @@ const devicesJson = [
];

jest.mock('@rnv/core');
jest.mock('@rnv/sdk-utils');
jest.mock('../deviceManager');
jest.mock('chalk', () => ({
bold: {
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
40 changes: 2 additions & 38 deletions packages/sdk-apple/src/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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');
Expand Down
23 changes: 16 additions & 7 deletions packages/sdk-tizen/src/deviceManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,11 @@ const formatXMLObject = (
return {};
};

export const launchTizenTarget = async (name: string | true, hideDevices?: boolean): Promise<boolean> => {
export const launchTizenTarget = async (
name: string | true,
hideDevices?: boolean,
updateDefault = false
): Promise<boolean> => {
const c = getContext();
logDefault(`launchTizenTarget:${name}`);
if (name === true) {
Expand Down Expand Up @@ -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}`,

Check warning

Code scanning / CodeQL

Unsafe shell command constructed from library input Medium

This string concatenation which depends on
library input
is later used in a
shell command
.
ExecOptionsPresets.SPINNER_FULL_ERROR_SUMMARY
);
return true;
}
} catch (e) {
if (typeof e === 'string') {
if (e.includes(ERROR_MSG.UNKNOWN_VM)) {
Expand Down Expand Up @@ -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({
Expand Down
51 changes: 34 additions & 17 deletions packages/sdk-utils/src/target.ts
Original file line number Diff line number Diff line change
@@ -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();
Expand Down Expand Up @@ -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}`
);
};

0 comments on commit 3818417

Please sign in to comment.