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

Add proxy environment variables for npm postinstalls #683

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
package com.github.eirslett.maven.plugins.frontend.lib;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static com.github.eirslett.maven.plugins.frontend.lib.Utils.implode;
import static com.github.eirslett.maven.plugins.frontend.lib.Utils.normalize;
import static com.github.eirslett.maven.plugins.frontend.lib.Utils.prepend;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

import static com.github.eirslett.maven.plugins.frontend.lib.Utils.implode;
import static com.github.eirslett.maven.plugins.frontend.lib.Utils.normalize;
import static com.github.eirslett.maven.plugins.frontend.lib.Utils.prepend;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

abstract class NodeTaskExecutor {
private static final String DS = "//";
private static final String AT = "@";

private final Logger logger;
private final String taskName;
private String taskLocation;
private final ArgumentsParser argumentsParser;
private final NodeExecutorConfig config;
private final Map<String, String> proxy;

public NodeTaskExecutor(NodeExecutorConfig config, String taskLocation) {
this(config, taskLocation, Collections.<String>emptyList());
Expand All @@ -37,11 +39,16 @@ public NodeTaskExecutor(NodeExecutorConfig config, String taskLocation, List<Str
}

public NodeTaskExecutor(NodeExecutorConfig config, String taskName, String taskLocation, List<String> additionalArguments) {
this(config, taskName, taskLocation, additionalArguments, Collections.<String, String>emptyMap());
}

public NodeTaskExecutor(NodeExecutorConfig config, String taskName, String taskLocation, List<String> additionalArguments, Map<String, String> proxy) {
this.logger = LoggerFactory.getLogger(getClass());
this.config = config;
this.taskName = taskName;
this.taskLocation = taskLocation;
this.argumentsParser = new ArgumentsParser(additionalArguments);
this.additionalArguments = additionalArguments;
this.proxy = proxy;
}

private static String getTaskNameFromLocation(String taskLocation) {
Expand All @@ -55,7 +62,14 @@ public final void execute(String args, Map<String, String> environment) throws T
logger.info("Running " + taskToString(taskName, arguments) + " in " + config.getWorkingDirectory());

try {
final int result = new NodeExecutor(config, prepend(absoluteTaskLocation, arguments), environment).executeAndRedirectOutput(logger);
Map<String, String> internalEnvironment = new HashMap<>();
if (environment != null && !environment.isEmpty()) {
internalEnvironment.putAll(environment);
}
if (!proxy.isEmpty()) {
internalEnvironment.putAll(proxy);
}
final int result = new NodeExecutor(config, prepend(absoluteTaskLocation, arguments), internalEnvironment ).executeAndRedirectOutput(logger);
if (result != 0) {
throw new TaskRunnerException(taskToString(taskName, arguments) + " failed. (error code " + result + ")");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,40 +1,33 @@
package com.github.eirslett.maven.plugins.frontend.lib;

import com.github.eirslett.maven.plugins.frontend.lib.ProxyConfig.Proxy;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.github.eirslett.maven.plugins.frontend.lib.ProxyConfig.Proxy;

public interface NpmRunner extends NodeTaskRunner {}

final class DefaultNpmRunner extends NodeTaskExecutor implements NpmRunner {
static final String TASK_NAME = "npm";

public DefaultNpmRunner(NodeExecutorConfig config, ProxyConfig proxyConfig, String npmRegistryURL) {
super(config, TASK_NAME, config.getNpmPath().getAbsolutePath(), buildArguments(proxyConfig, npmRegistryURL));
super(config, TASK_NAME, config.getNpmPath().getAbsolutePath(), buildArguments(proxyConfig, npmRegistryURL),
buildProxy(proxyConfig, npmRegistryURL));
}

// Visible for testing only.
static List<String> buildArguments(ProxyConfig proxyConfig, String npmRegistryURL) {
List<String> arguments = new ArrayList<String>();

if(npmRegistryURL != null && !npmRegistryURL.isEmpty()){
arguments.add ("--registry=" + npmRegistryURL);
}

if(!proxyConfig.isEmpty()){
Proxy proxy = null;
if(npmRegistryURL != null && !npmRegistryURL.isEmpty()){
proxy = proxyConfig.getProxyForUrl(npmRegistryURL);
}

if(proxy == null){
proxy = proxyConfig.getSecureProxy();
}

if(proxy == null){
proxy = proxyConfig.getInsecureProxy();
}
Proxy proxy = getProxyConfig(proxyConfig, npmRegistryURL);

arguments.add("--https-proxy=" + proxy.getUri().toString());
arguments.add("--proxy=" + proxy.getUri().toString());
Expand All @@ -47,7 +40,37 @@ static List<String> buildArguments(ProxyConfig proxyConfig, String npmRegistryUR
}
}
}

return arguments;
}

private static Map<String, String> buildProxy(ProxyConfig proxyConfig, String npmRegistryURL) {
Map<String, String> proxyEnvironmentVariables = Collections.emptyMap();

if(!proxyConfig.isEmpty()){
Proxy proxy = getProxyConfig(proxyConfig, npmRegistryURL);
proxyEnvironmentVariables = new HashMap<>();

proxyEnvironmentVariables.put("https_proxy", proxy.getUri().toString());
proxyEnvironmentVariables.put("http_proxy", proxy.getUri().toString());
}

return proxyEnvironmentVariables;
}

private static Proxy getProxyConfig(ProxyConfig proxyConfig, String npmRegistryURL) {
Proxy proxy = null;
if(npmRegistryURL != null && !npmRegistryURL.isEmpty()){
proxy = proxyConfig.getProxyForUrl(npmRegistryURL);
}

if(proxy == null){
proxy = proxyConfig.getSecureProxy();
}

if(proxy == null){
proxy = proxyConfig.getInsecureProxy();
}
return proxy;
}
}