Skip to content

Commit

Permalink
Support using the corepack version provided with Node.
Browse files Browse the repository at this point in the history
In most uses, users will want to ues the version of corepack provided
with the NodeJS version they are using, and the plugin now supports this
mode of usage by default if no corepack version is explicitly provided.
  • Loading branch information
stevestorey authored and eirslett committed Aug 11, 2024
1 parent ea9c8c9 commit 8c26a6d
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 25 deletions.
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,16 +176,19 @@ https://github.com/eirslett/frontend-maven-plugin/blob/master/frontend-maven-plu
### Installing node and corepack

You can choose to let corepack manage the package manager version in use. Node is
downloaded from `https://nodejs.org/dist`, and corepack currently comes from
`https://repository.npmjs.org`, extracted and put into a `node` folder created
in your installation directory.
downloaded from `https://nodejs.org/dist`, and corepack either comes provided with
Node, or will currently be downloaded from `https://repository.npmjs.org`, extracted
and put into a `node` folder created in your installation directory.

Node/corepack and any package managers will only be "installed" locally to your project.
It will not be installed globally on the whole system (and it will not interfere with any
Node/corepack installations already present).

Have a look at the example `POM` to see how it should be set up with corepack:
https://github.com/eirslett/frontend-maven-plugin/blob/master/frontend-maven-plugin/src/it/corepack-provided-integration/pom.xml
or
https://github.com/eirslett/frontend-maven-plugin/blob/master/frontend-maven-plugin/src/it/corepack-integration/pom.xml
if you need to override the version of corepack in use.


```xml
Expand All @@ -202,6 +205,9 @@ https://github.com/eirslett/frontend-maven-plugin/blob/master/frontend-maven-plu
</execution>
<configuration>
<nodeVersion>v20.12.2</nodeVersion>

<!-- Optional - only needed if Node <16.9, or if you need to use a version different
from the one packaged with Node -->
<corepackVersion>v0.25.2</corepackVersion>

<!-- optional: where to download node from. Defaults to https://nodejs.org/dist/ -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
enableGlobalCache: false

nodeLinker: node-modules
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "example",
"version": "0.0.1",
"dependencies": {
"less": "~3.0.2"
},
"packageManager": "yarn@4.1.1"
}
48 changes: 48 additions & 0 deletions frontend-maven-plugin/src/it/corepack-provided-integration/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?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 corepack</id>
<goals>
<goal>install-node-and-corepack</goal>
</goals>
<configuration>
<nodeVersion>v20.12.2</nodeVersion>
</configuration>
</execution>

<execution>
<id>yarn install</id>
<goals>
<goal>corepack</goal>
</goals>
<configuration>
<arguments>yarn install --no-immutable</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 @@ -3,6 +3,7 @@
import com.github.eirslett.maven.plugins.frontend.lib.CorepackInstaller;
import com.github.eirslett.maven.plugins.frontend.lib.FrontendPluginFactory;
import com.github.eirslett.maven.plugins.frontend.lib.InstallationException;
import com.github.eirslett.maven.plugins.frontend.lib.NodeInstaller;
import com.github.eirslett.maven.plugins.frontend.lib.ProxyConfig;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugins.annotations.Component;
Expand Down Expand Up @@ -36,8 +37,10 @@ public final class InstallNodeAndCorepackMojo extends AbstractFrontendMojo {
/**
* The version of corepack to install. Note that the version string can optionally be prefixed with
* 'v' (i.e., both 'v1.2.3' and '1.2.3' are valid).
*
* If not provided, then the corepack version bundled with Node will be used.
*/
@Parameter(property = "corepackVersion", required = true)
@Parameter(property = "corepackVersion", required = false, defaultValue = "provided")
private String corepackVersion;

/**
Expand Down Expand Up @@ -68,30 +71,30 @@ public void execute(FrontendPluginFactory factory) throws InstallationException
ProxyConfig proxyConfig = MojoUtils.getProxyConfig(session, decrypter);
String resolvedNodeDownloadRoot = getNodeDownloadRoot();
String resolvedCorepackDownloadRoot = getCorepackDownloadRoot();

// Setup the installers
NodeInstaller nodeInstaller = factory.getNodeInstaller(proxyConfig);
nodeInstaller.setNodeVersion(nodeVersion)
.setNodeDownloadRoot(resolvedNodeDownloadRoot);
if ("provided".equals(corepackVersion)) {
// This causes the node installer to copy over the whole
// node_modules directory including the corepack module
nodeInstaller.setNpmVersion("provided");
}
CorepackInstaller corepackInstaller = factory.getCorepackInstaller(proxyConfig);
corepackInstaller.setCorepackVersion(corepackVersion)
.setCorepackDownloadRoot(resolvedCorepackDownloadRoot);

// If pplicable, configure authentication details
Server server = MojoUtils.decryptServer(serverId, session, decrypter);
if (null != server) {
factory.getNodeInstaller(proxyConfig)
.setNodeVersion(nodeVersion)
.setNodeDownloadRoot(resolvedNodeDownloadRoot)
.setUserName(server.getUsername())
.setPassword(server.getPassword())
.install();
factory.getCorepackInstaller(proxyConfig)
.setCorepackVersion(corepackVersion)
.setCorepackDownloadRoot(resolvedCorepackDownloadRoot)
.setUserName(server.getUsername())
.setPassword(server.getPassword())
.install();
} else {
factory.getNodeInstaller(proxyConfig)
.setNodeVersion(nodeVersion)
.setNodeDownloadRoot(resolvedNodeDownloadRoot)
.install();
factory.getCorepackInstaller(proxyConfig)
.setCorepackVersion(this.corepackVersion)
.setCorepackDownloadRoot(resolvedCorepackDownloadRoot)
.install();
nodeInstaller.setUserName(server.getUsername()).setPassword(server.getPassword());
corepackInstaller.setUserName(server.getUsername()).setPassword(server.getPassword());
}

// Perform the installation
nodeInstaller.install();
corepackInstaller.install();
}

private String getNodeDownloadRoot() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ private boolean corepackIsAlreadyInstalled() {
final File corepackPackageJson = new File(
this.config.getInstallDirectory() + Utils.normalize("/node/node_modules/corepack/package.json"));
if (corepackPackageJson.exists()) {
if ("provided".equals(this.corepackVersion)) {
// Since we don't know which version it should be, we must assume that we have
// correctly setup the packaged version
return true;
}
HashMap<String, Object> data = new ObjectMapper().readValue(corepackPackageJson, HashMap.class);
if (data.containsKey(VERSION)) {
final String foundCorepackVersion = data.get(VERSION).toString();
Expand Down

0 comments on commit 8c26a6d

Please sign in to comment.