feat(): Auto generated types generation upon build (#14337)
* feat(): Auto generated types generation upon build * Create happy-steaks-cheer.md * test(): default types true * improvements * improvements * improvements * improvements * improvements
This commit is contained in:
committed by
GitHub
parent
125694626e
commit
181d5fa671
8
.changeset/happy-steaks-cheer.md
Normal file
8
.changeset/happy-steaks-cheer.md
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
---
|
||||||
|
"@medusajs/medusa": patch
|
||||||
|
"@medusajs/framework": patch
|
||||||
|
"@medusajs/modules-sdk": patch
|
||||||
|
"@medusajs/cli": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
feat(): Auto generated types generation upon build
|
||||||
@@ -310,13 +310,18 @@ export class MedusaAppLoader {
|
|||||||
* Load all modules and bootstrap all the modules and links to be ready to be consumed
|
* Load all modules and bootstrap all the modules and links to be ready to be consumed
|
||||||
* @param config
|
* @param config
|
||||||
*/
|
*/
|
||||||
async load(config = { registerInContainer: true }): Promise<MedusaAppOutput> {
|
async load(
|
||||||
|
config: { registerInContainer?: boolean; migrationOnly?: boolean } = {
|
||||||
|
registerInContainer: true,
|
||||||
|
migrationOnly: false,
|
||||||
|
}
|
||||||
|
): Promise<MedusaAppOutput> {
|
||||||
const configModule: ConfigModule = this.#container.resolve(
|
const configModule: ConfigModule = this.#container.resolve(
|
||||||
ContainerRegistrationKeys.CONFIG_MODULE
|
ContainerRegistrationKeys.CONFIG_MODULE
|
||||||
)
|
)
|
||||||
|
|
||||||
const { sharedResourcesConfig, injectedDependencies } =
|
const { sharedResourcesConfig, injectedDependencies } =
|
||||||
this.prepareSharedResourcesAndDeps()
|
!config.migrationOnly ? this.prepareSharedResourcesAndDeps() : {}
|
||||||
|
|
||||||
this.#container.register(
|
this.#container.register(
|
||||||
ContainerRegistrationKeys.REMOTE_QUERY,
|
ContainerRegistrationKeys.REMOTE_QUERY,
|
||||||
@@ -343,6 +348,7 @@ export class MedusaAppLoader {
|
|||||||
injectedDependencies,
|
injectedDependencies,
|
||||||
medusaConfigPath: this.#medusaConfigPath,
|
medusaConfigPath: this.#medusaConfigPath,
|
||||||
cwd: this.#cwd,
|
cwd: this.#cwd,
|
||||||
|
migrationOnly: config.migrationOnly,
|
||||||
})
|
})
|
||||||
|
|
||||||
if (!config.registerInContainer) {
|
if (!config.registerInContainer) {
|
||||||
|
|||||||
@@ -308,6 +308,11 @@ export type MedusaAppOptions = {
|
|||||||
* Forces the modules bootstrapper to only run the modules loaders and return prematurely
|
* Forces the modules bootstrapper to only run the modules loaders and return prematurely
|
||||||
*/
|
*/
|
||||||
loaderOnly?: boolean
|
loaderOnly?: boolean
|
||||||
|
/**
|
||||||
|
* Only partially load modules to retrieve their joiner configs without running loaders.
|
||||||
|
* Useful for type generation and migrations.
|
||||||
|
*/
|
||||||
|
migrationOnly?: boolean
|
||||||
cwd?: string
|
cwd?: string
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -325,9 +330,7 @@ async function MedusaApp_({
|
|||||||
loaderOnly = false,
|
loaderOnly = false,
|
||||||
workerMode = "shared",
|
workerMode = "shared",
|
||||||
cwd = process.cwd(),
|
cwd = process.cwd(),
|
||||||
}: MedusaAppOptions & {
|
}: MedusaAppOptions = {}): Promise<MedusaAppOutput> {
|
||||||
migrationOnly?: boolean
|
|
||||||
} = {}): Promise<MedusaAppOutput> {
|
|
||||||
const sharedContainer_ = createMedusaContainer({}, sharedContainer)
|
const sharedContainer_ = createMedusaContainer({}, sharedContainer)
|
||||||
|
|
||||||
const config = sharedContainer_.resolve(
|
const config = sharedContainer_.resolve(
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import { Compiler } from "@medusajs/framework/build-tools"
|
import { Compiler } from "@medusajs/framework/build-tools"
|
||||||
import { ContainerRegistrationKeys } from "@medusajs/framework/utils"
|
import { ContainerRegistrationKeys } from "@medusajs/framework/utils"
|
||||||
import { initializeContainer } from "../loaders"
|
import { initializeContainer } from "../loaders"
|
||||||
|
import { generateTypes } from "./utils/generate-types"
|
||||||
|
|
||||||
export default async function build({
|
export default async function build({
|
||||||
directory,
|
directory,
|
||||||
@@ -14,6 +15,12 @@ export default async function build({
|
|||||||
})
|
})
|
||||||
const logger = container.resolve(ContainerRegistrationKeys.LOGGER)
|
const logger = container.resolve(ContainerRegistrationKeys.LOGGER)
|
||||||
|
|
||||||
|
await generateTypes({
|
||||||
|
directory,
|
||||||
|
container,
|
||||||
|
logger,
|
||||||
|
})
|
||||||
|
|
||||||
logger.info("Starting build...")
|
logger.info("Starting build...")
|
||||||
const compiler = new Compiler(directory, logger)
|
const compiler = new Compiler(directory, logger)
|
||||||
|
|
||||||
@@ -32,7 +39,9 @@ export default async function build({
|
|||||||
promises.push(compiler.buildAppFrontend(adminOnly, tsConfig, bundler))
|
promises.push(compiler.buildAppFrontend(adminOnly, tsConfig, bundler))
|
||||||
const responses = await Promise.all(promises)
|
const responses = await Promise.all(promises)
|
||||||
|
|
||||||
if (responses.every((response) => response === true)) {
|
const buildSucceeded = responses.every((response) => response === true)
|
||||||
|
|
||||||
|
if (buildSucceeded) {
|
||||||
process.exit(0)
|
process.exit(0)
|
||||||
} else {
|
} else {
|
||||||
process.exit(1)
|
process.exit(1)
|
||||||
|
|||||||
72
packages/medusa/src/commands/utils/generate-types.ts
Normal file
72
packages/medusa/src/commands/utils/generate-types.ts
Normal file
@@ -0,0 +1,72 @@
|
|||||||
|
import { LinkLoader, MedusaAppLoader } from "@medusajs/framework"
|
||||||
|
import { MedusaModule } from "@medusajs/framework/modules-sdk"
|
||||||
|
import {
|
||||||
|
ContainerRegistrationKeys,
|
||||||
|
FileSystem,
|
||||||
|
generateContainerTypes,
|
||||||
|
getResolvedPlugins,
|
||||||
|
gqlSchemaToTypes,
|
||||||
|
mergePluginModules,
|
||||||
|
validateModuleName,
|
||||||
|
} from "@medusajs/framework/utils"
|
||||||
|
import { Logger, MedusaContainer } from "@medusajs/types"
|
||||||
|
import path, { join } from "path"
|
||||||
|
|
||||||
|
export async function generateTypes({
|
||||||
|
directory,
|
||||||
|
container,
|
||||||
|
logger,
|
||||||
|
}: {
|
||||||
|
directory: string
|
||||||
|
container: MedusaContainer
|
||||||
|
logger: Logger
|
||||||
|
}) {
|
||||||
|
logger.info("Generating types...")
|
||||||
|
|
||||||
|
const configModule = container.resolve(
|
||||||
|
ContainerRegistrationKeys.CONFIG_MODULE
|
||||||
|
)
|
||||||
|
|
||||||
|
const plugins = await getResolvedPlugins(directory, configModule, true)
|
||||||
|
mergePluginModules(configModule, plugins)
|
||||||
|
|
||||||
|
Object.keys(configModule.modules ?? {}).forEach((key) => {
|
||||||
|
validateModuleName(key)
|
||||||
|
})
|
||||||
|
|
||||||
|
const linksSourcePaths = plugins.map((plugin) =>
|
||||||
|
join(plugin.resolve, "links")
|
||||||
|
)
|
||||||
|
await new LinkLoader(linksSourcePaths, logger).load()
|
||||||
|
|
||||||
|
const { gqlSchema, modules } = await new MedusaAppLoader().load({
|
||||||
|
registerInContainer: false,
|
||||||
|
migrationOnly: true,
|
||||||
|
})
|
||||||
|
|
||||||
|
const typesDirectory = path.join(directory, ".medusa/types")
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Cleanup existing types directory before creating new artifacts
|
||||||
|
*/
|
||||||
|
await new FileSystem(typesDirectory).cleanup({ recursive: true })
|
||||||
|
|
||||||
|
await generateContainerTypes(modules, {
|
||||||
|
outputDir: typesDirectory,
|
||||||
|
interfaceName: "ModuleImplementations",
|
||||||
|
})
|
||||||
|
logger.debug("Generated container types")
|
||||||
|
|
||||||
|
if (gqlSchema) {
|
||||||
|
await gqlSchemaToTypes({
|
||||||
|
outputDir: typesDirectory,
|
||||||
|
filename: "query-entry-points",
|
||||||
|
interfaceName: "RemoteQueryEntryPoints",
|
||||||
|
schema: gqlSchema,
|
||||||
|
joinerConfigs: MedusaModule.getAllJoinerConfigs(),
|
||||||
|
})
|
||||||
|
logger.debug("Generated modules types")
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.info("Types generated successfully")
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user