diff --git a/README.md b/README.md index 6220e99..fae0eed 100644 --- a/README.md +++ b/README.md @@ -208,6 +208,22 @@ folder under the result of running `rvm info` as the default SSH user (usualy it is already installed before enabling the bucket, otherwise you won't benefit from this plugin. +#### npm + +```ruby +Vagrant.configure("2") do |config| + config.vm.box = 'some-box-with-nodejs-installed' + config.cache.enable :npm +end +``` + +Compatible with probably any type of linux guest distro, will hook into npm's +cache directory under the result of running `npm config get cache` as +the default SSH user (usually `vagrant`) on your guest. +If you use +[nvm](https://github.com/creationix/nvm) / [n](https://github.com/visionmedia/n) +on the guest machine, make sure it is already installed before enabling +the bucket, otherwise you won't benefit from this plugin. ##### APT-CACHER diff --git a/lib/vagrant-cachier/bucket.rb b/lib/vagrant-cachier/bucket.rb index 8b3e04f..93b067d 100644 --- a/lib/vagrant-cachier/bucket.rb +++ b/lib/vagrant-cachier/bucket.rb @@ -41,3 +41,4 @@ require_relative "bucket/yum" require_relative "bucket/rvm" require_relative "bucket/apt_cacher" require_relative "bucket/composer" +require_relative "bucket/npm" diff --git a/lib/vagrant-cachier/bucket/npm.rb b/lib/vagrant-cachier/bucket/npm.rb new file mode 100644 index 0000000..f10a2ce --- /dev/null +++ b/lib/vagrant-cachier/bucket/npm.rb @@ -0,0 +1,33 @@ +module VagrantPlugins + module Cachier + class Bucket + class Npm < Bucket + def self.capability + :npm_cache_dir + end + + def install + machine = @env[:machine] + guest = machine.guest + + if guest.capability?(:npm_cache_dir) + guest_path = guest.capability(:npm_cache_dir) + + @env[:cache_dirs] << guest_path + + machine.communicate.tap do |comm| + comm.execute("mkdir -p /tmp/vagrant-cache/#{@name}") + unless comm.test("test -L #{guest_path}") + comm.execute("rm -rf #{guest_path}") + comm.execute("mkdir -p `dirname #{guest_path}`") + comm.execute("ln -s /tmp/vagrant-cache/#{@name} #{guest_path}") + end + end + else + @env[:ui].info I18n.t('vagrant_cachier.skipping_bucket', bucket: 'npm') + end + end + end + end + end +end diff --git a/lib/vagrant-cachier/cap/linux/npm_cache_dir.rb b/lib/vagrant-cachier/cap/linux/npm_cache_dir.rb new file mode 100644 index 0000000..1b123e7 --- /dev/null +++ b/lib/vagrant-cachier/cap/linux/npm_cache_dir.rb @@ -0,0 +1,20 @@ +module VagrantPlugins + module Cachier + module Cap + module Linux + module NpmCacheDir + def self.npm_cache_dir(machine) + npm_cache_dir = nil + machine.communicate.tap do |comm| + return unless comm.test('which npm') + comm.execute 'npm config get cache' do |buffer, output| + npm_cache_dir = output.chomp if buffer == :stdout + end + end + return npm_cache_dir + end + end + end + end + end +end diff --git a/lib/vagrant-cachier/plugin.rb b/lib/vagrant-cachier/plugin.rb index 929e0f3..ec31076 100644 --- a/lib/vagrant-cachier/plugin.rb +++ b/lib/vagrant-cachier/plugin.rb @@ -36,6 +36,11 @@ module VagrantPlugins Cap::Linux::ChefFileCachePath end + guest_capability 'linux', 'npm_cache_dir' do + require_relative 'cap/linux/npm_cache_dir' + Cap::Linux::NpmCacheDir + end + guest_capability 'debian', 'apt_cache_dir' do require_relative 'cap/debian/apt_cache_dir' Cap::Debian::AptCacheDir