Skip to content

Commit

Permalink
configure installed node directory
Browse files Browse the repository at this point in the history
  • Loading branch information
flipatlas committed Dec 9, 2024
1 parent c965cd2 commit c181728
Show file tree
Hide file tree
Showing 24 changed files with 305 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# ignoring windows for now
invoker.os.family=!windows,unix,mac
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v20.15.1
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/bash

# invoker env variables are not loaded yet
export HOME="$(dirname "$0")"

export NVM_DIR="$HOME/.nvm";
mkdir "$NVM_DIR"
echo "NVM_DIR set";

(
git clone https://github.com/nvm-sh/nvm.git "$NVM_DIR" &&
cd "$NVM_DIR" &&
git checkout `git describe --abbrev=0 --tags --match "v[0-9]*" $(git rev-list --tags --max-count=1)`
);
echo "NVM checked out";

. "$NVM_DIR/nvm.sh" \
&& nvm install v20.15.1
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
invoker.environmentVariables.HOME=${basedir}/target/it/provided-node-dir/with-env-var
invoker.environmentVariables.AFMP_INSTALLED_NODE_DIRECTORY=${basedir}/target/it/provided-node-dir/with-env-var/.nvm/versions/node/v20.15.1/bin

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "example",
"version": "0.0.1",
"dependencies": {
"classnames": "^2.3.2"
},
"scripts": {
"prebuild": "npm install"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?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>
<workingDirectory>${basedir}</workingDirectory>
<!-- FMP should warn about version mismatch between provided node and requested node version -->
<nodeVersion>v20.16.0</nodeVersion>
</configuration>

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

</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def p = "bash $basedir/install-nvm.sh".execute()
p.waitForProcessOutput(System.out, System.err)
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import org.codehaus.plexus.util.FileUtils

// assert
assert !new File(basedir, 'node/node').exists() : "Node was installed in the custom install directory"
assert new File(basedir, 'node_modules').exists() : "Node modules were not installed in the base directory"

String buildLog = FileUtils.fileRead(new File(basedir, 'build.log'))
assert !buildLog.contains('Using NVM') : 'Node has been installed with a version manager but should use provided node'
assert buildLog.contains('Provided node executable has version v20.15.1, but v20.16.0 was requested in configuration.') : 'FMP should warn about version mismatch'
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v20.15.1
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/bash

# invoker env variables are not loaded yet
export HOME="$(dirname "$0")"

export NVM_DIR="$HOME/.nvm";
mkdir "$NVM_DIR"
echo "NVM_DIR set";

(
git clone https://github.com/nvm-sh/nvm.git "$NVM_DIR" &&
cd "$NVM_DIR" &&
git checkout `git describe --abbrev=0 --tags --match "v[0-9]*" $(git rev-list --tags --max-count=1)`
);
echo "NVM checked out";

. "$NVM_DIR/nvm.sh" \
&& nvm install v20.15.1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
invoker.environmentVariables.HOME=${basedir}/target/it/provided-node-dir/with-property

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "example",
"version": "0.0.1",
"dependencies": {
"classnames": "^2.3.2"
},
"scripts": {
"prebuild": "npm install"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?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>
<workingDirectory>${basedir}</workingDirectory>
<installedNodeDirectory>${basedir}/.nvm/versions/node/v20.15.1/bin</installedNodeDirectory>
<!-- FMP should warn about version mismatch between provided node and requested node version -->
<nodeVersion>v20.16.0</nodeVersion>
</configuration>

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

</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def p = "bash $basedir/install-nvm.sh".execute()
p.waitForProcessOutput(System.out, System.err)
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import org.codehaus.plexus.util.FileUtils

// assert
assert !new File(basedir, 'node/node').exists() : "Node was installed in the custom install directory"
assert new File(basedir, 'node_modules').exists() : "Node modules were not installed in the base directory"

String buildLog = FileUtils.fileRead(new File(basedir, 'build.log'))
assert !buildLog.contains('Using NVM') : 'Node has been installed with a version manager but should use provided node'
assert buildLog.contains('Provided node executable has version v20.15.1, but v20.16.0 was requested in configuration.') : 'FMP should warn about version mismatch'
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ public abstract class AbstractFrontendMojo extends AbstractMojo {
@Parameter(property = "useNodeVersionManager", defaultValue = "true")
private boolean useNodeVersionManager;

/**
* Directory with an existing installation of node
* Also can be provided as environment variable AFMP_INSTALLED_NODE_DIRECTORY
*/
@Parameter(property = "installedNodeDirectory")
private File installedNodeDirectory;

/**
* Determines if this execution should be skipped.
*/
Expand Down Expand Up @@ -103,8 +110,10 @@ public void execute() throws MojoFailureException {
installDirectory = workingDirectory;
}
try {
execute(new FrontendPluginFactory(workingDirectory, installDirectory,
new RepositoryCacheResolver(repositorySystemSession), useNodeVersionManager));
execute(new FrontendPluginFactory(
workingDirectory, installDirectory,
new RepositoryCacheResolver(repositorySystemSession),
useNodeVersionManager, installedNodeDirectory));
} catch (TaskRunnerException e) {
if (testFailureIgnore && isTestingPhase()) {
getLog().error("There are test failures.\nFailed to run task: " + e.getMessage(), e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,18 @@ public final class FrontendPluginFactory {
private final File installDirectory;
private final CacheResolver cacheResolver;
private final boolean useNodeVersionManager;
private String nodeVersion;
private final File installedNodeDirectory;

public FrontendPluginFactory(File workingDirectory, File installDirectory){
this(workingDirectory, installDirectory, getDefaultCacheResolver(installDirectory), false);
this(workingDirectory, installDirectory, getDefaultCacheResolver(installDirectory), false, null);
}

public FrontendPluginFactory(File workingDirectory, File installDirectory, CacheResolver cacheResolver, boolean useNodeVersionManager){
public FrontendPluginFactory(File workingDirectory, File installDirectory, CacheResolver cacheResolver, boolean useNodeVersionManager, File installedNodeDirectory){
this.workingDirectory = workingDirectory;
this.installDirectory = installDirectory;
this.cacheResolver = cacheResolver;
this.useNodeVersionManager = useNodeVersionManager;
this.installedNodeDirectory = installedNodeDirectory;

initializeGlobalCache();
}
Expand Down Expand Up @@ -133,7 +134,7 @@ private static CacheResolver getDefaultCacheResolver(File root) {
}

private void initializeGlobalCache() {
InstallConfig installConfig = new DefaultInstallConfig(installDirectory, workingDirectory, cacheResolver, defaultPlatform, useNodeVersionManager);
InstallConfig installConfig = new DefaultInstallConfig(installDirectory, workingDirectory, cacheResolver, defaultPlatform, useNodeVersionManager, installedNodeDirectory);
GlobalCache.setInstallConfig(installConfig);

if (installConfig.isUseNodeVersionManager()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public interface InstallConfig {
CacheResolver getCacheResolver();
Platform getPlatform();
boolean isUseNodeVersionManager();
File getInstalledNodeDirectory();
}

final class DefaultInstallConfig implements InstallConfig {
Expand All @@ -18,17 +19,20 @@ final class DefaultInstallConfig implements InstallConfig {
private final CacheResolver cacheResolver;
private final Platform platform;
private final boolean useNodeVersionManager;
private final File installedNodeDirectory;

public DefaultInstallConfig(File installDirectory,
File workingDirectory,
CacheResolver cacheResolver,
Platform platform,
boolean useNodeVersionManager) {
boolean useNodeVersionManager,
File installedNodeDirectory) {
this.installDirectory = installDirectory;
this.workingDirectory = workingDirectory;
this.cacheResolver = cacheResolver;
this.platform = platform;
this.useNodeVersionManager = useNodeVersionManager;
this.installedNodeDirectory = installedNodeDirectory;
}

@Override
Expand Down Expand Up @@ -59,4 +63,9 @@ public Platform getPlatform() {
public boolean isUseNodeVersionManager() {
return useNodeVersionManager;
}

@Override
public File getInstalledNodeDirectory() {
return this.installedNodeDirectory;
}
}
Loading

0 comments on commit c181728

Please sign in to comment.