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

Fixed npm version detection order. #1

Merged
merged 2 commits into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading