module.exports = function(gulp, settings) {
    var _ = require('lodash'),
        globby = require('globby'),
        path = require('path'),
        Promise = require('bluebird'),
        unirest = require('unirest');

    // Combine all documents into single array, under {"docs": []}.
    gulp.task("bulk-update", function() {
        var _logHeader = "bulk-update",
            pathGlobs = _.values(settings._docs),
            bulkDocs = {"docs": []},
            paths = globby.sync(pathGlobs);

        // Generate list of paths to documents that must be uploaded.
        _.each(paths, function(jsonPath) {
            bulkDocs.docs.push(require(path.join(process.cwd(), jsonPath)));
        });

        // Get docs matching keys of docs to add,
        // because they need to include the _rev value to force an update.
        var bulkKeys = {
            "keys": _.map(bulkDocs.docs, function(doc) {
                return doc._id;
            })
        };

        /**
         * Get all docs for the given keys (POST _all_docs).
         * @return {Promise}
         */
        var allDocsAsync = function() {
            return new Promise(function(resolve, reject) {
                unirest.post(settings.destination + "/_all_docs")
                    .headers({'Accept': 'application/json', 'Content-Type': 'application/json'})
                    .send(bulkKeys)
                    .end(function(response) {
                        console.log(_logHeader, "allDocsAsync OK"); //, JSON.stringify(bulkKeys));
                        resolve(response);
                    });
            });
        };

        /**
         * Update bulkDocs with _rev values.
         */
        var revUpdate = function(response) {
            var revsForDocs = {};

            // Put all rev values into object.
            _.each(response.body.rows, function(doc) {
                revsForDocs[doc.key] = doc.value.rev;
            });

            // Iterate through bulkDocs, update doc with rev.
            _.each(bulkDocs.docs, function(doc, idx) {
                if (_.has(revsForDocs, doc._id)) {
                    bulkDocs.docs[idx]._rev = revsForDocs[doc._id];
                }
            });

            console.log(_logHeader, "revUpdate OK");//, JSON.stringify(bulkDocs));
            return bulkDocs;
        };

        /**
         * Add or update docs
         * @param  {Object} bulkDocs CouchDB-ready payload for _bulkd_docs endpoint
         * @return {Promise}
         */
        var postBulkDocsAsync = function(bulkDocs) {
            return new Promise(function(resolve, reject) {
                // POST to _bulk_docs endpoint.
                unirest.post(settings.destination + "/_bulk_docs")
                    .headers({'Accept': 'application/json', 'Content-Type': 'application/json'})
                    .send(bulkDocs)
                    .end(function (response) {
                        // TODO: check that each doc has "ok": true response.
                        console.log(_logHeader, "postBulkDocsAsync OK", response.body);
                        resolve(response);
                    });
                });
        };

        // Run the promises in order.
        return allDocsAsync()
            .then(revUpdate)
            .then(postBulkDocsAsync);
    });
};

@@ -492,24 +492,52 @@
 athGlobs);%0A%0A
+        var debug = false;%0A%0A
         // G
@@ -594,16 +594,16 @@
 loaded.%0A
-
         
@@ -719,32 +719,112 @@
 );%0A        %7D);%0A%0A
+        if (debug) %7B%0A            console.info(_logHeader, bulkDocs);%0A        %7D%0A%0A
         // Get d
@@ -1065,32 +1065,117 @@
  %7D)%0A        %7D;%0A%0A
+        if (debug) %7B%0A            console.info(_logHeader, bulkKeys.keys);%0A        %7D%0A%0A
         /**%0A    
@@ -2053,32 +2053,320 @@
 function(doc) %7B%0A
+                if (debug) %7B%0A                    console.info(_logHeader, %22%5BrevUpdate%5D%22, doc);%0A                %7D%0A                // Make sure doc.value has %22rev%22. If a new doc is sent, it won't have a %22value%22 key.%0A                if (_.has(doc, %22value%22) && _.has(doc.value, %22rev%22)) %7B%0A    
                 
@@ -2403,16 +2403,34 @@
 ue.rev;%0A
+                %7D%0A
         

Fix bulk-update task for new docs