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:
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