@@ -10,7 +10,10 @@ import { anyOf, createRegExp } from 'magic-regexp'
1010import { consola } from 'consola'
1111import type { ModuleMeta , NuxtModule } from '@nuxt/schema'
1212import { findExports , resolvePath } from 'mlly'
13+ import { resolveSchema , generateTypes } from 'untyped'
14+ import type { InputObject } from 'untyped'
1315import { defineCommand } from 'citty'
16+ import { loadNuxt } from '@nuxt/kit'
1417
1518import { name , version } from '../../package.json'
1619
@@ -169,14 +172,24 @@ export default defineCommand({
169172 await fsp . writeFile ( metaFile , JSON . stringify ( moduleMeta , null , 2 ) , 'utf8' )
170173
171174 // Generate types
172- await writeTypes ( ctx . options . outDir , moduleMeta )
175+ await writeTypes ( ctx . options . outDir , moduleMeta , async ( ) => {
176+ const nuxt = await loadNuxt ( {
177+ cwd,
178+ overrides : {
179+ modules : [ resolve ( cwd , './src/module' ) ] ,
180+ } ,
181+ } )
182+ const moduleOptions = await moduleFn . getOptions ?.( { } , nuxt ) || { }
183+ await nuxt . close ( )
184+ return moduleOptions
185+ } )
173186 } ,
174187 } ,
175188 } )
176189 } ,
177190} )
178191
179- async function writeTypes ( distDir : string , meta : ModuleMeta ) {
192+ async function writeTypes ( distDir : string , meta : ModuleMeta , getOptions : ( ) => Promise < Record < string , unknown > > ) {
180193 const dtsFile = resolve ( distDir , 'types.d.ts' )
181194 const dtsFileMts = resolve ( distDir , 'types.d.mts' )
182195 if ( existsSync ( dtsFile ) ) {
@@ -206,6 +219,9 @@ async function writeTypes(distDir: string, meta: ModuleMeta) {
206219 schemaShims . push ( ` interface NuxtConfig { ['${ meta . configKey } ']?: Partial<ModuleOptions> }` )
207220 schemaShims . push ( ` interface NuxtOptions { ['${ meta . configKey } ']?: ModuleOptions }` )
208221 }
222+ else if ( meta . configKey ) {
223+ schemaShims . push ( ` ${ generateTypes ( await resolveSchema ( await getOptions ( ) as InputObject ) , { interfaceName : 'ModuleOptions' } ) } ` )
224+ }
209225 if ( hasTypeExport ( 'ModuleHooks' ) ) {
210226 moduleImports . push ( 'ModuleHooks' )
211227 schemaShims . push ( ' interface NuxtHooks extends ModuleHooks {}' )
0 commit comments