Skip to content

Commit

Permalink
Merge pull request #51 from tmatilai/npm
Browse files Browse the repository at this point in the history
Add support for npm proxy
  • Loading branch information
tmatilai committed Feb 19, 2014
2 parents 9ea4346 + e742a26 commit c4316d8
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 3 additions & 0 deletions lib/vagrant-proxyconf/action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
36 changes: 36 additions & 0 deletions lib/vagrant-proxyconf/action/configure_npm_proxy.rb
Original file line number Diff line number Diff line change
@@ -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
15 changes: 15 additions & 0 deletions lib/vagrant-proxyconf/cap/linux/npm_proxy_conf.rb
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions lib/vagrant-proxyconf/capability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions spec/unit/vagrant-proxyconf/action/configure_npm_proxy_spec.rb
Original file line number Diff line number Diff line change
@@ -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
25 changes: 25 additions & 0 deletions spec/unit/vagrant-proxyconf/cap/linux/npm_proxy_conf_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit c4316d8

Please sign in to comment.