Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Remove hardcoded npm registry url #2520

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

jasonjiicloud
Copy link

Summary:

fixes: #2519
Official npm registry url is hardcoded, the "cli" can't run successfully in a closed environment.

Following error is displayed when running npx @react-native-community/cli@latest init AwesomeProject --verbose.

error fetch failed.
TypeError: fetch failed
    at node:internal/deps/undici/undici:13178:13
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async getTemplateVersion (/Users/jilingyun/.npm/_npx/2788f5fbd47acb89/node_modules/@react-native-community/cli/build/tools/npm.js:121:16)
    at async createTemplateUri (/Users/jilingyun/.npm/_npx/2788f5fbd47acb89/node_modules/@react-native-community/cli/build/commands/init/version.js:53:29)
    at async createProject (/Users/jilingyun/.npm/_npx/2788f5fbd47acb89/node_modules/@react-native-community/cli/build/commands/init/init.js:341:23)
    at async Object.initialize [as func] (/Users/jilingyun/.npm/_npx/2788f5fbd47acb89/node_modules/@react-native-community/cli/build/commands/init/init.js:422:7)
    at async Command.handleAction (/Users/jilingyun/.npm/_npx/2788f5fbd47acb89/node_modules/@react-native-community/cli/build/index.js:116:9)

My solution is using getNpmRegistryUrl() to get real npm registry url in user's local environment, then it can avoid using official npm registry directly.

Test Plan:

Build and run node /path/to/react-native-cli/packages/cli/build/bin.js init AwesomeProject

Checklist

  • Documentation is up to date to reflect these changes.
  • Follows commit message convention described in CONTRIBUTING.md

@github-actions github-actions bot added the bugfix label Oct 6, 2024
@jasonjiicloud jasonjiicloud changed the title fix: Remove hardcoded npm registry url #2519 fix: Remove hardcoded npm registry url Oct 6, 2024
@@ -129,7 +127,7 @@ const minorVersion = (version: string) => {
export async function getTemplateVersion(
reactNativeVersion: string,
): Promise<TemplateVersion | undefined> {
const json = await fetch(TEMPLATE_VERSIONS_URL).then(
const json = await fetch(getNpmRegistryUrl() + '@react-native-community/template').then(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer leveraging here new URL() to make sure URL is always in a correct format, e.g. when getNpmRegistryUrl()'d return value without / at the end, previous would've fail.

Suggested change
const json = await fetch(getNpmRegistryUrl() + '@react-native-community/template').then(
const json = await fetch(new URL('@react-native-community/template', getNpmRegistryUrl())).then(

Copy link
Collaborator

@szymonrybczak szymonrybczak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes a lot of sense, thanks! 👍

@szymonrybczak
Copy link
Collaborator

cc @blakef

@blakef blakef self-requested a review October 7, 2024 10:54
Copy link
Contributor

@blakef blakef left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a really nice change. Couple of small things before you land it:

  1. update the URL handling to use URL as @szymonrybczak suggested
  2. fix the formatting

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Official npm registry url is hardcoded, the "cli" can't run successfully in a closed environment.
3 participants