The complete open source platform to run your entire marketplace. Storefront, vendor panel, admin console, and integrations in one enterprise-grade platform you own.
TypeScript b2b-marketplacecomposable-commerceecommerceenterpriseheadless-commercemarketplacemedusamedusajsmulti-vendortypescript
refactor: enhance file path resolution in CLI commands and utilities for improved consistency and flexibility
vholik2614cb53415c
diff --git a/packages/cli/src/commands/add.ts b/packages/cli/src/commands/add.tsindex 64fe8c658..952b7b7e6 100644--- a/packages/cli/src/commands/add.ts+++ b/packages/cli/src/commands/add.ts@@ -89,6 +89,7 @@ export const add = new Command()overwrite: options.overwrite,silent: options.silent,yes: options.yes,+ path: options.cwd,});} catch (error) {logger.break();diff --git a/packages/cli/src/commands/diff.ts b/packages/cli/src/commands/diff.tsindex 68820e448..3c0360cdc 100644--- a/packages/cli/src/commands/diff.ts+++ b/packages/cli/src/commands/diff.ts@@ -10,7 +10,8 @@ import { handleError } from "../utils/handle-error";import { highlighter } from "../utils/highlighter";import { logger } from "../utils/logger";import { transformImports } from "../utils/transform-import";-import { getRelativePath, getTargetDir } from "../utils/file-type";+import { resolveFilePath } from "../utils/file-type";+import { getProjectInfo } from "../utils/get-project-info";const diffOptionsSchema = z.object({blocks: z.array(z.string()).optional(),@@ -63,13 +64,14 @@ export const diff = new Command()process.exit(1);}+ const projectInfo = await getProjectInfo(config.resolvedPaths.cwd);+for (const block of blocks) {for (const file of block.files) {- const fileType = getTargetDir(file);- const filePath = path.resolve(- config.resolvedPaths[fileType],- getRelativePath(file.path)- );+ const filePath = resolveFilePath(file, config, {+ isSrcDir: projectInfo?.isSrcDir ?? false,+ path: options.cwd+ });if (!existsSync(filePath)) {logger.info(`File ${filePath} does not exist locally.`);diff --git a/packages/cli/src/utils/file-type.ts b/packages/cli/src/utils/file-type.tsindex 470d53430..32037b99b 100644--- a/packages/cli/src/utils/file-type.ts+++ b/packages/cli/src/utils/file-type.ts@@ -1,29 +1,63 @@-import type { RegistryItemCategory } from "../registry/schema";-+import type z from "zod";+import type { RegistryItemCategory, registryItemFileSchema } from "../registry/schema";+import type { Config } from "../schema";+import path from "path";/*** Get the target directory for a registry file based on its type.*/export function getTargetDir(file: {type: string;path: string;-}): RegistryItemCategory {- if (file.type === "registry:workflow") return "workflows";- if (file.type === "registry:api") return "api";- if (file.type === "registry:link") return "links";- if (file.type === "registry:module") return "modules";- if (file.type === "registry:vendor") return "vendor";- if (file.type === "registry:admin") return "admin";- if (file.type === "registry:lib") return "lib";+}, config: Config) {+ if (file.type === "registry:workflow") return config.resolvedPaths.workflows;+ if (file.type === "registry:api") return config.resolvedPaths.api;+ if (file.type === "registry:link") return config.resolvedPaths.links;+ if (file.type === "registry:module") return config.resolvedPaths.modules;+ if (file.type === "registry:vendor") return config.resolvedPaths.vendor;+ if (file.type === "registry:admin") return config.resolvedPaths.admin;+ if (file.type === "registry:lib") return config.resolvedPaths.lib;throw new Error(`Unknown file type: ${file.type}`);}-/**- * Get the relative path by stripping the type prefix from a file path.- */-export function getRelativePath(filePath: string) {- return filePath.replace(- /^(workflows|api|links|modules|vendor|admin|lib)\//,- ""- );++++export function resolveFilePath(file: z.infer<typeof registryItemFileSchema>, config: Config, options: {+ isSrcDir: boolean;+ path: string+}) {+ // todo: add target support if path handling is not enough+ const targetDir = getTargetDir(file, config);++ const relativePath = resolveNestedFilePath(file.path, targetDir);++ return path.join(targetDir, relativePath)}++export function resolveNestedFilePath(+ filePath: string,+ targetDir: string+): string {+ // Normalize paths by removing leading/trailing slashes+ const normalizedFilePath = filePath.replace(/^\/|\/$/g, "")+ const normalizedTargetDir = targetDir.replace(/^\/|\/$/g, "")++ // Split paths into segments+ const fileSegments = normalizedFilePath.split("/")+ const targetSegments = normalizedTargetDir.split("/")++ // Find the last matching segment from targetDir in filePath+ const lastTargetSegment = targetSegments[targetSegments.length - 1]+ const commonDirIndex = fileSegments.findIndex(+ (segment) => segment === lastTargetSegment+ )++ if (commonDirIndex === -1) {+ // Return just the filename if no common directory is found+ return fileSegments[fileSegments.length - 1]+ }++ // Return everything after the common directory+ return fileSegments.slice(commonDirIndex + 1).join("/")+}\ No newline at end of filediff --git a/packages/cli/src/utils/update-files.ts b/packages/cli/src/utils/update-files.tsindex 1c8b5f2ae..d26c4b729 100644--- a/packages/cli/src/utils/update-files.ts+++ b/packages/cli/src/utils/update-files.ts@@ -5,7 +5,7 @@ import prompts from "prompts";import type { RegistryItem } from "../registry/schema";import type { Config } from "../schema";import { isContentSame } from "./compare";-import { getRelativePath, getTargetDir } from "./file-type";+import { resolveFilePath } from "./file-type";import { getProjectInfo } from "./get-project-info";import { highlighter } from "./highlighter";import { logger } from "./logger";@@ -50,16 +50,10 @@ export async function updateFiles(const filesDeclined: string[] = [];for (const file of files) {- const fileType = getTargetDir(file);- const relativePath = file.target ?? getRelativePath(file.path);- const basePath = options.path || config.resolvedPaths[fileType];-- let filePath = path.resolve(basePath, relativePath);-- if (projectInfo?.isSrcDir && !filePath.includes("src")) {- const srcPath = path.resolve(config.resolvedPaths.cwd, "src");- filePath = path.resolve(srcPath, relativePath);- }+ const filePath = resolveFilePath(file, config, {+ isSrcDir: projectInfo?.isSrcDir ?? false,+ path: options.path ?? "",+ });const fileName = basename(file.path);const targetDir = path.dirname(filePath);diff --git a/packages/registry/r/block-1.json b/packages/registry/r/block-1.jsonindex aa765ce48..96331fabf 100644--- a/packages/registry/r/block-1.json+++ b/packages/registry/r/block-1.json@@ -11,20 +11,17 @@{"path": "block-1/modules/test/index.ts","content": "import { Module } from \"@medusajs/framework/utils\"\nimport TestModuleService from \"./service\"\n\nexport const TEST_MODULE = \"test\"\n\nexport default Module(TEST_MODULE, {\n service: TestModuleService,\n})\n",- "type": "registry:module",- "target": "test/index.ts"+ "type": "registry:module"},{"path": "block-1/modules/test/service.ts","content": "import { MedusaService } from \"@medusajs/framework/utils\"\nimport Test from \"./models/test\"\n\nclass TestModuleService extends MedusaService({\n Test,\n}) {}\n\nexport default TestModuleService\n",- "type": "registry:module",- "target": "test/service.ts"+ "type": "registry:module"},{"path": "block-1/modules/test/models/test.ts","content": "import { model } from \"@medusajs/framework/utils\"\n\nconst Test = model.define(\"test\", {\n id: model.id().primaryKey(),\n name: model.text(),\n})\n\nexport default Test\n",- "type": "registry:module",- "target": "test/models/test.ts"+ "type": "registry:module"},{"path": "block-1/links/product-test.ts",@@ -34,8 +31,7 @@{"path": "block-1/api/hello/test/route.ts","content": "import type { MedusaRequest, MedusaResponse } from \"@medusajs/framework/http\";\n\nexport const POST = async (req: MedusaRequest, res: MedusaResponse) => {\n res.json({\n test: true\n })\n}",- "type": "registry:api",- "target": "hello/test/route.ts"+ "type": "registry:api"}]}\ No newline at end of filediff --git a/packages/registry/r/registry.json b/packages/registry/r/registry.jsonindex 5c6753f07..2c167fdf6 100644--- a/packages/registry/r/registry.json+++ b/packages/registry/r/registry.json@@ -11,18 +11,15 @@"files": [{"path": "block-1/modules/test/index.ts",- "type": "registry:module",- "target": "test/index.ts"+ "type": "registry:module"},{"path": "block-1/modules/test/service.ts",- "type": "registry:module",- "target": "test/service.ts"+ "type": "registry:module"},{"path": "block-1/modules/test/models/test.ts",- "type": "registry:module",- "target": "test/models/test.ts"+ "type": "registry:module"},{"path": "block-1/links/product-test.ts",@@ -30,8 +27,7 @@},{"path": "block-1/api/hello/test/route.ts",- "type": "registry:api",- "target": "hello/test/route.ts"+ "type": "registry:api"}],"categories": [diff --git a/packages/registry/registry.json b/packages/registry/registry.jsonindex 5c6753f07..2c167fdf6 100644--- a/packages/registry/registry.json+++ b/packages/registry/registry.json@@ -11,18 +11,15 @@"files": [{"path": "block-1/modules/test/index.ts",- "type": "registry:module",- "target": "test/index.ts"+ "type": "registry:module"},{"path": "block-1/modules/test/service.ts",- "type": "registry:module",- "target": "test/service.ts"+ "type": "registry:module"},{"path": "block-1/modules/test/models/test.ts",- "type": "registry:module",- "target": "test/models/test.ts"+ "type": "registry:module"},{"path": "block-1/links/product-test.ts",@@ -30,8 +27,7 @@},{"path": "block-1/api/hello/test/route.ts",- "type": "registry:api",- "target": "hello/test/route.ts"+ "type": "registry:api"}],"categories": [