|
1
|
import { ConfigModule, PluginDetails } from "@medusajs/types" |
|
2
|
import fs from "fs/promises" |
|
3
|
import path from "path" |
|
4
|
import { isString } from "./is-string" |
|
5
|
import { readDir } from "./read-dir-recursive" |
|
6
|
|
|
7
|
const MEDUSA_APP_SOURCE_PATH = "src" |
|
8
|
const MEDUSA_PLUGIN_SOURCE_PATH = ".medusa/server/src" |
|
9
|
const MEDUSA_PLUGIN_OPTIONS_FILE_PATH = |
|
10
|
".medusa/server/medusa-plugin-options.json" |
|
11
|
export const MEDUSA_PROJECT_NAME = "project-plugin" |
|
12
|
|
|
13
|
function createPluginId(name: string): string { |
|
14
|
return name |
|
15
|
} |
|
16
|
|
|
17
|
function createFileContentHash(path: string, files: string): string { |
|
18
|
return path + files |
|
19
|
} |
|
20
|
|
|
21
|
/** |
|
22
|
* Returns the absolute path to the package.json file for a |
|
23
|
* given plugin identifier. |
|
24
|
*/ |
|
25
|
async function resolvePluginPkgFile( |
|
26
|
rootDirectory: string, |
|
27
|
pluginPath: string |
|
28
|
): Promise<{ path: string; contents: any }> { |
|
29
|
try { |
|
30
|
const pkgJSONPath = require.resolve(path.join(pluginPath, "package.json"), { |
|
31
|
paths: [rootDirectory], |
|
32
|
}) |
|
33
|
const packageJSONContents = JSON.parse( |
|
34
|
await fs.readFile(pkgJSONPath, "utf-8") |
|
35
|
) |
|
36
|
return { path: pkgJSONPath, contents: packageJSONContents } |
|
37
|
} catch (error) { |
|
38
|
if (error.code === "MODULE_NOT_FOUND" || error.code === "ENOENT") { |
|
39
|
throw new Error( |
|
40
|
`Unable to resolve plugin "${pluginPath}". Make sure the plugin directory has a package.json file` |
|
41
|
) |
|
42
|
} |
|
43
|
throw error |
|
44
|
} |
|
45
|
} |
|
46
|
|
|
47
|
/** |
|
48
|
* Reads the "medusa-plugin-options.json" file from the plugin root |
|
49
|
* directory and returns its contents as an object. |
|
50
|
*/ |
|
51
|
async function resolvePluginOptions( |
|
52
|
pluginRootDir: string |
|
53
|
): Promise<Record<string, any>> { |
|
54
|
try { |
|
55
|
const contents = await fs.readFile( |
|
56
|
path.join(pluginRootDir, MEDUSA_PLUGIN_OPTIONS_FILE_PATH), |
|
57
|
"utf-8" |
|
58
|
) |
|
59
|
return JSON.parse(contents) |
|
60
|
} catch (error) { |
|
61
|
if (error.code === "MODULE_NOT_FOUND" || error.code === "ENOENT") { |
|
62
|
return {} |
|
63
|
} |
|
64
|
throw error |
|
65
|
} |
|
66
|
} |
|
67
|
|
|
68
|
/** |
|
69
|
* Finds the correct path for the plugin. If it is a local plugin it will be |
|
70
|
* found in the plugins folder. Otherwise we will look for the plugin in the |
|
71
|
* installed npm packages. |
|
72
|
* @param {string} pluginPath - the name of the plugin to find. Should match |
|
73
|
* the name of the folder where the plugin is contained. |
|
74
|
* @return {object} the plugin details |
|
75
|
*/ |
|
76
|
async function resolvePlugin( |
|
77
|
rootDirectory: string, |
|
78
|
pluginPath: string, |
|
79
|
options?: any |
|
80
|
): Promise<PluginDetails> { |
|
81
|
const pkgJSON = await resolvePluginPkgFile(rootDirectory, pluginPath) |
|
82
|
const resolvedPath = path.dirname(pkgJSON.path) |
|
83
|
|
|
84
|
const name = pkgJSON.contents.name || pluginPath |
|
85
|
|
|
86
|
const resolve = path.join(resolvedPath, MEDUSA_PLUGIN_SOURCE_PATH) |
|
87
|
const pluginStaticOptions = await resolvePluginOptions(resolvedPath) |
|
88
|
const modules = await readDir(path.join(resolve, "modules"), { |
|
89
|
ignoreMissing: true, |
|
90
|
}) |
|
91
|
const pluginOptions = options ?? {} |
|
92
|
|
|
93
|
const hasAdmin = |
|
94
|
!!pkgJSON.contents.exports?.["./admin"] || !!pluginStaticOptions.srcDir |
|
95
|
const isAdminLocal = hasAdmin && !!pluginStaticOptions.srcDir |
|
96
|
|
|
97
|
const adminConfig = hasAdmin |
|
98
|
? { |
|
99
|
type: isAdminLocal ? ("local" as const) : ("package" as const), |
|
100
|
resolve: path.posix.join( |
|
101
|
isAdminLocal ? pluginStaticOptions.srcDir : name, |
|
102
|
"admin" |
|
103
|
), |
|
104
|
} |
|
105
|
: undefined |
|
106
|
|
|
107
|
return { |
|
108
|
resolve, |
|
109
|
name, |
|
110
|
id: createPluginId(name), |
|
111
|
options: pluginOptions, |
|
112
|
version: pkgJSON.contents.version || "0.0.0", |
|
113
|
admin: adminConfig, |
|
114
|
modules: modules.map((mod) => { |
|
115
|
return { |
|
116
|
resolve: `${pluginPath}/${MEDUSA_PLUGIN_SOURCE_PATH}/modules/${mod.name}`, |
|
117
|
options: pluginOptions, |
|
118
|
} |
|
119
|
}), |
|
120
|
} |
|
121
|
} |
|
122
|
|
|
123
|
export async function getResolvedPlugins( |
|
124
|
rootDirectory: string, |
|
125
|
configModule: ConfigModule, |
|
126
|
isMedusaProject = false |
|
127
|
): Promise<PluginDetails[]> { |
|
128
|
const resolved = await Promise.all( |
|
129
|
(configModule?.plugins || []).map(async (plugin) => { |
|
130
|
if (isString(plugin)) { |
|
131
|
return resolvePlugin(rootDirectory, plugin) |
|
132
|
} |
|
133
|
return resolvePlugin(rootDirectory, plugin.resolve, plugin.options) |
|
134
|
}) |
|
135
|
) |
|
136
|
|
|
137
|
if (isMedusaProject) { |
|
138
|
const extensionDirectory = path.join(rootDirectory, MEDUSA_APP_SOURCE_PATH) |
|
139
|
resolved.push({ |
|
140
|
resolve: extensionDirectory, |
|
141
|
name: MEDUSA_PROJECT_NAME, |
|
142
|
id: createPluginId(MEDUSA_PROJECT_NAME), |
|
143
|
admin: { |
|
144
|
type: "local", |
|
145
|
resolve: path.join(extensionDirectory, "admin"), |
|
146
|
}, |
|
147
|
options: configModule, |
|
148
|
version: createFileContentHash(process.cwd(), `**`), |
|
149
|
}) |
|
150
|
} |
|
151
|
|
|
152
|
return resolved |
|
153
|
} |