Skip to content

Commit

Permalink
allow for direct ref to plugin export
Browse files Browse the repository at this point in the history
  • Loading branch information
olivermrbl committed Jan 31, 2024
1 parent 18f4696 commit f6a9815
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 80 deletions.
2 changes: 1 addition & 1 deletion packages/modules-sdk/src/loaders/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export * from "./module-loader"
export * from "./module-provider-loader"
export * from "./register-modules"
export * from "./register-providers"

87 changes: 87 additions & 0 deletions packages/modules-sdk/src/loaders/module-provider-loader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { Constructor, MedusaContainer } from "@medusajs/types"
import { isString, promiseAll } from "@medusajs/utils"
import { Lifetime, asFunction } from "awilix"

type ModuleProviderExports = {
services: Constructor<any>[]
}

type ModuleProvider = {
resolve: string | ModuleProviderExports
provider_name?: string
options: Record<string, unknown>
}

export async function moduleProviderLoader({
container,
providers,
registerServiceFn,
}) {
if (!providers?.length) {
return
}

await promiseAll(
providers.map(async (pluginDetails) => {
await loadModuleProvider(container, pluginDetails, registerServiceFn)
})
)
}

export async function loadModuleProvider(
container: MedusaContainer,
provider: ModuleProvider,
registerServiceFn?: (klass, container, pluginDetails) => Promise<void>
) {
let loadedProvider: any

const pluginName = provider.resolve ?? provider.provider_name ?? ""

try {
if (isString(provider.resolve)) {
loadedProvider = await import(provider.resolve)
} else {
loadedProvider = provider.resolve
}
} catch (error) {
throw new Error(
`Unable to find plugin ${pluginName} -- perhaps you need to install its package?`
)
}

loadedProvider = (loadedProvider as any).default

if (!loadedProvider) {
throw new Error(
`No default export found in plugin ${pluginName} -- make sure your plugin exports a service.`
)
}

if (!loadedProvider?.services) {
throw new Error(
`No services found in plugin ${provider.resolve} -- make sure your plugin exports a service.`
)
}

const services = await promiseAll(
loadedProvider.services.map(async (service) => {
if (registerServiceFn) {
// Used to register the specific type of service in the provider
await registerServiceFn(service, container, provider.options)
} else {
container.register({
[service.name]: asFunction(
(cradle) => new service(cradle, provider.options),
{
lifetime: service.LIFE_TIME || Lifetime.SCOPED,
}
),
})
}

return service
})
)

return services
}
79 changes: 0 additions & 79 deletions packages/modules-sdk/src/loaders/register-providers.ts

This file was deleted.

0 comments on commit f6a9815

Please sign in to comment.