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 e3b19c7
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
2 changes: 2 additions & 0 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 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 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
30 changes: 30 additions & 0 deletions test/outputs.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,36 @@ test('file outputs', function (t) {
});
});

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 Down

0 comments on commit e3b19c7

Please sign in to comment.