Buckets:

download
raw
15.2 kB
"""
运行时验证脚本生成器 — 生成平台适配的验证脚本和 lint 规则。
"""
from __future__ import annotations
import os
import stat
from pathlib import Path
class ValidationScriptGenerator:
"""生成运行时验证脚本。"""
def generate(
self,
project_dir: Path,
frontend: str = "",
icon_library: str = "lucide",
) -> Path:
"""生成 scripts/validate-superdev.sh。
Returns:
Path to the generated script.
"""
scripts_dir = project_dir / "scripts"
scripts_dir.mkdir(parents=True, exist_ok=True)
script_path = scripts_dir / "validate-superdev.sh"
content = self._build_script(frontend=frontend, icon_library=icon_library)
script_path.write_text(content, encoding="utf-8")
# Make executable
try:
current = os.stat(script_path).st_mode
os.chmod(script_path, current | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH)
except OSError:
pass
return script_path
def generate_eslint_rules(
self,
project_dir: Path,
icon_library: str = "lucide",
) -> Path:
"""生成 .eslintrc.superdev.json 规则文件。
Returns:
Path to the generated eslint config.
"""
eslint_path = project_dir / ".eslintrc.superdev.json"
icon_pkg = self._icon_package(icon_library)
rules: dict = {
"rules": {
"no-restricted-syntax": [
"error",
{
"selector": "Literal[value=/[\\u2600-\\u27BF\\U0001F300-\\U0001FAFF]/]",
"message": (
f"Do not use emoji as icons. " f"Import from '{icon_pkg}' instead."
),
},
],
},
"_comment": "Auto-generated by Super Dev enforcement. Do not manually edit.",
}
import json
eslint_path.write_text(
json.dumps(rules, indent=2, ensure_ascii=False) + "\n",
encoding="utf-8",
)
return eslint_path
# ------------------------------------------------------------------
# internals
# ------------------------------------------------------------------
@staticmethod
def _icon_package(icon_library: str) -> str:
mapping = {
"lucide": "lucide-react",
"heroicons": "@heroicons/react",
"phosphor": "@phosphor-icons/react",
"tabler": "@tabler/icons-react",
"iconify": "@iconify/react",
}
return mapping.get(icon_library, "lucide-react")
def _build_script(self, *, frontend: str, icon_library: str) -> str:
icon_pkg = self._icon_package(icon_library)
# Determine source directories to scan
src_dirs = "src/ app/ components/ pages/ lib/"
# Build the shell script
lines = [
"#!/bin/bash",
"# Super Dev Runtime Validation",
"# Auto-generated by super-dev enforce install",
"# Levels: BLOCK (exit 2), WARN (reported), INFO (suggestions)",
"",
"set -euo pipefail",
"",
"BLOCKS=0",
"WARNINGS=0",
"INFOS=0",
"# Legacy alias used by existing checks",
"ERRORS=0",
"",
"# Helper: find existing source dirs",
'SRC_DIRS=""',
f"for d in {src_dirs}; do",
' [ -d "$d" ] && SRC_DIRS="$SRC_DIRS $d"',
"done",
"",
'if [ -z "$SRC_DIRS" ]; then',
' echo "No source directories found — skipping validation."',
" exit 0",
"fi",
"",
'echo "=============================="',
'echo " Super Dev Validation Report"',
'echo "=============================="',
'echo ""',
"",
"# ------------------------------------------------------------------",
"# [BLOCK] Check 1: No emoji characters used as functional icons",
"# ------------------------------------------------------------------",
'echo "[Level 1: BLOCKING checks]"',
'echo "Checking for emoji in source files..."',
(
"EMOJI_FILES=$(grep -rn "
"'[\\x{1F300}-\\x{1FAFF}\\x{2600}-\\x{27BF}]' "
"--include='*.tsx' --include='*.ts' --include='*.jsx' "
"--include='*.js' --include='*.vue' --include='*.svelte' "
"$SRC_DIRS 2>/dev/null || true)"
),
'if [ -n "$EMOJI_FILES" ]; then',
' echo "BLOCK: Emoji found in source files — use icon library instead:"',
' echo "$EMOJI_FILES" | head -10',
" BLOCKS=$((BLOCKS + 1))",
" ERRORS=$((ERRORS + 1))",
"fi",
"",
"# ------------------------------------------------------------------",
"# [BLOCK] Check 2: Icon library import consistency",
"# ------------------------------------------------------------------",
'echo "Checking icon library usage..."',
(
"WRONG_ICONS=$(grep -rn "
'"from [\'\\"]react-icons" '
"--include='*.tsx' --include='*.ts' --include='*.jsx' "
"$SRC_DIRS 2>/dev/null || true)"
),
'if [ -n "$WRONG_ICONS" ]; then',
f' echo "BLOCK: Found react-icons imports — use {icon_pkg} instead:"',
' echo "$WRONG_ICONS" | head -10',
" BLOCKS=$((BLOCKS + 1))",
" ERRORS=$((ERRORS + 1))",
"fi",
"",
"# ------------------------------------------------------------------",
"# [WARN] Check 3: No hardcoded color values outside theme/config",
"# ------------------------------------------------------------------",
'echo "Checking for hardcoded colors..."',
(
"HARDCODED_COLORS=$(grep -rn "
"'#[0-9a-fA-F]\\{3,8\\}' "
"--include='*.tsx' --include='*.jsx' --include='*.vue' "
"--include='*.svelte' "
"$SRC_DIRS 2>/dev/null "
"| grep -v 'theme\\|config\\|tailwind\\|token\\|color' "
"| grep -v '\\.test\\.' "
"| grep -v 'node_modules' || true)"
),
'if [ -n "$HARDCODED_COLORS" ]; then',
' echo "WARN: Potential hardcoded colors (review manually):"',
' echo "$HARDCODED_COLORS" | head -10',
" WARNINGS=$((WARNINGS + 1))",
"fi",
"",
]
# Check 4: Next.js specific
if frontend in ("next", "nextjs", "next.js", ""):
lines.extend(
[
"# ------------------------------------------------------------------",
"# [BLOCK] Check 4: Next.js App Router route pattern",
"# ------------------------------------------------------------------",
'if [ -d "app" ] || [ -d "src/app" ]; then',
' echo "Checking API route patterns..."',
(
" BAD_ROUTES=$(grep -rn "
"'export default function\\|module\\.exports' "
"--include='route.ts' --include='route.js' "
"$SRC_DIRS 2>/dev/null || true)"
),
' if [ -n "$BAD_ROUTES" ]; then',
' echo "BLOCK: API routes should use named exports '
'(GET, POST, etc.), not default export:"',
' echo "$BAD_ROUTES" | head -5',
" BLOCKS=$((BLOCKS + 1))",
" ERRORS=$((ERRORS + 1))",
" fi",
"fi",
"",
]
)
# Check 5: console.log / console.error in production code
lines.extend(
[
"# ------------------------------------------------------------------",
"# [BLOCK] Check 5: No console.log / console.error in production",
"# ------------------------------------------------------------------",
'echo "Checking for console.log/error statements..."',
(
"CONSOLE_STMTS=$(grep -rn "
"'console\\.\\(log\\|error\\|warn\\|debug\\)' "
"--include='*.tsx' --include='*.ts' --include='*.jsx' "
"--include='*.js' "
"$SRC_DIRS 2>/dev/null "
"| grep -v '\\.test\\.' "
"| grep -v '\\.spec\\.' "
"| grep -v 'node_modules' "
"| grep -v '__tests__' || true)"
),
'if [ -n "$CONSOLE_STMTS" ]; then',
' echo "BLOCK: console.log/error found in production code:"',
' echo "$CONSOLE_STMTS" | head -10',
" BLOCKS=$((BLOCKS + 1))",
" ERRORS=$((ERRORS + 1))",
"fi",
"",
]
)
# Check 6: Hardcoded localhost / 127.0.0.1 URLs
lines.extend(
[
"# ------------------------------------------------------------------",
"# [BLOCK] Check 6: No hardcoded localhost URLs",
"# ------------------------------------------------------------------",
'echo "Checking for hardcoded localhost URLs..."',
(
"LOCALHOST_URLS=$(grep -rn "
"'http://localhost\\|http://127\\.0\\.0\\.1\\|https://localhost' "
"--include='*.tsx' --include='*.ts' --include='*.jsx' "
"--include='*.js' --include='*.json' "
"$SRC_DIRS 2>/dev/null "
"| grep -v '\\.test\\.' "
"| grep -v '\\.spec\\.' "
"| grep -v 'node_modules' "
"| grep -v '\\.env' || true)"
),
'if [ -n "$LOCALHOST_URLS" ]; then',
' echo "BLOCK: Hardcoded localhost URLs found (use env vars instead):"',
' echo "$LOCALHOST_URLS" | head -10',
" BLOCKS=$((BLOCKS + 1))",
" ERRORS=$((ERRORS + 1))",
"fi",
"",
]
)
# Check 7: TODO/FIXME/HACK comments (warn only)
lines.extend(
[
"# ------------------------------------------------------------------",
"# [WARN] Check 7: TODO / FIXME / HACK comments",
"# ------------------------------------------------------------------",
'echo ""',
'echo "[Level 2: WARNING checks]"',
'echo "Checking for TODO/FIXME/HACK comments..."',
(
"TODO_COMMENTS=$(grep -rn "
"'TODO\\|FIXME\\|HACK\\|XXX' "
"--include='*.tsx' --include='*.ts' --include='*.jsx' "
"--include='*.js' --include='*.py' "
"$SRC_DIRS 2>/dev/null "
"| grep -v 'node_modules' || true)"
),
'if [ -n "$TODO_COMMENTS" ]; then',
' TODO_COUNT=$(echo "$TODO_COMMENTS" | wc -l | tr -d " ")',
' echo "WARN: $TODO_COUNT TODO/FIXME/HACK comment(s) found (review recommended):"',
' echo "$TODO_COMMENTS" | head -5',
" WARNINGS=$((WARNINGS + 1))",
"fi",
"",
]
)
# Check 8: Files larger than 500 lines
lines.extend(
[
"# ------------------------------------------------------------------",
"# [WARN] Check 8: Files larger than 500 lines (suggest splitting)",
"# ------------------------------------------------------------------",
'echo "Checking for oversized files..."',
(
"LARGE_FILES=$(find $SRC_DIRS -type f "
"\\( -name '*.tsx' -o -name '*.ts' -o -name '*.jsx' -o -name '*.js' \\) "
"2>/dev/null | while read f; do "
' lines=$(wc -l < "$f" 2>/dev/null || echo 0); '
' [ "$lines" -gt 500 ] && echo "$f ($lines lines)"; '
"done || true)"
),
'if [ -n "$LARGE_FILES" ]; then',
' echo "WARN: Files exceeding 500 lines (consider splitting):"',
' echo "$LARGE_FILES"',
" WARNINGS=$((WARNINGS + 1))",
"fi",
"",
]
)
# Check 9: package.json has build and lint scripts
lines.extend(
[
"# ------------------------------------------------------------------",
"# [WARN] Check 9: package.json should have build and lint scripts",
"# ------------------------------------------------------------------",
'if [ -f "package.json" ]; then',
' echo "Checking package.json scripts..."',
" if ! grep -q '\"build\"' package.json 2>/dev/null; then",
' echo "WARN: package.json missing \\"build\\" script"',
" WARNINGS=$((WARNINGS + 1))",
" fi",
" if ! grep -q '\"lint\"' package.json 2>/dev/null; then",
' echo "WARN: package.json missing \\"lint\\" script"',
" WARNINGS=$((WARNINGS + 1))",
" fi",
"fi",
"",
]
)
lines.extend(
[
"# ------------------------------------------------------------------",
"# Summary",
"# ------------------------------------------------------------------",
'echo ""',
'echo "=============================="',
'echo " Validation Summary"',
'echo "=============================="',
'echo " BLOCK: $BLOCKS"',
'echo " WARN: $WARNINGS"',
'echo ""',
"if [ $BLOCKS -eq 0 ]; then",
" if [ $WARNINGS -eq 0 ]; then",
' echo "All Super Dev validation checks passed"',
" else",
' echo "$WARNINGS warning(s) — review recommended but not blocking"',
" fi",
" exit 0",
"else",
' echo "$BLOCKS blocking issue(s) must be fixed before delivery"',
" exit 2",
"fi",
]
)
return "\n".join(lines) + "\n"

Xet Storage Details

Size:
15.2 kB
·
Xet hash:
1844b5e35f61b00c3c29957b04ece72b4bc437f62fd04f162763fec5d32c16a4

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.