chore(framework): Allow multiple source dir for subcribers loader (#8407)

* chore(framework): Allow multiple source dir for subcribers loader

* re add  log

* add logs
This commit is contained in:
Adrien de Peretti
2024-08-02 13:55:48 +02:00
committed by GitHub
parent afa740606f
commit e44332ba62
5 changed files with 29 additions and 30 deletions
@@ -129,6 +129,7 @@ export class JobLoader {
try { try {
await access(sourcePath) await access(sourcePath)
} catch { } catch {
logger.info(`No job to load from ${sourcePath}. skipped.`)
return return
} }
@@ -39,6 +39,7 @@ export class LinkLoader {
try { try {
await access(sourcePath) await access(sourcePath)
} catch { } catch {
logger.info(`No link to load from ${sourcePath}. skipped.`)
return return
} }
@@ -26,7 +26,7 @@ export class SubscriberLoader {
* The base directory from which to scan for the subscribers * The base directory from which to scan for the subscribers
* @private * @private
*/ */
#sourceDir: string #sourceDir: string | string[]
/** /**
* The list of file names to exclude from the subscriber scan * The list of file names to exclude from the subscriber scan
@@ -46,7 +46,10 @@ export class SubscriberLoader {
*/ */
#subscriberDescriptors: Map<string, SubscriberModule<any>> = new Map() #subscriberDescriptors: Map<string, SubscriberModule<any>> = new Map()
constructor(sourceDir: string, options: Record<string, unknown> = {}) { constructor(
sourceDir: string | string[],
options: Record<string, unknown> = {}
) {
this.#sourceDir = sourceDir this.#sourceDir = sourceDir
this.#pluginOptions = options this.#pluginOptions = options
} }
@@ -213,20 +216,21 @@ export class SubscriberLoader {
} }
async load() { async load() {
let hasSubscriberDir = false const normalizeSourcePaths = Array.isArray(this.#sourceDir)
? this.#sourceDir
: [this.#sourceDir]
const promises = normalizeSourcePaths.map(async (sourcePath) => {
try {
await access(sourcePath)
} catch {
logger.info(`No subscribers to load from ${sourcePath}. skipped.`)
return
}
try { return await this.createMap(sourcePath)
await access(this.#sourceDir) })
hasSubscriberDir = true
} catch (err) {
logger.debug(`No subscriber directory found in ${this.#sourceDir}`)
}
if (!hasSubscriberDir) { await promiseAll(promises)
return
}
await this.createMap(this.#sourceDir)
for (const [ for (const [
fileName, fileName,
@@ -39,6 +39,7 @@ export class WorkflowLoader {
try { try {
await access(sourcePath) await access(sourcePath)
} catch { } catch {
logger.info(`No workflow to load from ${sourcePath}. skipped.`)
return return
} }
+8 -16
View File
@@ -40,23 +40,15 @@ const shouldLoadBackgroundProcessors = (configModule) => {
} }
async function subscribersLoader(plugins: PluginDetails[]) { async function subscribersLoader(plugins: PluginDetails[]) {
/** const pluginSubscribersSourcePaths = [
* Load subscribers from the medusa/medusa package /**
*/ * Load subscribers from the medusa/medusa package. Remove once the medusa core is converted to a plugin
await new SubscriberLoader(join(__dirname, "../subscribers")).load() */
join(__dirname, "../subscribers"),
].concat(plugins.map((plugin) => join(plugin.resolve, "subscribers")))
// TODO: make it the same as the other loaders, taking an array of paths to load from const subscriberLoader = new SubscriberLoader(pluginSubscribersSourcePaths)
/** await subscriberLoader.load()
* Load subscribers from all the plugins.
*/
await Promise.all(
plugins.map(async (pluginDetails) => {
await new SubscriberLoader(
join(pluginDetails.resolve, "subscribers"),
pluginDetails.options
).load()
})
)
} }
async function jobsLoader(plugins: PluginDetails[]) { async function jobsLoader(plugins: PluginDetails[]) {