Skip to content

Commit 181d5fa

Browse files
authored
feat(): Auto generated types generation upon build (medusajs#14337)
* feat(): Auto generated types generation upon build * Create happy-steaks-cheer.md * test(): default types true * improvements * improvements * improvements * improvements * improvements
1 parent 1256946 commit 181d5fa

File tree

5 files changed

+104
-6
lines changed

5 files changed

+104
-6
lines changed

.changeset/happy-steaks-cheer.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
"@medusajs/medusa": patch
3+
"@medusajs/framework": patch
4+
"@medusajs/modules-sdk": patch
5+
"@medusajs/cli": patch
6+
---
7+
8+
feat(): Auto generated types generation upon build

packages/core/framework/src/medusa-app-loader.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,13 +310,18 @@ export class MedusaAppLoader {
310310
* Load all modules and bootstrap all the modules and links to be ready to be consumed
311311
* @param config
312312
*/
313-
async load(config = { registerInContainer: true }): Promise<MedusaAppOutput> {
313+
async load(
314+
config: { registerInContainer?: boolean; migrationOnly?: boolean } = {
315+
registerInContainer: true,
316+
migrationOnly: false,
317+
}
318+
): Promise<MedusaAppOutput> {
314319
const configModule: ConfigModule = this.#container.resolve(
315320
ContainerRegistrationKeys.CONFIG_MODULE
316321
)
317322

318323
const { sharedResourcesConfig, injectedDependencies } =
319-
this.prepareSharedResourcesAndDeps()
324+
!config.migrationOnly ? this.prepareSharedResourcesAndDeps() : {}
320325

321326
this.#container.register(
322327
ContainerRegistrationKeys.REMOTE_QUERY,
@@ -343,6 +348,7 @@ export class MedusaAppLoader {
343348
injectedDependencies,
344349
medusaConfigPath: this.#medusaConfigPath,
345350
cwd: this.#cwd,
351+
migrationOnly: config.migrationOnly,
346352
})
347353

348354
if (!config.registerInContainer) {

packages/core/modules-sdk/src/medusa-app.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,11 @@ export type MedusaAppOptions = {
308308
* Forces the modules bootstrapper to only run the modules loaders and return prematurely
309309
*/
310310
loaderOnly?: boolean
311+
/**
312+
* Only partially load modules to retrieve their joiner configs without running loaders.
313+
* Useful for type generation and migrations.
314+
*/
315+
migrationOnly?: boolean
311316
cwd?: string
312317
}
313318

@@ -325,9 +330,7 @@ async function MedusaApp_({
325330
loaderOnly = false,
326331
workerMode = "shared",
327332
cwd = process.cwd(),
328-
}: MedusaAppOptions & {
329-
migrationOnly?: boolean
330-
} = {}): Promise<MedusaAppOutput> {
333+
}: MedusaAppOptions = {}): Promise<MedusaAppOutput> {
331334
const sharedContainer_ = createMedusaContainer({}, sharedContainer)
332335

333336
const config = sharedContainer_.resolve(

packages/medusa/src/commands/build.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Compiler } from "@medusajs/framework/build-tools"
22
import { ContainerRegistrationKeys } from "@medusajs/framework/utils"
33
import { initializeContainer } from "../loaders"
4+
import { generateTypes } from "./utils/generate-types"
45

56
export default async function build({
67
directory,
@@ -14,6 +15,12 @@ export default async function build({
1415
})
1516
const logger = container.resolve(ContainerRegistrationKeys.LOGGER)
1617

18+
await generateTypes({
19+
directory,
20+
container,
21+
logger,
22+
})
23+
1724
logger.info("Starting build...")
1825
const compiler = new Compiler(directory, logger)
1926

@@ -32,7 +39,9 @@ export default async function build({
3239
promises.push(compiler.buildAppFrontend(adminOnly, tsConfig, bundler))
3340
const responses = await Promise.all(promises)
3441

35-
if (responses.every((response) => response === true)) {
42+
const buildSucceeded = responses.every((response) => response === true)
43+
44+
if (buildSucceeded) {
3645
process.exit(0)
3746
} else {
3847
process.exit(1)
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import { LinkLoader, MedusaAppLoader } from "@medusajs/framework"
2+
import { MedusaModule } from "@medusajs/framework/modules-sdk"
3+
import {
4+
ContainerRegistrationKeys,
5+
FileSystem,
6+
generateContainerTypes,
7+
getResolvedPlugins,
8+
gqlSchemaToTypes,
9+
mergePluginModules,
10+
validateModuleName,
11+
} from "@medusajs/framework/utils"
12+
import { Logger, MedusaContainer } from "@medusajs/types"
13+
import path, { join } from "path"
14+
15+
export async function generateTypes({
16+
directory,
17+
container,
18+
logger,
19+
}: {
20+
directory: string
21+
container: MedusaContainer
22+
logger: Logger
23+
}) {
24+
logger.info("Generating types...")
25+
26+
const configModule = container.resolve(
27+
ContainerRegistrationKeys.CONFIG_MODULE
28+
)
29+
30+
const plugins = await getResolvedPlugins(directory, configModule, true)
31+
mergePluginModules(configModule, plugins)
32+
33+
Object.keys(configModule.modules ?? {}).forEach((key) => {
34+
validateModuleName(key)
35+
})
36+
37+
const linksSourcePaths = plugins.map((plugin) =>
38+
join(plugin.resolve, "links")
39+
)
40+
await new LinkLoader(linksSourcePaths, logger).load()
41+
42+
const { gqlSchema, modules } = await new MedusaAppLoader().load({
43+
registerInContainer: false,
44+
migrationOnly: true,
45+
})
46+
47+
const typesDirectory = path.join(directory, ".medusa/types")
48+
49+
/**
50+
* Cleanup existing types directory before creating new artifacts
51+
*/
52+
await new FileSystem(typesDirectory).cleanup({ recursive: true })
53+
54+
await generateContainerTypes(modules, {
55+
outputDir: typesDirectory,
56+
interfaceName: "ModuleImplementations",
57+
})
58+
logger.debug("Generated container types")
59+
60+
if (gqlSchema) {
61+
await gqlSchemaToTypes({
62+
outputDir: typesDirectory,
63+
filename: "query-entry-points",
64+
interfaceName: "RemoteQueryEntryPoints",
65+
schema: gqlSchema,
66+
joinerConfigs: MedusaModule.getAllJoinerConfigs(),
67+
})
68+
logger.debug("Generated modules types")
69+
}
70+
71+
logger.info("Types generated successfully")
72+
}

0 commit comments

Comments
 (0)