Skip to content

Instantly share code, notes, and snippets.

@nick-thompson
Created August 8, 2013 16:08
Show Gist options
  • Save nick-thompson/6185976 to your computer and use it in GitHub Desktop.
Save nick-thompson/6185976 to your computer and use it in GitHub Desktop.
{
"source": "http://isaacs.iriscouch.com/registry",
"target": "https://nick-thompson.cloudant.com/registry",
"doc_ids": ["_design/app"]
}
bootstrap:
@curl -X POST -H 'Content-Type: application/json' -d @bootstrap.json \
https://nick-thompson.cloudant.com/_replicate
update:
@./scripts/update.js
#!/usr/bin/env node
var follow = require('follow')
, nano = require('nano')
, npm = nano('http://isaacs.iriscouch.com/registry')
, wac = nano('http://nick-thompson.cloudant.com/registry');
/**
* Fetch a package at a particular revision from NPM and
* write it to the WAC registry.
*
* @param {string} id
* @param {string} rev
* @param {function} callback (err, response)
*/
function push (id, rev, callback) {
npm.get(id, { revs: true, rev: rev, attachments: true }, function (err, doc) {
if (err) return callback.call(null, err);
var latest = doc['dist-tags']['latest']
, pkg = doc['versions'][latest];
if (doc._id === '_design/app' || pkg.hasOwnProperty('web-audio')) {
wac.bulk({
docs: [doc],
new_edits: false
}, callback);
}
});
}
// Open and follow a continuous changes stream from NPM
// and update our registry every time a change registers.
follow({
db: 'http://isaacs.iriscouch.com/registry',
since: 'now',
include_docs: true
}, function (err, change) {
if (err) return console.log(err);
if (change.deleted) return false;
change.changes.forEach(function (o) {
push(change.id, o.rev, function (err, res) {
if (err) return console.log('Error: ' + JSON.stringify(err));
console.log(res);
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment