From 1860441d848b8f620e0b959e43122628b1cd04c9 Mon Sep 17 00:00:00 2001 From: szymonrybczak Date: Thu, 23 May 2024 09:43:02 -0700 Subject: [PATCH] fix: warn only in `init` command when CLI uses cached `npx` version (#44644) Summary: In https://github.com/facebook/react-native/pull/37510, a check was introduced to check if user is using `latest` version of `npx`, but right now it checks for every command executed, but it should only ensure that `latest` is included when creating a new project. In this Pull Request I've added a condition to only warn if `init` was fired. [GENERAL] [FIXED] - Warn only in `init` command when CLI uses cached `npx` version Pull Request resolved: https://github.com/facebook/react-native/pull/44644 Test Plan: Warning about using `latest` version CLI should only be presented when running `init` command. Reviewed By: arushikesarwani94 Differential Revision: D57681864 Pulled By: blakef fbshipit-source-id: 5c81b9a08141396efcd24539b2560cea16028dd9 --- packages/react-native/cli.js | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/packages/react-native/cli.js b/packages/react-native/cli.js index 0e1bd5216512d0..8bb052ecab6529 100755 --- a/packages/react-native/cli.js +++ b/packages/react-native/cli.js @@ -17,6 +17,7 @@ const {get} = require('https'); const {URL} = require('url'); const isNpxRuntime = process.env.npm_lifecycle_event === 'npx'; +const isInitCommand = process.argv[2] === 'init'; const DEFAULT_REGISTRY_HOST = process.env.npm_config_registry ?? 'https://registry.npmjs.org/'; const HEAD = '1000.0.0'; @@ -44,11 +45,25 @@ async function getLatestVersion(registryHost = DEFAULT_REGISTRY_HOST) { * @see https://github.com/react-native-community/discussions-and-proposals/tree/main/proposals/0759-react-native-frameworks.md */ function warnWhenRunningInit() { - if (process.argv[2] === 'init') { - console.warn('\nRunning: npx @react-native-community/cli init\n'); + if (isInitCommand) { + console.warn( + `\nRunning: ${chalk.grey.bold('npx @react-native-community/cli init')}\n`, + ); } } +function warnWithDeprecated() { + if (isInitCommand) { + return; + } + + console.warn(` +${chalk.yellow('⚠')}️ The \`init\` command is deprecated. + +- Switch to ${chalk.dim('npx @react-native-community/cli init')} for the identical behavior. +- Refer to the documentation for information about alternative tools: ${chalk.dim('https://reactnative.dev/docs/getting-started')}`); +} + /** * npx react-native -> @react-native-community/cli * @@ -59,7 +74,12 @@ function warnWhenRunningInit() { * */ async function main() { - if (isNpxRuntime && !process.env.SKIP && currentVersion !== HEAD) { + if ( + isNpxRuntime && + !process.env.SKIP && + currentVersion !== HEAD && + isInitCommand + ) { try { const latest = await getLatestVersion(); if (latest !== currentVersion) {