Last active
December 11, 2015 01:08
-
-
Save realyze/4520937 to your computer and use it in GitHub Desktop.
A script that mirrors changes made in your project to a local deployment in a Vagrant box.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# HOWTO | |
# ===== | |
# 1. Share your project root directory with the Vagrant box. Let's say you | |
# call it `/home/vagrant/myapp`. | |
# | |
# 2. `vagrant shh into the Vagrant box and go to `/home/vagrant/myapp`. | |
# | |
# 3. Run | |
# | |
# sudo su - my_deploy_user -c 'coffee /home/vagrant/myapp/watcher.coffee' | |
# | |
# This will mirror the changes you make in the project's root dir to the | |
# running instance and restart it. | |
# | |
# 4. Profit. | |
{exec, spawn} = require 'child_process' | |
watchr = require 'watchr' | |
path = require 'path' | |
fs = require 'fs' | |
# Here we assume we're in `<project_root>/` now (that's where this | |
# script is). | |
SOURCE_ROOT = __dirname | |
TARGET_ROOT = '/srv/myapp/current' | |
stream = (command, args, options, callback) -> | |
sub = spawn command, args, options | |
sub.stdout.on 'data', (data) -> console.log data.toString() | |
sub.stderr.on 'data', (data) -> console.log data.toString() | |
sub.on 'exit', (status) -> callback?() if status is 0 | |
watchr.watch | |
paths: [SOURCE_ROOT], | |
listeners: | |
change: (changeType,filePath,fileCurrentStat,filePreviousStat) -> | |
# console.log('a change event occured:', arguments) | |
if fileCurrentStat?.isDirectory() or filePreviousStat?.isDirectory() | |
# console.log "Ignoring directory: #{filePath}" | |
return | |
relativePath = filePath.replace SOURCE_ROOT + '/', '' | |
console.log "Detected change '#{changeType}' for file #{relativePath}" | |
if changeType == 'delete' | |
fs.unlinkSync TARGET_ROOT + '/' + filePath | |
else | |
# This just copies the file. | |
fs.createReadStream(filePath) | |
.pipe(fs.createWriteStream("#{TARGET_ROOT}/#{relativePath}")) | |
opts = | |
env: NODE_PATH: '.' | |
cwd: TARGET_ROOT + '/server' | |
# Restart the node server. | |
stream 'cake ', ['restart'], opts |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment