Skip to content

Instantly share code, notes, and snippets.

@Cryptophobia
Created July 14, 2016 14:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Cryptophobia/e6d1833242647a84aa325d74f5a01d05 to your computer and use it in GitHub Desktop.
Save Cryptophobia/e6d1833242647a84aa325d74f5a01d05 to your computer and use it in GitHub Desktop.
Rake task for nvm - node version manager
namespace :nvm do
desc "Load nvm and the node environment variables"
def file_or_symlink_exists?(path_to_file)
File.exist?(path_to_file) || File.symlink?(path_to_file)
end
desc "Load node if nvm is already installed but not on the path"
task :load_node do
sh "/bin/bash -c '. ~/.nvm/nvm.sh'"
system(". ~/.nvm/nvm.sh")
nodefile = `which nodejs`
nodeversion = `node -v`
puts "The node version is: #{nodeversion}"
puts "The node file is: #{nodefile}"
output = File.exist?('#{nodefile}')
puts "Return is: #{output}"
if file_or_symlink_exists?('#{nodefile}') == true
puts "Nodejs symlink exists."
else
puts "Nodejs symlink does not exist. Making the link now"
sh "/bin/bash -c 'source ~/.nvm/nvm.sh && ln -sf $(which node) $(which node)js'"
end
end
desc "Install nvm - Node Version Manager"
task :install_nvm do
begin
if `which curl`.size.>(0)
sh "curl https://raw.github.com/creationix/nvm/master/install.sh | sh"
else
sh "wget -qO- https://raw.github.com/creationix/nvm/master/install.sh | sh"
end
puts "NVM : https://github.com/creationix/nvm"
puts "has been installed for your account. NVM is the node/npm version manager."
puts "If you like RVM, you will feel like at home with NVM."
puts
rescue Exception => e
puts e
puts e.backtrace
end
end
desc "Install a node version NODE_VERSION (default 4.4.3)"
task :install_node do
Rake::Task['nvm:install_nvm'].invoke unless ENV['NVM_DIR']
node_version = ENV['NODE_VERSION'] || '4.4.3'
sh "/bin/bash -c 'source ~/.nvm/nvm.sh && nvm install #{node_version}'"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment