From a41ae8efac9403a428672e8638be0b9d2ac687aa Mon Sep 17 00:00:00 2001 From: Max Thayer Date: Fri, 6 Sep 2013 10:02:16 -0400 Subject: [PATCH 1/2] rmcouchdb doesn't drop everything if dbname is absent; mkcouchdb reports helpful error message if dbname is absent --- README.md | 3 +-- tasks/couchapp.js | 61 ++++++++++++++++++++++++++++------------------- 2 files changed, 37 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index f043944..30c09d1 100644 --- a/README.md +++ b/README.md @@ -68,8 +68,7 @@ possible to write in your configuration file: }); -Note, however, that if you call 'rmcouchdb' without a sub-argument, in -keeping with grunt's standards, it will drop *all* of your databases! +Note that if you call `rmcouchdb` without a sub-argument, it will not delete any databases. ## Demo diff --git a/tasks/couchapp.js b/tasks/couchapp.js index 1382875..464b63f 100755 --- a/tasks/couchapp.js +++ b/tasks/couchapp.js @@ -46,21 +46,26 @@ module.exports = function(grunt) { dbname = parts.pathname.replace(/^\//, ''); try { nano = require('nano')(parts.protocol + '//' + parts.host); - 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) ; + 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."); + } } else { - grunt.warn("Database " + dbname + " does not exist."); + grunt.warn(err); } - } else { - grunt.warn(err); } - } - return done(err, null); - }); + return done(err, null); + }); + } else { + grunt.log.writeln("No database specified... skipping."); + return done(null, null); + } } catch (e) { grunt.warn(e); done(e, null); @@ -74,20 +79,26 @@ module.exports = function(grunt) { parts = urls.parse(this.data.db); dbname = parts.pathname.replace(/^\//, ''); try { - 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"); + 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); } - return done(null, null); - } else { - if (err){ - grunt.warn(err); - } - return done(err, null); - } - }); + }); + } else { + var err_msg = "No database specified... skipping."; + grunt.warn(err_msg); + return done(new Error(err_msg), null); + } } catch (e) { grunt.warn(e); done(e, null); From b957e5a1c1823e714364ef88f934b39808c0f4e4 Mon Sep 17 00:00:00 2001 From: Max Thayer Date: Fri, 6 Sep 2013 10:04:20 -0400 Subject: [PATCH 2/2] more helpful error message --- tasks/couchapp.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tasks/couchapp.js b/tasks/couchapp.js index 464b63f..9df6922 100755 --- a/tasks/couchapp.js +++ b/tasks/couchapp.js @@ -95,7 +95,7 @@ module.exports = function(grunt) { } }); } else { - var err_msg = "No database specified... skipping."; + var err_msg = "No database specified to create!"; grunt.warn(err_msg); return done(new Error(err_msg), null); }