Skip to content

Commit

Permalink
support installing yarn 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 d800399 commit d78f3e7
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "example",
"version": "0.0.1",
"engines": {
"yarn": "^1.10"
},
"dependencies": {
"less": "~3.0.2"
},
"scripts": {
"prebuild": "npm install"
}
}
50 changes: 50 additions & 0 deletions frontend-maven-plugin/src/it/yarn-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 yarn</id>
<goals>
<goal>install-node-and-yarn</goal>
</goals>
<configuration>
<nodeVersion>v16.0.0</nodeVersion>
<yarnVersion>engines</yarnVersion>
</configuration>
</execution>

<execution>
<id>yarn install</id>
<goals>
<goal>yarn</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,6 @@
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, 'node_modules/less/package.json').exists() : "Less dependency has not been installed successfully";

String buildLog = new File(basedir, 'build.log').text
assert buildLog.contains('BUILD SUCCESS') : 'build was not successful'
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@
import java.io.FileNotFoundException;
import java.io.IOException;
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 @@ -31,6 +38,8 @@ public class YarnInstaller {

private final FileDownloader fileDownloader;

private Requirement yarnVersionRequirement;

YarnInstaller(InstallConfig config, ArchiveExtractor archiveExtractor, FileDownloader fileDownloader) {
logger = LoggerFactory.getLogger(getClass());
this.config = config;
Expand Down Expand Up @@ -64,7 +73,53 @@ public void install() throws InstallationException {
if (yarnDownloadRoot == null || yarnDownloadRoot.isEmpty()) {
yarnDownloadRoot = DEFAULT_YARN_DOWNLOAD_ROOT;
}
if ("engines".equals(this.yarnVersion)) {
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("yarn")) {
this.yarnVersionRequirement = Requirement.buildNPM((String) engines.get("yarn"));
} else {
this.logger.info("Could not read yarn 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 yarn engine version from package.json", e);
}
}
if (!yarnIsAlreadyInstalled()) {
if (this.yarnVersionRequirement != null) {
// download available node versions
try {
String downloadUrl = "https://api.github.com/repos/yarnpkg/yarn/releases";

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

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

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

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

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

logger.debug("Available Yarn versions: {}", yarnVersions);
this.yarnVersion = yarnVersions.stream().filter(version -> yarnVersionRequirement.isSatisfiedBy(new Semver(version, Semver.SemverType.NPM))).findFirst().orElseThrow(() -> new InstallationException("Could not find matching node version satisfying requirement " + this.yarnVersionRequirement));
this.logger.info("Found matching Yarn version {} satisfying requirement {}.", this.yarnVersion, this.yarnVersionRequirement);
} catch (IOException | DownloadException e) {
throw new InstallationException("Could not get available Yarn versions.", e);
}
}
if (!yarnVersion.startsWith("v")) {
throw new InstallationException("Yarn version has to start with prefix 'v'.");
}
Expand All @@ -81,7 +136,12 @@ private boolean yarnIsAlreadyInstalled() {
final String version =
new YarnExecutor(executorConfig, Arrays.asList("--version"), null).executeAndGetResult(logger).trim();

if (version.equals(yarnVersion.replaceFirst("^v", ""))) {
if (yarnVersionRequirement != null && yarnVersionRequirement.isSatisfiedBy(new Semver(version, Semver.SemverType.NPM))) {
//update version with installed version
this.yarnVersion = version;
this.logger.info("Yarn {} matches required version range {} installed.", version, yarnVersionRequirement);
return true;
} else if (version.equals(yarnVersion.replaceFirst("^v", ""))) {
logger.info("Yarn {} is already installed.", version);
return true;
} else {
Expand Down

0 comments on commit d78f3e7

Please sign in to comment.