From 219fb895f7138d1a72e6ae81f83ebd91a31b6eb3 Mon Sep 17 00:00:00 2001 From: Mark Watts Date: Sat, 21 Dec 2019 14:55:06 -0600 Subject: [PATCH] Copying npm scripts for embedded node - Modifying the node-provided-npm to check for npm binary - Resolves #860, Resolves #458 --- .../src/it/node-provided-npm/package.json | 3 ++ .../src/it/node-provided-npm/verify.groovy | 1 + .../plugins/frontend/lib/NPMInstaller.java | 36 +++++++++++++------ 3 files changed, 30 insertions(+), 10 deletions(-) diff --git a/frontend-maven-plugin/src/it/node-provided-npm/package.json b/frontend-maven-plugin/src/it/node-provided-npm/package.json index cfe09957b..e3d588d64 100644 --- a/frontend-maven-plugin/src/it/node-provided-npm/package.json +++ b/frontend-maven-plugin/src/it/node-provided-npm/package.json @@ -3,5 +3,8 @@ "version": "0.0.1", "dependencies": { "less": "~3.0.2" + }, + "scripts": { + "prebuild": "npm install" } } diff --git a/frontend-maven-plugin/src/it/node-provided-npm/verify.groovy b/frontend-maven-plugin/src/it/node-provided-npm/verify.groovy index 81247d7d6..0a9d23ea4 100644 --- a/frontend-maven-plugin/src/it/node-provided-npm/verify.groovy +++ b/frontend-maven-plugin/src/it/node-provided-npm/verify.groovy @@ -1,5 +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, 'target/node/npm').exists() : "npm was not copied to the node directory"; import org.codehaus.plexus.util.FileUtils; diff --git a/frontend-plugin-core/src/main/java/com/github/eirslett/maven/plugins/frontend/lib/NPMInstaller.java b/frontend-plugin-core/src/main/java/com/github/eirslett/maven/plugins/frontend/lib/NPMInstaller.java index d9c517e60..4b5a1e452 100644 --- a/frontend-plugin-core/src/main/java/com/github/eirslett/maven/plugins/frontend/lib/NPMInstaller.java +++ b/frontend-plugin-core/src/main/java/com/github/eirslett/maven/plugins/frontend/lib/NPMInstaller.java @@ -82,6 +82,7 @@ public void install() throws InstallationException { if (!npmProvided() && !npmIsAlreadyInstalled()) { installNpm(); } + copyNpmScripts(); } } @@ -169,16 +170,6 @@ private void installNpm() throws InstallationException { } } - // create a copy of the npm scripts next to the node executable - for (String script : Arrays.asList("npm", "npm.cmd")) { - File scriptFile = new File(npmDirectory, "bin" + File.separator + script); - if (scriptFile.exists()) { - File copy = new File(installDirectory, script); - FileUtils.copyFile(scriptFile, copy); - copy.setExecutable(true); - } - } - this.logger.info("Installed npm locally."); } catch (DownloadException e) { throw new InstallationException("Could not download npm", e); @@ -189,6 +180,31 @@ private void installNpm() throws InstallationException { } } + private void copyNpmScripts() throws InstallationException{ + File installDirectory = getNodeInstallDirectory(); + + File nodeModulesDirectory = new File(installDirectory, "node_modules"); + File npmDirectory = new File(nodeModulesDirectory, "npm"); + // create a copy of the npm scripts next to the node executable + for (String script : Arrays.asList("npm", "npm.cmd")) { + File scriptFile = new File(npmDirectory, "bin" + File.separator + script); + if (scriptFile.exists()) { + File copy = new File(installDirectory, script); + if (!copy.exists()) { + try + { + FileUtils.copyFile(scriptFile, copy); + } + catch (IOException e) + { + throw new InstallationException("Could not copy npm", e); + } + copy.setExecutable(true); + } + } + } + } + private File getNodeInstallDirectory() { File installDirectory = new File(this.config.getInstallDirectory(), NodeInstaller.INSTALL_PATH); if (!installDirectory.exists()) {