Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

seems to work

  • Loading branch information...
commit 79b719ba01038a95d8eb997b0449763af1dca54b 0 parents
James Halliday authored
Showing with 96 additions and 0 deletions.
  1. +44 −0 bin/autorev.js
  2. +52 −0 index.js
44 bin/autorev.js
@@ -0,0 +1,44 @@
+#!/usr/bin/env node
+var autorev = require('../');
+var fs = require('fs');
+var vm = require('vm');
+
+if (process.argv.length <= 2) {
+ console.error([
+ 'Usage:',
+ '',
+ ' autorev uri',
+ ' read docs from stdin',
+ '',
+ ' autorev uri [file,...]',
+ ' read docs from filenames',
+ ].join('\n'));
+}
+else {
+ var uri = process.argv[2];
+ var streams = [];
+
+ if (process.argv.length === 3 || process.argv[4] === '-') {
+ streams.push(process.stdin);
+ }
+ else {
+ process.argv.slice(3).forEach(function (file) {
+ streams.push(fs.createReadStream(file));
+ });
+ }
+
+ streams.forEach(function (s) {
+ var data = '';
+ s.on('data', function (buf) { data += buf });
+ s.on('end', function () {
+ try {
+ var doc = JSON.parse(data);
+ }
+ catch (err0) {
+ var doc = vm.runInNewContext('(' + data + ')');
+ }
+ autorev(uri, doc).pipe(process.stdout, { end : false });
+ });
+ s.resume();
+ });
+}
52 index.js
@@ -0,0 +1,52 @@
+var request = require('request');
+var deepEqual = require('deep-equal');
+var traverse = require('traverse');
+var Stream = require('stream').Stream;
+
+module.exports = function autorev (uri, doc) {
+ if (doc === undefined) return autorev.bind(null, uri);
+ var u = uri.replace(/\/$/, '') + '/';
+
+ var out = new Stream;
+ out.readable = true;
+ out.writable = true;
+ out.write = function (buf) { out.emit('data', buf) };
+ out.end = function (buf) {
+ if (buf !== undefined) out.write(buf)
+ out.emit('end');
+ };
+
+ request.get(u + doc._id, function (err, res, body) {
+ if (err || res.statusCode !== 200) {
+ var res = request.put({
+ uri : u + doc._id,
+ headers : { 'content-type' : 'application/json' },
+ });
+ res.end(stringify(doc));
+ res.pipe(out);
+ }
+ else {
+ var prevDoc = JSON.parse(body);
+ doc._rev = prevDoc._rev;
+ if (deepEqual(doc, prevDoc)) {
+ out.end('# not updated\n');
+ }
+ else {
+ var res = request.put({
+ uri : u + doc._id,
+ headers : { 'content-type' : 'application/json' },
+ });
+ res.end(stringify(doc));
+ res.pipe(out);
+ }
+ }
+ });
+
+ return out;
+};
+
+function stringify (obj) {
+ return JSON.stringify(traverse(obj).map(function (node) {
+ if (typeof node === 'function') return node.toString();
+ }));
+}
Please sign in to comment.
Something went wrong with that request. Please try again.