Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-record tape if deleted #39

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -33,7 +34,20 @@ module.exports = function (host, opts) {
var file = path.join(opts.dirname, tapename(req, body));

return Promise.try(function () {
return require.resolve(file);
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
if (!fs.existsSync(fileName)) {
err = new Error('File does not exist');

err.code = 'MODULE_NOT_FOUND';
throw err;
}

return fileName;
}).catch(ModuleNotFoundError, function (/* err */) {

if (opts.noRecord) {
Expand Down