@@ -407,7 +407,7 @@ function generateDeclarationFile(items, filePath, organizedData) {
407
407
const moduleName = getModuleInfo ( items [ 0 ] ) . module ;
408
408
409
409
// Begin module declaration
410
- output += `declare module '${ moduleName } ' {\n` ;
410
+ output += `declare module 'p5 ' {\n` ;
411
411
412
412
// Find the class documentation if it exists
413
413
const classDoc = items . find ( item => item . kind === 'class' ) ;
@@ -528,9 +528,47 @@ function generateAllDeclarationFiles() {
528
528
} ) ;
529
529
}
530
530
531
+ function findTypeDefinitionFiles ( rootDir , p5DtsPath ) {
532
+ const typeFiles = new Set ( ) ;
533
+ const srcDir = path . join ( rootDir , 'src' ) ;
534
+
535
+ // Check if src directory exists
536
+ if ( ! fs . existsSync ( srcDir ) ) {
537
+ return [ ] ;
538
+ }
539
+
540
+ function scan ( dir ) {
541
+ const files = fs . readdirSync ( dir ) ;
542
+ files . forEach ( file => {
543
+ const fullPath = path . join ( dir , file ) ;
544
+ if ( fs . statSync ( fullPath ) . isDirectory ( ) ) {
545
+ scan ( fullPath ) ;
546
+ } else if ( file . endsWith ( '.d.ts' ) ) {
547
+ // Get path relative to p5.d.ts location and normalize slashes
548
+ const relativePath = path . relative ( path . dirname ( p5DtsPath ) , fullPath )
549
+ . replace ( / \\ / g, '/' ) ;
550
+ typeFiles . add ( relativePath ) ;
551
+ }
552
+ } ) ;
553
+ }
554
+
555
+ // Only scan the src directory
556
+ scan ( srcDir ) ;
557
+ return Array . from ( typeFiles ) . sort ( ) ;
558
+ }
559
+
531
560
function generateCoreTypeDefinitions ( organizedData ) {
561
+ const p5DtsPath = path . join ( process . cwd ( ) , 'types' , 'p5.d.ts' ) ;
562
+
532
563
// Generate p5.d.ts
533
564
let p5Output = '// This file is auto-generated from JSDoc documentation\n\n' ;
565
+
566
+ // Add reference paths to other .d.ts files
567
+ const typeFiles = findTypeDefinitionFiles ( process . cwd ( ) , p5DtsPath ) ;
568
+ typeFiles . forEach ( file => {
569
+ p5Output += `/// <reference types="${ file } " />\n` ;
570
+ } ) ;
571
+ p5Output += '\n' ;
534
572
535
573
// Generate the p5 class
536
574
p5Output += `declare class p5 {\n` ;
@@ -596,21 +634,63 @@ function generateCoreTypeDefinitions(organizedData) {
596
634
let globalOutput = '// This file is auto-generated from JSDoc documentation\n\n' ;
597
635
globalOutput += `import p5 from 'p5';\n\n` ;
598
636
globalOutput += `declare global {\n` ;
599
- globalOutput += ` interface Window {\n` ;
600
637
638
+ // Generate global function declarations first
639
+ instanceItems . forEach ( item => {
640
+ if ( item . kind === 'function' ) {
641
+ // Add JSDoc for global function
642
+ if ( item . description ) {
643
+ globalOutput += ` /**\n${ formatJSDocComment ( item . description , 2 ) } \n */\n` ;
644
+ }
645
+
646
+ // Handle function overloads
647
+ if ( item . overloads ?. length > 0 ) {
648
+ item . overloads . forEach ( overload => {
649
+ const params = ( overload . params || [ ] )
650
+ . map ( param => generateParamDeclaration ( param ) )
651
+ . join ( ', ' ) ;
652
+ const returnType = overload . returns ?. [ 0 ] ?. type
653
+ ? generateTypeFromTag ( overload . returns [ 0 ] )
654
+ : 'void' ;
655
+ globalOutput += ` function ${ item . name } (${ params } ): ${ returnType } ;\n` ;
656
+ } ) ;
657
+ }
658
+
659
+ // Add main function declaration
660
+ const params = ( item . params || [ ] )
661
+ . map ( param => generateParamDeclaration ( param ) )
662
+ . join ( ', ' ) ;
663
+ globalOutput += ` function ${ item . name } (${ params } ): ${ item . returnType } ;\n\n` ;
664
+ }
665
+ } ) ;
666
+
667
+ // Add global constants
668
+ Object . values ( organizedData . consts ) . forEach ( constData => {
669
+ if ( constData . kind === 'constant' ) {
670
+ if ( constData . description ) {
671
+ globalOutput += ` /**\n${ formatJSDocComment ( constData . description , 2 ) } \n */\n` ;
672
+ }
673
+ globalOutput += ` const ${ constData . name . toUpperCase ( ) } : p5.${ constData . name . toUpperCase ( ) } ;\n\n` ;
674
+ }
675
+ } ) ;
676
+
677
+ // Generate Window interface
678
+ globalOutput += ` interface Window {\n` ;
601
679
602
- // Add instance methods
680
+ // Add function references to Window interface
603
681
instanceItems . forEach ( item => {
604
- globalOutput += generateMethodDeclarations ( item ) ;
682
+ if ( item . kind === 'function' ) {
683
+ globalOutput += ` ${ item . name } : typeof ${ item . name } ;\n` ;
684
+ }
605
685
} ) ;
606
686
607
- // Add constants to global scope
687
+ // Add constant references to Window interface
608
688
Object . values ( organizedData . consts ) . forEach ( constData => {
609
689
if ( constData . kind === 'constant' ) {
610
690
if ( constData . description ) {
611
691
globalOutput += ` /**\n * ${ constData . description } \n */\n` ;
612
692
}
613
- globalOutput += ` readonly ${ constData . name . toUpperCase ( ) } : ${ constData . type } ;\n \n` ;
693
+ globalOutput += ` readonly ${ constData . name . toUpperCase ( ) } : typeof ${ constData . name . toUpperCase ( ) } ; \n` ;
614
694
}
615
695
} ) ;
616
696
0 commit comments