validate-component.sh 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #!/usr/bin/env bash
  2. #############################################################################
  3. # Component Validation Script
  4. # Validates component structure and metadata for PRs
  5. #############################################################################
  6. set -e
  7. # Colors
  8. RED='\033[0;31m'
  9. GREEN='\033[0;32m'
  10. YELLOW='\033[1;33m'
  11. BLUE='\033[0;34m'
  12. NC='\033[0m'
  13. ERRORS=0
  14. WARNINGS=0
  15. print_success() { echo -e "${GREEN}✓${NC} $1"; }
  16. print_error() { echo -e "${RED}✗${NC} $1"; ((ERRORS++)); }
  17. print_warning() { echo -e "${YELLOW}⚠${NC} $1"; ((WARNINGS++)); }
  18. print_info() { echo -e "${BLUE}ℹ${NC} $1"; }
  19. validate_markdown_frontmatter() {
  20. local file=$1
  21. print_info "Validating $file"
  22. # Check if file has frontmatter
  23. if ! head -n 1 "$file" | grep -q "^---$"; then
  24. print_warning "Missing frontmatter in $file"
  25. return
  26. fi
  27. # Extract frontmatter
  28. local frontmatter=$(awk '/^---$/{if(++n==2)exit;next}n==1' "$file")
  29. # Check for description
  30. if ! echo "$frontmatter" | grep -q "^description:"; then
  31. print_warning "Missing 'description' in frontmatter of $file"
  32. else
  33. print_success "Has description"
  34. fi
  35. # For agents, check for mode
  36. if [[ "$file" == *"/agent/"* ]] && [[ "$file" != *"/subagents/"* ]]; then
  37. if ! echo "$frontmatter" | grep -q "^mode:"; then
  38. print_warning "Missing 'mode' in agent frontmatter of $file"
  39. fi
  40. fi
  41. }
  42. validate_typescript_file() {
  43. local file=$1
  44. print_info "Validating $file"
  45. # Check for basic TypeScript syntax (very basic check)
  46. if ! grep -q "export" "$file"; then
  47. print_warning "No exports found in $file"
  48. else
  49. print_success "Has exports"
  50. fi
  51. # Check for comments/documentation
  52. if ! grep -q "^\s*\*" "$file"; then
  53. print_warning "No JSDoc comments found in $file"
  54. else
  55. print_success "Has documentation"
  56. fi
  57. }
  58. validate_directory_structure() {
  59. print_info "Validating directory structure"
  60. local required_dirs=(
  61. ".opencode"
  62. ".opencode/agent"
  63. ".opencode/command"
  64. ".opencode/tool"
  65. )
  66. for dir in "${required_dirs[@]}"; do
  67. if [ ! -d "$dir" ]; then
  68. print_error "Missing required directory: $dir"
  69. else
  70. print_success "Directory exists: $dir"
  71. fi
  72. done
  73. }
  74. validate_registry() {
  75. print_info "Validating registry.json"
  76. if [ ! -f "registry.json" ]; then
  77. print_error "registry.json not found"
  78. return
  79. fi
  80. # Check if valid JSON
  81. if ! jq empty registry.json 2>/dev/null; then
  82. print_error "registry.json is not valid JSON"
  83. return
  84. fi
  85. print_success "registry.json is valid JSON"
  86. # Check required fields
  87. local required_fields=("version" "repository" "components" "profiles" "metadata")
  88. for field in "${required_fields[@]}"; do
  89. if ! jq -e ".$field" registry.json > /dev/null 2>&1; then
  90. print_error "Missing required field in registry.json: $field"
  91. else
  92. print_success "Has field: $field"
  93. fi
  94. done
  95. }
  96. main() {
  97. echo "╔════════════════════════════════════════════════════════════════╗"
  98. echo "║ Component Validation ║"
  99. echo "╚════════════════════════════════════════════════════════════════╝"
  100. echo ""
  101. # Validate directory structure
  102. validate_directory_structure
  103. echo ""
  104. # Validate registry
  105. validate_registry
  106. echo ""
  107. # Validate all markdown files
  108. echo "Validating markdown files..."
  109. while IFS= read -r -d '' file; do
  110. validate_markdown_frontmatter "$file"
  111. done < <(find .opencode -name "*.md" -type f -print0 2>/dev/null)
  112. echo ""
  113. # Validate TypeScript files
  114. echo "Validating TypeScript files..."
  115. while IFS= read -r -d '' file; do
  116. validate_typescript_file "$file"
  117. done < <(find .opencode -name "*.ts" -type f -not -path "*/node_modules/*" -print0 2>/dev/null)
  118. echo ""
  119. # Summary
  120. echo "════════════════════════════════════════════════════════════════"
  121. echo "Validation Summary:"
  122. echo " Errors: $ERRORS"
  123. echo " Warnings: $WARNINGS"
  124. echo "════════════════════════════════════════════════════════════════"
  125. if [ $ERRORS -gt 0 ]; then
  126. echo ""
  127. print_error "Validation failed with $ERRORS error(s)"
  128. exit 1
  129. elif [ $WARNINGS -gt 0 ]; then
  130. echo ""
  131. print_warning "Validation passed with $WARNINGS warning(s)"
  132. exit 0
  133. else
  134. echo ""
  135. print_success "All validations passed!"
  136. exit 0
  137. fi
  138. }
  139. main "$@"