Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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