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: remove dead code and refactor the logic of resolving plugins #10874

Merged
merged 8 commits into from
Jan 9, 2025
40 changes: 40 additions & 0 deletions packages/core/types/src/common/config-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -922,10 +922,50 @@ export type ConfigModule = {
featureFlags: Record<string, boolean | string | Record<string, boolean>>
}

type InternalModuleDeclarationOverride = InternalModuleDeclaration & {
/**
* Optional key to be used to identify the module, if not provided, it will be inferred from the module joiner config service name.
*/
key?: string
/**
* By default, modules are enabled, if provided as true, this will disable the module entirely.
*/
disable?: boolean
}

type ExternalModuleDeclarationOverride = ExternalModuleDeclaration & {
/**
* key to be used to identify the module, if not provided, it will be inferred from the module joiner config service name.
*/
key: string
/**
* By default, modules are enabled, if provided as true, this will disable the module entirely.
*/
disable?: boolean
}

/**
* The configuration accepted by the "defineConfig" helper
*/
export type InputConfig = Partial<
Omit<ConfigModule, "admin" | "modules"> & {
admin: Partial<ConfigModule["admin"]>
modules:
| Partial<
InternalModuleDeclarationOverride | ExternalModuleDeclarationOverride
>[]
/**
* @deprecated use the array instead
*/
| ConfigModule["modules"]
}
>

export type PluginDetails = {
resolve: string
name: string
id: string
options: Record<string, unknown>
version: string
modules?: InputConfig["modules"]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Introduced the modules property on the plugin detail . Later we will merge these modules with the main config top-level modules

}
44 changes: 4 additions & 40 deletions packages/core/utils/src/common/define-config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
ConfigModule,
ExternalModuleDeclaration,
InputConfig,
InternalModuleDeclaration,
} from "@medusajs/types"
import {
Expand Down Expand Up @@ -29,42 +29,6 @@ export const DEFAULT_STORE_RESTRICTED_FIELDS = [
"payment_collections"*/
]

type InternalModuleDeclarationOverride = InternalModuleDeclaration & {
/**
* Optional key to be used to identify the module, if not provided, it will be inferred from the module joiner config service name.
*/
key?: string
/**
* By default, modules are enabled, if provided as true, this will disable the module entirely.
*/
disable?: boolean
}

type ExternalModuleDeclarationOverride = ExternalModuleDeclaration & {
/**
* key to be used to identify the module, if not provided, it will be inferred from the module joiner config service name.
*/
key: string
/**
* By default, modules are enabled, if provided as true, this will disable the module entirely.
*/
disable?: boolean
}

type Config = Partial<
Omit<ConfigModule, "admin" | "modules"> & {
admin: Partial<ConfigModule["admin"]>
modules:
| Partial<
InternalModuleDeclarationOverride | ExternalModuleDeclarationOverride
>[]
/**
* @deprecated use the array instead
*/
| ConfigModule["modules"]
}
>

/**
* The "defineConfig" helper can be used to define the configuration
* of a medusa application.
Expand All @@ -73,7 +37,7 @@ type Config = Partial<
* make an application work seamlessly, but still provide you the ability
* to override configuration as needed.
*/
export function defineConfig(config: Config = {}): ConfigModule {
export function defineConfig(config: InputConfig = {}): ConfigModule {
const { http, ...restOfProjectConfig } = config.projectConfig || {}

/**
Expand Down Expand Up @@ -132,14 +96,14 @@ export function defineConfig(config: Config = {}): ConfigModule {
* @param configModules
*/
function resolveModules(
configModules: Config["modules"]
configModules: InputConfig["modules"]
): ConfigModule["modules"] {
/**
* The default set of modules to always use. The end user can swap
* the modules by providing an alternate implementation via their
* config. But they can never remove a module from this list.
*/
const modules: Config["modules"] = [
const modules: InputConfig["modules"] = [
{ resolve: MODULE_PACKAGE_NAMES[Modules.CACHE] },
{ resolve: MODULE_PACKAGE_NAMES[Modules.EVENT_BUS] },
{ resolve: MODULE_PACKAGE_NAMES[Modules.WORKFLOW_ENGINE] },
Expand Down
52 changes: 41 additions & 11 deletions packages/core/utils/src/common/read-dir-recursive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,51 @@ import { Dirent } from "fs"
import { readdir } from "fs/promises"
import { join } from "path"

export async function readDirRecursive(dir: string): Promise<Dirent[]> {
let allEntries: Dirent[] = []
const readRecursive = async (dir) => {
const MISSING_NODE_ERRORS = ["ENOTDIR", "ENOENT"]

export async function readDir(
dir: string,
options?: {
ignoreMissing?: boolean
}
) {
try {
const entries = await readdir(dir, { withFileTypes: true })
return entries
} catch (error) {
if (options?.ignoreMissing && MISSING_NODE_ERRORS.includes(error.code)) {
return []
}
throw error
}
}

for (const entry of entries) {
const fullPath = join(dir, entry.name)
Object.defineProperty(entry, "path", {
value: dir,
})
allEntries.push(entry)
export async function readDirRecursive(
thetutlage marked this conversation as resolved.
Show resolved Hide resolved
dir: string,
options?: {
ignoreMissing?: boolean
}
): Promise<Dirent[]> {
let allEntries: Dirent[] = []
const readRecursive = async (dir: string) => {
try {
const entries = await readdir(dir, { withFileTypes: true })
for (const entry of entries) {
const fullPath = join(dir, entry.name)
Object.defineProperty(entry, "path", {
value: dir,
})
allEntries.push(entry)

if (entry.isDirectory()) {
await readRecursive(fullPath)
if (entry.isDirectory()) {
await readRecursive(fullPath)
}
}
} catch (error) {
if (options?.ignoreMissing && error.code === "ENOENT") {
return
}
throw error
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/medusa/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"watch": "tsc --build --watch",
"build": "rimraf dist && tsc --build",
"serve": "node dist/app.js",
"test": "jest --silent --bail --maxWorkers=50% --forceExit"
"test": "jest --silent=false --bail --maxWorkers=50% --forceExit"
},
"devDependencies": {
"@medusajs/framework": "^2.2.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/medusa/src/commands/db/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const main = async function ({ directory, modules }) {
ContainerRegistrationKeys.CONFIG_MODULE
)

const plugins = getResolvedPlugins(directory, configModule, true) || []
const plugins = await getResolvedPlugins(directory, configModule, true)
const linksSourcePaths = plugins.map((plugin) =>
join(plugin.resolve, "links")
)
Expand Down
2 changes: 1 addition & 1 deletion packages/medusa/src/commands/db/migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export async function migrate({
ContainerRegistrationKeys.CONFIG_MODULE
)

const plugins = getResolvedPlugins(directory, configModule, true) || []
const plugins = await getResolvedPlugins(directory, configModule, true)
const linksSourcePaths = plugins.map((plugin) =>
join(plugin.resolve, "links")
)
Expand Down
2 changes: 1 addition & 1 deletion packages/medusa/src/commands/db/rollback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const main = async function ({ directory, modules }) {
ContainerRegistrationKeys.CONFIG_MODULE
)

const plugins = getResolvedPlugins(directory, configModule, true) || []
const plugins = await getResolvedPlugins(directory, configModule, true)
const linksSourcePaths = plugins.map((plugin) =>
join(plugin.resolve, "links")
)
Expand Down
2 changes: 1 addition & 1 deletion packages/medusa/src/commands/db/sync-links.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ const main = async function ({ directory, executeSafe, executeAll }) {

const medusaAppLoader = new MedusaAppLoader()

const plugins = getResolvedPlugins(directory, configModule, true) || []
const plugins = await getResolvedPlugins(directory, configModule, true)
const linksSourcePaths = plugins.map((plugin) =>
join(plugin.resolve, "links")
)
Expand Down
Loading
Loading