First use environment variable as npm registry (#4082)
* First use environment variable as npm registry * use NPM_CONFIG_REGISTRY as env variable --------- Co-authored-by: 류형주/인공지능팀 <hyungju.ryu@ahnlab.com>
This commit is contained in:
parent
0e7627d1c4
commit
f3b8b2e336
@ -37,7 +37,7 @@ from reflex.config import Config, get_config
|
|||||||
from reflex.utils import console, net, path_ops, processes
|
from reflex.utils import console, net, path_ops, processes
|
||||||
from reflex.utils.exceptions import GeneratedCodeHasNoFunctionDefs
|
from reflex.utils.exceptions import GeneratedCodeHasNoFunctionDefs
|
||||||
from reflex.utils.format import format_library_name
|
from reflex.utils.format import format_library_name
|
||||||
from reflex.utils.registry import _get_best_registry
|
from reflex.utils.registry import _get_npm_registry
|
||||||
|
|
||||||
CURRENTLY_INSTALLING_NODE = False
|
CURRENTLY_INSTALLING_NODE = False
|
||||||
|
|
||||||
@ -620,7 +620,7 @@ def initialize_package_json():
|
|||||||
code = _compile_package_json()
|
code = _compile_package_json()
|
||||||
output_path.write_text(code)
|
output_path.write_text(code)
|
||||||
|
|
||||||
best_registry = _get_best_registry()
|
best_registry = _get_npm_registry()
|
||||||
bun_config_path = get_web_dir() / constants.Bun.CONFIG_PATH
|
bun_config_path = get_web_dir() / constants.Bun.CONFIG_PATH
|
||||||
bun_config_path.write_text(
|
bun_config_path.write_text(
|
||||||
f"""
|
f"""
|
||||||
|
@ -1,8 +1,10 @@
|
|||||||
"""Utilities for working with registries."""
|
"""Utilities for working with registries."""
|
||||||
|
|
||||||
|
import os
|
||||||
|
|
||||||
import httpx
|
import httpx
|
||||||
|
|
||||||
from reflex.utils import console
|
from reflex.utils import console, net
|
||||||
|
|
||||||
|
|
||||||
def latency(registry: str) -> int:
|
def latency(registry: str) -> int:
|
||||||
@ -15,7 +17,7 @@ def latency(registry: str) -> int:
|
|||||||
int: The latency of the registry in microseconds.
|
int: The latency of the registry in microseconds.
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
return httpx.get(registry).elapsed.microseconds
|
return net.get(registry).elapsed.microseconds
|
||||||
except httpx.HTTPError:
|
except httpx.HTTPError:
|
||||||
console.info(f"Failed to connect to {registry}.")
|
console.info(f"Failed to connect to {registry}.")
|
||||||
return 10_000_000
|
return 10_000_000
|
||||||
@ -34,7 +36,7 @@ def average_latency(registry, attempts: int = 3) -> int:
|
|||||||
return sum(latency(registry) for _ in range(attempts)) // attempts
|
return sum(latency(registry) for _ in range(attempts)) // attempts
|
||||||
|
|
||||||
|
|
||||||
def _get_best_registry() -> str:
|
def get_best_registry() -> str:
|
||||||
"""Get the best registry based on latency.
|
"""Get the best registry based on latency.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
@ -46,3 +48,15 @@ def _get_best_registry() -> str:
|
|||||||
]
|
]
|
||||||
|
|
||||||
return min(registries, key=average_latency)
|
return min(registries, key=average_latency)
|
||||||
|
|
||||||
|
|
||||||
|
def _get_npm_registry() -> str:
|
||||||
|
"""Get npm registry. If environment variable is set, use it first.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
str:
|
||||||
|
"""
|
||||||
|
if npm_registry := os.environ.get("NPM_CONFIG_REGISTRY", ""):
|
||||||
|
return npm_registry
|
||||||
|
else:
|
||||||
|
return get_best_registry()
|
||||||
|
Loading…
Reference in New Issue
Block a user