@@ -15,22 +15,18 @@ export async function findAppFolderPath() {
1515 return null ;
1616}
1717
18- function advancedEval ( code : string ) {
19- try {
20- return eval ( code ) ;
21- } catch ( error ) {
22- if ( error instanceof ReferenceError ) {
23- const refName = error . message . replace ( "is not defined" , "" ) . trim ( ) ;
24- return advancedEval ( code . replace ( new RegExp ( `\\b${ refName } \\b` , "g" ) , `"${ refName } "` ) ) ;
25- }
26- throw error ;
27- // console.error(error);
28- // return {};
29- }
18+ function injectSchemas ( code : string , refName : string ) {
19+ return code
20+ . replace ( new RegExp ( `\\b${ refName } \\.` , "g" ) , `global.schemas[${ refName } ].` )
21+ . replace ( new RegExp ( `\\b${ refName } \\b` , "g" ) , `"${ refName } "` ) ;
3022}
3123
32- export async function getRouteExports ( routePath : string ) {
24+ export async function getRouteExports ( routePath : string , schemas : Record < string , unknown > ) {
3325 const content = await fs . readFile ( routePath , "utf-8" ) ;
3426 const code = transpile ( content ) ;
35- return advancedEval ( code ) as Record < string , { apiData ?: unknown } | undefined > ;
27+ const fixedCode = Object . keys ( schemas ) . reduce ( injectSchemas , code ) ;
28+ ( global as Record < string , unknown > ) . schemas = schemas ;
29+ const result = eval ( fixedCode ) ;
30+ delete ( global as Record < string , unknown > ) . schemas ;
31+ return result as Record < string , { apiData ?: unknown } | undefined > ;
3632}
0 commit comments