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

Correct .npmrc for newer versions of NPM #787

Open
wants to merge 12 commits into
base: npm-8.19
Choose a base branch
from
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 {
Spaction marked this conversation as resolved.
Show resolved Hide resolved
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");
}
}
Spaction marked this conversation as resolved.
Show resolved Hide resolved

// 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
Loading