Skip to content

Instantly share code, notes, and snippets.

@tboerger
Created May 6, 2014 20:37
Show Gist options
  • Save tboerger/da772667e5631cd2a10c to your computer and use it in GitHub Desktop.
Save tboerger/da772667e5631cd2a10c to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require "rubygems"
require "pathname"
require "dante"
require "file-monitor"
require "mixlib/shellout"
module Dante
class Runner
protected
def parse_options
headline = [@name, @description].compact.join(" - ")
OptionParser.new do |opts|
opts.summary_width = 25
opts.banner = [
headline,
"\n\n",
"Usage: #{@name} [-P file] [-l file] [-d] [-k] [-f]\n",
" #{@name} --help\n"
].compact.join("")
opts.separator ""
opts.on("-d", "--daemon", "Daemonized mode") do |v|
options[:daemonize] = v
end
opts.on("-k", "--kill", "Kill running daemon") do |v|
options[:kill] = v
end
opts.on("-f", "--force", String, "Force kill if SIGTERM fails") do |v|
options[:force] = true
end
opts.on("-P", "--pid FILE", String, "Save PID in FILE") do |v|
options[:pid_path] = File.expand_path(v)
end
opts.on("-l", "--log FILE", String, "Logfile for output") do |v|
options[:log_path] = v
end
opts.on("-u", "--user USER", String, "User to run as") do |user|
options[:user] = user
end
opts.on("-G", "--group GROUP", String, "Group to run as") do |group|
options[:group] = group
end
instance_exec(opts, &@with_options) if @with_options
opts.separator ""
opts.on_tail("-h", "--help", "Display this information") do
puts [opts, "\n"].join
exit
end
end.parse!
options
end
end
end
module Webdev
class Npm < FileMonitor
def initialize(dir)
super
frequency 0.5
follow_symlink true
ignore_dirs /\.git$|\.svn$/
filter_files {
disallow /.*/
allow /package\.json$/
}
end
def check(events)
begin
path = nil
events.each do |event|
event.flags.each do |flag|
if [:modify, :create].include? flag
path = event.watcher.path
end
end
end
unless path.nil?
cmd = command_in(
path
)
puts "Running #{cmd.command.join(" ")} in #{path}"
cmd.run_command
end
rescue => e
puts "Error: #{e.message}"
end
end
protected
def command_in(dir)
Mixlib::ShellOut.new(
"npm",
"install",
:cwd => dir.to_s
)
end
end
end
runner = Dante::Runner.new(
"webdev-npm-runner",
:pid_path => "/var/run/webdev/npm.pid",
:log_path => "/var/log/webdev/npm.log"
)
runner.description = "Webdev npm installation"
runner.with_options do |opts|
opts.on("-D", "--directory PATH", String, "Directory to monitor") do |dir|
options[:directory] = File.expand_path(dir)
end
end
runner.verify_options_hook = lambda { |opts|
opts[:directory] = "/vagrant" if opts[:directory].nil?
}
runner.execute do |options|
monitor = Webdev::Npm.new options[:directory]
monitor.run
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment