Skip to content

Commit 8ea9b6a

Browse files
authored
fix(react-native): implement build-macos command (#2225)
1 parent 6eb69da commit 8ea9b6a

File tree

2 files changed

+96
-60
lines changed

2 files changed

+96
-60
lines changed

packages/react-native/local-cli/runMacOS/runMacOS.js

Lines changed: 95 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,10 @@ const {logger, CLIError, getDefaultUserTerminal} = (() => {
3737
})();
3838

3939
/**
40-
* @param {string[]} _
41-
* @param {Record<string, unknown>} _ctx
4240
* @param {Options} args
41+
* @returns {{ xcodeProject: XcodeProject, scheme: string }}
4342
*/
44-
function runMacOS(_, _ctx, args) {
43+
function parseArgs(args) {
4544
if (!fs.existsSync(args.projectPath)) {
4645
throw new CLIError(
4746
'macOS project folder not found. Are you sure this is a React Native project?',
@@ -78,6 +77,26 @@ function runMacOS(_, _ctx, args) {
7877
} "${chalk.bold(xcodeProject.name)}"`,
7978
);
8079

80+
return {xcodeProject, scheme};
81+
}
82+
83+
/**
84+
* @param {string[]} _
85+
* @param {Record<string, unknown>} _ctx
86+
* @param {Options} args
87+
*/
88+
function buildMacOS(_, _ctx, args) {
89+
const {xcodeProject, scheme} = parseArgs(args);
90+
return buildProject(xcodeProject, scheme, {...args, packager: false});
91+
}
92+
93+
/**
94+
* @param {string[]} _
95+
* @param {Record<string, unknown>} _ctx
96+
* @param {Options} args
97+
*/
98+
function runMacOS(_, _ctx, args) {
99+
const {xcodeProject, scheme} = parseArgs(args);
81100
return run(xcodeProject, scheme, args);
82101
}
83102

@@ -113,7 +132,7 @@ async function run(xcodeProject, scheme, args) {
113132

114133
child_process.exec(
115134
'open -b ' + bundleID + ' -a ' + '"' + appPath + '"',
116-
(error, stdout, stderr) => {
135+
(error, _stdout, stderr) => {
117136
if (error) {
118137
logger.error('Failed to launch the app', stderr);
119138
} else {
@@ -272,58 +291,75 @@ function getProcessOptions({packager, terminal, port}) {
272291
};
273292
}
274293

275-
module.exports = {
276-
name: 'run-macos',
277-
description: 'builds your app and starts it',
278-
func: runMacOS,
279-
examples: [
280-
{
281-
desc: 'Run the macOS app',
282-
cmd: 'react-native run-macos',
283-
},
284-
],
285-
options: [
286-
{
287-
name: '--configuration [string]',
288-
description:
289-
'[Deprecated] Explicitly set the scheme configuration to use',
290-
default: 'Debug',
291-
},
292-
{
293-
name: '--mode [string]',
294-
description:
295-
'Explicitly set the scheme configuration to use, corresponds to `--configuration` in `xcodebuild` command, e.g. Debug, Release.',
296-
},
297-
{
298-
name: '--scheme [string]',
299-
description:
300-
'Explicitly set Xcode scheme to use, corresponds to `--scheme` in `xcodebuild` command.',
301-
},
302-
{
303-
name: '--project-path [string]',
304-
description:
305-
'Path relative to project root where the Xcode project ' +
306-
'(.xcodeproj) lives.',
307-
default: 'macos',
308-
},
309-
{
310-
name: '--no-packager',
311-
description: 'Do not launch packager while building',
312-
},
313-
{
314-
name: '--verbose',
315-
description: 'Do not use xcpretty even if installed',
316-
},
317-
{
318-
name: '--port [number]',
319-
default: process.env.RCT_METRO_PORT || 8081,
320-
parse: val => Number(val),
321-
},
322-
{
323-
name: '--terminal [string]',
324-
description:
325-
'Launches the Metro Bundler in a new window using the specified terminal path.',
326-
default: getDefaultUserTerminal,
327-
},
328-
],
329-
};
294+
const commonOptions = [
295+
{
296+
name: '--mode [string]',
297+
description:
298+
'Explicitly set the scheme configuration to use, corresponds to `--configuration` in `xcodebuild` command, e.g. Debug, Release.',
299+
},
300+
{
301+
name: '--scheme [string]',
302+
description:
303+
'Explicitly set Xcode scheme to use, corresponds to `--scheme` in `xcodebuild` command.',
304+
},
305+
{
306+
name: '--project-path [string]',
307+
description:
308+
'Path relative to project root where the Xcode project (.xcodeproj) lives.',
309+
default: 'macos',
310+
},
311+
{
312+
name: '--verbose',
313+
description: 'Do not use xcpretty even if installed',
314+
},
315+
];
316+
317+
module.exports = [
318+
{
319+
name: 'build-macos',
320+
description: 'builds your app',
321+
func: buildMacOS,
322+
examples: [
323+
{
324+
desc: 'Build the macOS app',
325+
cmd: 'react-native build-macos',
326+
},
327+
],
328+
options: commonOptions,
329+
},
330+
{
331+
name: 'run-macos',
332+
description: 'builds your app and starts it',
333+
func: runMacOS,
334+
examples: [
335+
{
336+
desc: 'Run the macOS app',
337+
cmd: 'react-native run-macos',
338+
},
339+
],
340+
options: [
341+
{
342+
name: '--configuration [string]',
343+
description:
344+
'[Deprecated] Explicitly set the scheme configuration to use',
345+
default: 'Debug',
346+
},
347+
...commonOptions,
348+
{
349+
name: '--no-packager',
350+
description: 'Do not launch packager while building',
351+
},
352+
{
353+
name: '--port [number]',
354+
default: process.env.RCT_METRO_PORT || 8081,
355+
parse: val => Number(val),
356+
},
357+
{
358+
name: '--terminal [string]',
359+
description:
360+
'Launches the Metro Bundler in a new window using the specified terminal path.',
361+
default: getDefaultUserTerminal,
362+
},
363+
],
364+
},
365+
];

packages/react-native/react-native.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ try {
4242
}
4343
}
4444

45-
const macosCommands = [require('./local-cli/runMacOS/runMacOS')]; // [macOS]
45+
const macosCommands = require('./local-cli/runMacOS/runMacOS'); // [macOS]
4646
const {
4747
bundleCommand,
4848
startCommand,

0 commit comments

Comments
 (0)