Skip to content

Commit

Permalink
support installing node version from package.json engines
Browse files Browse the repository at this point in the history
fixes #798
  • Loading branch information
tisoft committed Sep 19, 2021
1 parent 4d13f04 commit 6e30d51
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "example",
"version": "0.0.1",
"engines": {
"node": ">=10.3 <15"
},
"dependencies": {
"less": "~3.0.2"
},
"scripts": {
"prebuild": "npm install"
}
}
49 changes: 49 additions & 0 deletions frontend-maven-plugin/src/it/node-version-from-engines/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.github.eirslett</groupId>
<artifactId>example</artifactId>
<version>0</version>
<packaging>pom</packaging>

<build>
<plugins>
<plugin>
<groupId>com.github.eirslett</groupId>
<artifactId>frontend-maven-plugin</artifactId>
<!-- NB! Set <version> to the latest released version of frontend-maven-plugin, like in README.md -->
<version>@project.version@</version>

<configuration>
<installDirectory>target</installDirectory>
</configuration>

<executions>

<execution>
<id>install node and npm</id>
<goals>
<goal>install-node-and-npm</goal>
</goals>
<configuration>
<nodeVersion>engines</nodeVersion>
</configuration>
</execution>

<execution>
<id>npm install</id>
<goals>
<goal>npm</goal>
</goals>
<!-- Optional configuration which provides for running any npm command -->
<configuration>
<arguments>install</arguments>
</configuration>
</execution>

</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
assert new File(basedir, 'target/node').exists() : "Node was not installed in the custom install directory";
assert new File(basedir, 'node_modules').exists() : "Node modules were not installed in the base directory";
assert new File(basedir, 'target/node/npm').exists() : "npm was not copied to the node directory";

import org.codehaus.plexus.util.FileUtils;

String buildLog = FileUtils.fileRead(new File(basedir, 'build.log'));

assert buildLog.contains('BUILD SUCCESS') : 'build was not successful'
Original file line number Diff line number Diff line change
Expand Up @@ -78,28 +78,28 @@ public void execute(FrontendPluginFactory factory) throws InstallationException
String npmDownloadRoot = getNpmDownloadRoot();
Server server = MojoUtils.decryptServer(serverId, session, decrypter);
if (null != server) {
factory.getNodeInstaller(proxyConfig)
String installedNodeVersion=factory.getNodeInstaller(proxyConfig)
.setNodeVersion(nodeVersion)
.setNodeDownloadRoot(nodeDownloadRoot)
.setNpmVersion(npmVersion)
.setUserName(server.getUsername())
.setPassword(server.getPassword())
.install();
factory.getNPMInstaller(proxyConfig)
.setNodeVersion(nodeVersion)
.setNodeVersion(installedNodeVersion)
.setNpmVersion(npmVersion)
.setNpmDownloadRoot(npmDownloadRoot)
.setUserName(server.getUsername())
.setPassword(server.getPassword())
.install();
} else {
factory.getNodeInstaller(proxyConfig)
String installedNodeVersion=factory.getNodeInstaller(proxyConfig)
.setNodeVersion(nodeVersion)
.setNodeDownloadRoot(nodeDownloadRoot)
.setNpmVersion(npmVersion)
.install();
factory.getNPMInstaller(proxyConfig)
.setNodeVersion(this.nodeVersion)
.setNodeVersion(installedNodeVersion)
.setNpmVersion(this.npmVersion)
.setNpmDownloadRoot(npmDownloadRoot)
.install();
Expand Down
6 changes: 6 additions & 0 deletions frontend-plugin-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
<packaging>jar</packaging>

<dependencies>
<dependency>
<groupId>com.vdurmont</groupId>
<artifactId>semver4j</artifactId>
<version>3.1.0</version>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.Arrays;

import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.vdurmont.semver4j.Requirement;
import com.vdurmont.semver4j.Semver;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -28,6 +34,8 @@ public class NodeInstaller {

private final FileDownloader fileDownloader;

private Requirement nodeVersionRequirement;

NodeInstaller(InstallConfig config, ArchiveExtractor archiveExtractor, FileDownloader fileDownloader) {
this.logger = LoggerFactory.getLogger(getClass());
this.config = config;
Expand Down Expand Up @@ -74,13 +82,65 @@ private boolean npmProvided() throws InstallationException {
return false;
}

public void install() throws InstallationException {
public String install() throws InstallationException {
// use static lock object for a synchronized block
synchronized (LOCK) {
if (this.nodeDownloadRoot == null || this.nodeDownloadRoot.isEmpty()) {
this.nodeDownloadRoot = this.config.getPlatform().getNodeDownloadRoot();
}

if ("engines".equals(this.nodeVersion)) {
try {
File packageFile = new File(this.config.getWorkingDirectory(), "package.json");
HashMap<String, Object> data = new ObjectMapper().readValue(packageFile, HashMap.class);
if (data.containsKey("engines")) {
HashMap<String, Object> engines = (HashMap<String, Object>) data.get("engines");
if (engines.containsKey("node")) {
this.nodeVersionRequirement = Requirement.buildNPM((String) engines.get("node"));
} else {
this.logger.info("Could not read node from engines from package.json");
}
} else {
this.logger.info("Could not read engines from package.json");
}
} catch (IOException e) {
throw new InstallationException("Could not read node engine version from package.json", e);
}
}

if (!nodeIsAlreadyInstalled()) {
if (this.nodeVersionRequirement != null) {
// download available node versions
try {
String downloadUrl = this.nodeDownloadRoot
+ "index.json";

File tmpDirectory = getTempDirectory();

File archive = File.createTempFile("node_versions", ".json", tmpDirectory);

downloadFile(downloadUrl, archive, this.userName, this.password);

HashMap<String, Object>[] data = new ObjectMapper().readValue(archive, HashMap[].class);

List<String> nodeVersions = new LinkedList<>();
for (HashMap<String, Object> d : data) {
if (d.containsKey("version")) {
nodeVersions.add((String) d.get("version"));
}
}

// we want the oldest possible version, that satisfies the requirements
Collections.reverse(nodeVersions);

logger.debug("Available node versions: {}", nodeVersions);
this.nodeVersion = nodeVersions.stream().filter(version -> nodeVersionRequirement.isSatisfiedBy(new Semver(version, Semver.SemverType.NPM))).findFirst().orElseThrow(() -> new InstallationException("Could not find matching node version satisfying requirement " + this.nodeVersionRequirement));
this.logger.info("Found matching node version {} satisfying requirement {}.", this.nodeVersion, this.nodeVersionRequirement);
} catch (IOException | DownloadException e) {
throw new InstallationException("Could not get available node versions.", e);
}
}

this.logger.info("Installing node version {}", this.nodeVersion);
if (!this.nodeVersion.startsWith("v")) {
this.logger.warn("Node version does not start with naming convention 'v'.");
Expand All @@ -96,6 +156,8 @@ public void install() throws InstallationException {
}
}
}

return nodeVersion;
}

private boolean nodeIsAlreadyInstalled() {
Expand All @@ -104,14 +166,19 @@ private boolean nodeIsAlreadyInstalled() {
File nodeFile = executorConfig.getNodePath();
if (nodeFile.exists()) {
final String version =
new NodeExecutor(executorConfig, Arrays.asList("--version"), null).executeAndGetResult(logger);
new NodeExecutor(executorConfig, Arrays.asList("--version"), null).executeAndGetResult(logger);

if (version.equals(this.nodeVersion)) {
if (nodeVersionRequirement != null && nodeVersionRequirement.isSatisfiedBy(new Semver(version, Semver.SemverType.NPM))) {
//update version with installed version
this.nodeVersion = version;
this.logger.info("Node {} matches required version range {} installed.", version, nodeVersionRequirement);
return true;
} else if (version.equals(this.nodeVersion)) {
this.logger.info("Node {} is already installed.", version);
return true;
} else {
this.logger.info("Node {} was installed, but we need version {}", version,
this.nodeVersion);
this.nodeVersion);
return false;
}
} else {
Expand Down

0 comments on commit 6e30d51

Please sign in to comment.