Skip to content

Commit 87123c7

Browse files
committed
added reference paths
1 parent 0f9e43d commit 87123c7

File tree

2 files changed

+87
-7
lines changed

2 files changed

+87
-7
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ docs/reference/*
1010
examples/3d/
1111
.idea
1212
dist/
13-
*d.ts
13+
*.d.ts
1414
p5.zip
1515
bower-repo/
1616
p5-website/

utils/generate-types.js

Lines changed: 86 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ function generateDeclarationFile(items, filePath, organizedData) {
407407
const moduleName = getModuleInfo(items[0]).module;
408408

409409
// Begin module declaration
410-
output += `declare module '${moduleName}' {\n`;
410+
output += `declare module 'p5' {\n`;
411411

412412
// Find the class documentation if it exists
413413
const classDoc = items.find(item => item.kind === 'class');
@@ -528,9 +528,47 @@ function generateAllDeclarationFiles() {
528528
});
529529
}
530530

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+
531560
function generateCoreTypeDefinitions(organizedData) {
561+
const p5DtsPath = path.join(process.cwd(), 'types', 'p5.d.ts');
562+
532563
// Generate p5.d.ts
533564
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';
534572

535573
// Generate the p5 class
536574
p5Output += `declare class p5 {\n`;
@@ -596,21 +634,63 @@ function generateCoreTypeDefinitions(organizedData) {
596634
let globalOutput = '// This file is auto-generated from JSDoc documentation\n\n';
597635
globalOutput += `import p5 from 'p5';\n\n`;
598636
globalOutput += `declare global {\n`;
599-
globalOutput += ` interface Window {\n`;
600637

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`;
601679

602-
// Add instance methods
680+
// Add function references to Window interface
603681
instanceItems.forEach(item => {
604-
globalOutput += generateMethodDeclarations(item);
682+
if (item.kind === 'function') {
683+
globalOutput += ` ${item.name}: typeof ${item.name};\n`;
684+
}
605685
});
606686

607-
// Add constants to global scope
687+
// Add constant references to Window interface
608688
Object.values(organizedData.consts).forEach(constData => {
609689
if (constData.kind === 'constant') {
610690
if (constData.description) {
611691
globalOutput += ` /**\n * ${constData.description}\n */\n`;
612692
}
613-
globalOutput += ` readonly ${constData.name.toUpperCase()}: ${constData.type};\n\n`;
693+
globalOutput += ` readonly ${constData.name.toUpperCase()}: typeof ${constData.name.toUpperCase()};\n`;
614694
}
615695
});
616696

0 commit comments

Comments
 (0)