1 import {
2 ExternalModuleDeclaration,
3 InternalModuleDeclaration,
4 ModuleDefinition,
5 ModuleExports,
6 ModuleResolution,
7 } from "@medusajs/types"
8
9 import {
10 isObject,
11 isString,
12 normalizeImportPathWithSource,
13 } from "@medusajs/utils"
14 import { ModulesDefinition } from "../definitions"
15 import { MODULE_SCOPE } from "../types"
16
17 export const registerMedusaModule = ({
18 moduleKey,
19 moduleDeclaration,
20 moduleExports,
21 definition,
22 cwd,
23 }: {
24 moduleKey: string
25 moduleDeclaration?:
26 | Partial<InternalModuleDeclaration | ExternalModuleDeclaration>
27 | string
28 | false
29 moduleExports?: ModuleExports
30 definition?: ModuleDefinition
31 cwd?: string
32 }): Record<string, ModuleResolution> => {
33 const moduleResolutions = {} as Record<string, ModuleResolution>
34
35 const modDefinition = definition ?? ModulesDefinition[moduleKey]
36
37 const modDeclaration =
38 moduleDeclaration ??
39 (modDefinition?.defaultModuleDeclaration as InternalModuleDeclaration)
40
41 if (modDeclaration !== false && !modDeclaration) {
42 throw new Error(`Module: ${moduleKey} has no declaration.`)
43 }
44
45 if (
46 isObject(modDeclaration) &&
47 modDeclaration?.scope === MODULE_SCOPE.EXTERNAL
48 ) {
49 // TODO: getExternalModuleResolution(...)
50 throw new Error("External Modules are not supported yet.")
51 }
52
53 if (modDefinition === undefined) {
54 moduleResolutions[moduleKey] = getCustomModuleResolution(
55 moduleKey,
56 moduleDeclaration as InternalModuleDeclaration,
57 cwd
58 )
59 return moduleResolutions
60 }
61
62 moduleResolutions[moduleKey] = getInternalModuleResolution(
63 modDefinition,
64 moduleDeclaration as InternalModuleDeclaration,
65 moduleExports,
66 cwd
67 )
68
69 return moduleResolutions
70 }
71
72 function getCustomModuleResolution(
73 key: string,
74 moduleConfig: InternalModuleDeclaration | string,
75 cwd: string = process.cwd()
76 ): ModuleResolution {
77 const originalPath = normalizeImportPathWithSource(
78 (isString(moduleConfig) ? moduleConfig : moduleConfig.resolve) as string,
79 cwd
80 )
81 const resolutionPath = require.resolve(originalPath, {
82 paths: [cwd],
83 })
84
85 const conf = isObject(moduleConfig)
86 ? moduleConfig
87 : ({} as InternalModuleDeclaration)
88
89 const dependencies = conf?.dependencies ?? []
90
91 return {
92 resolutionPath,
93 definition: {
94 key,
95 label: `Custom: ${key}`,
96 isRequired: false,
97 defaultPackage: "",
98 dependencies,
99 defaultModuleDeclaration: {
100 scope: MODULE_SCOPE.INTERNAL,
101 },
102 },
103 moduleDeclaration: {
104 scope: MODULE_SCOPE.INTERNAL,
105 },
106 dependencies,
107 options: conf?.options ?? {},
108 }
109 }
110
111 export const registerMedusaLinkModule = (
112 definition: ModuleDefinition,
113 moduleDeclaration: Partial<InternalModuleDeclaration>,
114 moduleExports?: ModuleExports,
115 cwd: string = process.cwd()
116 ): Record<string, ModuleResolution> => {
117 const moduleResolutions = {} as Record<string, ModuleResolution>
118
119 moduleResolutions[definition.key] = getInternalModuleResolution(
120 definition,
121 moduleDeclaration as InternalModuleDeclaration,
122 moduleExports,
123 cwd
124 )
125
126 return moduleResolutions
127 }
128
129 function getInternalModuleResolution(
130 definition: ModuleDefinition,
131 moduleConfig: InternalModuleDeclaration | string | false,
132 moduleExports?: ModuleExports,
133 cwd: string = process.cwd()
134 ): ModuleResolution {
135 if (typeof moduleConfig === "boolean") {
136 if (!moduleConfig && definition.isRequired) {
137 throw new Error(`Module: ${definition.label} is required`)
138 }
139
140 if (!moduleConfig) {
141 return {
142 resolutionPath: false,
143 definition,
144 dependencies: [],
145 options: {},
146 }
147 }
148 }
149
150 const isObj = isObject(moduleConfig)
151 let resolutionPath = definition.defaultPackage
152
153 // If user added a module and it's overridable, we resolve that instead
154 const isStr = isString(moduleConfig)
155 if (isStr || (isObj && moduleConfig.resolve)) {
156 const originalPath = normalizeImportPathWithSource(
157 (isString(moduleConfig) ? moduleConfig : moduleConfig.resolve) as string,
158 cwd
159 )
160 resolutionPath = require.resolve(originalPath, {
161 paths: [cwd],
162 })
163 }
164
165 const moduleDeclaration = isObj ? moduleConfig : {}
166 const additionalDependencies = isObj ? moduleConfig.dependencies || [] : []
167
168 return {
169 resolutionPath,
170 definition,
171 dependencies: [
172 ...new Set(
173 (definition.dependencies || []).concat(additionalDependencies)
174 ),
175 ],
176 moduleDeclaration: {
177 ...(definition.defaultModuleDeclaration ?? {}),
178 ...moduleDeclaration,
179 },
180 moduleExports,
181 options: isObj ? moduleConfig.options ?? {} : {},
182 }
183 }