validate-component.sh 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. # Check for category subdirectories (optional but recommended)
  74. local category_dirs=(
  75. ".opencode/agent/core"
  76. ".opencode/agent/development"
  77. ".opencode/agent/content"
  78. ".opencode/agent/data"
  79. ".opencode/agent/learning"
  80. ".opencode/agent/product"
  81. )
  82. local found_categories=0
  83. for dir in "${category_dirs[@]}"; do
  84. if [ -d "$dir" ]; then
  85. ((found_categories++))
  86. fi
  87. done
  88. if [ $found_categories -gt 0 ]; then
  89. print_success "Found $found_categories category subdirectories"
  90. else
  91. print_warning "No category subdirectories found (optional)"
  92. fi
  93. }
  94. validate_registry() {
  95. print_info "Validating registry.json"
  96. if [ ! -f "registry.json" ]; then
  97. print_error "registry.json not found"
  98. return
  99. fi
  100. # Check if valid JSON
  101. if ! jq empty registry.json 2>/dev/null; then
  102. print_error "registry.json is not valid JSON"
  103. return
  104. fi
  105. print_success "registry.json is valid JSON"
  106. # Check required fields
  107. local required_fields=("version" "repository" "components" "profiles" "metadata")
  108. for field in "${required_fields[@]}"; do
  109. if ! jq -e ".$field" registry.json > /dev/null 2>&1; then
  110. print_error "Missing required field in registry.json: $field"
  111. else
  112. print_success "Has field: $field"
  113. fi
  114. done
  115. }
  116. main() {
  117. echo "╔════════════════════════════════════════════════════════════════╗"
  118. echo "║ Component Validation ║"
  119. echo "╚════════════════════════════════════════════════════════════════╝"
  120. echo ""
  121. # Validate directory structure
  122. validate_directory_structure
  123. echo ""
  124. # Validate registry
  125. validate_registry
  126. echo ""
  127. # Validate all markdown files (excluding symlinks)
  128. echo "Validating markdown files..."
  129. while IFS= read -r -d '' file; do
  130. # Skip symlinks
  131. if [ -L "$file" ]; then
  132. continue
  133. fi
  134. validate_markdown_frontmatter "$file"
  135. done < <(find .opencode -name "*.md" -type f -print0 2>/dev/null)
  136. echo ""
  137. # Validate TypeScript files
  138. echo "Validating TypeScript files..."
  139. while IFS= read -r -d '' file; do
  140. validate_typescript_file "$file"
  141. done < <(find .opencode -name "*.ts" -type f -not -path "*/node_modules/*" -print0 2>/dev/null)
  142. echo ""
  143. # Summary
  144. echo "════════════════════════════════════════════════════════════════"
  145. echo "Validation Summary:"
  146. echo " Errors: $ERRORS"
  147. echo " Warnings: $WARNINGS"
  148. echo "════════════════════════════════════════════════════════════════"
  149. if [ $ERRORS -gt 0 ]; then
  150. echo ""
  151. print_error "Validation failed with $ERRORS error(s)"
  152. exit 1
  153. elif [ $WARNINGS -gt 0 ]; then
  154. echo ""
  155. print_warning "Validation passed with $WARNINGS warning(s)"
  156. exit 0
  157. else
  158. echo ""
  159. print_success "All validations passed!"
  160. exit 0
  161. fi
  162. }
  163. main "$@"