Skip to content

Commit

Permalink
Merge pull request #1095 from josehenriqueventura/fix#657_add_proxy_e…
Browse files Browse the repository at this point in the history
…nvironment_variables_for_npm_postinstalls

Fix#657 add proxy environment variables for npm postinstalls
  • Loading branch information
eirslett authored Jul 2, 2023
2 parents 26740c8 + 40b6c1e commit 993bc2e
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
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 +38,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.proxy = proxy;
}

private static String getTaskNameFromLocation(String taskLocation) {
Expand All @@ -55,7 +61,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;
}
}

0 comments on commit 993bc2e

Please sign in to comment.