Skip to content

Commit

Permalink
support installing npm 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 6e30d51 commit d800399
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 1 deletion.
13 changes: 13 additions & 0 deletions frontend-maven-plugin/src/it/npm-version-from-engines/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "example",
"version": "0.0.1",
"engines": {
"npm": ">=7 <8"
},
"dependencies": {
"less": "~3.0.2"
},
"scripts": {
"prebuild": "npm install"
}
}
50 changes: 50 additions & 0 deletions frontend-maven-plugin/src/it/npm-version-from-engines/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?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>v16.0.0</nodeVersion>
<npmVersion>engines</npmVersion>
</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 @@ -6,6 +6,10 @@
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
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 +32,8 @@ public class NPMInstaller {

private final FileDownloader fileDownloader;

private Requirement npmVersionRequirement;

NPMInstaller(InstallConfig config, ArchiveExtractor archiveExtractor, FileDownloader fileDownloader) {
this.logger = LoggerFactory.getLogger(getClass());
this.config = config;
Expand Down Expand Up @@ -78,7 +84,53 @@ public void install() throws InstallationException {
if (this.npmDownloadRoot == null || this.npmDownloadRoot.isEmpty()) {
this.npmDownloadRoot = DEFAULT_NPM_DOWNLOAD_ROOT;
}
if ("engines".equals(this.npmVersion)) {
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("npm")) {
this.npmVersionRequirement = Requirement.buildNPM((String) engines.get("npm"));
} else {
this.logger.info("Could not read npm 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 npm engine version from package.json", e);
}
}

if (!npmProvided() && !npmIsAlreadyInstalled()) {
if (this.npmVersionRequirement != null) {
// download available node versions
try {
String downloadUrl = this.npmDownloadRoot
+ "..";

File archive = File.createTempFile("npm_versions", ".json");

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

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

List<String> npmVersions = new LinkedList<>();
if (data.containsKey("versions")) {
HashMap<String, Object> versions = (HashMap<String, Object>) data.get("versions");
npmVersions.addAll(versions.keySet());
} else {
this.logger.info("Could not read versions from NPM registry");
}

logger.debug("Available NPM versions: {}", npmVersions);
this.npmVersion = npmVersions.stream().filter(version -> npmVersionRequirement.isSatisfiedBy(new Semver(version, Semver.SemverType.NPM))).findFirst().orElseThrow(() -> new InstallationException("Could not find matching node version satisfying requirement " + this.npmVersionRequirement));
this.logger.info("Found matching NPM version {} satisfying requirement {}.", this.npmVersion, this.npmVersionRequirement);
} catch (IOException | DownloadException e) {
throw new InstallationException("Could not get available node versions.", e);
}
}
installNpm();
}
copyNpmScripts();
Expand All @@ -93,7 +145,12 @@ private boolean npmIsAlreadyInstalled() {
HashMap<String, Object> data = new ObjectMapper().readValue(npmPackageJson, HashMap.class);
if (data.containsKey(VERSION)) {
final String foundNpmVersion = data.get(VERSION).toString();
if (foundNpmVersion.equals(this.npmVersion)) {
if (npmVersionRequirement != null && npmVersionRequirement.isSatisfiedBy(new Semver(foundNpmVersion, Semver.SemverType.NPM))) {
//update version with installed version
this.nodeVersion = foundNpmVersion;
this.logger.info("NPM {} matches required version range {} installed.", foundNpmVersion, npmVersionRequirement);
return true;
} else if (foundNpmVersion.equals(this.npmVersion)) {
this.logger.info("NPM {} is already installed.", foundNpmVersion);
return true;
} else {
Expand Down

0 comments on commit d800399

Please sign in to comment.