diff --git a/.changeset/nasty-cats-live.md b/.changeset/nasty-cats-live.md new file mode 100644 index 0000000000..81803b65b6 --- /dev/null +++ b/.changeset/nasty-cats-live.md @@ -0,0 +1,5 @@ +--- +"@medusajs/medusa": patch +--- + +fix(medusa): Ensures that file based routing is compatible with Windows pathing diff --git a/packages/medusa/src/loaders/helpers/routing/index.ts b/packages/medusa/src/loaders/helpers/routing/index.ts index 546fbf391c..f0d3abb1dc 100644 --- a/packages/medusa/src/loaders/helpers/routing/index.ts +++ b/packages/medusa/src/loaders/helpers/routing/index.ts @@ -2,7 +2,7 @@ import cors from "cors" import { Express, json, urlencoded } from "express" import { readdir } from "fs/promises" import { parseCorsOrigins } from "medusa-core-utils" -import { extname, join } from "path" +import { extname, join, sep } from "path" import { authenticate, authenticateCustomer, @@ -95,7 +95,7 @@ export class RoutesLoader { protected excludes: RegExp[] = [ /\.DS_Store/, /(\.ts\.map|\.js\.map|\.d\.ts)/, - /^_/, + /^_[^/\\]*(\.[^/\\]+)?$/, ] constructor({ @@ -157,7 +157,7 @@ export class RoutesLoader { * @param route - The route to parse * * @example - * "/admin/orders/[id]/index.ts" => "/admin/orders/:id/index.ts" + * "/admin/orders/[id]/route.ts => "/admin/orders/:id/route.ts" */ protected parseRoute(route: string): string { let route_ = route @@ -308,7 +308,7 @@ export class RoutesLoader { let routeToParse = childPath - const pathSegments = childPath.split("/") + const pathSegments = childPath.split(sep) const lastSegment = pathSegments[pathSegments.length - 1] if (lastSegment.startsWith("route")) { @@ -404,8 +404,6 @@ export class RoutesLoader { await readdir(dirPath, { withFileTypes: true }).then((entries) => { return entries .filter((entry) => { - const fullPath = join(dirPath, entry.name) - if ( this.excludes.length && this.excludes.some((exclude) => exclude.test(entry.name)) @@ -413,8 +411,13 @@ export class RoutesLoader { return false } - // Get entry name without extension - const name = entry.name.replace(/\.[^/.]+$/, "") + let name = entry.name + + const extension = extname(name) + + if (extension) { + name = name.replace(extension, "") + } if (entry.isFile() && name !== ROUTE_NAME) { return false @@ -541,7 +544,7 @@ export class RoutesLoader { /** * Since the file based routing does not require a index file - * we can check if it exists using require. Instead we try + * we can't check if it exists using require. Instead we try * to read the directory and if it fails we know that the * directory does not exist. */ diff --git a/packages/medusa/src/loaders/plugins.ts b/packages/medusa/src/loaders/plugins.ts index 45527f4702..c1d9d59116 100644 --- a/packages/medusa/src/loaders/plugins.ts +++ b/packages/medusa/src/loaders/plugins.ts @@ -38,8 +38,8 @@ import path from "path" import { EntitySchema } from "typeorm" import { MiddlewareService } from "../services" import { getModelExtensionsMap } from "./helpers/get-model-extension-map" -import logger from "./logger" import { RoutesLoader } from "./helpers/routing" +import logger from "./logger" type Options = { rootDirectory: string @@ -362,7 +362,7 @@ async function registerApi( */ await new RoutesLoader({ app, - rootDir: `${pluginDetails.resolve}/api`, + rootDir: path.join(pluginDetails.resolve, "api"), activityId: activityId, configModule: configmodule, }).load()