Skip to content

Commit

Permalink
transform stream
Browse files Browse the repository at this point in the history
  • Loading branch information
James Halliday committed Jul 20, 2014
1 parent 4f80da9 commit 999b5c8
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 61 deletions.
8 changes: 3 additions & 5 deletions example/deps.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
var mdeps = require('../');
var JSONStream = require('JSONStream');

var stringify = JSONStream.stringify();
stringify.pipe(process.stdout);

var file = __dirname + '/files/main.js';
mdeps(file).pipe(stringify);
var md = mdeps();
md.pipe(JSONStream.stringify()).pipe(process.stdout);
md.end({ file: __dirname + '/files/main.js' });
78 changes: 22 additions & 56 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,17 @@ var combine = require('stream-combiner');
var duplexer = require('duplexer2');

var inherits = require('inherits');
var Readable = require('readable-stream').Readable;
var Transform = require('readable-stream').Transform;

module.exports = Deps;
inherits(Deps, Readable);
inherits(Deps, Transform);

function Deps (mains, opts) {
function Deps (opts) {
var self = this;
if (!(this instanceof Deps)) return new Deps(mains, opts);
Readable.call(this, { objectMode: true });
if (!(this instanceof Deps)) return new Deps(opts);
Transform.call(this, { objectMode: true });

if (!opts) opts = {};
if (!Array.isArray(mains)) mains = [ mains ].filter(Boolean);

this.basedir = opts.basedir || process.cwd();
this.cache = opts.cache;
Expand All @@ -32,76 +31,43 @@ function Deps (mains, opts) {
this.pkgFileCachePending = {};
this.visited = {};
this.walking = {};
this.entries = [];

this.paths = opts.paths || process.env.NODE_PATH;
if (typeof this.paths === 'string') {
this.paths = process.env.NODE_PATH.split(':');
}
if (!this.paths) this.paths = [];

this.entries = [];
this.mains = [];

this.transforms = [].concat(opts.transform).filter(Boolean);
this.resolver = opts.resolve || browserResolve;
this.options = opts;
this.pending = 0;
this.top = { id: '/', filename: '/', paths: this.paths };

mains.forEach(function (file) { self.add(file) });
}

Deps.prototype._read = function () {
if (this._started) return;
this._started = true;
this._start();
};

Deps.prototype._start = function () {
Deps.prototype._transform = function (row, enc, next) {
var self = this;
self.pending ++;
if (row.entry) self.entries.push(row.file);

if (this.entries.length === 0) {
return this.push(null);
}

for (var i = 0; i < this.entries.length; i++) (function (i) {
var main = self.mains[i];
var file = self.entries[i];

self.lookupPackage(file, function (err, pkg) {
if (err) return self.emit('error', err)
else start(main, file, pkg)
});
})(i);
self.lookupPackage(row.file, function (err, pkg) {
if (err) return self.emit('error', err)
self.pending --;
start(pkg)
});
next();

function start (main, file, pkg) {
function start (pkg) {
if (!pkg) pkg = {};
if (!pkg.__dirname) pkg.__dirname = path.dirname(file);

if (typeof main === 'object') {
self.walk({ stream: main, file: main.path || file }, main);
}
else self.walk(main, self.top);
if (!pkg.__dirname) pkg.__dirname = path.dirname(row.file);
self.walk(row.file, self.top);
}
};

Deps.prototype.add = function (main) {
var self = this;

var file;
if (typeof main.pipe === 'function') {
var n = Math.floor(Math.pow(16,8) * Math.random()).toString(16);
file = path.join(this.basedir, 'fake_' + n + '.js');
if (typeof main.read !== 'function') {
var old = main;
main = Readable().wrap(main);
if (old.path) main.path = old.path;
}
}
else file = main;
file = path.resolve(file);
this.mains.push(main);
this.entries.push(file);
Deps.prototype._flush = function () {
if (this.pending === 0) this.push(null);
this._ended = true;
};

Deps.prototype.resolve = function (id, parent, cb) {
Expand Down Expand Up @@ -319,7 +285,7 @@ Deps.prototype.walk = function (id, parent, cb) {
self.push(rec);

if (cb) cb(null, file);
if (-- self.pending === 0) self.push(null);
if (-- self.pending === 0 && self._ended) self.push(null);
}
}
};
Expand Down

0 comments on commit 999b5c8

Please sign in to comment.