Skip to content
Browse files

initial index thing seems to work

  • Loading branch information...
0 parents commit f47ce96de35be654a1930ccb327e705c1dd3ce97 @substack committed Jun 14, 2012
Showing with 37 additions and 0 deletions.
  1. +37 −0 index.js
37 index.js
@@ -0,0 +1,37 @@
+var fs = require('fs');
+var path = require('path');
+var EventEmitter = require('events').EventEmitter;
+
+module.exports = function (files, opts) {
+ if (!opts) opts = {};
+ if (!files) files = [];
+ if (!Array.isArray(files)) files = [ files ];
+ if (opts.ext && !/^\./.test(opts.ext)) opts.ext = '.' + opts.ext;
+ var emitter = new EventEmitter;
+
+ var pending = files.length;
+ files.forEach(function (file) {
+ var ext = path.extname(file);
+ if (opts.ext && ext !== ext) return;
+ if (ext === '.js') return; // already a js file, NICE TRY
+ emitter.emit('started', file);
+
+ var rs = fs.createReadStream(file);
+ rs.on('error', emitter.emit.bind(emitter, 'error'));
+ var ws = fs.createWriteStream(file + '.js');
+ ws.on('error', emitter.emit.bind(emitter, 'error'));
+
+ ws.write('module.exports="');
+ rs.on('data', function (buf) {
+ var s = JSON.stringify(String(buf)).slice(1,-1); // chop off the "s
+ ws.write(s);
+ });
+ rs.on('end', function () {
+ ws.end('"');
+ emitter.emit('finished', file);
+ if (--pending === 0) emitter.emit('end');
+ });
+ });
+
+ return emitter;
+};

0 comments on commit f47ce96

Please sign in to comment.
Something went wrong with that request. Please try again.