forked from medusajs/medusa
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(framework): Move and improve workflows loader (medusajs#8363)
**What** Refactoring Workflows loader and move FIXES FRMW-2627
- Loading branch information
Showing
15 changed files
with
192 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
packages/framework/framework/src/config/__fixtures__/medusa-config-2.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { defineConfig } from "@medusajs/utils" | ||
|
||
export default defineConfig({ | ||
projectConfig: { | ||
databaseName: "foo", | ||
}, | ||
}) |
3 changes: 3 additions & 0 deletions
3
packages/framework/framework/src/config/__fixtures__/medusa-config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { defineConfig } from "@medusajs/utils" | ||
|
||
export default defineConfig() |
30 changes: 30 additions & 0 deletions
30
packages/framework/framework/src/config/__tests__/index.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { configLoader } from "../loader" | ||
import { join } from "path" | ||
import { container } from "../../container" | ||
import { ContainerRegistrationKeys } from "@medusajs/utils" | ||
|
||
describe("configLoader", () => { | ||
const entryDirectory = join(__dirname, "../__fixtures__") | ||
|
||
it("should load the config properly", async () => { | ||
let configModule = container.resolve( | ||
ContainerRegistrationKeys.CONFIG_MODULE | ||
) | ||
|
||
expect(configModule).toBeUndefined() | ||
|
||
configLoader(entryDirectory, "medusa-config.js") | ||
|
||
configModule = container.resolve(ContainerRegistrationKeys.CONFIG_MODULE) | ||
|
||
expect(configModule).toBeDefined() | ||
expect(configModule.projectConfig.databaseName).toBeUndefined() | ||
|
||
configLoader(entryDirectory, "medusa-config-2.js") | ||
|
||
configModule = container.resolve(ContainerRegistrationKeys.CONFIG_MODULE) | ||
|
||
expect(configModule).toBeDefined() | ||
expect(configModule.projectConfig.databaseName).toBe("foo") | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
...ramework/framework/src/workflows/__fixtures__/workflows/deep-workflows/product-updater.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { | ||
createStep, | ||
createWorkflow, | ||
WorkflowResponse, | ||
} from "@medusajs/workflows-sdk" | ||
|
||
export const productWorkflowId = "product-notifier-workflow" | ||
|
||
const step = createStep("product-step", () => { | ||
return {} as any | ||
}) | ||
|
||
export const productUpdatedWorkflow = createWorkflow(productWorkflowId, () => { | ||
step() | ||
return new WorkflowResponse(void 0) | ||
}) |
16 changes: 16 additions & 0 deletions
16
packages/framework/framework/src/workflows/__fixtures__/workflows/order-notifier.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { | ||
createStep, | ||
createWorkflow, | ||
WorkflowResponse, | ||
} from "@medusajs/workflows-sdk" | ||
|
||
export const orderWorkflowId = "order-notifier-workflow" | ||
|
||
const step = createStep("order-step", () => { | ||
return {} as any | ||
}) | ||
|
||
export const orderNotifierWorkflow = createWorkflow(orderWorkflowId, () => { | ||
step() | ||
return new WorkflowResponse(void 0) | ||
}) |
21 changes: 21 additions & 0 deletions
21
packages/framework/framework/src/workflows/__tests__/index.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { join } from "path" | ||
import { WorkflowLoader } from "../workflow-loader" | ||
import { WorkflowManager } from "@medusajs/orchestration" | ||
import { orderWorkflowId } from "../__fixtures__/workflows/order-notifier" | ||
import { productWorkflowId } from "../__fixtures__/workflows/deep-workflows/product-updater" | ||
|
||
describe("WorkflowLoader", () => { | ||
const rootDir = join(__dirname, "../__fixtures__", "workflows") | ||
|
||
beforeAll(async () => { | ||
await new WorkflowLoader(rootDir).load() | ||
}) | ||
|
||
it("should register each workflow in the '/workflows' folder and sub folder", async () => { | ||
const registeredWorkflows = WorkflowManager.getWorkflows() | ||
|
||
expect(registeredWorkflows.size).toBe(2) | ||
expect(registeredWorkflows.has(orderWorkflowId)).toBe(true) | ||
expect(registeredWorkflows.has(productWorkflowId)).toBe(true) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./workflow-loader" |
71 changes: 71 additions & 0 deletions
71
packages/framework/framework/src/workflows/workflow-loader.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { promiseAll } from "@medusajs/utils" | ||
import { logger } from "../logger" | ||
import { access, readdir } from "fs/promises" | ||
import { join } from "path" | ||
|
||
export class WorkflowLoader { | ||
/** | ||
* The directory from which to load the workflows | ||
* @private | ||
*/ | ||
#sourceDir: string | string[] | ||
|
||
/** | ||
* The list of file names to exclude from the subscriber scan | ||
* @private | ||
*/ | ||
#excludes: RegExp[] = [ | ||
/index\.js/, | ||
/index\.ts/, | ||
/\.DS_Store/, | ||
/(\.ts\.map|\.js\.map|\.d\.ts|\.md)/, | ||
/^_[^/\\]*(\.[^/\\]+)?$/, | ||
] | ||
|
||
constructor(sourceDir: string | string[]) { | ||
this.#sourceDir = sourceDir | ||
} | ||
|
||
/** | ||
* Load workflows from the source paths, workflows are registering themselves, | ||
* therefore we only need to import them | ||
*/ | ||
async load() { | ||
const normalizedSourcePath = Array.isArray(this.#sourceDir) | ||
? this.#sourceDir | ||
: [this.#sourceDir] | ||
|
||
const promises = normalizedSourcePath.map(async (sourcePath) => { | ||
try { | ||
await access(sourcePath) | ||
} catch { | ||
return | ||
} | ||
|
||
return await readdir(sourcePath, { | ||
recursive: true, | ||
withFileTypes: true, | ||
}).then(async (entries) => { | ||
const fileEntries = entries.filter((entry) => { | ||
return ( | ||
!entry.isDirectory() && | ||
!this.#excludes.some((exclude) => exclude.test(entry.name)) | ||
) | ||
}) | ||
|
||
logger.debug(`Registering workflows from ${sourcePath}.`) | ||
|
||
return await promiseAll( | ||
fileEntries.map(async (entry) => { | ||
const fullPath = join(entry.path, entry.name) | ||
return await import(fullPath) | ||
}) | ||
) | ||
}) | ||
}) | ||
|
||
await promiseAll(promises) | ||
|
||
logger.debug(`Workflows registered.`) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters