Skip to content

Commit

Permalink
Issue-1005 split off npm arguments for npx run
Browse files Browse the repository at this point in the history
  • Loading branch information
rsynek committed Nov 23, 2021
1 parent 7625e2d commit 0e2b66d
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> buildArguments(ProxyConfig proxyConfig, String npmRegistryURL) {
List<String> arguments = new ArrayList<String>();
// 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<String> buildNpmArguments(ProxyConfig proxyConfig, String npmRegistryURL) {
List<String> arguments = new ArrayList<>();

if(npmRegistryURL != null && !npmRegistryURL.isEmpty()){
arguments.add ("--registry=" + npmRegistryURL);
Expand All @@ -38,7 +45,16 @@ private static List<String> buildArguments(ProxyConfig proxyConfig, String npmRe
arguments.add("--https-proxy=" + proxy.getUri().toString());
arguments.add("--proxy=" + proxy.getUri().toString());
}

List<String> npmArguments;
if (arguments.isEmpty()) {
npmArguments = arguments;
} else {
npmArguments = new ArrayList<>();
npmArguments.add("--");
npmArguments.addAll(arguments);
}

return arguments;
return npmArguments;
}
}
Original file line number Diff line number Diff line change
@@ -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<String> arguments = DefaultNpxRunner.buildNpmArguments(new ProxyConfig(Collections.emptyList()), null);
Assert.assertEquals(0, arguments.size());
}

@Test
public void buildArgument_withRegistryUrl() {
List<String> arguments = DefaultNpxRunner.buildNpmArguments(new ProxyConfig(Collections.emptyList()), registryUrl);
Assert.assertEquals(2, arguments.size());
assertThat(arguments, CoreMatchers.hasItems("--", "--registry=" + registryUrl));
}
}

0 comments on commit 0e2b66d

Please sign in to comment.