Skip to content

Commit 9322199

Browse files
authored
fix(install): support JFrog registry metadata JSON format (#1249)
JFrog npm registries return package metadata with spaces around colons (e.g. `"version" : "0.1.12"`) which caused version extraction to fail. Relax grep patterns to allow optional spaces and add ConvertFrom-Json fallback in install.ps1 for registries returning non-JSON content types. Also include the metadata URL in error messages for easier debugging. Closes #962
1 parent bf39b75 commit 9322199

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

packages/cli/install.ps1

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ function Get-PackageMetadata {
7474
try {
7575
$errorJson = $errorMsg | ConvertFrom-Json
7676
if ($errorJson.error) {
77-
Write-Error-Exit "Failed to fetch version '${versionPath}': $($errorJson.error)"
77+
Write-Error-Exit "Failed to fetch version '${versionPath}': $($errorJson.error)`n URL: $metadataUrl"
7878
}
7979
} catch {
8080
# JSON parsing failed, fall through to generic error
@@ -85,11 +85,17 @@ function Get-PackageMetadata {
8585
# Check for error in successful response
8686
# npm can return {"error":"..."} object or a plain string like "version not found: test"
8787
if ($script:PackageMetadata -is [string]) {
88-
# Plain string response means error
89-
Write-Error-Exit "Failed to fetch version '${versionPath}': $script:PackageMetadata"
88+
# Some registries (e.g. JFrog) may return JSON with a non-JSON content type,
89+
# causing Invoke-RestMethod to return a raw string. Try parsing it as JSON first.
90+
try {
91+
$script:PackageMetadata = $script:PackageMetadata | ConvertFrom-Json
92+
} catch {
93+
# Not valid JSON - treat as plain string error
94+
Write-Error-Exit "Failed to fetch version '${versionPath}': $script:PackageMetadata`n URL: $metadataUrl"
95+
}
9096
}
9197
if ($script:PackageMetadata.error) {
92-
Write-Error-Exit "Failed to fetch version '${versionPath}': $($script:PackageMetadata.error)"
98+
Write-Error-Exit "Failed to fetch version '${versionPath}': $($script:PackageMetadata.error)`n URL: $metadataUrl"
9399
}
94100
}
95101
return $script:PackageMetadata

packages/cli/install.sh

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -245,16 +245,16 @@ fetch_package_metadata() {
245245
# npm can return either {"error":"..."} or a plain JSON string like "version not found: test"
246246
if echo "$PACKAGE_METADATA" | grep -q '"error"'; then
247247
local error_msg
248-
error_msg=$(echo "$PACKAGE_METADATA" | grep -o '"error":"[^"]*"' | cut -d'"' -f4)
249-
error "Failed to fetch version '${version_path}': ${error_msg:-unknown error}"
248+
error_msg=$(echo "$PACKAGE_METADATA" | grep -o '"error" *: *"[^"]*"' | cut -d'"' -f4)
249+
error "Failed to fetch version '${version_path}': ${error_msg:-unknown error}\n URL: $metadata_url"
250250
fi
251251
# Check if response is a plain error string (not a valid package object)
252252
# Use '"version":' to match JSON property, not just the word "version"
253-
if ! echo "$PACKAGE_METADATA" | grep -q '"version":'; then
253+
if ! echo "$PACKAGE_METADATA" | grep -q '"version" *:'; then
254254
# Remove surrounding quotes from the error message if present
255255
local error_msg
256256
error_msg=$(echo "$PACKAGE_METADATA" | sed 's/^"//;s/"$//')
257-
error "Failed to fetch version '${version_path}': ${error_msg:-unknown error}"
257+
error "Failed to fetch version '${version_path}': ${error_msg:-unknown error}\n URL: $metadata_url"
258258
fi
259259
fi
260260
# PACKAGE_METADATA is set as a global variable, no need to echo
@@ -266,7 +266,7 @@ get_version_from_metadata() {
266266
# Call fetch_package_metadata to populate PACKAGE_METADATA global
267267
# Don't use command substitution as it would swallow the exit from error()
268268
fetch_package_metadata
269-
RESOLVED_VERSION=$(echo "$PACKAGE_METADATA" | grep -o '"version":"[^"]*"' | head -1 | cut -d'"' -f4)
269+
RESOLVED_VERSION=$(echo "$PACKAGE_METADATA" | grep -o '"version" *: *"[^"]*"' | head -1 | cut -d'"' -f4)
270270
if [ -z "$RESOLVED_VERSION" ]; then
271271
error "Failed to extract version from package metadata"
272272
fi

0 commit comments

Comments
 (0)