diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4040c6c --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +*.gem +.bundle +Gemfile.lock +pkg/* diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..1f58faf --- /dev/null +++ b/Gemfile @@ -0,0 +1,4 @@ +source 'https://rubygems.org' + +# Specify your gem's dependencies in capistrano-composer.gemspec +gemspec diff --git a/README.md b/README.md new file mode 100644 index 0000000..37ad909 --- /dev/null +++ b/README.md @@ -0,0 +1,56 @@ +# capistrano-npm + +capistrano-npm is a [Capistrano](https://github.com/capistrano/capistrano) extension that will let you run [npm](https://npmjs.org/) during your deploy process. + +## Installation + +1. Install the Gem + +```bash +gem install capistrano-npm +``` + +Or if you're using Bundler, add it to your `Gemfile`: + +```ruby +gem 'capistrano-npm', github: 'swalkinshaw/npm' +``` + +2. Add to `Capfile` or `config/deploy.rb`: + +```ruby +require 'capistrano/npm' +``` + +## Usage + +Add the task to your `deploy.rb`: + +```ruby +after 'deploy:finalize_update', 'npm:install' +``` + +To speed up your deployments, you should also add `node_modules` to Capistrano's `shared_children` variable: + +```ruby +set :shared_children, shared_children + %w{node_modules} +``` + +This will symlink `node_modules` from the `shared` directory so it isn't re-created from scratch each deploy. + +Ideally when using npm, you should add `node_modules` to your `.gitignore` file to keep them out of your repository. + +Since `npm install` does not guarantee that you will get the same package versions when deploying, you should use `npm shrinkwrap` (https://npmjs.org/doc/shrinkwrap.html) first. + +Running `npm shrinkwrap` will created a `npm-shrinkwrap.json` file that locks down the dependency versions. Check this file into your repository. + +Now when deploying, `npm install` will detect the `npm-shrinkwrap.json` file and use that to install the packages. + +### Tasks + +* `npm:install`: Runs `npm install`. + +### Configuration + +* `npm_path`: Path to npm bin on the remote server. Defaults to just `npm` as its assumed to be in your `$PATH`. +* `npm_options`: Options for `npm` command. Defaults to an empty string. diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..c702cfc --- /dev/null +++ b/Rakefile @@ -0,0 +1 @@ +require 'bundler/gem_tasks' diff --git a/capistrano-npm.gemspec b/capistrano-npm.gemspec new file mode 100644 index 0000000..dd55887 --- /dev/null +++ b/capistrano-npm.gemspec @@ -0,0 +1,20 @@ +# coding: utf-8 +lib = File.expand_path('../lib', __FILE__) +$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) + +require 'capistrano/npm/version' + +Gem::Specification.new do |s| + s.name = 'capistrano-npm' + s.version = Capistrano::Npm::VERSION + s.authors = ['Scott Walkinshaw'] + s.email = ['scott.walkinshaw@gmail.com'] + s.homepage = 'https://github.com/swalkinshaw/capistrano-npm' + s.summary = %q{Capistrano extension for npm (Node Packaged Modules)} + s.license = 'MIT' + + s.files = `git ls-files`.split($/) + s.require_paths = %w(lib) + + s.add_dependency 'capistrano', '>= 2.5.5' +end diff --git a/lib/capistrano/npm.rb b/lib/capistrano/npm.rb new file mode 100644 index 0000000..bd7af5c --- /dev/null +++ b/lib/capistrano/npm.rb @@ -0,0 +1,11 @@ +Capistrano::Configuration.instance(true).load do + set :npm_path, 'npm' + set :npm_options, nil + + namespace :npm do + desc 'Runs npm install.' + task :install, :roles => :app, :except => { :no_release => true } do + try_sudo "cd #{latest_release} && #{npm_path} #{npm_options} install" + end + end +end diff --git a/lib/capistrano/npm/version.rb b/lib/capistrano/npm/version.rb new file mode 100644 index 0000000..51c3b64 --- /dev/null +++ b/lib/capistrano/npm/version.rb @@ -0,0 +1,5 @@ +module Capistrano + module Npm + VERSION = '0.0.1' + end +end