dev.zsh 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #!/bin/zsh
  2. function _package_manager {
  3. # Set up completion on first call
  4. if [[ -z "$_package_manager_completion_setup" ]]; then
  5. compdef _package_manager_completion _package_manager p 2>/dev/null
  6. _package_manager_completion_setup=1
  7. fi
  8. local pkg_manager=""
  9. if [[ -f bun.lockb ]]; then
  10. pkg_manager="bun"
  11. elif [[ -f pnpm-lock.yaml ]]; then
  12. pkg_manager="pnpm"
  13. elif [[ -f yarn.lock ]]; then
  14. pkg_manager="yarn"
  15. elif [[ -f package-lock.json ]]; then
  16. pkg_manager="npm"
  17. else
  18. pkg_manager="pnpm"
  19. fi
  20. if [[ -f package.json ]] && [[ $1 != "run" ]] && [[ $1 != "install" ]] && [[ $1 != "add" ]] && [[ $1 != "remove" ]] && [[ $1 != "i" ]] && [[ $1 != "rm" ]]; then
  21. if which jq >/dev/null 2>&1 && jq -e ".scripts[\"$1\"] != null" package.json >/dev/null 2>&1; then
  22. set -- "run" "$@"
  23. fi
  24. fi
  25. if which corepack >/dev/null 2>&1; then
  26. corepack ${pkg_manager} "$@"
  27. else
  28. command ${pkg_manager} "$@"
  29. fi
  30. }
  31. function vimrc {
  32. if [[ -z "$XDG_CONFIG_HOME" ]]; then
  33. echo "Error: XDG_CONFIG_HOME not set" >&2
  34. return 1
  35. fi
  36. local original_dir=$(pwd)
  37. cd "$XDG_CONFIG_HOME/nvim" || {
  38. echo "Error: Cannot access $XDG_CONFIG_HOME/nvim" >&2
  39. return 1
  40. }
  41. nvim
  42. cd "$original_dir"
  43. }
  44. function nixrc {
  45. if [[ -z "$XDG_CONFIG_HOME" ]]; then
  46. echo "Error: XDG_CONFIG_HOME not set" >&2
  47. return 1
  48. fi
  49. local original_dir=$(pwd)
  50. cd "$XDG_CONFIG_HOME/nixos" || {
  51. echo "Error: Cannot access $XDG_CONFIG_HOME/nixos" >&2
  52. return 1
  53. }
  54. nvim
  55. cd "$original_dir"
  56. }
  57. function zshrc {
  58. if [[ -z "$XDG_CONFIG_HOME" ]]; then
  59. echo "Error: XDG_CONFIG_HOME not set" >&2
  60. return 1
  61. fi
  62. local original_dir=$(pwd)
  63. cd "$XDG_CONFIG_HOME/zsh" || {
  64. echo "Error: Cannot access $XDG_CONFIG_HOME/zsh" >&2
  65. return 1
  66. }
  67. nvim
  68. cd "$original_dir"
  69. }
  70. function sb {
  71. if [[ "$1" == "-h" || "$1" == "--help" ]]; then
  72. echo "Usage: sb"
  73. echo "Launch SilverBullet for notes and open in browser"
  74. echo "Notes directory: ~/Documents/Notes"
  75. echo "Port: 42069"
  76. return 0
  77. fi
  78. local notes_dir="$HOME/Documents/Notes"
  79. local port=42069
  80. # Check if silverbullet command exists
  81. if ! command -v silverbullet >/dev/null 2>&1; then
  82. echo "Error: silverbullet command not found" >&2
  83. echo "Please install SilverBullet first" >&2
  84. return 1
  85. fi
  86. # Check if notes directory exists
  87. if [[ ! -d "$notes_dir" ]]; then
  88. echo "Error: Notes directory not found at $notes_dir" >&2
  89. return 1
  90. fi
  91. # Check if SilverBullet is already running on port 42069
  92. if lsof -i ":$port" >/dev/null 2>&1 && pgrep -f "silverbullet.*$notes_dir" >/dev/null 2>&1; then
  93. echo "SilverBullet is already running for $notes_dir on port $port"
  94. echo "Opening browser at http://127.0.0.1:$port..."
  95. xdg-open "http://127.0.0.1:$port" 2>/dev/null || {
  96. echo "Error: Could not open browser. Please open http://127.0.0.1:$port manually" >&2
  97. return 1
  98. }
  99. return 0
  100. fi
  101. echo "Launching SilverBullet on port $port..."
  102. echo "Notes directory: $notes_dir"
  103. # Launch SilverBullet in the background
  104. silverbullet --port "$port" "$notes_dir" >/dev/null 2>&1 &
  105. local sb_pid=$!
  106. # Wait a moment for SilverBullet to start
  107. local wait_count=0
  108. local max_wait=10
  109. while [[ $wait_count -lt $max_wait ]]; do
  110. if lsof -i ":$port" >/dev/null 2>&1; then
  111. break
  112. fi
  113. sleep 0.5
  114. wait_count=$((wait_count + 1))
  115. done
  116. # Check if process is still running
  117. if ! kill -0 "$sb_pid" 2>/dev/null; then
  118. echo "Error: SilverBullet failed to start" >&2
  119. return 1
  120. fi
  121. # Open browser
  122. echo "Opening browser at http://127.0.0.1:$port..."
  123. xdg-open "http://127.0.0.1:$port" 2>/dev/null || {
  124. echo "Error: Could not open browser. Please open http://127.0.0.1:$port manually" >&2
  125. return 1
  126. }
  127. echo "SilverBullet is running (PID: $sb_pid) on port $port"
  128. }
  129. _package_manager_completion() {
  130. local -a commands
  131. commands=(
  132. 'run:Run npm script'
  133. 'install:Install dependencies'
  134. 'add:Add dependency'
  135. 'remove:Remove dependency'
  136. 'dev:Start dev server'
  137. 'build:Build project'
  138. 'test:Run tests'
  139. 'lint:Run linter'
  140. )
  141. _describe 'package manager commands' commands
  142. }