From 88532f06bc39f18870d5c0895d53c28ad2808d4d Mon Sep 17 00:00:00 2001 From: Dan Allen Date: Tue, 16 Apr 2024 01:59:08 -0600 Subject: [PATCH] resolves #42 clean local Node.js installation directory when Node.js version is changed in configuration --- CHANGELOG.adoc | 1 + src/it/node-version/invoker.properties | 3 ++- src/it/node-version/verify.groovy | 5 ++++- .../java/org/antora/maven/AntoraMavenPlugin.java | 5 +++-- src/main/java/org/antora/maven/FileUtils.java | 9 +++++++++ .../org/antora/maven/FrontendMojoExecutor.java | 14 ++++++++++++++ 6 files changed, 33 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index 2301ead..4fa8992 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -22,6 +22,7 @@ This project utilizes semantic versioning. * Add hint to error message when `antora` command fails to inspect log to find cause * Set execution ID (-antora) when executing frontend-maven-plugin mojo * Deduce Antora CLI version from package.json or package-lock.json when needed (#41) +* Clean local Node.js installation directory when Node.js version is changed in configuration (#42) === Fixed diff --git a/src/it/node-version/invoker.properties b/src/it/node-version/invoker.properties index 317ef40..c17381b 100644 --- a/src/it/node-version/invoker.properties +++ b/src/it/node-version/invoker.properties @@ -1 +1,2 @@ -invoker.goals = antora:antora -Dnode.installDirectory=.cache -Dnode.version=v18.19.1 -DnpmRegistryURL=https://registry.yarnpkg.com +invoker.goals.1 = antora:antora -Dnode.installDirectory=.cache -Dnode.version=v18.19.1 -DnpmRegistryURL=https://registry.npmjs.org +invoker.goals.2 = antora:antora -Dnode.installDirectory=.cache -Dnode.version=v16.20.2 -DnpmRegistryURL=https://registry.yarnpkg.com diff --git a/src/it/node-version/verify.groovy b/src/it/node-version/verify.groovy index 1493379..1e6ba75 100644 --- a/src/it/node-version/verify.groovy +++ b/src/it/node-version/verify.groovy @@ -6,4 +6,7 @@ File siteIndexFile = new File(basedir, "target/site/test/1.0/index.html") assert siteIndexFile.isFile() File nodeExec = new File(nodeDir, 'node') String nodeVersion = "${nodeExec} --version".execute().text.trim() -assert nodeVersion.equals("v18.19.1") +assert nodeVersion.equals("v16.20.2") +String stdout = new File(basedir, "build.log").text +assert stdout.contains("Installing node version v18.19.1") +assert stdout.contains("Installing node version v16.20.2") diff --git a/src/main/java/org/antora/maven/AntoraMavenPlugin.java b/src/main/java/org/antora/maven/AntoraMavenPlugin.java index 4dc8a9d..919b9ec 100644 --- a/src/main/java/org/antora/maven/AntoraMavenPlugin.java +++ b/src/main/java/org/antora/maven/AntoraMavenPlugin.java @@ -176,9 +176,10 @@ public class AntoraMavenPlugin extends AbstractMojo { SystemNodeLinker systemNodeLinker = new SystemNodeLinker(getLog(), nodeHomeDir); if (this.nodeExecutable == null) { systemNodeLinker.unlinkNode(); - frontendMojoExecutor.executeMojo("install-node-and-npm", - element("nodeVersion", new NodeVersionResolver(getLog()).resolveVersion(this.nodeVersion))); + String resolvedNodeVersion = new NodeVersionResolver(getLog()).resolveVersion(this.nodeVersion); + frontendMojoExecutor.handleNodeVersionChange(nodeHomeDir, resolvedNodeVersion); npmCacheDir = new File(nodeHomeDir, "_npm"); + frontendMojoExecutor.executeMojo("install-node-and-npm", element("nodeVersion", resolvedNodeVersion)); } else { systemNodeLinker.linkNode(Path.of(this.nodeExecutable).toString()); } diff --git a/src/main/java/org/antora/maven/FileUtils.java b/src/main/java/org/antora/maven/FileUtils.java index 8fd8dd0..10e206e 100644 --- a/src/main/java/org/antora/maven/FileUtils.java +++ b/src/main/java/org/antora/maven/FileUtils.java @@ -51,6 +51,15 @@ public class FileUtils { return true; } + public static boolean hasContents(File file, String contents) { + if (!file.exists()) return false; + try { + return contents.equals(Files.readString(file.toPath()).stripTrailing()); + } catch (IOException e) { + return false; + } + } + public static boolean isEmptyDirectory(File directory) { if (!directory.isDirectory()) return false; try (Stream entries = Files.list(directory.toPath())) { diff --git a/src/main/java/org/antora/maven/FrontendMojoExecutor.java b/src/main/java/org/antora/maven/FrontendMojoExecutor.java index a49c7f5..022eb18 100644 --- a/src/main/java/org/antora/maven/FrontendMojoExecutor.java +++ b/src/main/java/org/antora/maven/FrontendMojoExecutor.java @@ -21,6 +21,9 @@ import org.codehaus.plexus.util.xml.Xpp3Dom; import org.eclipse.aether.RepositorySystemSession; import org.eclipse.aether.repository.RemoteRepository; +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; import java.util.Arrays; import java.util.List; @@ -84,6 +87,17 @@ public class FrontendMojoExecutor { } } + public void handleNodeVersionChange(File nodeHomeDir, String nodeVersion) throws MojoExecutionException { + File nodeVersionFile = new File(nodeHomeDir, ".node-version"); + if (FileUtils.hasContents(nodeVersionFile, nodeVersion)) return; + try { + FileUtils.cleanDirectory(nodeHomeDir); + Files.writeString(nodeVersionFile.toPath(), nodeVersion); + } catch (IOException e) { + throw new MojoExecutionException("Failed to handle Node.js version change", e); + } + } + private PluginDescriptor loadFrontendMavenPlugin(BuildPluginManager buildPluginManager, MavenSession mavenSession) throws MojoExecutionException { Plugin plugin = getFrontendMavenPlugin(mavenSession); -- GitLab