Skip to content

Commit

Permalink
Ensure output directory exists before creating write stream
Browse files Browse the repository at this point in the history
  • Loading branch information
nwoltman committed Feb 6, 2016
1 parent ee80ac4 commit ed37438
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 29 deletions.
28 changes: 15 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var defined = require('defined');
var splicer = require('labeled-stream-splicer');
var outpipe = require('outpipe');
var isarray = require('isarray');
var mkdirp = require('mkdirp');

module.exports = function f (b, opts) {
if (!opts) opts = {};
Expand All @@ -26,9 +27,9 @@ module.exports = function f (b, opts) {
.concat(opts._).filter(Boolean);

var needRecords = !files.length;

var outopt = defined(opts.outputs, opts.output, opts.o);

opts.objectMode = true;
opts.raw = true;
opts.rmap = {};
Expand Down Expand Up @@ -59,6 +60,7 @@ module.exports = function f (b, opts) {
if (isarray(outopt)) {
outputs = outopt.map(function (o) {
if (isStream(o)) return o;
mkdirp.sync(path.dirname(o)); // ensure the containing directory exists
return fs.createWriteStream(o);
});
} else {
Expand All @@ -82,7 +84,7 @@ module.exports = function f (b, opts) {
outputs.push.apply(outputs, moreOutputs(x));
}
if (outputs[ix]) pipeline.pipe(outputs[ix]);

acc[path.resolve(cwd, x)] = pipeline;
return acc;
}, {});
Expand Down Expand Up @@ -118,10 +120,10 @@ module.exports = function f (b, opts) {

function createStream (files, opts) {
if (!opts) opts = {};

var fr = new Factor(files, opts);
var parse, dup;

if (opts.objectMode) {
dup = combine(depsTopoSort(), reverse(), fr);
}
Expand All @@ -135,7 +137,7 @@ function createStream (files, opts) {
;
parse.on('error', function (err) { dup.emit('error', err) });
}

fr.on('error', function (err) { dup.emit('error', err) });
fr.on('stream', function (s) {
if (opts.raw) dup.emit('stream', s)
Expand All @@ -150,21 +152,21 @@ function Factor (files, opts) {
var self = this;
if (!(this instanceof Factor)) return new Factor(files, opts);
Transform.call(this, { objectMode: true });

if (!opts) opts = {};
this.basedir = defined(opts.basedir, process.cwd());

this._streams = {};
this._groups = {};
this._buffered = {};

this._ensureCommon = {};
this._files = files.reduce(function (acc, file) {
acc[path.resolve(self.basedir, file)] = true;
return acc;
}, {});
this._rmap = opts.rmap || {};

this._thresholdVal = typeof opts.threshold === "number"
? opts.threshold : 1
;
Expand Down Expand Up @@ -200,9 +202,9 @@ Factor.prototype._transform = function (row, enc, next) {
self._streams[id].push(row);
});
}

next();

function addGroups (gid) {
Object.keys(row.deps || {}).forEach(function (key) {
var file = row.deps[key];
Expand All @@ -215,7 +217,7 @@ Factor.prototype._transform = function (row, enc, next) {

Factor.prototype._flush = function () {
var self = this;

Object.keys(self._streams).forEach(function (key) {
self._streams[key].push(null);
});
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"isarray": "0.0.1",
"labeled-stream-splicer": "^1.0.0",
"minimist": "~0.2.0",
"mkdirp": "~0.5.1",
"nub": "0.0.0",
"outpipe": "^1.1.0",
"reversepoint": "~0.2.0",
Expand All @@ -24,7 +25,6 @@
"browser-unpack": "^1.1.1",
"browserify": "^11.0.1",
"concat-stream": "^1.4.6",
"mkdirp": "~0.5.0",
"module-deps": "^3.9.0",
"osenv": "^0.1.0",
"tape": "^4.0.1",
Expand Down
62 changes: 47 additions & 15 deletions test/outputs.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,54 @@ test('file outputs', function (t) {
});
var w = fs.createWriteStream(path.join(tmpdir, 'common.js'));
b.bundle().pipe(w);

w.on('finish', function () {
var common = fs.readFileSync(tmpdir + '/common.js', 'utf8');
var x = fs.readFileSync(tmpdir + '/x.js', 'utf8');
var y = fs.readFileSync(tmpdir + '/y.js', 'utf8');


vm.runInNewContext(common + x, { console: { log: function (msg) {
t.equal(msg, 55500);
} } });

vm.runInNewContext(common + y, { console: { log: function (msg) {
t.equal(msg, 333);
} } });
});
});


test('file outputs - new folder', function (t) {
var tmpdir = tmp() + '/factor-bundle-' + Math.random();
mkdirp.sync(tmpdir);

t.plan(2);
var b = browserify(files);
b.plugin(factor, {
outputs: [
path.join(tmpdir, 'deps/x.js'),
path.join(tmpdir, 'deps/y.js')
]
});
var w = fs.createWriteStream(path.join(tmpdir, 'common.js'));
b.bundle().pipe(w);

w.on('finish', function () {
var common = fs.readFileSync(tmpdir + '/common.js', 'utf8');
var x = fs.readFileSync(path.join(tmpdir, 'deps/x.js'), 'utf8');
var y = fs.readFileSync(path.join(tmpdir, 'deps/y.js'), 'utf8');

vm.runInNewContext(common + x, { console: { log: function (msg) {
t.equal(msg, 55500);
} } });

vm.runInNewContext(common + y, { console: { log: function (msg) {
t.equal(msg, 333);
} } });
});
});


test('stream outputs', function (t) {
var tmpdir = tmp() + '/factor-bundle-' + Math.random();
mkdirp.sync(tmpdir);
Expand All @@ -55,18 +87,18 @@ test('stream outputs', function (t) {
if (-- pending === 0) done();
});
}

var b = browserify(files);
b.plugin(factor, { outputs: [ write('x'), write('y') ] });
b.bundle().pipe(write('common'));

function done () {
var common = sources.common, x = sources.x, y = sources.y;

vm.runInNewContext(common + x, { console: { log: function (msg) {
t.equal(msg, 55500);
} } });

vm.runInNewContext(common + y, { console: { log: function (msg) {
t.equal(msg, 333);
} } });
Expand All @@ -84,16 +116,16 @@ test('function outputs', function (t) {
});
var w = fs.createWriteStream(path.join(tmpdir, 'common.js'));
b.bundle().pipe(w);

w.on('finish', function () {
var common = fs.readFileSync(tmpdir + '/common.js', 'utf8');
var x = fs.readFileSync(tmpdir + '/x.js', 'utf8');
var y = fs.readFileSync(tmpdir + '/y.js', 'utf8');

vm.runInNewContext(common + x, { console: { log: function (msg) {
t.equal(msg, 55500);
} } });

vm.runInNewContext(common + y, { console: { log: function (msg) {
t.equal(msg, 333);
} } });
Expand Down Expand Up @@ -132,11 +164,11 @@ test('bundle twice', function (t) {
var common = fs.readFileSync(tmpdir + '/common.js', 'utf8');
var x = fs.readFileSync(tmpdir + '/x.js', 'utf8');
var y = fs.readFileSync(tmpdir + '/y.js', 'utf8');

vm.runInNewContext(common + x, { console: { log: function (msg) {
t.equal(msg, xVal);
} } });

vm.runInNewContext(common + y, { console: { log: function (msg) {
t.equal(msg, yVal);
} } });
Expand All @@ -156,16 +188,16 @@ test('outpipe outputs', function (t) {
});
var w = fs.createWriteStream(path.join(tmpdir, 'common.js'));
b.bundle().pipe(w);

w.on('finish', function () {
var common = fs.readFileSync(tmpdir + '/common.js', 'utf8');
var x = fs.readFileSync(tmpdir + '/x.js', 'utf8');
var y = fs.readFileSync(tmpdir + '/y.js', 'utf8');

vm.runInNewContext(common + x, { console: { log: function (msg) {
t.equal(msg, 55500);
} } });

vm.runInNewContext(common + y, { console: { log: function (msg) {
t.equal(msg, 333);
} } });
Expand Down

0 comments on commit ed37438

Please sign in to comment.