Skip to content

Commit

Permalink
Merge pull request #868 from mwatts15/master
Browse files Browse the repository at this point in the history
Copying npm scripts for embedded node
  • Loading branch information
eirslett committed Dec 23, 2019
2 parents 381357d + 219fb89 commit 6ea943f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
3 changes: 3 additions & 0 deletions frontend-maven-plugin/src/it/node-provided-npm/package.json
Expand Up @@ -3,5 +3,8 @@
"version": "0.0.1",
"dependencies": {
"less": "~3.0.2"
},
"scripts": {
"prebuild": "npm install"
}
}
@@ -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;

Expand Down
Expand Up @@ -82,6 +82,7 @@ public void install() throws InstallationException {
if (!npmProvided() && !npmIsAlreadyInstalled()) {
installNpm();
}
copyNpmScripts();
}
}

Expand Down Expand Up @@ -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);
Expand All @@ -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()) {
Expand Down

0 comments on commit 6ea943f

Please sign in to comment.