diff --git a/frontend-plugin-core/src/main/java/com/github/eirslett/maven/plugins/frontend/lib/NpxRunner.java b/frontend-plugin-core/src/main/java/com/github/eirslett/maven/plugins/frontend/lib/NpxRunner.java index fb7c0b3e..b4f4db1d 100644 --- a/frontend-plugin-core/src/main/java/com/github/eirslett/maven/plugins/frontend/lib/NpxRunner.java +++ b/frontend-plugin-core/src/main/java/com/github/eirslett/maven/plugins/frontend/lib/NpxRunner.java @@ -11,11 +11,18 @@ final class DefaultNpxRunner extends NodeTaskExecutor implements NpxRunner { static final String TASK_NAME = "npx"; public DefaultNpxRunner(NodeExecutorConfig config, ProxyConfig proxyConfig, String npmRegistryURL) { - super(config, TASK_NAME, config.getNpxPath().getAbsolutePath(), buildArguments(proxyConfig, npmRegistryURL)); + super(config, TASK_NAME, config.getNpxPath().getAbsolutePath(), buildNpmArguments(proxyConfig, npmRegistryURL)); } - private static List buildArguments(ProxyConfig proxyConfig, String npmRegistryURL) { - List arguments = new ArrayList(); + // Visible for testing only. + /** + * These are, in fact, npm arguments, that need to be split from the npx arguments by '--'. + * + * See an example: + * npx some-package -- --registry=http://myspecialregisty.com + */ + static List buildNpmArguments(ProxyConfig proxyConfig, String npmRegistryURL) { + List arguments = new ArrayList<>(); if(npmRegistryURL != null && !npmRegistryURL.isEmpty()){ arguments.add ("--registry=" + npmRegistryURL); @@ -38,7 +45,16 @@ private static List buildArguments(ProxyConfig proxyConfig, String npmRe arguments.add("--https-proxy=" + proxy.getUri().toString()); arguments.add("--proxy=" + proxy.getUri().toString()); } + + List npmArguments; + if (arguments.isEmpty()) { + npmArguments = arguments; + } else { + npmArguments = new ArrayList<>(); + npmArguments.add("--"); + npmArguments.addAll(arguments); + } - return arguments; + return npmArguments; } } diff --git a/frontend-plugin-core/src/test/java/com/github/eirslett/maven/plugins/frontend/lib/DefaultNpxRunnerTest.java b/frontend-plugin-core/src/test/java/com/github/eirslett/maven/plugins/frontend/lib/DefaultNpxRunnerTest.java new file mode 100644 index 00000000..6d5449c1 --- /dev/null +++ b/frontend-plugin-core/src/test/java/com/github/eirslett/maven/plugins/frontend/lib/DefaultNpxRunnerTest.java @@ -0,0 +1,28 @@ +package com.github.eirslett.maven.plugins.frontend.lib; + +import static org.junit.Assert.assertThat; + +import java.util.Collections; +import java.util.List; + +import org.hamcrest.CoreMatchers; +import org.junit.Assert; +import org.junit.Test; + +public class DefaultNpxRunnerTest { + + private final String registryUrl = "www.npm.org"; + + @Test + public void buildArgument_basicTest() { + List arguments = DefaultNpxRunner.buildNpmArguments(new ProxyConfig(Collections.emptyList()), null); + Assert.assertEquals(0, arguments.size()); + } + + @Test + public void buildArgument_withRegistryUrl() { + List arguments = DefaultNpxRunner.buildNpmArguments(new ProxyConfig(Collections.emptyList()), registryUrl); + Assert.assertEquals(2, arguments.size()); + assertThat(arguments, CoreMatchers.hasItems("--", "--registry=" + registryUrl)); + } +}