Created
November 5, 2011 13:56
-
-
Save josephg/1341527 to your computer and use it in GitHub Desktop.
Script to automatically re-save a sharejs document as its edited.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This script watches for changes in a document and constantly resaves a file | |
# with the document's contents. | |
# | |
# Usage: coffee saver.coffee -d <DOCNAME> [--url http://somehost:8000/sjs] [-f filename] | |
client = require('share').client | |
fs = require('fs') | |
argv = require('optimist') | |
.usage('Usage: $0 -d docname [--url URL] [-f filename]') | |
.default('d', 'hello') | |
.default('url', 'http://sharejs.org:8000/sjs') | |
.argv | |
filename = argv.f || argv.d | |
console.log "Opening '#{argv.d}' at #{argv.url}. Saving to '#{filename}'" | |
timeout = null | |
doc = null | |
# Rate limit writes to the file at once per second. | |
write = -> | |
if (timeout == null) | |
timeout = setTimeout -> | |
console.log "Saved version " + doc.version | |
fs.writeFile filename, doc.snapshot | |
timeout = null | |
, 1000 | |
client.open argv.d, 'text', argv.url, (error, d) -> | |
doc = d | |
console.log('Document ' + argv.d + ' open at version ' + doc.version) | |
write() | |
doc.on 'change', (op) -> | |
write() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
==666
*ff|
ITAL
OK
Sorry