This commit is contained in:
Diana Thayer 2014-10-06 20:38:22 +00:00
commit 5604e604a3
5 changed files with 150 additions and 84 deletions

1
.gitignore vendored
View File

@ -3,3 +3,4 @@
*~ *~
npm-debug.log npm-debug.log
node_modules/* node_modules/*
.idea

View File

@ -1,4 +1,6 @@
Copyright (c) 2012 Ken Elf Mathieu Sternberg Copyright (c)
2012 Ken Elf Mathieu Sternberg
2014 Sam Hiatt
Permission is hereby granted, free of charge, to any person Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation obtaining a copy of this software and associated documentation

View File

@ -10,9 +10,7 @@ grunt-couchapp`
Then add this line to your project's `grunt.js` gruntfile: Then add this line to your project's `grunt.js` gruntfile:
```javascript grunt.loadNpmTasks('grunt-couchapp');
grunt.loadNpmTasks('grunt-couchapp');
```
[grunt]: https://github.com/cowboy/grunt [grunt]: https://github.com/cowboy/grunt
[getting_started]: https://github.com/cowboy/grunt/blob/master/docs/getting_started.md [getting_started]: https://github.com/cowboy/grunt/blob/master/docs/getting_started.md
@ -26,7 +24,10 @@ which installs a specified couchapp into the database.
mkcouchdb: { mkcouchdb: {
demo: { demo: {
db: 'http://localhost:5984/grunt-couchapp-demo' db: 'http://localhost:5984/grunt-couchapp-demo',
options: {
okay_if_exists: true
}
} }
}, },
@ -54,7 +55,8 @@ possible to write in your configuration file:
db: 'http://localhost:5984/grunt-couchapp-demo', db: 'http://localhost:5984/grunt-couchapp-demo',
app: './demo/app.js', app: './demo/app.js',
options: { options: {
okay_if_missing: true okay_if_missing: true,
okay_if_exists: true
} }
} }
} }
@ -68,8 +70,7 @@ possible to write in your configuration file:
}); });
Note, however, that if you call 'rmcouchdb' without a sub-argument, in Note that if you call `rmcouchdb` without a sub-argument, it will not delete any databases.
keeping with grunt's standards, it will drop *all* of your databases!
## Demo ## Demo

View File

@ -1,43 +1,56 @@
{ {
"name": "grunt-couchapp", "name": "grunt-couchapp",
"description": "A grunt plugin for building and uploading couchapps", "description": "A grunt plugin for building and uploading couchapps",
"version": "0.1.0", "version": "0.2.2",
"homepage": "https://github.com/elf/grunt-couchapp", "homepage": "https://github.com/garbados/grunt-couchapp",
"author": { "author": {
"name": "Ken Elf Mathieu Sternberg", "name": "Ken Elf Mathieu Sternberg",
"email": "elf.sternberg@gmail.com", "email": "elf.sternberg@gmail.com",
"url": "http://elfsternberg.com" "url": "http://elfsternberg.com"
},
"contributors": [
{
"name": "Max Thayer",
"email": "garbados@gmail.com",
"url": "http://maxthayer.org"
}, },
"repository": { {
"type": "git", "name":"Sam Hiatt",
"url": "git://github.com/elf/grunt-couchapp.git" "email": "sam.hiatt@weather.com"
}, }
"bugs": { ],
"url": "https://github.com/elf/grunt-couchapp/issues" "repository": {
}, "type": "git",
"licenses": [ "url": "git://github.com/garbados/grunt-couchapp.git"
{ },
"type": "MIT", "bugs": {
"url": "https://github.com/elf/grunt-couchapp/blob/master/LICENSE-MIT" "url": "https://github.com/garbados/grunt-couchapp/issues"
} },
], "licenses": [
"main": "grunt.js", {
"bin": "bin/grunt-couchapp", "type": "MIT",
"engines": { "url": "https://github.com/garbados/grunt-couchapp/blob/master/LICENSE-MIT"
"node": "*" }
}, ],
"scripts": { "main": "grunt.js",
"test": "grunt test" "bin": "bin/grunt-couchapp",
}, "engines": {
"dependencies": { "node": "*"
"couchapp": "0.9.1", },
"grunt": "~0.3.12", "scripts": {
"nano": "3.3.0" "test": "grunt test"
}, },
"devDependencies": { "dependencies": {
"grunt": "~0.3.12" "couchapp": "~0.11.0",
}, "grunt": "~0.3.12",
"keywords": [ "nano": "3.3.0"
"gruntplugin" },
] "devDependencies": {
} "grunt": "~0.3.12"
},
"keywords": [
"gruntplugin",
"couchdb",
"couchapp"
]
}

119
tasks/couchapp.js Normal file → Executable file
View File

@ -11,6 +11,18 @@ path = require('path');
couchapp = require('couchapp'); couchapp = require('couchapp');
urls = require('url'); urls = require('url');
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
};
};
module.exports = function(grunt) { module.exports = function(grunt) {
// ========================================================================== // ==========================================================================
@ -20,66 +32,103 @@ module.exports = function(grunt) {
grunt.registerMultiTask("couchapp", "Install Couchapp", function() { grunt.registerMultiTask("couchapp", "Install Couchapp", function() {
var appobj, done; var appobj, done;
done = this.async(); done = this.async();
appobj = require(path.join(process.cwd(), path.normalize(this.data.app))); if (require("fs").lstatSync(this.data.app).isDirectory()) { // if new-style (directory-based) couchapp app
appobj = couchapp.loadFiles(this.data.app);
delete appobj._attachments;
delete appobj[''];
couchapp.loadAttachments(appobj, this.data.app+"/_attachments");
} else { // otherwise, fall back to old style.
appobj = require(path.join(process.cwd(), path.normalize(this.data.app)))
}
return couchapp.createApp(appobj, this.data.db, function(app) { return couchapp.createApp(appobj, this.data.db, function(app) {
return app.push(done); return app.push(done);
}); });
}); });
grunt.registerMultiTask("rmcouchdb", "Delete a Couch Database", function() { grunt.registerMultiTask("rmcouchdb", "Delete a Couch Database", function() {
var done, parts, nano, dbname, _this; var done, parts, nano, dbname, _this, db;
_this = this; _this = this;
done = this.async(); done = this.async();
parts = urls.parse(this.data.db); db = genDB(this.data.db);
dbname = parts.pathname.replace(/^\//, '');
try { try {
nano = require('nano')(parts.protocol + '//' + parts.host); nano = require('nano')(db.url);
nano.db.destroy(dbname, function(err) { if (db.name) {
if (err) { nano.db.destroy(db.name, function(err) {
if (err.status_code && err.status_code === 404) { if (err) {
if (_this.data.options && _this.data.options.okay_if_missing) { if (err.status_code && err.status_code === 404) {
grunt.log.writeln("Database " + dbname + " not present... skipping."); if (_this.data.options && _this.data.options.okay_if_missing) {
return done(null, null) ; grunt.log.writeln("Database " + db.name + " not present... skipping.");
return done(null, null) ;
} else {
throw ("Database " + db.name + " does not exist.");
}
} else { } else {
grunt.warn("Database " + dbname + " does not exist."); throw err;
} }
} else {
grunt.warn(err);
} }
} // remove db was a success
return done(err, null); return done();
}); });
} else {
grunt.log.writeln("No database specified... skipping.");
return done(null, null);
}
} catch (e) { } catch (e) {
grunt.warn(e); grunt.warn(e);
done(e, null); return done(e, null);
} }
return null;
}); });
grunt.registerMultiTask("mkcouchdb", "Delete a Couch Database", function() { grunt.registerMultiTask("mkcouchdb", "Make a Couch Database", function() {
var done, parts, nano, dbname, _this; var done, parts, nano, dbname, _this, db;
_this = this; _this = this;
done = this.async(); done = this.async();
parts = urls.parse(this.data.db); parts = urls.parse(this.data.db);
dbname = parts.pathname.replace(/^\//, ''); db = genDB(this.data.db);
try { try {
nano = require('nano')(parts.protocol + '//' + parts.host); if (db.name) {
nano.db.create(dbname, function(err) { nano = require('nano')(db.url);
if (_this.data.options && _this.data.options.okay_if_exists) { nano.db.create(db.name, function(err, res) {
if (err){ if (err) {
grunt.log.writeln("Database " + dbname + " exists, skipping"); if (err.error && err.error=="unauthorized") {
grunt.warn(err.reason);
throw err;
} else if (err.code && err.description) { // probably connection error
throw err;
}
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;
}
} else {
console.warn("Unrecognized error.");
throw err
}
} else if (res && res.ok==true) {
grunt.log.ok("Database " + db.name + " created.");
return done(null, null);
} else {
console.log("Unexpected response received.");
console.dir(res);
throw "Unexpected response";
} }
return done(null, null); });
} else { } else {
if (err){ var err_msg = "No database specified to create!";
grunt.warn(err); throw err_msg;
} }
return done(err, null);
}
});
} catch (e) { } catch (e) {
grunt.warn(e); grunt.warn(e);
done(e, null); return done(e, null);
} }
return null;
}); });
}; };