batch-auto-knowledge.js 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /**
  2. * Batch auto_knowledge — generate complete COMPONENT_PROPERTIES from d.ts
  3. * Output: knowledge JSON files + combined TS snippet for knowledge-handler.ts
  4. */
  5. const path = require('path');
  6. const fs = require('fs');
  7. const mod = require(path.resolve(__dirname, '../dist/tools/cocos/utils/component-knowledge'));
  8. const ALL_COMPONENTS = [
  9. 'cc.Label', 'cc.Sprite', 'cc.Button', 'cc.Widget', 'cc.Layout',
  10. 'cc.UITransform', 'cc.RichText', 'cc.ScrollView', 'cc.Toggle',
  11. 'cc.ToggleContainer', 'cc.ProgressBar', 'cc.Slider', 'cc.PageView',
  12. 'cc.Mask', 'cc.Graphics', 'cc.UIOpacity', 'cc.SafeArea',
  13. 'cc.BlockInputEvents', 'cc.LabelOutline', 'cc.LabelShadow',
  14. 'cc.PageViewIndicator', 'cc.EditBox',
  15. 'cc.AudioSource', 'cc.VideoPlayer', 'cc.WebView',
  16. 'cc.ParticleSystem2D', 'cc.ParticleSystem',
  17. 'cc.Camera', 'cc.Canvas', 'cc.Animation', 'cc.SkeletalAnimation',
  18. 'cc.MeshRenderer', 'cc.SkinnedMeshRenderer',
  19. 'cc.DirectionalLight', 'cc.PointLight', 'cc.SpotLight', 'cc.SphereLight',
  20. 'cc.RigidBody2D', 'cc.BoxCollider2D', 'cc.CircleCollider2D', 'cc.PolygonCollider2D',
  21. 'cc.RigidBody', 'cc.BoxCollider', 'cc.SphereCollider', 'cc.CapsuleCollider', 'cc.MeshCollider',
  22. 'cc.MotionStreak', 'cc.Billboard', 'cc.Line',
  23. 'cc.TiledMap', 'cc.TiledLayer',
  24. 'cc.HingeJoint2D', 'cc.DistanceJoint2D', 'cc.SpringJoint2D',
  25. 'cc.FixedJoint2D', 'cc.WheelJoint2D', 'cc.SliderJoint2D',
  26. 'cc.RelativeJoint2D', 'cc.MouseJoint2D',
  27. 'cc.HingeConstraint', 'cc.FixedConstraint', 'cc.PointToPointConstraint',
  28. 'cc.Terrain', 'cc.LODGroup', 'cc.ReflectionProbe',
  29. 'sp.Skeleton', 'dragonBones.ArmatureDisplay',
  30. ];
  31. const snippets = [];
  32. let saved = 0, failed = 0;
  33. for (const comp of ALL_COMPONENTS) {
  34. const className = mod.componentTypeToClassName(comp);
  35. const dtsInfo = mod.parseDtsClass(className, { recursive: true });
  36. if (!dtsInfo) {
  37. console.log(` ❌ ${comp}: not found in d.ts`);
  38. failed++;
  39. continue;
  40. }
  41. const classified = mod.classifyProperties(dtsInfo, null);
  42. const knowledge = mod.buildRecommendedKnowledge(comp, dtsInfo, classified);
  43. // Save knowledge file
  44. mod.saveKnowledgeFile(comp, knowledge);
  45. // Generate TS snippet
  46. const snippet = mod.generateTsSnippet(comp, knowledge);
  47. snippets.push(snippet);
  48. const propCount = Object.keys(knowledge.properties).length;
  49. console.log(` ✅ ${comp}: ${propCount} props`);
  50. saved++;
  51. }
  52. // Write combined snippets to file
  53. const outputPath = path.resolve(__dirname, '../dev-knowledge/COMPONENT_PROPERTIES_GENERATED.ts');
  54. const outputDir = path.dirname(outputPath);
  55. if (!fs.existsSync(outputDir)) fs.mkdirSync(outputDir, { recursive: true });
  56. const header = `/**
  57. * AUTO-GENERATED by batch-auto-knowledge.js
  58. * Date: ${new Date().toISOString()}
  59. * Components: ${saved}
  60. *
  61. * Paste this into COMPONENT_PROPERTIES in knowledge-handler.ts
  62. */
  63. const COMPONENT_PROPERTIES: Record<string, any> = {\n`;
  64. const footer = `\n};\n`;
  65. fs.writeFileSync(outputPath, header + snippets.join('\n') + footer, 'utf-8');
  66. console.log(`\n Saved: ${saved} | Failed: ${failed}`);
  67. console.log(` Knowledge files: dev-knowledge/components/`);
  68. console.log(` TS output: ${outputPath}`);