From e1476e644c1da9cee5bc933b32219027248bd5af Mon Sep 17 00:00:00 2001 From: Szymon Rybczak Date: Mon, 18 Dec 2023 11:37:25 +0100 Subject: [PATCH] fix: get commands options and description from new package (#477) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: get commands options and description from new package * chore: add changset * fix: keep backward compatability * Update packages/repack/commands.js Co-authored-by: Jakub Romańczyk * Update packages/repack/commands.js Co-authored-by: Jakub Romańczyk * Update packages/repack/commands.js Co-authored-by: Jakub Romańczyk * fix: linter * chore(repack): document the changes * chore: update changeset --------- Co-authored-by: Jakub Romańczyk Co-authored-by: Jakub Romańczyk --- .changeset/eighty-elephants-report.md | 5 ++++ packages/repack/commands.js | 34 ++++++++++++++++++++------- 2 files changed, 30 insertions(+), 9 deletions(-) create mode 100644 .changeset/eighty-elephants-report.md diff --git a/.changeset/eighty-elephants-report.md b/.changeset/eighty-elephants-report.md new file mode 100644 index 000000000..180635cda --- /dev/null +++ b/.changeset/eighty-elephants-report.md @@ -0,0 +1,5 @@ +--- +"@callstack/repack": minor +--- + +fix: get commands options and description from new package (support RN>=0.73) diff --git a/packages/repack/commands.js b/packages/repack/commands.js index 2ebdebf3a..3574818a5 100644 --- a/packages/repack/commands.js +++ b/packages/repack/commands.js @@ -1,7 +1,7 @@ const path = require('path'); const { createRequire } = require('module'); -function getReactNativeCliPath() { +function getCommands() { let cliPath; try { @@ -25,19 +25,35 @@ function getReactNativeCliPath() { // NOOP } - if (!cliPath) { - throw new Error('Cannot resolve @react-native-community/cli package'); + const { projectCommands } = require(`${cliPath}/commands`); + const commandNames = Object.values(projectCommands).map(({ name }) => name); + + if (commandNames.includes('bundle') && commandNames.includes('start')) { + return projectCommands; + } + + // RN >= 0.73 + let commands; + + try { + commands = require(require.resolve( + 'react-native/react-native.config.js' + )).commands; + } catch (e) { + // NOOP + } + + if (!commands) { + throw new Error('Cannot resolve path to react-native package'); } - return cliPath; + return commands; } -const { - projectCommands: cliCommands, -} = require(`${getReactNativeCliPath()}/commands`); +const cliCommands = Object.values(getCommands()); -const startCommand = cliCommands.find((command) => command.name === 'start'); -const bundleCommand = cliCommands.find((command) => command.name === 'bundle'); +const startCommand = cliCommands.find(({ name }) => name === 'start'); +const bundleCommand = cliCommands.find(({ name }) => name === 'bundle'); const webpackConfigOption = { name: '--webpackConfig ',