substack/node-browserify — pull request #995 by jackwanders (2014-11-16 00:19:05+00:00)
Title: Ensure path to outfile exists before opening stream

I found today that the `--outfile` option for browserify will fail when the user attempts to output the browserify output to a file in a non-existent directory. For example, when running `browserify ./my/src/script.js --o ./dist/bundle.js`, the following error occurs when `./dist/` doesn't exist:

```bash
events.js:72
throw er; // Unhandled 'error' event
^
Error: ENOENT, open './dist/bundle.js'
Warning: Command failed:
events.js:72
throw er; // Unhandled 'error' event
^
Error: ENOENT, open './dist/bundle.js'
Use --force to continue.
```

This seems like a common enough case (e.g. browserify may run during a build that contains a clean step that blows away the intended destination directory) that I believe browserify should simply make the directory rather than blow up when `fs.createWriteStream` fails.

This pull request uses `mkdirp` to ensure the outfile directory exists before opening the stream, and modifies a test case that would fail without this change.