Skip to content

Commit

Permalink
fix: warn only in init command when CLI uses cached npx version (#…
Browse files Browse the repository at this point in the history
…44644)

Summary:
In #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: #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
  • Loading branch information
szymonrybczak authored and cipolleschi committed Jun 3, 2024
1 parent a88a3c5 commit 1860441
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions packages/react-native/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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
*
Expand All @@ -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) {
Expand Down

0 comments on commit 1860441

Please sign in to comment.