Skip to content

Commit 2f4cfa5

Browse files
adamTrzthymikee
andauthored
fix: use variant arg in build-android command (#1798)
* fix: use variant arg in build-android command * chore: extract toPascalCase * chore: extract getTaskNames Co-authored-by: Michał Pierzchała <[email protected]>
1 parent f308a56 commit 2f4cfa5

File tree

6 files changed

+44
-32
lines changed

6 files changed

+44
-32
lines changed

docs/commands.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,8 @@ Override the root directory for the Android build (which contains the android di
323323

324324
#### `--variant <string>`
325325

326+
> **DEPRECATED** – use "mode" instead
327+
326328
> default: 'debug'
327329
328330
Specify your app's build variant.

packages/cli-platform-android/src/commands/buildAndroid/index.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@ import {
77
import {Config} from '@react-native-community/cli-types';
88
import execa from 'execa';
99
import {getAndroidProject} from '../../config/getAndroidProject';
10-
import {getTaskNames, toPascalCase} from '../runAndroid/runOnAllDevices';
1110
import adb from '../runAndroid/adb';
1211
import getAdbPath from '../runAndroid/getAdbPath';
1312
import {startServerInNewWindow} from './startServerInNewWindow';
13+
import {getTaskNames} from '../runAndroid/getTaskNames';
1414

1515
export interface BuildFlags {
16-
mode: string;
17-
variant: string;
16+
mode?: string;
17+
variant?: string;
1818
activeArchOnly?: boolean;
19-
packager: boolean;
19+
packager?: boolean;
2020
port: number;
2121
terminal: string;
2222
tasks?: Array<string>;
@@ -58,17 +58,18 @@ async function buildAndroid(
5858
);
5959
}
6060

61-
const mode = args.variant || args.mode;
62-
6361
if (args.tasks && args.mode) {
6462
logger.warn(
6563
'Both "tasks" and "mode" parameters were passed to "build" command. Using "tasks" for building the app.',
6664
);
6765
}
6866

69-
const tasks = args.tasks || ['assemble' + toPascalCase(mode)];
70-
71-
let gradleArgs = getTaskNames(androidProject.appName, tasks);
67+
let gradleArgs = getTaskNames(
68+
androidProject.appName,
69+
args.mode || args.variant,
70+
args.tasks,
71+
'assemble',
72+
);
7273

7374
if (args.extraParams) {
7475
gradleArgs = [...gradleArgs, ...args.extraParams];
@@ -117,7 +118,6 @@ export const options = [
117118
{
118119
name: '--mode <string>',
119120
description: "Specify your app's build variant",
120-
default: 'debug',
121121
},
122122
{
123123
name: '--variant <string>',
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import {toPascalCase} from './toPascalCase';
2+
import type {BuildFlags} from '../buildAndroid';
3+
4+
export function getTaskNames(
5+
appName: string,
6+
mode: BuildFlags['mode'] = 'debug',
7+
tasks: BuildFlags['tasks'],
8+
taskPrefix: 'assemble' | 'install',
9+
): Array<string> {
10+
const appTasks = tasks || [taskPrefix + toPascalCase(mode)];
11+
12+
return appName
13+
? appTasks.map((command) => `${appName}:${command}`)
14+
: appTasks;
15+
}

packages/cli-platform-android/src/commands/runAndroid/listAndroidDevices.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {execSync} from 'child_process';
22
import adb from './adb';
33
import getAdbPath from './getAdbPath';
44
import {getEmulators} from './tryLaunchEmulator';
5+
import {toPascalCase} from './toPascalCase';
56
import os from 'os';
67
import prompts from 'prompts';
78
import chalk from 'chalk';
@@ -14,10 +15,6 @@ type DeviceData = {
1415
type: 'emulator' | 'phone';
1516
};
1617

17-
function toPascalCase(value: string) {
18-
return value !== '' ? value[0].toUpperCase() + value.slice(1) : value;
19-
}
20-
2118
/**
2219
*
2320
* @param deviceId string

packages/cli-platform-android/src/commands/runAndroid/runOnAllDevices.ts

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,8 @@ import tryRunAdbReverse from './tryRunAdbReverse';
1515
import tryLaunchAppOnDevice from './tryLaunchAppOnDevice';
1616
import tryLaunchEmulator from './tryLaunchEmulator';
1717
import tryInstallAppOnDevice from './tryInstallAppOnDevice';
18-
import {Flags} from '.';
19-
20-
export function getTaskNames(
21-
appName: string,
22-
commands: Array<string>,
23-
): Array<string> {
24-
return appName
25-
? commands.map((command) => `${appName}:${command}`)
26-
: commands;
27-
}
28-
29-
export function toPascalCase(value: string) {
30-
return value !== '' ? value[0].toUpperCase() + value.slice(1) : value;
31-
}
18+
import {getTaskNames} from './getTaskNames';
19+
import type {Flags} from '.';
3220

3321
type AndroidProject = NonNullable<Config['project']['android']>;
3422

@@ -54,13 +42,20 @@ async function runOnAllDevices(
5442
);
5543
}
5644
}
45+
if (args.variant) {
46+
logger.warn(
47+
'"variant" flag is deprecated and will be removed in future release. Please switch to "mode" flag.',
48+
);
49+
}
5750

5851
try {
5952
if (!args.binaryPath) {
60-
const tasks = args.tasks || [
61-
'install' + toPascalCase(args.mode ?? 'debug'),
62-
];
63-
let gradleArgs = getTaskNames(androidProject.appName, tasks);
53+
let gradleArgs = getTaskNames(
54+
androidProject.appName,
55+
args.mode || args.variant,
56+
args.tasks,
57+
'install',
58+
);
6459

6560
if (args.extraParams) {
6661
gradleArgs = [...gradleArgs, ...args.extraParams];
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function toPascalCase(value: string) {
2+
return value !== '' ? value[0].toUpperCase() + value.slice(1) : value;
3+
}

0 commit comments

Comments
 (0)