grunt-couchapp/tasks/couchapp.js

97 lines
3.4 KiB
JavaScript
Raw Normal View History

2012-08-10 01:34:00 +00:00
/*
* grunt-couchapp https://github.com/elf/grunt-couchapp
*
* Copyright (c) 2012 Ken "Elf" Mathieu Sternberg
* Licensed under the MIT license.
*/
var path, couchapp, nanolib, urls;
path = require('path');
couchapp = require('couchapp');
urls = require('url');
module.exports = function(grunt) {
// ==========================================================================
// TASKS
// ==========================================================================
grunt.registerMultiTask("couchapp", "Install Couchapp", function() {
var appobj, done;
done = this.async();
appobj = require(path.join(process.cwd(), path.normalize(this.data.app)));
return couchapp.createApp(appobj, this.data.db, function(app) {
return app.push(done);
});
});
grunt.registerMultiTask("rmcouchdb", "Delete a Couch Database", function() {
var done, parts, nano, dbname, _this;
_this = this;
done = this.async();
parts = urls.parse(this.data.db);
dbname = parts.pathname.replace(/^\//, '');
try {
nano = require('nano')(parts.protocol + '//' + parts.host);
if (dbname) {
nano.db.destroy(dbname, function(err) {
if (err) {
if (err.status_code && err.status_code === 404) {
if (_this.data.options && _this.data.options.okay_if_missing) {
grunt.log.writeln("Database " + dbname + " not present... skipping.");
return done(null, null) ;
} else {
grunt.warn("Database " + dbname + " does not exist.");
}
2012-08-10 01:34:00 +00:00
} else {
grunt.warn(err);
2012-08-10 01:34:00 +00:00
}
}
return done(err, null);
});
} else {
grunt.log.writeln("No database specified... skipping.");
return done(null, null);
}
2012-08-10 01:34:00 +00:00
} catch (e) {
grunt.warn(e);
done(e, null);
}
});
grunt.registerMultiTask("mkcouchdb", "Delete a Couch Database", function() {
2012-10-30 19:17:55 +00:00
var done, parts, nano, dbname, _this;
_this = this;
2012-08-10 01:34:00 +00:00
done = this.async();
parts = urls.parse(this.data.db);
dbname = parts.pathname.replace(/^\//, '');
try {
if (dbname) {
nano = require('nano')(parts.protocol + '//' + parts.host);
nano.db.create(dbname, function(err) {
if (_this.data.options && _this.data.options.okay_if_exists) {
if (err){
grunt.log.writeln("Database " + dbname + " exists, skipping");
}
return done(null, null);
} else {
if (err){
grunt.warn(err);
}
return done(err, null);
2012-10-30 19:17:55 +00:00
}
});
} else {
2013-09-06 14:04:20 +00:00
var err_msg = "No database specified to create!";
grunt.warn(err_msg);
return done(new Error(err_msg), null);
}
2012-08-10 01:34:00 +00:00
} catch (e) {
grunt.warn(e);
done(e, null);
}
});
};