forked from react-native-community/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
194 lines (180 loc) · 5.18 KB
/
index.ts
File metadata and controls
194 lines (180 loc) · 5.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import {
CLIError,
getDefaultUserTerminal,
isPackagerRunning,
logger,
printRunDoctorTip,
} from '@react-native-community/cli-tools';
import {Config} from '@react-native-community/cli-types';
import execa from 'execa';
import {getAndroidProject} from '../../config/getAndroidProject';
import adb from '../runAndroid/adb';
import getAdbPath from '../runAndroid/getAdbPath';
import {startServerInNewWindow} from './startServerInNewWindow';
import {getTaskNames} from '../runAndroid/getTaskNames';
import {promptForTaskSelection} from '../runAndroid/listAndroidTasks';
export interface BuildFlags {
mode?: string;
variant?: string;
activeArchOnly?: boolean;
packager?: boolean;
port: number;
terminal: string;
tasks?: Array<string>;
extraParams?: Array<string>;
interactive?: boolean;
}
export async function runPackager(args: BuildFlags, config: Config) {
if (!args.packager) {
return;
}
const result = await isPackagerRunning(args.port);
if (result === 'running') {
logger.info('JS server already running.');
} else if (result === 'unrecognized') {
logger.warn('JS server not recognized, continuing with build...');
} else {
// result == 'not_running'
logger.info('Starting JS server...');
try {
startServerInNewWindow(
args.port,
args.terminal,
config.root,
config.reactNativePath,
);
} catch (error) {
logger.warn(
`Failed to automatically start the packager server. Please run "react-native start" manually. Error details: ${error.message}`,
);
}
}
}
async function buildAndroid(
_argv: Array<string>,
config: Config,
args: BuildFlags,
) {
const androidProject = getAndroidProject(config);
if (args.variant) {
logger.warn(
'"variant" flag is deprecated and will be removed in future release. Please switch to "mode" flag.',
);
}
if (args.tasks && args.mode) {
logger.warn(
'Both "tasks" and "mode" parameters were passed to "build" command. Using "tasks" for building the app.',
);
}
let {tasks} = args;
if (args.interactive) {
const selectedTask = await promptForTaskSelection(
'build',
androidProject.sourceDir,
);
if (selectedTask) {
tasks = [selectedTask];
}
}
let gradleArgs = getTaskNames(
androidProject.appName,
args.mode || args.variant,
tasks,
'assemble',
androidProject.sourceDir,
);
if (args.extraParams) {
gradleArgs.push(...args.extraParams);
}
if (args.activeArchOnly) {
const adbPath = getAdbPath();
const devices = adb.getDevices(adbPath);
const architectures = devices
.map((device) => {
return adb.getCPU(adbPath, device);
})
.filter(
(arch, index, array) => arch != null && array.indexOf(arch) === index,
);
if (architectures.length > 0) {
logger.info(`Detected architectures ${architectures.join(', ')}`);
// `reactNativeDebugArchitectures` was renamed to `reactNativeArchitectures` in 0.68.
// Can be removed when 0.67 no longer needs to be supported.
gradleArgs.push(
'-PreactNativeDebugArchitectures=' + architectures.join(','),
);
gradleArgs.push('-PreactNativeArchitectures=' + architectures.join(','));
}
}
await runPackager(args, config);
return build(gradleArgs, androidProject.sourceDir);
}
export function build(gradleArgs: string[], sourceDir: string) {
process.chdir(sourceDir);
const cmd = process.platform.startsWith('win') ? 'gradlew.bat' : './gradlew';
logger.info('Building the app...');
logger.debug(`Running command "${cmd} ${gradleArgs.join(' ')}"`);
try {
execa.sync(cmd, gradleArgs, {
stdio: 'inherit',
cwd: sourceDir,
});
} catch (error) {
printRunDoctorTip();
throw new CLIError('Failed to build the app.', error);
}
}
export const options = [
{
name: '--mode <string>',
description: "Specify your app's build variant",
},
{
name: '--variant <string>',
description:
"Specify your app's build variant. Deprecated! Use 'mode' instead",
},
{
name: '--no-packager',
description: 'Do not launch packager while building',
},
{
name: '--port <number>',
default: process.env.RCT_METRO_PORT || 8081,
parse: Number,
},
{
name: '--terminal <string>',
description:
'Launches the Metro Bundler in a new window using the specified terminal path.',
default: getDefaultUserTerminal(),
},
{
name: '--tasks <list>',
description:
'Run custom Gradle tasks. By default it\'s "assembleDebug". Will override passed mode and variant arguments.',
parse: (val: string) => val.split(','),
},
{
name: '--active-arch-only',
description:
'Build native libraries only for the current device architecture for debug builds.',
default: false,
},
{
name: '--extra-params <string>',
description: 'Custom params passed to gradle build command',
parse: (val: string) => val.split(' '),
},
{
name: '--interactive',
description:
'Explicitly select build type and flavour to use before running a build',
},
];
export default {
name: 'build-android',
description: 'builds your app',
func: buildAndroid,
options,
};