Skip to content

Commit

Permalink
Merge pull request #995 from jackwanders/allow-outfile-in-new-directory
Browse files Browse the repository at this point in the history
Ensure path to outfile exists before opening stream
  • Loading branch information
goto-bus-stop committed Jan 14, 2018
2 parents 6dbd142 + 5c85031 commit 672cc64
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
4 changes: 4 additions & 0 deletions bin/cmd.js
Expand Up @@ -2,6 +2,8 @@
var fs = require('fs');
var JSONStream = require('JSONStream');
var through = require('through2');
var mkdirp = require('mkdirp');
var path = require('path');

var b = require('./args')(process.argv.slice(2));
process.stdout.on('error', process.exit);
Expand Down Expand Up @@ -59,6 +61,8 @@ bundle.on('end', successExit);
var tmpfile;
var outfile = b.argv.o || b.argv.outfile;
if (outfile) {
mkdirp.sync(path.dirname(outfile));

// we'll output to a temp file within same filesystem, then atomically overwrite outfile once successful
tmpfile = outfile + ".tmp-browserify-" + Math.random().toFixed(20).slice(2)
bundle.pipe(fs.createWriteStream(tmpfile));
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -46,6 +46,7 @@
"inherits": "~2.0.1",
"insert-module-globals": "^7.0.0",
"labeled-stream-splicer": "^2.0.0",
"mkdirp": "^0.5.0",
"module-deps": "^5.0.1",
"os-browserify": "~0.3.0",
"parents": "^1.0.1",
Expand Down
8 changes: 4 additions & 4 deletions test/externalize.js
Expand Up @@ -26,18 +26,18 @@ fs.writeFileSync(

test('externalize bin', function (t) {
t.plan(5);

var commands = [
[ '-r', './robot.js', '-o', path.join(pubdir, 'common.js') ],
[ '-x', './robot.js', 'beep.js', '-o', path.join(pubdir, 'beep.js') ],
[ '-x', './robot.js', 'boop.js', '-o', path.join(pubdir, 'boop.js') ]
[ '-x', './robot.js', 'boop.js', '-o', path.join(pubdir, 'boop/bop.js') ]
];
(function next () {
if (commands.length === 0) {
var common = fs.readFileSync(path.join(pubdir, 'common.js'));
var beep = fs.readFileSync(path.join(pubdir, 'beep.js'));
var boop = fs.readFileSync(path.join(pubdir, 'boop.js'));
var boop = fs.readFileSync(path.join(pubdir, 'boop/bop.js'));

vm.runInNewContext(common + beep, {
console: { log: function (msg) { t.equal(msg, 'BEEP!') } }
});
Expand Down

0 comments on commit 672cc64

Please sign in to comment.