From ee6174d33c271a3f7d1d5680180224e653805519 Mon Sep 17 00:00:00 2001 From: George Miroshnykov Date: Wed, 23 Oct 2013 15:44:49 +0300 Subject: [PATCH] Add npm support --- README.md | 16 +++++++++ lib/vagrant-cachier/bucket.rb | 1 + lib/vagrant-cachier/bucket/npm.rb | 33 +++++++++++++++++++ .../cap/linux/npm_cache_dir.rb | 20 +++++++++++ lib/vagrant-cachier/plugin.rb | 5 +++ 5 files changed, 75 insertions(+) create mode 100644 lib/vagrant-cachier/bucket/npm.rb create mode 100644 lib/vagrant-cachier/cap/linux/npm_cache_dir.rb 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 0e425a8..ec660df 100644 --- a/lib/vagrant-cachier/bucket.rb +++ b/lib/vagrant-cachier/bucket.rb @@ -40,3 +40,4 @@ require_relative "bucket/pacman" require_relative "bucket/yum" require_relative "bucket/rvm" require_relative "bucket/apt_cacher" +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 29dc502..4ba12a9 100644 --- a/lib/vagrant-cachier/plugin.rb +++ b/lib/vagrant-cachier/plugin.rb @@ -31,6 +31,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