Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,32 @@ public JsonNode list(File workingDirectory, List<String> extraArgs) throws IOExc
}
}

public int compareVersionTo(File workingDirectory, String compareVersion) throws IOException, InterruptedException {
String currentVersion = version(workingDirectory);

List<Integer> currentComponents = Arrays.stream(currentVersion.split("\\."))
.map(Integer::parseInt)
.collect(Collectors.toList());
List<Integer> compareComponents = Arrays.stream(compareVersion.split("\\."))
.map(Integer::parseInt)
.collect(Collectors.toList());

int currentSize = currentComponents.size();
int compareSize = compareComponents.size();
int maxLength = Math.max(currentSize, compareSize);

for (int i = 0; i < maxLength; i++){
int currentComp = i < currentSize ? currentComponents.get(i) : 0;
int compareComp = i < compareSize ? compareComponents.get(i) : 0;

int comp = compareComp - currentComp;
if(comp != 0){
return comp;
}
}
return 0;
}

public String version(File workingDirectory) throws IOException, InterruptedException {
return runCommand(workingDirectory, new String[]{"--version"}, Collections.emptyList()).getRes();
}
Expand Down Expand Up @@ -131,4 +157,4 @@ private CommandResults runCommand(File workingDirectory, String[] args, List<Str

return npmCommandRes;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,24 @@ private void createTempNpmrc(Path workingDir, List<String> commandArgs) throws I
npmrcBuilder.append("proxy = ").append(this.npmProxy).append("\n");
}

//Update Auth property for newer npm versions
if( this.npmDriver.compareVersionTo(workingDir.toFile(), "8.19") >= 0){
try (ArtifactoryManager artifactoryManager = artifactoryManagerBuilder.build()) {
String authProp = npmAuth.getProperty("_auth");

String newAuthKey = artifactoryManager.getUrl();
if (!StringUtils.endsWith(newAuthKey, "/")) {
newAuthKey += "/";
}
newAuthKey += "api/npm/:_auth";
newAuthKey = newAuthKey.replaceAll("^http(s)?:","") + ":_auth";
npmAuth.setProperty(newAuthKey, authProp);
npmAuth.remove("_auth");
}
}

// Save npm auth
npmAuth.forEach((key, value) -> npmrcBuilder.append(key).append("=").append(value).append("\n"));

// Write npmrc file
try (FileWriter fileWriter = new FileWriter(npmrcPath.toFile());
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter)) {
Expand Down