Skip to content

Commit 47a3f52

Browse files
blakeffacebook-github-bot
authored andcommitted
Add RFC-0759 deprecation logging cli init
Summary: Implements the RFC which progressively provides warnings to users of the `npx react-native init` command as we gradually deprecate. Changelog: [General][Deprecated] - init cli deprecation logging Reviewed By: cortinico Differential Revision: D54423109 fbshipit-source-id: 679b6672bdbfc42a9b82a2aad38fd3253c6ea6a2
1 parent 082decb commit 47a3f52

File tree

1 file changed

+103
-3
lines changed

1 file changed

+103
-3
lines changed

packages/react-native/cli.js

Lines changed: 103 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,33 @@
1111
'use strict';
1212

1313
const {name, version: currentVersion} = require('./package.json');
14-
const cli = require('@react-native-community/cli');
1514
const chalk = require('chalk');
1615
const {get} = require('https');
1716
const {URL} = require('url');
1817

18+
const deprecated = () => {
19+
throw new Error(
20+
'react-native/cli is deprecated, please use @react-native-community/cli instead',
21+
);
22+
};
23+
24+
let cli = {
25+
bin: '/dev/null',
26+
loadConfig: deprecated,
27+
run: deprecated,
28+
};
29+
1930
const isNpxRuntime = process.env.npm_lifecycle_event === 'npx';
2031
const DEFAULT_REGISTRY_HOST =
2132
process.env.npm_config_registry ?? 'https://registry.npmjs.org/';
2233
const HEAD = '1000.0.0';
2334

35+
// We're going to deprecate the `init` command proxying requests to @react-native-community/cli transparently
36+
// on September 30th, 2024 or 0.76 (whichever arrives first). This is part of work to decouple of community CLI from React Native core.
37+
//
38+
// See https://github.com/react-native-community/discussions-and-proposals/blob/main/proposals/0759-react-native-frameworks.md
39+
const CLI_DEPRECATION_DATE = new Date('2024-09-30');
40+
2441
async function getLatestVersion(registryHost = DEFAULT_REGISTRY_HOST) {
2542
return new Promise((res, rej) => {
2643
const url = new URL(registryHost);
@@ -41,14 +58,64 @@ async function getLatestVersion(registryHost = DEFAULT_REGISTRY_HOST) {
4158

4259
/**
4360
* Warn when users are using `npx react-native init`, to raise awareness of the changes from RFC 0759.
61+
*
62+
* Phase 1
63+
*
4464
* @see https://github.com/react-native-community/discussions-and-proposals/tree/main/proposals/0759-react-native-frameworks.md
4565
*/
4666
function warnWhenRunningInit() {
4767
if (process.argv[2] === 'init') {
48-
console.warn('\nRunning: npx @react-native-community/cli init\n');
68+
console.warn(
69+
`\nRunning: ${chalk.grey.bold('npx @react-native-community/cli init')}\n`,
70+
);
4971
}
5072
}
5173

74+
/**
75+
* Warn more sternly that the ability to call `npx react-native init` is going away.
76+
*
77+
* Phase 2
78+
*
79+
* @see https://github.com/react-native-community/discussions-and-proposals/tree/main/proposals/0759-react-native-frameworks.md
80+
*/
81+
function warnWithDeprecationSchedule() {
82+
if (process.argv[2] !== 'init') {
83+
return;
84+
}
85+
86+
const daysRemaining = Math.ceil(
87+
(CLI_DEPRECATION_DATE.getTime() - new Date().getTime()) / 86_400_000,
88+
);
89+
90+
const emphasis =
91+
daysRemaining < 10
92+
? chalk.bgRed.white.bold
93+
: daysRemaining < 30
94+
? chalk.red.bold
95+
: daysRemaining < 60
96+
? chalk.green.bold
97+
: chalk.blueBright.bold;
98+
99+
console.warn(`
100+
${chalk.yellow('⚠️')} The \`init\` command is deprecated.
101+
The behavior will be changed on ${chalk.white.bold(CLI_DEPRECATION_DATE.toLocaleDateString())} ${emphasis(`(${daysRemaining} day${daysRemaining > 1 ? 's' : ''})`)}.
102+
103+
- Switch to ${chalk.dim('npx @react-native-community/cli init')} for the identical behavior.
104+
- Refer to the documentation for information about alternative tools: ${chalk.dim('https://reactnative.dev/docs/getting-started')}`);
105+
}
106+
107+
function warnWithDeprecated() {
108+
if (process.argv[2] !== 'init') {
109+
return;
110+
}
111+
112+
console.warn(`
113+
${chalk.yellow('⚠')}️ The \`init\` command is deprecated.
114+
115+
- Switch to ${chalk.dim('npx @react-native-community/cli init')} for the identical behavior.
116+
- Refer to the documentation for information about alternative tools: ${chalk.dim('https://reactnative.dev/docs/getting-started')}`);
117+
}
118+
52119
/**
53120
* npx react-native -> @react-native-community/cli
54121
*
@@ -77,13 +144,46 @@ async function main() {
77144
}
78145
}
79146

147+
const isDeprecated =
148+
CLI_DEPRECATION_DATE.getTime() <= new Date().getTime() ||
149+
currentVersion.startsWith('0.76');
150+
151+
/**
152+
* This command now fails as it's fully deprecated. It will be entirely removed in 0.77.
153+
*
154+
* Phase 3
155+
*
156+
* @see https://github.com/react-native-community/discussions-and-proposals/tree/main/proposals/0759-react-native-frameworks.md
157+
*/
158+
if (currentVersion !== HEAD && isDeprecated) {
159+
warnWithDeprecated();
160+
process.exit(1);
161+
}
162+
163+
if (currentVersion.startsWith('0.75')) {
164+
warnWithDeprecationSchedule();
165+
}
166+
80167
warnWhenRunningInit();
81168

82-
return cli.run(name);
169+
return require('@react-native-community/cli').run(name);
83170
}
84171

85172
if (require.main === module) {
86173
main();
174+
} else {
175+
try {
176+
cli = require('@react-native-community/cli');
177+
} catch (e) {
178+
// We silence @react-native-community/cli missing as it is no
179+
// longer a dependency
180+
if (
181+
!e.code === 'MODULE_NOT_FOUND' &&
182+
/@react-native-community\/cli/.test(e.message)
183+
) {
184+
throw e;
185+
}
186+
}
87187
}
88188

89189
module.exports = cli;

0 commit comments

Comments
 (0)