Skip to content

Instantly share code, notes, and snippets.

@jbarnette
Created August 7, 2011 16:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jbarnette/1130525 to your computer and use it in GitHub Desktop.
Save jbarnette/1130525 to your computer and use it in GitHub Desktop.
fs = require "fs"
path = require "path"
rimraf = require "rimraf"
url = require "url"
{exec, spawn} = require "child_process"
heroku = (callback) ->
exec "heroku config --shell", (err, stdout, stderr) ->
console.log "heroku config error: #{err}" if err
config = {}
pattern = /^([^=]+)=(.*)$/gm
config[m[1]] = m[2] while m = pattern.exec stdout
callback config
# Spawn the given command, hook up stdin, stout, and stderr to the
# current process, and exit when the command does. Options are
# identical to spawn or exec, but env vars are merged with the parent
# process' values.
run = (cmd, args, opts) ->
if opts?.env?
for own k, v of process.env
opts.env[k] = v unless opts.env[k]
proc = spawn cmd, args, opts
process.stdin.resume()
process.stdin.pipe proc.stdin
proc.stdout.pipe process.stdout
proc.stderr.pipe process.stderr
proc.on "exit", (code) ->
process.exit code
task "console", "Run a CoffeeScript console w/lib.", ->
run "coffee", ["--interactive"], env: { NODE_PATH: "lib" }
task "db", "Run a DB console.", ->
run "mongo", ["local-dev"]
task "db:pull", "Grab a copy of the production DB.", ->
heroku (cfg) ->
dbURL = url.parse cfg.MONGOHQ_URL
db = dbURL.pathname.substr 1
dir = "tmp/mongodump"
host = dbURL.hostname
port = dbURL.port
[username, password] = dbURL.auth.split ":"
args = ["--host", host, "--port", port, "--db", db,
"--username", username, "--password", password,
"--out", dir]
rimraf dir, (err) ->
console.log "rimraf: #{err}" if err
spawn("mongodump", args).on "exit", (code) ->
console.log "mongodump exit: #{code}" if code isnt 0
args = ["local-dev", "--eval", "db.dropDatabase()"]
spawn("mongo", args).on "exit", (code) ->
console.log "mongo exit: #{code}" if code isnt 0
args = ["--drop", "--db", "local-dev", path.join(dir, db)]
spawn("mongorestore", args).on "exit", (code) ->
console.log "mongorestore exit: #{code}" if code isnt 0
rimraf dir, (err) ->
console.log "last rimraf: #{err}" if err
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment