From 8d90da8d2803f7151c9d9407e43bc6bb3e0b2e16 Mon Sep 17 00:00:00 2001 From: flipatlas <162554003+flipatlas@users.noreply.github.com> Date: Mon, 3 Feb 2025 12:32:39 +0100 Subject: [PATCH] Issue/master/dca11 y 1145 node version manager support windows (#21) * add fnm test for windows * add windows fnm support --- .../node-version-manager/windows/fnm/.nvmrc | 1 + .../windows/fnm/install-fnm.ps1 | 25 ++++++++++ .../windows/fnm/invoker.properties | 3 ++ .../windows/fnm/package-lock.json | 27 +++++++++++ .../windows/fnm/package.json | 10 ++++ .../node-version-manager/windows/fnm/pom.xml | 43 +++++++++++++++++ .../windows/fnm/prebuild.groovy | 2 + .../windows/fnm/verify.groovy | 7 +++ .../windows/invoker.properties | 2 + .../lib/version/manager/client/FnmClient.java | 46 +++++++++++++------ 10 files changed, 152 insertions(+), 14 deletions(-) create mode 100644 frontend-maven-plugin/src/it/node-version-manager/windows/fnm/.nvmrc create mode 100755 frontend-maven-plugin/src/it/node-version-manager/windows/fnm/install-fnm.ps1 create mode 100644 frontend-maven-plugin/src/it/node-version-manager/windows/fnm/invoker.properties create mode 100644 frontend-maven-plugin/src/it/node-version-manager/windows/fnm/package-lock.json create mode 100644 frontend-maven-plugin/src/it/node-version-manager/windows/fnm/package.json create mode 100644 frontend-maven-plugin/src/it/node-version-manager/windows/fnm/pom.xml create mode 100644 frontend-maven-plugin/src/it/node-version-manager/windows/fnm/prebuild.groovy create mode 100644 frontend-maven-plugin/src/it/node-version-manager/windows/fnm/verify.groovy create mode 100644 frontend-maven-plugin/src/it/node-version-manager/windows/invoker.properties diff --git a/frontend-maven-plugin/src/it/node-version-manager/windows/fnm/.nvmrc b/frontend-maven-plugin/src/it/node-version-manager/windows/fnm/.nvmrc new file mode 100644 index 000000000..119f15a0a --- /dev/null +++ b/frontend-maven-plugin/src/it/node-version-manager/windows/fnm/.nvmrc @@ -0,0 +1 @@ +20.15.1 \ No newline at end of file diff --git a/frontend-maven-plugin/src/it/node-version-manager/windows/fnm/install-fnm.ps1 b/frontend-maven-plugin/src/it/node-version-manager/windows/fnm/install-fnm.ps1 new file mode 100755 index 000000000..8b87069b2 --- /dev/null +++ b/frontend-maven-plugin/src/it/node-version-manager/windows/fnm/install-fnm.ps1 @@ -0,0 +1,25 @@ +# Set the HOME directory to the directory of the script +$WORKING_DIR = Split-Path -Parent $MyInvocation.MyCommand.Definition + +Write-Output $WORKING_DIR + +# Set the FNM_DIR variable +$FNM_DIR = Join-Path -Path $WORKING_DIR -ChildPath ".fnm" + +# Create the directory if it does not exist +if (-Not (Test-Path -Path $FNM_DIR)) { + New-Item -ItemType Directory -Path $FNM_DIR | Out-Null +} + +# Download and execute the FNM install script +$zipUrl = "https://github.com/Schniz/fnm/releases/download/v1.38.1/fnm-windows.zip" +$zipFilePath = "$FNM_DIR\installer.zip" +Invoke-WebRequest -Uri $zipUrl -OutFile $zipFilePath + +$destinationPath = $FNM_DIR +Expand-Archive -Path $zipFilePath -DestinationPath $destinationPath -Force + +# Load the FNM environment and install Node.js version 20.15.1 +Invoke-Expression "$FNM_DIR\fnm.exe install 20.15.1 --fnm-dir $FNM_DIR" + +Write-Output "Finished FNM installation" \ No newline at end of file diff --git a/frontend-maven-plugin/src/it/node-version-manager/windows/fnm/invoker.properties b/frontend-maven-plugin/src/it/node-version-manager/windows/fnm/invoker.properties new file mode 100644 index 000000000..56f6851d9 --- /dev/null +++ b/frontend-maven-plugin/src/it/node-version-manager/windows/fnm/invoker.properties @@ -0,0 +1,3 @@ +invoker.environmentVariables.HOME=${basedir}/target/it/node-version-manager/windows/fnm +invoker.environmentVariables.FNM_DIR=${basedir}/target/it/node-version-manager/windows/fnm/.fnm +invoker.environmentVariables.XDG_DATA_HOME=${basedir}/target/it/node-version-manager/windows/fnm \ No newline at end of file diff --git a/frontend-maven-plugin/src/it/node-version-manager/windows/fnm/package-lock.json b/frontend-maven-plugin/src/it/node-version-manager/windows/fnm/package-lock.json new file mode 100644 index 000000000..1a8b7d458 --- /dev/null +++ b/frontend-maven-plugin/src/it/node-version-manager/windows/fnm/package-lock.json @@ -0,0 +1,27 @@ +{ + "name": "example", + "version": "0.0.1", + "lockfileVersion": 2, + "requires": true, + "packages": { + "": { + "name": "example", + "version": "0.0.1", + "dependencies": { + "classnames": "^2.3.2" + } + }, + "node_modules/classnames": { + "version": "2.3.2", + "resolved": "https://registry.npmjs.org/classnames/-/classnames-2.3.2.tgz", + "integrity": "sha512-CSbhY4cFEJRe6/GQzIk5qXZ4Jeg5pcsP7b5peFSDpffpe1cqjASH/n9UTjBwOp6XpMSTwQ8Za2K5V02ueA7Tmw==" + } + }, + "dependencies": { + "classnames": { + "version": "2.3.2", + "resolved": "https://registry.npmjs.org/classnames/-/classnames-2.3.2.tgz", + "integrity": "sha512-CSbhY4cFEJRe6/GQzIk5qXZ4Jeg5pcsP7b5peFSDpffpe1cqjASH/n9UTjBwOp6XpMSTwQ8Za2K5V02ueA7Tmw==" + } + } +} diff --git a/frontend-maven-plugin/src/it/node-version-manager/windows/fnm/package.json b/frontend-maven-plugin/src/it/node-version-manager/windows/fnm/package.json new file mode 100644 index 000000000..e36bdbfca --- /dev/null +++ b/frontend-maven-plugin/src/it/node-version-manager/windows/fnm/package.json @@ -0,0 +1,10 @@ +{ + "name": "example", + "version": "0.0.1", + "dependencies": { + "classnames": "^2.3.2" + }, + "scripts": { + "prebuild": "npm install" + } +} diff --git a/frontend-maven-plugin/src/it/node-version-manager/windows/fnm/pom.xml b/frontend-maven-plugin/src/it/node-version-manager/windows/fnm/pom.xml new file mode 100644 index 000000000..9304df9e6 --- /dev/null +++ b/frontend-maven-plugin/src/it/node-version-manager/windows/fnm/pom.xml @@ -0,0 +1,43 @@ + + + 4.0.0 + + com.github.eirslett + fnm-with-npm + 0 + pom + + + + + com.github.eirslett + frontend-maven-plugin + + @project.version@ + + + ${basedir} + + + + + install node + + install-node-and-npm + + + + npm ci + + npm + + + + ci + + + + + + + diff --git a/frontend-maven-plugin/src/it/node-version-manager/windows/fnm/prebuild.groovy b/frontend-maven-plugin/src/it/node-version-manager/windows/fnm/prebuild.groovy new file mode 100644 index 000000000..a0316adc2 --- /dev/null +++ b/frontend-maven-plugin/src/it/node-version-manager/windows/fnm/prebuild.groovy @@ -0,0 +1,2 @@ +def p = "powershell $basedir\\install-fnm.ps1".execute() +p.waitForProcessOutput(System.out, System.err) \ No newline at end of file diff --git a/frontend-maven-plugin/src/it/node-version-manager/windows/fnm/verify.groovy b/frontend-maven-plugin/src/it/node-version-manager/windows/fnm/verify.groovy new file mode 100644 index 000000000..24b755ebe --- /dev/null +++ b/frontend-maven-plugin/src/it/node-version-manager/windows/fnm/verify.groovy @@ -0,0 +1,7 @@ +import org.codehaus.plexus.util.FileUtils + +String buildLog = FileUtils.fileRead(new File(basedir, 'build.log')) +assert buildLog.contains('Using FNM version manager') : 'Node has been installed with a different version manager' + +assert !new File(basedir, 'node/node').exists() : "Node was installed bypassing version manager" +assert new File(basedir, 'node_modules').exists() : "Node modules were not installed in the base directory" \ No newline at end of file diff --git a/frontend-maven-plugin/src/it/node-version-manager/windows/invoker.properties b/frontend-maven-plugin/src/it/node-version-manager/windows/invoker.properties new file mode 100644 index 000000000..3fbd027d2 --- /dev/null +++ b/frontend-maven-plugin/src/it/node-version-manager/windows/invoker.properties @@ -0,0 +1,2 @@ +# ignoring windows for now +invoker.os.family=windows \ No newline at end of file diff --git a/frontend-plugin-core/src/main/java/com/github/eirslett/maven/plugins/frontend/lib/version/manager/client/FnmClient.java b/frontend-plugin-core/src/main/java/com/github/eirslett/maven/plugins/frontend/lib/version/manager/client/FnmClient.java index f530e32b2..0d84278f0 100644 --- a/frontend-plugin-core/src/main/java/com/github/eirslett/maven/plugins/frontend/lib/version/manager/client/FnmClient.java +++ b/frontend-plugin-core/src/main/java/com/github/eirslett/maven/plugins/frontend/lib/version/manager/client/FnmClient.java @@ -35,22 +35,33 @@ public File getNodeExecutable(String nodeVersion) { String architecture = platform.getArchitectureName(); String osCodename = platform.getCodename(); String nodeOnPlatform = String.format("node-%s-%s-%s", nodeVersionWithV, osCodename, architecture); - Path path = Paths.get(fnmDir, "node-versions", nodeVersionWithV, nodeOnPlatform, "bin", "node"); + if (installConfig.getPlatform().isWindows()) { + return Paths.get(fnmDir, "node-versions", nodeVersionWithV, "installation", "node.exe").toFile(); + } + + Path path = Paths.get(fnmDir, "node-versions", nodeVersionWithV, nodeOnPlatform, "bin", "node"); if (Files.exists(path)) { return path.toFile(); } + return Paths.get(fnmDir, "node-versions", nodeVersionWithV, "installation", "bin", "node").toFile(); } @Override public File getNpmExecutable(String nodeVersion) { File nodeExec = getNodeExecutable(nodeVersion); + + if (installConfig.getPlatform().isWindows()) { + return Paths.get(nodeExec.getParent(), "node_modules", "npm", "bin", "npm-cli.js").toFile(); + } + return new File(nodeExec.getParent(), "npm"); } private String getFnmDir() { String fnmDir = System.getenv("FNM_DIR"); + System.out.println("XXX " + fnmDir); if (fnmDir != null) { Path path = Paths.get(fnmDir); if (Files.exists(path)) { @@ -60,19 +71,26 @@ private String getFnmDir() { String home = System.getenv("HOME"); if (home != null) { - Path path = Paths.get(home, ".fnm"); - if (Files.exists(path)) { - return path.toString(); - } - - path = Paths.get(home, "Library", "Application Support", "fnm"); - if (Files.exists(path)) { - return path.toString(); - } - - path = Paths.get(home, ".local", "share", "fnm"); - if (Files.exists(path)) { - return path.toString(); + if (installConfig.getPlatform().isWindows()) { + Path path = Paths.get(home, "AppData", "Roaming", "fnm"); + if (Files.exists(path)) { + return path.toString(); + } + } else { + Path path = Paths.get(home, ".fnm"); + if (Files.exists(path)) { + return path.toString(); + } + + path = Paths.get(home, "Library", "Application Support", "fnm"); + if (Files.exists(path)) { + return path.toString(); + } + + path = Paths.get(home, ".local", "share", "fnm"); + if (Files.exists(path)) { + return path.toString(); + } } }