grunt-couchapp/tasks/couchapp.js

98 lines
3.0 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.
*/
2013-08-22 03:49:53 +00:00
var path, couchapp, nanolib, urls, auth;
2012-08-10 01:34:00 +00:00
path = require('path');
couchapp = require('couchapp');
urls = require('url');
2013-08-22 03:49:53 +00:00
auth = require('../lib/auth');
2012-08-10 01:34:00 +00:00
var genDB = function(db) {
var parts, dbname, auth;
parts = urls.parse(db);
dbname = parts.pathname.replace(/^\//, '');
auth = parts.auth ? (parts.auth + '@') : '';
return {
name: dbname,
url: parts.protocol + '//' + auth + parts.host
};
};
2012-08-10 01:34:00 +00:00
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();
db = genDB(this.data.db);
2012-08-10 01:34:00 +00:00
try {
nano = require('nano')(db.url);
nano.db.destroy(db.name, function(err) {
2012-08-10 01:34:00 +00:00
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.");
}
} else {
grunt.warn(err);
}
}
return done(err, null);
});
} catch (e) {
grunt.warn(e);
done(e, null);
}
});
2013-08-22 03:49:53 +00:00
grunt.registerMultiTask("mkcouchdb", "Make a Couch Database", function() {
var done, parts, nano, dbname, _this;
2012-10-30 19:17:55 +00:00
_this = this;
2012-08-10 01:34:00 +00:00
done = this.async();
parts = urls.parse(this.data.db);
db = genDB(this.data.db);
2012-08-10 01:34:00 +00:00
try {
nano = require('nano')(db.url);
2013-07-12 20:15:58 +00:00
nano.db.create(db.name, function(err) {
2012-10-30 19:17:55 +00:00
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-08-10 01:34:00 +00:00
}
});
} catch (e) {
grunt.warn(e);
done(e, null);
}
});
};