From: dsc Date: Tue, 17 Apr 2012 18:26:40 +0000 (-0700) Subject: Async read multiple files. X-Git-Url: http://git.less.ly:3516/?a=commitdiff_plain;h=8477f5639eea808d9def4979bfaf96e616466d95;p=limn-bak.git Async read multiple files. --- diff --git a/lib/server/files.co b/lib/server/files.co new file mode 100644 index 0000000..aefb548 --- /dev/null +++ b/lib/server/files.co @@ -0,0 +1,53 @@ +/** + * @fileOverview Filesystem utilities. + */ + + +fs = require 'fs' +path = require 'path' + +_ = require 'underscore' +Seq = require 'seq' + + + +/** + * Asynchronously reads each filepath supplied, returning a map + * from filepath to contents. + * + * @param {Array} files List of paths to read. + * @param {Function} cb Callback taking `(error, data)` where `data` is a map + * from filepath to contents. As always, `error` will be null on success. + * @returns {Seq} The Seq object representing the async operation chain. (You + * can usually ignore this.) + */ +readFilesAsync = exports.readFilesAsync = (files, cb) -> + data = {} + Seq files + .parMap (f) -> + # console.log "fs.readFile '#CWD/data/#f'" + fs.readFile f, 'utf8', this + .parEach (text, i) -> + f = files[i] + data[f] = text + @ok() + .seq -> + # Success! + cb null, data + .catch (err) -> + console.error '[readFilesAsync] catch!' + console.error err + console.error that if err.stack + cb err + + + + +# _ = require 'underscore' +# files = require 'kraken/server/files' +# files.readFilesAsync <[ package.co package.json deploy.sh ]>, (err, data) -> +# if err +# console.error err +# else +# console.log _.map data, (txt, f) -> "#f: #{txt.length}" +