persist-skill.sh 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #!/usr/bin/env bash
  2. # Persists the canonical Spin skill bundle (SKILL.md + scripts/ + references/)
  3. # from cloudflare/skills to the user's repo so the agent can re-load it on
  4. # follow-up tasks without re-pasting the bootstrap prompt.
  5. #
  6. # Args:
  7. # --path <path> SKILL.md destination, e.g. .claude/skills/turnstile-spin/SKILL.md.
  8. # The bundle is extracted into the parent directory of <path>,
  9. # so scripts land at e.g. .claude/skills/turnstile-spin/scripts/.
  10. #
  11. # Outputs JSON. Exit 0 if the bundle was written, 1 on failure.
  12. # ok: {"status":"ok","path":"<path>","bundle_root":"<dir>","scripts":[<list>]}
  13. # fail: {"status":"error","reason":"<reason>"}
  14. set -uo pipefail
  15. PATH_ARG=""
  16. while [[ $# -gt 0 ]]; do
  17. case $1 in
  18. --path) PATH_ARG="$2"; shift 2 ;;
  19. *) echo "persist-skill: unknown arg $1" >&2; exit 2 ;;
  20. esac
  21. done
  22. : "${PATH_ARG:?--path required}"
  23. TARGET_DIR=$(dirname "$PATH_ARG")
  24. mkdir -p "$TARGET_DIR"
  25. # Install the canonical bundle from cloudflare/skills via degit. This writes
  26. # SKILL.md, scripts/, references/, templates/, tests/ into $TARGET_DIR.
  27. if ! npx --yes degit cloudflare/skills/skills/turnstile-spin "$TARGET_DIR" >/dev/null 2>&1; then
  28. echo "persist-skill: degit failed; cannot fetch cloudflare/skills/skills/turnstile-spin." >&2
  29. echo "persist-skill: ensure your network can reach github.com and try again, or install manually." >&2
  30. echo "{\"status\":\"error\",\"reason\":\"degit_failed\"}"
  31. exit 1
  32. fi
  33. if [ ! -f "$TARGET_DIR/SKILL.md" ]; then
  34. echo "persist-skill: bundle extracted but SKILL.md is missing at $TARGET_DIR/SKILL.md." >&2
  35. echo "{\"status\":\"error\",\"reason\":\"skill_missing\"}"
  36. exit 1
  37. fi
  38. # Make scripts executable so the agent can invoke them directly.
  39. if [ -d "$TARGET_DIR/scripts" ]; then
  40. chmod +x "$TARGET_DIR/scripts"/*.sh 2>/dev/null || true
  41. fi
  42. scripts_list=$(ls "$TARGET_DIR/scripts" 2>/dev/null | sed 's/.*/"&"/' | paste -sd, -)
  43. echo "persist-skill: wrote bundle to $TARGET_DIR" >&2
  44. echo "{\"status\":\"ok\",\"path\":\"$PATH_ARG\",\"bundle_root\":\"$TARGET_DIR\",\"scripts\":[$scripts_list]}"
  45. exit 0