Skip to content

Commit 9aed45a

Browse files
blakeffacebook-github-bot
authored andcommitted
always proxy to @react-native-community/cli (#45464)
Summary: Pull Request resolved: #45464 Previous work would cause versions >= react-native 0.76 to exit if called through `npx react-native <cmd>`. This was intended to be full deprecated and removed. The intention was to shift users to calling react-native-community/cli directly. This change allows commands to be proxied to react-native-community/cli but with no guarantees of success. It's up to each framework / project to explicitly create that dependency. This also provides warnings, which won't go away, suggesting the supported method of calling the community CLI directly. The outcome is that we're not going to break existing workflows. closes: #45461 Changelog: [General][Fixed] allow proxying commands from react-native to react-native-community/cli with explicit warning Reviewed By: cortinico Differential Revision: D59805357 fbshipit-source-id: 21e23b082a9c709effa050d8e7dd04a40f5ab0e6
1 parent 6e34283 commit 9aed45a

File tree

1 file changed

+45
-16
lines changed

1 file changed

+45
-16
lines changed

packages/react-native/cli.js

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ const deprecated = () => {
2222
);
2323
};
2424

25+
function isMissingCliDependency(error) {
26+
return (
27+
error.code === 'MODULE_NOT_FOUND' &&
28+
/@react-native-community\/cli/.test(error.message)
29+
);
30+
}
31+
2532
let cli = {
2633
bin: '/dev/null',
2734
loadConfig: deprecated,
@@ -107,17 +114,30 @@ The behavior will be changed on ${chalk.white.bold(CLI_DEPRECATION_DATE.toLocale
107114
}
108115

109116
function warnWithDeprecated() {
110-
if (process.argv[2] !== 'init') {
117+
if (!isInitCommand) {
111118
return;
112119
}
113120

114121
console.warn(`
115-
${chalk.yellow('')}️ The \`init\` command is deprecated.
122+
${chalk.yellow('🚨')}️ The \`init\` command is deprecated.
116123
117124
- Switch to ${chalk.dim('npx @react-native-community/cli init')} for the identical behavior.
118125
- Refer to the documentation for information about alternative tools: ${chalk.dim('https://reactnative.dev/docs/getting-started')}`);
119126
}
120127

128+
function warnWithExplicitDependency(version = '*') {
129+
console.warn(`
130+
${chalk.yellow('⚠')}${chalk.dim('react-native')} depends on ${chalk.dim('@react-native-community/cli')} for cli commands. To fix update your ${chalk.dim('package.json')} to include:
131+
132+
${chalk.white.bold(`
133+
"devDependencies": {
134+
"@react-native-community/cli": "latest",
135+
}
136+
`)}
137+
138+
`);
139+
}
140+
121141
/**
122142
* npx react-native -> @react-native-community/cli
123143
*
@@ -157,24 +177,36 @@ async function main() {
157177
currentVersion.startsWith('0.76');
158178

159179
/**
160-
* This command now fails as it's fully deprecated. It will be entirely removed in 0.77.
180+
* This command is now deprecated. We will continue to proxy commands to @react-native-community/cli, but it
181+
* isn't supported anymore. We'll always show the warning.
182+
*
183+
* WARNING: Projects will have to have an explicit dependency on @react-native-community/cli to use the CLI.
161184
*
162185
* Phase 3
163186
*
164187
* @see https://github.com/react-native-community/discussions-and-proposals/tree/main/proposals/0759-react-native-frameworks.md
165188
*/
166-
if (currentVersion !== HEAD && isDeprecated) {
167-
warnWithDeprecated();
168-
process.exit(1);
189+
if (isInitCommand) {
190+
if (currentVersion !== HEAD && isDeprecated) {
191+
warnWithDeprecated();
192+
// We only exit if the user calls `init` and it's deprecated. All other cases should proxy to to @react-native-community/cli.
193+
// Be careful with this as it can break a lot of users.
194+
process.exit(1);
195+
} else if (currentVersion.startsWith('0.75')) {
196+
warnWithDeprecationSchedule();
197+
}
198+
warnWhenRunningInit();
169199
}
170200

171-
if (currentVersion.startsWith('0.75')) {
172-
warnWithDeprecationSchedule();
201+
try {
202+
return require('@react-native-community/cli').run(name);
203+
} catch (e) {
204+
if (isMissingCliDependency(e)) {
205+
warnWithExplicitDependency();
206+
process.exit(1);
207+
}
208+
throw e;
173209
}
174-
175-
warnWhenRunningInit();
176-
177-
return require('@react-native-community/cli').run(name);
178210
}
179211

180212
if (require.main === module) {
@@ -185,10 +217,7 @@ if (require.main === module) {
185217
} catch (e) {
186218
// We silence @react-native-community/cli missing as it is no
187219
// longer a dependency
188-
if (
189-
!e.code === 'MODULE_NOT_FOUND' &&
190-
/@react-native-community\/cli/.test(e.message)
191-
) {
220+
if (!isMissingCliDependency(e)) {
192221
throw e;
193222
}
194223
}

0 commit comments

Comments
 (0)