1- import type { FileInfo , FileInfoMap , ModuleType } from '../types' ;
1+ import type { FileInfo , FileInfoMap , ModuleType , TypeInfo } from '../types' ;
2+ import { isNull } from '@cmtlyt/base' ;
23import { readPackageJSON } from 'pkg-types' ;
34import { pascalCase } from 'scule' ;
45import { NEED_READ_PROTOTYPE_TYPES , NEED_RETURN_TYPES } from '../constant' ;
@@ -14,10 +15,6 @@ export function getItemType(item: Pick<FileInfo, 'type' | 'relativePath' | 'path
1415 return `typeof import('${ path } ')['default']${ NEED_READ_PROTOTYPE_TYPES . includes ( item . type ) ? `['prototype']` : '' } ` ;
1516}
1617
17- interface TypeInfo {
18- [ key : string ] : string | TypeInfo ;
19- }
20-
2118function generateTypeInfo ( fileInfoMap : FileInfoMap ) {
2219 const { ignoreModules } = getStorage ( 'config' ) ;
2320 const typeInfo : TypeInfo = { } ;
@@ -55,12 +52,63 @@ function generateType(typeInfoMap: TypeInfo, indent = 0) {
5552 return typeList . join ( '\n' ) ;
5653}
5754
55+ // function getObjectFlatValues(typeInfoMap: TypeInfo, propName: string) {
56+ // const result = typeInfoMap[propName];
57+ // if (!result)
58+ // return [];
59+ // if (typeof result === 'string')
60+ // return [result];
61+ // const names: string[] = [];
62+ // for (const name in result) {
63+ // names.push(...getObjectFlatValues(result, name));
64+ // }
65+ // return names;
66+ // }
67+
68+ // function routerSchemaTypeMerge(typeInfoMap: TypeInfo) {
69+ // const allExtendsTypes = getObjectFlatValues(typeInfoMap, 'routerSchema');
70+
71+ // if (!allExtendsTypes.length)
72+ // return '';
73+
74+ // delete typeInfoMap.routerSchema;
75+
76+ // return `interface IRouterSchema extends ${allExtendsTypes.join(', ')}, Record<string, any> {}`;
77+ // }
78+
5879export async function generateTypeString ( fileInfo : FileInfoMap ) {
5980 const { name } = await readPackageJSON ( import . meta. url ) ;
81+ const { generateTypeConfig : { extendsInfo, getInterface } } = getStorage ( 'config' ) ;
82+ const originTypeInfoMap = generateTypeInfo ( fileInfo ) ;
83+ const typeInfoMap = { ...originTypeInfoMap } ;
84+
85+ const userTypeContent = [ ] ;
6086
61- const typeInfoMap = generateTypeInfo ( fileInfo ) ;
87+ for ( const type in typeInfoMap ) {
88+ const _interface = await getInterface ( type , typeInfoMap [ type ] as TypeInfo ) ;
89+ if ( ! _interface )
90+ continue ;
91+ delete typeInfoMap [ type ] ;
92+ userTypeContent . push ( _interface ) ;
93+ }
94+
95+ const routerSchemaTypeContent = '' ; // routerSchemaTypeMerge(typeInfoMap);
6296
6397 const typeContent = generateType ( typeInfoMap ) ;
6498
65- return `import { MergeConfig } from '@cmtlyt/tee';\nexport {};\ndeclare module '${ name } ' {\n${ typeContent } \n #{configType}\n}` ;
99+ let computedType = `import { MergeConfig } from '${ name } ';\n#{extendsImport}\nexport {};\ndeclare module '${ name } ' {\n${ typeContent } \n ${ userTypeContent . join ( '\n' ) } \n ${ routerSchemaTypeContent } \n #{configType}\n #{extendsType}\n}` ;
100+
101+ if ( extendsInfo ) {
102+ const { importContent, typeContent } = extendsInfo ;
103+ if ( ! isNull ( importContent ) ) {
104+ const content = typeof importContent === 'string' ? importContent : await importContent ( typeInfoMap ) ;
105+ computedType = computedType . replace ( '#{extendsImport}' , content ) ;
106+ }
107+ if ( ! isNull ( typeContent ) ) {
108+ const content = typeof typeContent === 'string' ? typeContent : await typeContent ( typeInfoMap ) ;
109+ computedType = computedType . replace ( '#{extendsType}' , content ) ;
110+ }
111+ }
112+
113+ return computedType ;
66114}
0 commit comments