From e742a26c864652735ca7dfd85edc73c0ce59fa1d Mon Sep 17 00:00:00 2001 From: Teemu Matilainen Date: Mon, 17 Feb 2014 23:40:41 -0300 Subject: [PATCH] Add support for npm proxy Configure npm to use proxies if it is installed. Hook also after each provisioner run. Fixes #50 --- README.md | 1 + lib/vagrant-proxyconf/action.rb | 3 ++ .../action/configure_npm_proxy.rb | 36 +++++++++++++++++++ .../cap/linux/npm_proxy_conf.rb | 15 ++++++++ lib/vagrant-proxyconf/capability.rb | 5 +++ locales/en.yml | 8 +++++ .../action/configure_npm_proxy_spec.rb | 10 ++++++ .../cap/linux/npm_proxy_conf_spec.rb | 25 +++++++++++++ 8 files changed, 103 insertions(+) create mode 100644 lib/vagrant-proxyconf/action/configure_npm_proxy.rb create mode 100644 lib/vagrant-proxyconf/cap/linux/npm_proxy_conf.rb create mode 100644 spec/unit/vagrant-proxyconf/action/configure_npm_proxy_spec.rb create mode 100644 spec/unit/vagrant-proxyconf/cap/linux/npm_proxy_conf_spec.rb diff --git a/README.md b/README.md index 4ee5d73..8c8ac6d 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,7 @@ The plugin can set: * generic `http_proxy` etc. environment variables that many programs support * default proxy configuration for all Chef provisioners * proxy configuration for Apt +* proxy configuration for npm * proxy configuration for Yum * proxy configuration for PEAR diff --git a/lib/vagrant-proxyconf/action.rb b/lib/vagrant-proxyconf/action.rb index 4d97495..72ab287 100644 --- a/lib/vagrant-proxyconf/action.rb +++ b/lib/vagrant-proxyconf/action.rb @@ -3,6 +3,7 @@ require_relative 'action/configure_chef_proxy' require_relative 'action/configure_env_proxy' require_relative 'action/configure_git_proxy' +require_relative 'action/configure_npm_proxy' require_relative 'action/configure_pear_proxy' require_relative 'action/configure_svn_proxy' require_relative 'action/configure_yum_proxy' @@ -32,6 +33,7 @@ def self.configure_after_provisoner next if !env[:result] b2.use ConfigureGitProxy + b2.use ConfigureNpmProxy b2.use ConfigurePearProxy b2.use ConfigureSvnProxy end @@ -51,6 +53,7 @@ def self.config_actions b2.use ConfigureChefProxy b2.use ConfigureEnvProxy b2.use ConfigureGitProxy + b2.use ConfigureNpmProxy b2.use ConfigurePearProxy b2.use ConfigureSvnProxy b2.use ConfigureYumProxy diff --git a/lib/vagrant-proxyconf/action/configure_npm_proxy.rb b/lib/vagrant-proxyconf/action/configure_npm_proxy.rb new file mode 100644 index 0000000..75c8de0 --- /dev/null +++ b/lib/vagrant-proxyconf/action/configure_npm_proxy.rb @@ -0,0 +1,36 @@ +require_relative 'base' + +module VagrantPlugins + module ProxyConf + class Action + # Action for configuring npm on the guest + class ConfigureNpmProxy < Base + def config_name + 'npm_proxy' + end + + private + + # @return [Vagrant::Plugin::V2::Config] the configuration + def config + # Use global proxy config + @config ||= finalize_config(@machine.config.proxy) + end + + def configure_machine + set_or_delete_proxy('proxy', config.http) + set_or_delete_proxy('https-proxy', config.https) + end + + def set_or_delete_proxy(key, value) + if value + command = "npm config set #{key} #{escape(config.http)}" + else + command = "npm config delete #{key}" + end + @machine.communicate.sudo(command) + end + end + end + end +end diff --git a/lib/vagrant-proxyconf/cap/linux/npm_proxy_conf.rb b/lib/vagrant-proxyconf/cap/linux/npm_proxy_conf.rb new file mode 100644 index 0000000..e7e0634 --- /dev/null +++ b/lib/vagrant-proxyconf/cap/linux/npm_proxy_conf.rb @@ -0,0 +1,15 @@ +module VagrantPlugins + module ProxyConf + module Cap + module Linux + # Capability for npm proxy configuration + module NpmProxyConf + # @return [Boolean] if npm is installed + def self.npm_proxy_conf(machine) + machine.communicate.test('which npm') + end + end + end + end + end +end diff --git a/lib/vagrant-proxyconf/capability.rb b/lib/vagrant-proxyconf/capability.rb index 580ddc4..a2aa49b 100644 --- a/lib/vagrant-proxyconf/capability.rb +++ b/lib/vagrant-proxyconf/capability.rb @@ -22,6 +22,11 @@ class Plugin < Vagrant.plugin('2') Cap::Linux::GitProxyConf end + guest_capability 'linux', 'npm_proxy_conf' do + require_relative 'cap/linux/npm_proxy_conf' + Cap::Linux::NpmProxyConf + end + guest_capability 'linux', 'pear_proxy_conf' do require_relative 'cap/linux/pear_proxy_conf' Cap::Linux::PearProxyConf diff --git a/locales/en.yml b/locales/en.yml index 6bd5eab..d3dfa0c 100644 --- a/locales/en.yml +++ b/locales/en.yml @@ -24,6 +24,14 @@ en: configuring: |- Configuring proxy environment variables... + npm_proxy: + not_enabled: |- + npm_proxy not enabled or configured + not_supported: |- + Skipping npm proxy config as `npm` is not found + configuring: |- + Configuring proxy for npm... + pear_proxy: not_enabled: |- pear_proxy not enabled or configured diff --git a/spec/unit/vagrant-proxyconf/action/configure_npm_proxy_spec.rb b/spec/unit/vagrant-proxyconf/action/configure_npm_proxy_spec.rb new file mode 100644 index 0000000..9c63278 --- /dev/null +++ b/spec/unit/vagrant-proxyconf/action/configure_npm_proxy_spec.rb @@ -0,0 +1,10 @@ +require 'spec_helper' +require 'vagrant-proxyconf/action/configure_npm_proxy' + +describe VagrantPlugins::ProxyConf::Action::ConfigureNpmProxy do + + describe '#config_name' do + subject { described_class.new(double, double).config_name } + it { should eq 'npm_proxy' } + end +end diff --git a/spec/unit/vagrant-proxyconf/cap/linux/npm_proxy_conf_spec.rb b/spec/unit/vagrant-proxyconf/cap/linux/npm_proxy_conf_spec.rb new file mode 100644 index 0000000..bb4d117 --- /dev/null +++ b/spec/unit/vagrant-proxyconf/cap/linux/npm_proxy_conf_spec.rb @@ -0,0 +1,25 @@ +require 'spec_helper' +require 'vagrant-proxyconf/cap/linux/npm_proxy_conf' + +describe VagrantPlugins::ProxyConf::Cap::Linux::NpmProxyConf do + + describe '.npm_proxy_conf' do + let(:machine) { double } + let(:communicator) { double } + + before do + machine.stub(:communicate => communicator) + end + + it "returns true when npm is installed" do + expect(communicator).to receive(:test).with('which npm').and_return(true) + expect(described_class.npm_proxy_conf(machine)).to be_true + end + + it "returns false when npm is not installed" do + expect(communicator).to receive(:test).with('which npm').and_return(false) + expect(described_class.npm_proxy_conf(machine)).to be_false + end + end + +end