feature: Add MikroORM CLI wrapper to bypass hardcoded module system (#9426)
FIXES: FRMW-2727 MikroORM (with version 5.9) has [hardcoded the TypeScript module](https://github.com/mikro-orm/mikro-orm/blob/5.x/packages/core/src/utils/ConfigurationLoader.ts#L138-L139) system to `commonjs`, which makes it incompatible with the module system we are using, ie `Node16`. So, in order to continue using the Mikro ORM CLI within our modules, we will have to monkey-patch the block of code responsible for configuring `ts-node`. However, the monkey-patching must be done before their CLI gets booted. As a result of this, we have to create a wrapper CLI on top of Mikro ORM CLI that performs the following steps. - Monkey-patch the relevant code - Register Mikro ORM CLI as the second step. Due do this, we will have to use this new wrapper CLI within the modules, which is exposed as `medusa-db`. Maybe, `medusa-db` is not a great name, so please send your suggestions.
This commit is contained in:
@@ -10,6 +10,9 @@
|
||||
"!dist/**/__mocks__",
|
||||
"!dist/**/__fixtures__"
|
||||
],
|
||||
"bin": {
|
||||
"medusa-mikro-orm": "./dist/mikro-orm-cli/bin.js"
|
||||
},
|
||||
"exports": {
|
||||
".": "./dist/index.js",
|
||||
"./config": "./dist/config/index.js",
|
||||
@@ -68,6 +71,7 @@
|
||||
"vite": "^5.2.11"
|
||||
},
|
||||
"dependencies": {
|
||||
"@jercle/yargonaut": "^1.1.5",
|
||||
"@medusajs/modules-sdk": "^1.12.11",
|
||||
"@medusajs/orchestration": "^0.5.7",
|
||||
"@medusajs/types": "^1.11.16",
|
||||
@@ -82,10 +86,12 @@
|
||||
"jsonwebtoken": "^9.0.2",
|
||||
"medusa-telemetry": "^0.0.17",
|
||||
"morgan": "^1.9.1",
|
||||
"tsconfig-paths": "^4.2.0",
|
||||
"zod": "3.22.4"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@medusajs/medusa-cli": "^1.3.22",
|
||||
"@mikro-orm/cli": "5.9.7",
|
||||
"@mikro-orm/core": "5.9.7",
|
||||
"@mikro-orm/knex": "5.9.7",
|
||||
"@mikro-orm/migrations": "5.9.7",
|
||||
@@ -96,6 +102,9 @@
|
||||
"vite": "^5.2.11"
|
||||
},
|
||||
"peerDependenciesMeta": {
|
||||
"@mikro-orm/cli": {
|
||||
"optional": true
|
||||
},
|
||||
"ioredis": {
|
||||
"optional": true
|
||||
},
|
||||
|
||||
61
packages/core/framework/src/mikro-orm-cli/bin.ts
Normal file
61
packages/core/framework/src/mikro-orm-cli/bin.ts
Normal file
@@ -0,0 +1,61 @@
|
||||
/**
|
||||
* Custom wrapper on top of MikroORM CLI to override the issue
|
||||
* they have when importing TypeScript files.
|
||||
*
|
||||
* They have hardcoded the module system of TypeScript to CommonJS
|
||||
* and that makes it impossible to use any other module system
|
||||
* like Node16 or NodeNext and so on.
|
||||
*
|
||||
* With this wrapper, we monkey patch the code responsible for register
|
||||
* ts-node and then boot their CLI. Since, the code footprint is
|
||||
* small, we should be okay with managing this wrapper.
|
||||
*/
|
||||
|
||||
import { isAbsolute, join } from "path"
|
||||
import { ConfigurationLoader } from "@mikro-orm/core"
|
||||
import { CLIConfigurator, CLIHelper } from "@mikro-orm/cli"
|
||||
|
||||
/**
|
||||
* Monkey patching the TSNode registration of Mikro ORM to not use
|
||||
* hardcoded module system with TypeScript.
|
||||
*/
|
||||
ConfigurationLoader.registerTsNode = async function (
|
||||
configPath = "tsconfig.json"
|
||||
) {
|
||||
const tsConfigPath = isAbsolute(configPath)
|
||||
? configPath
|
||||
: join(process.cwd(), configPath)
|
||||
|
||||
const tsNode = require(require.resolve("ts-node", { paths: [tsConfigPath] }))
|
||||
if (!tsNode) {
|
||||
return false
|
||||
}
|
||||
|
||||
const { options } = tsNode.register({
|
||||
project: tsConfigPath,
|
||||
transpileOnly: true,
|
||||
}).config
|
||||
|
||||
if (Object.entries(options?.paths ?? {}).length > 0) {
|
||||
require("tsconfig-paths").register({
|
||||
baseUrl: options.baseUrl ?? ".",
|
||||
paths: options.paths,
|
||||
})
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
require("@jercle/yargonaut")
|
||||
.style("blue")
|
||||
.style("yellow", "required")
|
||||
.helpStyle("green")
|
||||
.errorsStyle("red")
|
||||
|
||||
;(async () => {
|
||||
const argv = await CLIConfigurator.configure()
|
||||
const args = await argv.parse(process.argv.slice(2))
|
||||
if (args._.length === 0) {
|
||||
CLIHelper.showHelp()
|
||||
}
|
||||
})()
|
||||
Reference in New Issue
Block a user