From 727196a2d5b1a33a364af5e95e60940c30f1d013 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. ## Changelog: [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 | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/packages/react-native/cli.js b/packages/react-native/cli.js index 28c17f96a0656b..416bd2775590c6 100755 --- a/packages/react-native/cli.js +++ b/packages/react-native/cli.js @@ -29,6 +29,7 @@ let cli = { }; 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'; @@ -65,7 +66,7 @@ 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') { + if (isInitCommand) { console.warn( `\nRunning: ${chalk.grey.bold('npx @react-native-community/cli init')}\n`, ); @@ -80,7 +81,7 @@ function warnWhenRunningInit() { * @see https://github.com/react-native-community/discussions-and-proposals/tree/main/proposals/0759-react-native-frameworks.md */ function warnWithDeprecationSchedule() { - if (process.argv[2] !== 'init') { + if (!isInitCommand) { return; } @@ -127,7 +128,12 @@ ${chalk.yellow('⚠')}️ The \`init\` command is deprecated. * */ async function main() { - if (isNpxRuntime && !process.env.SKIP && currentVersion !== HEAD) { + if ( + isNpxRuntime && + !process.env.SKIP && + currentVersion !== HEAD && + isInitCommand + ) { try { const latest = await getLatestVersion(); // TODO: T184416093 When cli is deprecated, remove semver from package.json