Created
December 4, 2014 08:31
-
-
Save shunjikonishi/b42decee4446c0652cdd to your computer and use it in GitHub Desktop.
Gruntからherokuコマンドを実行する
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
module.exports = function (grunt) { | |
'use strict'; | |
function loadDependencies(deps) { | |
if (deps) { | |
for (var key in deps) { | |
if (key.indexOf("grunt-") == 0) { | |
grunt.loadNpmTasks(key); | |
} | |
} | |
} | |
} | |
grunt.initConfig({ | |
pkg: grunt.file.readJSON('package.json'), | |
restorePgbackups: { | |
test: { | |
app: "myapp", | |
backupId: "b002" | |
} | |
} | |
}); | |
loadDependencies(grunt.config("pkg").devDependencies); | |
grunt.task.loadTasks('tasks'); | |
grunt.registerTask('default', ['restorePgbackups']); | |
}; |
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
var TASK_NAME = "restorePgbackups", | |
grunt = require("grunt"), | |
exec = require("child_process").exec, | |
EventEmitter = require("events").EventEmitter, | |
emitter = new EventEmitter(); | |
function trimLinefeed(str) { | |
var c = str.charAt(str.length - 1); | |
while (c == "\r" || c == "\n") { | |
str = str.substring(0, str.length - 1); | |
c = str.charAt(str.length - 1); | |
} | |
return str; | |
} | |
grunt.registerMultiTask(TASK_NAME, 'Restore pgbackups', function () { | |
var config = grunt.config(TASK_NAME)[this.target], | |
app = config.app, | |
backupApp = config.backupApp || config.app, | |
backupId = config.backupId; | |
if (!app) { | |
grunt.fail.warn("app is not specified."); | |
} | |
if (!backupId) { | |
grunt.fail.warn("app is not specified."); | |
} | |
var done = this.async(); | |
emitter.on("url", function(url) { | |
var command = "heroku pgbackups:restore DATABASE_URL \"" + trimLinefeed(url) + "\" -a " + app + " --confirm " + app; | |
exec(command, function(err, stdout, stderr) { | |
if (err) { | |
grunt.fail.warn(err); | |
} | |
grunt.log.writeln(stdout); | |
done(); | |
}); | |
}); | |
exec("heroku pgbackups:url " + backupId + " -a " + backupApp, function(err, stdout, stderr) { | |
if (err) { | |
grunt.fail.warn(err); | |
} | |
emitter.emit("url", stdout); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment