-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
63 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 45 additions & 24 deletions
69
sdk-commons/src/main/java/org/openmrs/maven/plugins/utility/NpmVersionHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,68 @@ | ||
package org.openmrs.maven.plugins.utility; | ||
|
||
import org.json.JSONArray; | ||
import org.openmrs.maven.plugins.model.PackageJson; | ||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.net.HttpURLConnection; | ||
import java.net.URL; | ||
import org.json.JSONObject; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class NpmVersionHelper { | ||
|
||
public String getLatestReleasedVersionFromNpmRegistry(PackageJson packageJson) { | ||
private static final Logger log = LoggerFactory.getLogger(NpmVersionHelper.class); | ||
|
||
/** | ||
* Retrieves the resolved version of an NPM package based on the supplied semver range. | ||
* <p> | ||
* This method runs the `npm pack --dry-run --json <package>@<version>` command to | ||
* get the exact version of the package that satisfies the specified semver range. | ||
* | ||
* @param packageJson The PackageJson object containing the name of the package. | ||
* @param versionRange The semver range to resolve the version against. | ||
* @return The resolved version of the package that satisfies the semver range. | ||
* @throws RuntimeException if the command fails or the resolved version cannot be determined. | ||
*/ | ||
public String getLatestReleasedVersionFromNpmRegistry(PackageJson packageJson, String versionRange) { | ||
try { | ||
String packageName = packageJson.getName(); | ||
if (packageName == null || packageName.isEmpty()) { | ||
throw new IllegalArgumentException("Package name cannot be null or empty"); | ||
JSONArray jsonArray = getJsonArray(versionRange, packageName); | ||
if (jsonArray.isEmpty()) { | ||
throw new RuntimeException("No versions found for the specified range: " + versionRange); | ||
} | ||
|
||
URL url = new URL("https://registry.npmjs.org/" + packageName.replace("/", "%2F")); | ||
JSONObject json = getJson(url); | ||
return json.getJSONObject("dist-tags").getString("latest"); | ||
|
||
} catch (Exception e) { | ||
throw new RuntimeException("Error retrieving latest version from NPM ", e); | ||
JSONObject jsonObject = jsonArray.getJSONObject(0); | ||
return jsonObject.getString("version"); | ||
} catch (IOException | InterruptedException e) { | ||
log.error(e.getMessage()); | ||
throw new RuntimeException("Error retrieving resolved version from NPM", e); | ||
} | ||
} | ||
|
||
private static JSONObject getJson(URL url) throws IOException { | ||
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); | ||
conn.setRequestMethod("GET"); | ||
conn.setRequestProperty("Accept", "application/json"); | ||
if (conn.getResponseCode() != 200) { | ||
throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode()); | ||
private static JSONArray getJsonArray(String versionRange, String packageName) throws IOException, InterruptedException { | ||
if (packageName == null || packageName.isEmpty()) { | ||
throw new IllegalArgumentException("Package name cannot be null or empty"); | ||
} | ||
|
||
BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream()))); | ||
StringBuilder sb = new StringBuilder(); | ||
String output; | ||
while ((output = br.readLine()) != null) { | ||
sb.append(output); | ||
ProcessBuilder processBuilder = new ProcessBuilder() | ||
.command("npm", "pack", "--dry-run", "--json", packageName + "@" + versionRange) | ||
.redirectErrorStream(true) | ||
.inheritIO(); | ||
Process process = processBuilder.start(); | ||
|
||
// Read the command output | ||
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); | ||
StringBuilder outputBuilder = new StringBuilder(); | ||
String line; | ||
while ((line = reader.readLine()) != null) { | ||
outputBuilder.append(line); | ||
} | ||
|
||
conn.disconnect(); | ||
return new JSONObject(sb.toString()); | ||
int exitCode = process.waitFor(); | ||
if (exitCode != 0) { | ||
throw new RuntimeException("npm pack --dry-run --json command failed with exit code " + exitCode); | ||
} | ||
return new JSONArray(outputBuilder.toString()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters