From 54e33b9dc1011971e2f441f1e7f2f21b601706a0 Mon Sep 17 00:00:00 2001 From: Tommy Nguyen <4123478+tido64@users.noreply.github.com> Date: Tue, 22 Oct 2024 17:45:59 +0200 Subject: [PATCH] fix(react-native): implement `build-macos` command (#2225) (#2229) --- .../local-cli/runMacOS/runMacOS.js | 154 +++++++++++------- packages/react-native/react-native.config.js | 2 +- 2 files changed, 96 insertions(+), 60 deletions(-) diff --git a/packages/react-native/local-cli/runMacOS/runMacOS.js b/packages/react-native/local-cli/runMacOS/runMacOS.js index dfecfb3c7107dc..0089810dfe25f1 100644 --- a/packages/react-native/local-cli/runMacOS/runMacOS.js +++ b/packages/react-native/local-cli/runMacOS/runMacOS.js @@ -37,11 +37,10 @@ const {logger, CLIError, getDefaultUserTerminal} = (() => { })(); /** - * @param {string[]} _ - * @param {Record} _ctx * @param {Options} args + * @returns {{ xcodeProject: XcodeProject, scheme: string }} */ -function runMacOS(_, _ctx, args) { +function parseArgs(args) { if (!fs.existsSync(args.projectPath)) { throw new CLIError( 'macOS project folder not found. Are you sure this is a React Native project?', @@ -78,6 +77,26 @@ function runMacOS(_, _ctx, args) { } "${chalk.bold(xcodeProject.name)}"`, ); + return {xcodeProject, scheme}; +} + +/** + * @param {string[]} _ + * @param {Record} _ctx + * @param {Options} args + */ +function buildMacOS(_, _ctx, args) { + const {xcodeProject, scheme} = parseArgs(args); + return buildProject(xcodeProject, scheme, {...args, packager: false}); +} + +/** + * @param {string[]} _ + * @param {Record} _ctx + * @param {Options} args + */ +function runMacOS(_, _ctx, args) { + const {xcodeProject, scheme} = parseArgs(args); return run(xcodeProject, scheme, args); } @@ -113,7 +132,7 @@ async function run(xcodeProject, scheme, args) { child_process.exec( 'open -b ' + bundleID + ' -a ' + '"' + appPath + '"', - (error, stdout, stderr) => { + (error, _stdout, stderr) => { if (error) { logger.error('Failed to launch the app', stderr); } else { @@ -272,58 +291,75 @@ function getProcessOptions({packager, terminal, port}) { }; } -module.exports = { - name: 'run-macos', - description: 'builds your app and starts it', - func: runMacOS, - examples: [ - { - desc: 'Run the macOS app', - cmd: 'react-native run-macos', - }, - ], - options: [ - { - name: '--configuration [string]', - description: - '[Deprecated] Explicitly set the scheme configuration to use', - default: 'Debug', - }, - { - name: '--mode [string]', - description: - 'Explicitly set the scheme configuration to use, corresponds to `--configuration` in `xcodebuild` command, e.g. Debug, Release.', - }, - { - name: '--scheme [string]', - description: - 'Explicitly set Xcode scheme to use, corresponds to `--scheme` in `xcodebuild` command.', - }, - { - name: '--project-path [string]', - description: - 'Path relative to project root where the Xcode project ' + - '(.xcodeproj) lives.', - default: 'macos', - }, - { - name: '--no-packager', - description: 'Do not launch packager while building', - }, - { - name: '--verbose', - description: 'Do not use xcpretty even if installed', - }, - { - name: '--port [number]', - default: process.env.RCT_METRO_PORT || 8081, - parse: val => Number(val), - }, - { - name: '--terminal [string]', - description: - 'Launches the Metro Bundler in a new window using the specified terminal path.', - default: getDefaultUserTerminal, - }, - ], -}; +const commonOptions = [ + { + name: '--mode [string]', + description: + 'Explicitly set the scheme configuration to use, corresponds to `--configuration` in `xcodebuild` command, e.g. Debug, Release.', + }, + { + name: '--scheme [string]', + description: + 'Explicitly set Xcode scheme to use, corresponds to `--scheme` in `xcodebuild` command.', + }, + { + name: '--project-path [string]', + description: + 'Path relative to project root where the Xcode project (.xcodeproj) lives.', + default: 'macos', + }, + { + name: '--verbose', + description: 'Do not use xcpretty even if installed', + }, +]; + +module.exports = [ + { + name: 'build-macos', + description: 'builds your app', + func: buildMacOS, + examples: [ + { + desc: 'Build the macOS app', + cmd: 'react-native build-macos', + }, + ], + options: commonOptions, + }, + { + name: 'run-macos', + description: 'builds your app and starts it', + func: runMacOS, + examples: [ + { + desc: 'Run the macOS app', + cmd: 'react-native run-macos', + }, + ], + options: [ + { + name: '--configuration [string]', + description: + '[Deprecated] Explicitly set the scheme configuration to use', + default: 'Debug', + }, + ...commonOptions, + { + name: '--no-packager', + description: 'Do not launch packager while building', + }, + { + name: '--port [number]', + default: process.env.RCT_METRO_PORT || 8081, + parse: val => Number(val), + }, + { + name: '--terminal [string]', + description: + 'Launches the Metro Bundler in a new window using the specified terminal path.', + default: getDefaultUserTerminal, + }, + ], + }, +]; diff --git a/packages/react-native/react-native.config.js b/packages/react-native/react-native.config.js index 10aac90c87943c..644d850d64bf95 100644 --- a/packages/react-native/react-native.config.js +++ b/packages/react-native/react-native.config.js @@ -17,7 +17,7 @@ const { startCommand, } = require('@react-native/community-cli-plugin'); -const macosCommands = [require('./local-cli/runMacOS/runMacOS')]; // [macOS] +const macosCommands = require('./local-cli/runMacOS/runMacOS'); // [macOS] const codegenCommand = { name: 'codegen',