Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Copying npm scripts for embedded node #868

Merged
merged 1 commit into from Dec 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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