Skip to content

Commit

Permalink
Merge branch 'patch-1' of https://github.com/natevw/node-browserify
Browse files Browse the repository at this point in the history
  • Loading branch information
substack authored and substack committed Feb 14, 2017
2 parents 691239d + ea6c299 commit 67ea8b1
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion bin/cmd.js
Expand Up @@ -54,16 +54,23 @@ if (b.argv.list) {

var bundle = b.bundle();
bundle.on('error', errorExit);
bundle.on('end', successExit);

var tmpfile;
var outfile = b.argv.o || b.argv.outfile;
if (outfile) {
bundle.pipe(fs.createWriteStream(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));
}
else {
bundle.pipe(process.stdout);
}

function errorExit(err) {
if (tmpfile) fs.unlink(tmpfile, function (err) {
if (err) /* no-op, we're already exiting unhappily… */;
});
if (err.stack) {
console.error(err.stack);
}
Expand All @@ -72,3 +79,9 @@ function errorExit(err) {
}
process.exit(1);
}

function successExit() {
if (tmpfile && outfile) fs.rename(tmpfile, outfile, function (err) {
if (err) errorExit(err);
});
}

0 comments on commit 67ea8b1

Please sign in to comment.