live-server icon indicating copy to clipboard operation
live-server copied to clipboard

Working dir resolution for middleware

Open rasmniel opened this issue 4 years ago • 2 comments

Prepend process.cwd() to to supplied middleware path in order to resolve the working directory from cli execution.

I'm unsure what the point of the bare require(mw) statement was, as I found it hard pressed to match anything useful other than absolute paths. If you believe this is required to maintain backwards compat, let me know and I will rethink the PR.

rasmniel avatar Oct 23 '19 12:10 rasmniel

path.resolve(process.cwd(), mw) would support absolute paths easily enough.

This would still be a breaking change tho, for the relative case. To maintain backwards compatibility we could check for both locations with a try-catch, and rethrow in the catch for errors that aren't not found errors.

var m;
try {
    m = require(modulePath);
} catch (e) {
    if (e.code !== 'MODULE_NOT_FOUND') {
        throw e;
    }
    m = require(modulePathLegacy);
}

That might be a little weird, but it could be for a deprecation period, or whatever.

1j01 avatar Oct 24 '19 19:10 1j01

Great idea! I've updated the PR as per your suggestion. It should now be possible to resolve middleware both relatively to working directory and relatively to the live-server installation folder. Absolute resolution is also still an option.

I checked that both clauses work. Feel free to verify that and let me know if you find errors. As for deprecation; I'll let someone else figure out how and when to remove the backwards compatible path resolution — if it should be removed at all 🤷‍♂

rasmniel avatar Oct 25 '19 09:10 rasmniel