Repository: GoogleChrome/sw-precache (js)

Issue:
Defining a directory that doesn't exist
If you attempt to write to a path that doesn't exist at the time its run, swPrecache.write throws:

Error: ENOENT: no such file or directory, open '<Directory>/service-worker.js'

Pull request:
Use mkdirp to create parent directory inside write() if needed
R: @gauntface @addyosmani 

Closes #42 

Patch:
diff --git a/lib/sw-precache.js b/lib/sw-precache.js
index 79406ee..83f84e0 100644
--- a/lib/sw-precache.js
+++ b/lib/sw-precache.js
@@ -22,6 +22,7 @@ var defaults = require('lodash.defaults');
 var externalFunctions = require('./functions.js');
 var fs = require('fs');
 var glob = require('glob');
+var mkdirp = require('mkdirp');
 var path = require('path');
 var prettyBytes = require('pretty-bytes');
 var template = require('lodash.template');
@@ -196,6 +197,8 @@ function write(filePath, params, callback) {
       }
     }
 
+    mkdirp.sync(path.dirname(filePath));
+
     generate(params).then(function(serviceWorkerFileContents) {
       fs.writeFile(filePath, serviceWorkerFileContents, finish);
     }, finish);
diff --git a/package.json b/package.json
index e46832c..015e259 100644
--- a/package.json
+++ b/package.json
@@ -41,6 +41,7 @@
     "lodash.defaults": "^3.1.1",
     "lodash.template": "^3.6.1",
     "meow": "^3.3.0",
+    "mkdirp": "^0.5.1",
     "pretty-bytes": "^2.0.1"
   },
   "repository": "googlechrome/sw-precache",


Test patch:
diff --git a/test/test.js b/test/test.js
index 4e3ccf1..12d049c 100644
--- a/test/test.js
+++ b/test/test.js
@@ -21,6 +21,7 @@ var assert = require('assert');
 var externalFunctions = require('../lib/functions.js');
 var fs = require('fs');
 var generate = require('../lib/sw-precache.js').generate;
+var path = require('path');
 var write = require('../lib/sw-precache.js').write;
 
 var NOOP = function() {};
@@ -174,6 +175,27 @@ describe('sw-precache write functionality', function() {
   });
 });
 
+describe('sw-precache write functionality with missing parent directory', function() {
+  var SW_FILE = 'test/data/new_directory/generated_sw.js';
+
+  it('should write to disk, creating a new parent directory', function(done) {
+    write(SW_FILE, {logger: NOOP}, function(error) {
+      assert.ifError(error);
+      fs.stat(SW_FILE, function(error, stats) {
+        assert.ifError(error);
+        assert(stats.isFile(), 'file exists');
+        assert(stats.size > 0, 'file contains data');
+        done();
+      });
+    });
+  });
+
+  after(function() {
+    fs.unlinkSync(SW_FILE);
+    fs.rmdirSync(path.dirname(SW_FILE));
+  });
+});
+
 describe('sw-precache parameters', function() {
   it('should exclude files larger than maximumFileSizeToCacheInBytes', function(done) {
     var file = 'test/data/one/a.txt';