Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure output directory exists before creating write stream #84

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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