chore(framework): Initial commit (#8221)

**What**
- Initiate the framework package (which is just a place to move things around for now)
- move the config loader and related resources as well as the `ConfigModule` type
- Create a ConfigManager singleton which prepare and store the config (later can be stored entirely in the container) and allow for easier test override
- re export the logger from the framework
- replace medusa config loader with the framework one
- `build` run type check on tests as well but `prepublishOnly` will not fail on build if tests are typed broken

FIXES FRMW-2607
FIXES FRMW-2609
FIXES FRMW-2614
FIXES FRMW-2618
This commit is contained in:
Adrien de Peretti
2024-07-23 15:46:28 +00:00
committed by GitHub
parent 97f64a5cfe
commit 47dde05517
30 changed files with 1305 additions and 170 deletions
@@ -1,7 +1,7 @@
import { StepResponse, createStep } from "@medusajs/workflows-sdk"
import { createStep, StepResponse } from "@medusajs/workflows-sdk"
import { IAuthModuleService } from "@medusajs/types"
import { ModuleRegistrationName, isDefined } from "@medusajs/utils"
import { isDefined, ModuleRegistrationName } from "@medusajs/utils"
type StepInput = {
authIdentityId: string
+5 -1
View File
@@ -21,7 +21,7 @@
"files": [
"dist"
],
"author": "Sebastian Rindom",
"author": "Medusa",
"license": "MIT",
"devDependencies": {
"@medusajs/types": "^1.11.16",
@@ -31,6 +31,7 @@
"typescript": "^5.1.6"
},
"peerDependencies": {
"@medusajs/framework": "workspace:^",
"@medusajs/medusa": ">1.19",
"@medusajs/modules-sdk": "^1.12.10",
"axios": "^0.28.0",
@@ -39,6 +40,9 @@
"pg-god": "^1.0.12"
},
"peerDependenciesMeta": {
"@medusajs/framework": {
"optional": true
},
"@medusajs/medusa": {
"optional": true
},
@@ -0,0 +1,31 @@
import { getConfigFile } from "@medusajs/utils"
export async function configLoaderOverride(
entryDirectory: string,
override: { clientUrl: string; debug?: boolean }
) {
// in case it is not install as it is optional and required only when using this util
// @ts-ignore
const { configManager } = await import("@medusajs/framework/config")
const { configModule, error } = getConfigFile<
ReturnType<typeof configManager.loadConfig>
>(entryDirectory, "medusa-config.js")
if (error) {
throw new Error(error.message || "Error during config loading")
}
configModule.projectConfig.databaseUrl = override.clientUrl
configModule.projectConfig.databaseLogging = !!override.debug
configModule.projectConfig.databaseDriverOptions =
override.clientUrl.includes("localhost")
? {}
: {
connection: {
ssl: { rejectUnauthorized: false },
},
idle_in_transaction_session_timeout: 20000,
}
configManager.loadConfig(configModule)
}
@@ -5,21 +5,17 @@ import {
} from "@medusajs/utils"
import { asValue } from "awilix"
export async function initDb({
cwd,
env = {},
}: {
cwd: string
env?: Record<any, any>
}) {
export async function initDb({ env = {} }: { env?: Record<any, any> }) {
if (isObject(env)) {
Object.entries(env).forEach(([k, v]) => (process.env[k] = v))
}
const container = createMedusaContainer()
const configModule =
await require("@medusajs/medusa/dist/loaders/config").default(cwd)
// in case it is not install as it is optional and required only when using this util
// @ts-ignore
const { configManager } = await import("@medusajs/framework/config")
const configModule = configManager.config
const pgConnection =
await require("@medusajs/medusa/dist/loaders/pg-connection").default({
@@ -7,6 +7,7 @@ import { createDatabase, dropDatabase } from "pg-god"
import { getDatabaseURL } from "./database"
import { startApp } from "./medusa-test-runner-utils/bootstrap-app"
import { initDb } from "./medusa-test-runner-utils/use-db"
import { configLoaderOverride } from "./medusa-test-runner-utils/config"
const axios = require("axios").default
@@ -102,27 +103,6 @@ export function medusaIntegrationTestRunner({
debug,
}
const originalConfigLoader =
require("@medusajs/medusa/dist/loaders/config").default
require("@medusajs/medusa/dist/loaders/config").default = (
rootDirectory: string
) => {
const config = originalConfigLoader(rootDirectory)
config.projectConfig.databaseUrl = dbConfig.clientUrl
config.projectConfig.databaseLogging = !!dbConfig.debug
config.projectConfig.databaseDriverOptions = dbConfig.clientUrl.includes(
"localhost"
)
? {}
: {
connection: {
ssl: { rejectUnauthorized: false },
},
idle_in_transaction_session_timeout: 20000,
}
return config
}
const cwd = process.cwd()
let shutdown = async () => void 0
@@ -159,12 +139,13 @@ export function medusaIntegrationTestRunner({
let isFirstTime = true
const beforeAll_ = async () => {
await configLoaderOverride(cwd, dbConfig)
console.log(`Creating database ${dbName}`)
await dbUtils.create(dbName)
try {
dbUtils.pgConnection_ = await initDb({
cwd,
env,
})
} catch (error) {
@@ -6,8 +6,8 @@
"esModuleInterop": true,
"declarationMap": true,
"declaration": true,
"module": "commonjs",
"moduleResolution": "node",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"sourceMap": true,
+6 -5
View File
@@ -6,9 +6,10 @@ import { isObject } from "./is-object"
*
* @param obj
*/
export function deepCopy<T extends Record<any, any> = Record<any, any>>(
obj: T | T[]
): T | T[] {
export function deepCopy<
T extends Record<any, any> | Record<any, any>[] = Record<any, any>,
TOutput = T extends [] ? T[] : T
>(obj: T): TOutput {
if (obj === null || typeof obj !== "object") {
return obj
}
@@ -18,14 +19,14 @@ export function deepCopy<T extends Record<any, any> = Record<any, any>>(
for (let i = 0; i < obj.length; i++) {
copy[i] = deepCopy(obj[i])
}
return copy
return copy as TOutput
}
if (isObject(obj)) {
const copy: Record<any, any> = {}
for (let attr in obj) {
if (obj.hasOwnProperty(attr)) {
copy[attr] = deepCopy(obj[attr])
copy[attr] = deepCopy(obj[attr] as T)
}
}
return copy