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

feat(medusa): Improve error messaging in plugin loader #4410

Merged
merged 5 commits into from
Jun 26, 2023
Merged
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions .changeset/fair-kids-tease.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@medusajs/medusa": patch
---

feat(medusa): Improve error messaging in plugin loader
6 changes: 4 additions & 2 deletions packages/medusa/src/commands/utils/get-migrations.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
} from "medusa-core-utils"
import path from "path"
import { handleConfigError } from "../../loaders/config"
import { MEDUSA_PROJECT_NAME } from "../../loaders/plugins"

function createFileContentHash(path, files) {
return path + files
Expand Down Expand Up @@ -143,10 +144,11 @@ export default (directory, featureFlagRouter) => {
return details
})

// Resolve user's project as a plugin for loading purposes
resolved.push({
resolve: `${directory}/dist`,
name: `project-plugin`,
id: createPluginId(`project-plugin`),
name: MEDUSA_PROJECT_NAME,
id: createPluginId(MEDUSA_PROJECT_NAME),
options: {},
version: createFileContentHash(process.cwd(), `**`),
})
Expand Down
8 changes: 4 additions & 4 deletions packages/medusa/src/loaders/__tests__/plugins.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import {
Resolver,
} from "awilix"
import { mkdirSync, rmSync, writeFileSync } from "fs"
import { createMedusaContainer } from "medusa-core-utils"
import { resolve } from "path"
import Logger from "../logger"
import { registerServices, registerStrategies } from "../plugins"
import { DataSource, EntityManager } from "typeorm"
import { createMedusaContainer } from "medusa-core-utils"
import Logger from "../logger"
import { MEDUSA_PROJECT_NAME, registerServices, registerStrategies } from "../plugins"

// ***** TEMPLATES *****
const buildServiceTemplate = (name: string): string => {
Expand Down Expand Up @@ -116,7 +116,7 @@ describe("plugins loader", () => {

const pluginsDetails = {
resolve: resolve(__dirname, "__pluginsLoaderTest__"),
name: `project-plugin`,
name: MEDUSA_PROJECT_NAME,
id: "fakeId",
options: {},
version: '"fakeVersion',
Expand Down
29 changes: 19 additions & 10 deletions packages/medusa/src/loaders/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ type PluginDetails = {

export const isSearchEngineInstalledResolutionKey = "isSearchEngineInstalled"

export const MEDUSA_PROJECT_NAME = "project-plugin"

/**
* Registers all services in the services directory
*/
Expand Down Expand Up @@ -108,10 +110,11 @@ function getResolvedPlugins(
return details
})

// Resolve user's project as a plugin for loading purposes
resolved.push({
resolve: `${rootDirectory}/dist`,
name: `project-plugin`,
id: createPluginId(`project-plugin`),
name: MEDUSA_PROJECT_NAME,
id: createPluginId(MEDUSA_PROJECT_NAME),
options: configModule,
version: createFileContentHash(process.cwd(), `**`),
})
Expand Down Expand Up @@ -325,23 +328,29 @@ function registerApi(
activityId: string
): Express {
const logger = container.resolve<Logger>("logger")
logger.progress(
activityId,
`Registering custom endpoints for ${pluginDetails.name}`
)
const projectName =
pluginDetails.name === MEDUSA_PROJECT_NAME
? "your Medusa project"
: `${pluginDetails.name}`

logger.progress(activityId, `Registering custom endpoints for ${projectName}`)
try {
const routes = require(`${pluginDetails.resolve}/api`).default
if (routes) {
app.use("/", routes(rootDirectory, pluginDetails.options))
}
return app
} catch (err) {
if (err.message !== `Cannot find module '${pluginDetails.resolve}/api'`) {
logger.progress(
activityId,
`No customer endpoints registered for ${pluginDetails.name}`
if (err.code !== "MODULE_NOT_FOUND") {
logger.warn(
`An error occured while registering endpoints in ${projectName}`
)

if (err.stack) {
logger.warn(`${err.stack}`)
}
}

return app
}
}
Expand Down