validate-component.sh 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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
  29. frontmatter=$(awk '/^---$/{if(++n==2)exit;next}n==1' "$file")
  30. # Check for description
  31. if ! echo "$frontmatter" | grep -q "^description:"; then
  32. print_warning "Missing 'description' in frontmatter of $file"
  33. else
  34. print_success "Has description"
  35. fi
  36. # For agents, check for mode
  37. if [[ "$file" == *"/agent/"* ]] && [[ "$file" != *"/subagents/"* ]]; then
  38. if ! echo "$frontmatter" | grep -q "^mode:"; then
  39. print_warning "Missing 'mode' in agent frontmatter of $file"
  40. fi
  41. fi
  42. }
  43. validate_typescript_file() {
  44. local file=$1
  45. print_info "Validating $file"
  46. # Check for basic TypeScript syntax (very basic check)
  47. if ! grep -q "export" "$file"; then
  48. print_warning "No exports found in $file"
  49. else
  50. print_success "Has exports"
  51. fi
  52. # Check for comments/documentation
  53. if ! grep -q "^\s*\*" "$file"; then
  54. print_warning "No JSDoc comments found in $file"
  55. else
  56. print_success "Has documentation"
  57. fi
  58. }
  59. validate_directory_structure() {
  60. print_info "Validating directory structure"
  61. local required_dirs=(
  62. ".opencode"
  63. ".opencode/agent"
  64. ".opencode/command"
  65. ".opencode/tool"
  66. )
  67. for dir in "${required_dirs[@]}"; do
  68. if [ ! -d "$dir" ]; then
  69. print_error "Missing required directory: $dir"
  70. else
  71. print_success "Directory exists: $dir"
  72. fi
  73. done
  74. # Check for category subdirectories (optional but recommended)
  75. local category_dirs=(
  76. ".opencode/agent/core"
  77. ".opencode/agent/development"
  78. ".opencode/agent/content"
  79. ".opencode/agent/data"
  80. ".opencode/agent/learning"
  81. ".opencode/agent/product"
  82. )
  83. local found_categories=0
  84. for dir in "${category_dirs[@]}"; do
  85. if [ -d "$dir" ]; then
  86. ((found_categories++))
  87. fi
  88. done
  89. if [ $found_categories -gt 0 ]; then
  90. print_success "Found $found_categories category subdirectories"
  91. else
  92. print_warning "No category subdirectories found (optional)"
  93. fi
  94. }
  95. validate_registry() {
  96. print_info "Validating registry.json"
  97. if [ ! -f "registry.json" ]; then
  98. print_error "registry.json not found"
  99. return
  100. fi
  101. # Check if valid JSON
  102. if ! jq empty registry.json 2>/dev/null; then
  103. print_error "registry.json is not valid JSON"
  104. return
  105. fi
  106. print_success "registry.json is valid JSON"
  107. # Check required fields
  108. local required_fields=("version" "repository" "components" "profiles" "metadata")
  109. for field in "${required_fields[@]}"; do
  110. if ! jq -e ".$field" registry.json > /dev/null 2>&1; then
  111. print_error "Missing required field in registry.json: $field"
  112. else
  113. print_success "Has field: $field"
  114. fi
  115. done
  116. }
  117. main() {
  118. echo "╔════════════════════════════════════════════════════════════════╗"
  119. echo "║ Component Validation ║"
  120. echo "╚════════════════════════════════════════════════════════════════╝"
  121. echo ""
  122. # Validate directory structure
  123. validate_directory_structure
  124. echo ""
  125. # Validate registry
  126. validate_registry
  127. echo ""
  128. # Validate all markdown files (excluding symlinks)
  129. echo "Validating markdown files..."
  130. while IFS= read -r -d '' file; do
  131. # Skip symlinks
  132. if [ -L "$file" ]; then
  133. continue
  134. fi
  135. validate_markdown_frontmatter "$file"
  136. done < <(find .opencode -name "*.md" -type f -print0 2>/dev/null)
  137. echo ""
  138. # Validate TypeScript files
  139. echo "Validating TypeScript files..."
  140. while IFS= read -r -d '' file; do
  141. validate_typescript_file "$file"
  142. done < <(find .opencode -name "*.ts" -type f -not -path "*/node_modules/*" -print0 2>/dev/null)
  143. echo ""
  144. # Summary
  145. echo "════════════════════════════════════════════════════════════════"
  146. echo "Validation Summary:"
  147. echo " Errors: $ERRORS"
  148. echo " Warnings: $WARNINGS"
  149. echo "════════════════════════════════════════════════════════════════"
  150. if [ $ERRORS -gt 0 ]; then
  151. echo ""
  152. print_error "Validation failed with $ERRORS error(s)"
  153. exit 1
  154. elif [ $WARNINGS -gt 0 ]; then
  155. echo ""
  156. print_warning "Validation passed with $WARNINGS warning(s)"
  157. exit 0
  158. else
  159. echo ""
  160. print_success "All validations passed!"
  161. exit 0
  162. fi
  163. }
  164. main "$@"