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-09-09 03:59:17 +00:00
|
|
|
var path, couchapp, nanolib, urls;
|
2012-08-10 01:34:00 +00:00
|
|
|
|
|
|
|
path = require('path');
|
|
|
|
couchapp = require('couchapp');
|
|
|
|
urls = require('url');
|
|
|
|
|
2013-09-09 02:06:49 +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
|
|
|
|
2012-11-17 00:50:59 +00:00
|
|
|
|
2012-08-10 01:34:00 +00:00
|
|
|
module.exports = function(grunt) {
|
|
|
|
|
|
|
|
// ==========================================================================
|
|
|
|
// TASKS
|
|
|
|
// ==========================================================================
|
|
|
|
|
|
|
|
grunt.registerMultiTask("couchapp", "Install Couchapp", function() {
|
2013-09-09 03:59:17 +00:00
|
|
|
var appobj, done;
|
|
|
|
done = this.async();
|
2014-09-12 02:58:43 +00:00
|
|
|
if (require("fs").lstatSync(this.data.app).isDirectory()) { // if new-style (directory-based) couchapp app
|
|
|
|
appobj = couchapp.loadFiles(this.data.app);
|
|
|
|
} else { // otherwise, fall back to old style.
|
|
|
|
appobj = require(path.join(process.cwd(), path.normalize(this.data.app)))
|
|
|
|
}
|
2013-09-09 03:59:17 +00:00
|
|
|
|
|
|
|
return couchapp.createApp(appobj, this.data.db, function(app) {
|
|
|
|
return app.push(done);
|
|
|
|
});
|
2012-08-10 01:34:00 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
grunt.registerMultiTask("rmcouchdb", "Delete a Couch Database", function() {
|
2013-10-16 20:17:10 +00:00
|
|
|
var done, parts, nano, dbname, _this, db;
|
2012-08-10 01:34:00 +00:00
|
|
|
_this = this;
|
|
|
|
done = this.async();
|
2012-11-17 00:50:59 +00:00
|
|
|
db = genDB(this.data.db);
|
2012-08-10 01:34:00 +00:00
|
|
|
try {
|
2012-11-17 00:50:59 +00:00
|
|
|
nano = require('nano')(db.url);
|
2013-09-09 02:11:23 +00:00
|
|
|
if (db.name) {
|
|
|
|
nano.db.destroy(db.name, function(err) {
|
2013-09-06 14:02:16 +00:00
|
|
|
if (err) {
|
|
|
|
if (err.status_code && err.status_code === 404) {
|
|
|
|
if (_this.data.options && _this.data.options.okay_if_missing) {
|
2013-09-09 02:11:23 +00:00
|
|
|
grunt.log.writeln("Database " + db.name + " not present... skipping.");
|
2013-09-06 14:02:16 +00:00
|
|
|
return done(null, null) ;
|
|
|
|
} else {
|
2014-09-12 02:30:58 +00:00
|
|
|
throw ("Database " + db.name + " does not exist.");
|
2013-09-06 14:02:16 +00:00
|
|
|
}
|
2012-08-10 01:34:00 +00:00
|
|
|
} else {
|
2014-09-12 02:30:58 +00:00
|
|
|
throw err;
|
2012-08-10 01:34:00 +00:00
|
|
|
}
|
|
|
|
}
|
2014-09-12 02:30:58 +00:00
|
|
|
// remove db was a success
|
|
|
|
return done();
|
2013-10-16 20:22:50 +00:00
|
|
|
});
|
2013-09-06 14:02:16 +00:00
|
|
|
} 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);
|
2014-09-12 02:30:58 +00:00
|
|
|
return done(e, null);
|
2012-08-10 01:34:00 +00:00
|
|
|
}
|
2013-10-16 20:22:50 +00:00
|
|
|
return null;
|
2012-08-10 01:34:00 +00:00
|
|
|
});
|
|
|
|
|
2013-08-22 03:49:53 +00:00
|
|
|
grunt.registerMultiTask("mkcouchdb", "Make a Couch Database", function() {
|
2013-10-16 20:17:10 +00:00
|
|
|
var done, parts, nano, dbname, _this, db;
|
2012-10-30 19:17:55 +00:00
|
|
|
_this = this;
|
2012-11-17 00:50:59 +00:00
|
|
|
|
2012-08-10 01:34:00 +00:00
|
|
|
done = this.async();
|
|
|
|
parts = urls.parse(this.data.db);
|
2012-11-17 00:50:59 +00:00
|
|
|
db = genDB(this.data.db);
|
2012-08-10 01:34:00 +00:00
|
|
|
try {
|
2013-09-09 02:11:23 +00:00
|
|
|
if (db.name) {
|
|
|
|
nano = require('nano')(db.url);
|
2013-09-09 03:59:17 +00:00
|
|
|
nano.db.create(db.name, function(err, res) {
|
|
|
|
if (err) {
|
2014-09-12 02:30:58 +00:00
|
|
|
if (err.error && err.error=="unauthorized") {
|
|
|
|
grunt.warn(err.reason);
|
|
|
|
throw err;
|
|
|
|
} else if (err.code && err.description) { // probably connection error
|
|
|
|
throw err;
|
|
|
|
}
|
2014-09-13 01:50:10 +00:00
|
|
|
else if (err.error && err.error=="file_exists") {
|
|
|
|
if (_this.data.options && _this.data.options.okay_if_exists) {
|
|
|
|
grunt.log.writeln("Database " + db.name + " exists, skipping");
|
|
|
|
return done(null, null);
|
|
|
|
} else {
|
|
|
|
grunt.warn("Database " + db.name + " exists and okay_if_exists set to false. Aborting.");
|
|
|
|
throw err;
|
|
|
|
}
|
2013-09-09 03:59:17 +00:00
|
|
|
} else {
|
2014-09-13 01:50:10 +00:00
|
|
|
console.warn("Unrecognized error.");
|
|
|
|
throw err
|
2013-09-06 14:02:16 +00:00
|
|
|
}
|
2014-09-12 02:30:58 +00:00
|
|
|
} else if (res && res.ok==true) {
|
|
|
|
grunt.log.ok("Database " + db.name + " created.");
|
2013-09-09 03:59:17 +00:00
|
|
|
return done(null, null);
|
2014-09-12 02:30:58 +00:00
|
|
|
} else {
|
|
|
|
console.log("Unexpected response received.");
|
|
|
|
console.dir(res);
|
|
|
|
throw "Unexpected response";
|
2012-10-30 19:17:55 +00:00
|
|
|
}
|
2013-10-16 20:22:50 +00:00
|
|
|
});
|
2013-09-06 14:02:16 +00:00
|
|
|
} else {
|
2013-09-06 14:04:20 +00:00
|
|
|
var err_msg = "No database specified to create!";
|
2014-09-12 02:30:58 +00:00
|
|
|
throw err_msg;
|
2013-09-06 14:02:16 +00:00
|
|
|
}
|
2012-08-10 01:34:00 +00:00
|
|
|
} catch (e) {
|
|
|
|
grunt.warn(e);
|
2014-09-12 02:30:58 +00:00
|
|
|
return done(e, null);
|
2012-08-10 01:34:00 +00:00
|
|
|
}
|
2013-10-16 20:22:50 +00:00
|
|
|
return null;
|
2012-08-10 01:34:00 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
};
|