Skip to content

Commit f9c2743

Browse files
authored
Fixed npm version detection order. (#1)
2 parents 972e12d + cddc735 commit f9c2743

File tree

3 files changed

+31
-25
lines changed

3 files changed

+31
-25
lines changed

CHANGELOG.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
# Webtools releases
22

33
## [Unreleased]
4+
### Fixed
5+
- Fixed order of npm version detection.
46

57
## [0.1.0] - 2024-07-05
68

7-
First ever release
9+
First release.

src/main/java/com/diffplug/webtools/node/NodePlugin.java

+28-3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
package com.diffplug.webtools.node;
1717

1818
import com.github.eirslett.maven.plugins.frontend.lib.ProxyConfig;
19+
import java.io.File;
20+
import java.io.IOException;
21+
import java.nio.charset.StandardCharsets;
22+
import java.nio.file.Files;
1923
import java.util.Collections;
2024
import java.util.Objects;
2125
import org.gradle.api.Action;
@@ -46,12 +50,18 @@ public static class Extension {
4650

4751
public Extension(Project project) {
4852
this.project = Objects.requireNonNull(project);
49-
5053
}
5154

5255
public TaskProvider<?> npm_run(String name, Action<Task> taskConfig) {
5356
return project.getTasks().register("npm_run_" + name, NpmRunTask.class, task -> {
5457
task.taskName = name;
58+
try {
59+
setup.nodeVersion = nvmRc(findNvmRc(project.getProjectDir()));
60+
setup.npmVersion = "provided";
61+
} catch (IOException e) {
62+
throw new RuntimeException(e);
63+
}
64+
5565
task.getSetup().set(setup);
5666
task.getProjectDir().set(project.getProjectDir());
5767
task.getInputs().file("package-lock.json").withPathSensitivity(PathSensitivity.RELATIVE);
@@ -91,8 +101,23 @@ public void npmCiRunTask() throws Exception {
91101

92102
@Override
93103
public void apply(Project project) {
94-
extension = project.getExtensions().create(EXTENSION_NAME, Extension.class, project);
104+
project.getExtensions().create(EXTENSION_NAME, Extension.class, project);
105+
}
106+
107+
private static String nvmRc(File file) throws IOException {
108+
String str = new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8).trim();
109+
return "v" + str;
95110
}
96111

97-
private Extension extension;
112+
private static File findNvmRc(File projectDir) {
113+
File nvmRc = new File(projectDir, ".nvmrc");
114+
if (nvmRc.exists()) {
115+
return nvmRc;
116+
}
117+
nvmRc = new File(projectDir.getParentFile(), ".nvmrc");
118+
if (nvmRc.exists()) {
119+
return nvmRc;
120+
}
121+
throw new IllegalArgumentException("Could not find .nvmrc in " + projectDir + " or its parent.");
122+
}
98123
}

src/main/java/com/diffplug/webtools/node/SetupCleanupNode.java

-21
Original file line numberDiff line numberDiff line change
@@ -20,39 +20,18 @@
2020
import com.github.eirslett.maven.plugins.frontend.lib.ProxyConfig;
2121
import com.github.eirslett.maven.plugins.frontend.lib.TaskRunnerException;
2222
import java.io.File;
23-
import java.io.IOException;
2423
import java.io.Serializable;
25-
import java.nio.charset.StandardCharsets;
2624
import java.nio.file.Files;
2725
import java.util.Collections;
2826

2927
class SetupCleanupNode implements Serializable {
30-
private static String nvmRc(File file) throws IOException {
31-
String str = new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8).trim();
32-
return "v" + str;
33-
}
34-
35-
private static File findNvmRc(File projectDir) {
36-
File nvmRc = new File(projectDir, ".nvmrc");
37-
if (nvmRc.exists()) {
38-
return nvmRc;
39-
}
40-
nvmRc = new File(projectDir.getParentFile(), ".nvmrc");
41-
if (nvmRc.exists()) {
42-
return nvmRc;
43-
}
44-
throw new IllegalArgumentException("Could not find .nvmrc in " + projectDir + " or its parent.");
45-
}
46-
4728
public String nodeVersion;
4829
public String npmVersion;
4930
private File workingDir, installDir;
5031
@SuppressWarnings("unused") // used for serialized equality
5132
private byte[] packageLockJson;
5233

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

0 commit comments

Comments
 (0)