Skip to content

Commit

Permalink
Fixed npm version detection order. (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
nedtwigg authored Jul 5, 2024
2 parents 972e12d + cddc735 commit f9c2743
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 25 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# Webtools releases

## [Unreleased]
### Fixed
- Fixed order of npm version detection.

## [0.1.0] - 2024-07-05

First ever release
First release.
31 changes: 28 additions & 3 deletions src/main/java/com/diffplug/webtools/node/NodePlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
package com.diffplug.webtools.node;

import com.github.eirslett.maven.plugins.frontend.lib.ProxyConfig;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.Collections;
import java.util.Objects;
import org.gradle.api.Action;
Expand Down Expand Up @@ -46,12 +50,18 @@ public static class Extension {

public Extension(Project project) {
this.project = Objects.requireNonNull(project);

}

public TaskProvider<?> npm_run(String name, Action<Task> taskConfig) {
return project.getTasks().register("npm_run_" + name, NpmRunTask.class, task -> {
task.taskName = name;
try {
setup.nodeVersion = nvmRc(findNvmRc(project.getProjectDir()));
setup.npmVersion = "provided";
} catch (IOException e) {
throw new RuntimeException(e);
}

task.getSetup().set(setup);
task.getProjectDir().set(project.getProjectDir());
task.getInputs().file("package-lock.json").withPathSensitivity(PathSensitivity.RELATIVE);
Expand Down Expand Up @@ -91,8 +101,23 @@ public void npmCiRunTask() throws Exception {

@Override
public void apply(Project project) {
extension = project.getExtensions().create(EXTENSION_NAME, Extension.class, project);
project.getExtensions().create(EXTENSION_NAME, Extension.class, project);
}

private static String nvmRc(File file) throws IOException {
String str = new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8).trim();
return "v" + str;
}

private Extension extension;
private static File findNvmRc(File projectDir) {
File nvmRc = new File(projectDir, ".nvmrc");
if (nvmRc.exists()) {
return nvmRc;
}
nvmRc = new File(projectDir.getParentFile(), ".nvmrc");
if (nvmRc.exists()) {
return nvmRc;
}
throw new IllegalArgumentException("Could not find .nvmrc in " + projectDir + " or its parent.");
}
}
21 changes: 0 additions & 21 deletions src/main/java/com/diffplug/webtools/node/SetupCleanupNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,39 +20,18 @@
import com.github.eirslett.maven.plugins.frontend.lib.ProxyConfig;
import com.github.eirslett.maven.plugins.frontend.lib.TaskRunnerException;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.Collections;

class SetupCleanupNode implements Serializable {
private static String nvmRc(File file) throws IOException {
String str = new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8).trim();
return "v" + str;
}

private static File findNvmRc(File projectDir) {
File nvmRc = new File(projectDir, ".nvmrc");
if (nvmRc.exists()) {
return nvmRc;
}
nvmRc = new File(projectDir.getParentFile(), ".nvmrc");
if (nvmRc.exists()) {
return nvmRc;
}
throw new IllegalArgumentException("Could not find .nvmrc in " + projectDir + " or its parent.");
}

public String nodeVersion;
public String npmVersion;
private File workingDir, installDir;
@SuppressWarnings("unused") // used for serialized equality
private byte[] packageLockJson;

public void start(File projectDir) throws Exception {
nodeVersion = nvmRc(findNvmRc(projectDir));
npmVersion = "provided";
workingDir = projectDir;
installDir = new File(projectDir, "build/node-install");
packageLockJson = Files.readAllBytes(workingDir.toPath().resolve("package-lock.json"));
Expand Down

0 comments on commit f9c2743

Please sign in to comment.