From 53f50f6b9c4e80b5d4ea2cfd22d7fbd7dae09239 Mon Sep 17 00:00:00 2001 From: Chandra Sivaraman Date: Tue, 21 Nov 2017 21:45:29 -0800 Subject: [PATCH 1/3] Re-record tape if deleted --- index.js | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 6a1fdea..4ee882b 100644 --- a/index.js +++ b/index.js @@ -11,6 +11,7 @@ var proxy = require('./lib/proxy'); var record = require('./lib/record'); var curl = require('./lib/curl'); var debug = require('debug')('yakbak:server'); +var fs = require('fs'); /** * Returns a new yakbak proxy middleware. @@ -33,7 +34,18 @@ module.exports = function (host, opts) { var file = path.join(opts.dirname, tapename(req, body)); return Promise.try(function () { - return require.resolve(file); + const fileName = require.resolve(file); + + // If tape was deleted, then throw module not found error + // so that it can be re-recorded instead of failing on + // require. + if (!fs.existsSync(fileName)) { + const err = new Error('File does not exist'); + err.code = 'MODULE_NOT_FOUND'; + throw err; + } + + return fileName; }).catch(ModuleNotFoundError, function (/* err */) { if (opts.noRecord) { From de23bebaba20e9d2d6e93cdebb67ffd9b47c4827 Mon Sep 17 00:00:00 2001 From: Chandra Sivaraman Date: Tue, 21 Nov 2017 22:00:19 -0800 Subject: [PATCH 2/3] Fix compile error --- index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 4ee882b..14a9842 100644 --- a/index.js +++ b/index.js @@ -34,13 +34,13 @@ module.exports = function (host, opts) { var file = path.join(opts.dirname, tapename(req, body)); return Promise.try(function () { - const fileName = require.resolve(file); + var fileName = require.resolve(file); // If tape was deleted, then throw module not found error // so that it can be re-recorded instead of failing on // require. if (!fs.existsSync(fileName)) { - const err = new Error('File does not exist'); + var err = new Error('File does not exist'); err.code = 'MODULE_NOT_FOUND'; throw err; } From 5d5506baa74e8fbdc5e1c767691eba32b0e26e7f Mon Sep 17 00:00:00 2001 From: Chandra Sivaraman Date: Tue, 21 Nov 2017 22:05:16 -0800 Subject: [PATCH 3/3] Fix linter issues --- index.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 14a9842..88a5144 100644 --- a/index.js +++ b/index.js @@ -35,12 +35,14 @@ module.exports = function (host, opts) { return Promise.try(function () { var fileName = require.resolve(file); - + var err; + // If tape was deleted, then throw module not found error - // so that it can be re-recorded instead of failing on - // require. + // so that it can be re-recorded instead of failing on + // require if (!fs.existsSync(fileName)) { - var err = new Error('File does not exist'); + err = new Error('File does not exist'); + err.code = 'MODULE_NOT_FOUND'; throw err; }