_ng 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #compdef ng
  2. setopt localoptions extendedglob
  3. if (( CURRENT == 2 )); then
  4. local -a cmds alias
  5. # Sample output (ng help):
  6. # Available Commands:
  7. # add Adds support for an external library to your project.
  8. for line in ${(@f)"$(ng help 2>/dev/null | sed -n '/^ /p')"}; do
  9. if [[ "$line" =~ '^ ([^ ]+) \(([^)]+)\) (.*)$' ]]; then
  10. alias=(${match[1]} ${(s:, :)match[2]})
  11. cmds+=(${^alias}:"${match[3]//:/}")
  12. elif [[ "$line" =~ '^ ([^ ]+) (.*)$' ]]; then
  13. cmds+=("${match[1]}:${match[2]//:/}")
  14. fi
  15. done
  16. _describe commands cmds && return 0
  17. elif (( CURRENT == 3 )); then
  18. local -a flags args
  19. local section description
  20. # Sample output (ng build --help):
  21. # --configuration (-c)
  22. # One or more named builder configurations as a comma-separated list as specified in the "configurations" section of angular.json.
  23. # The builder uses the named configurations to run the given target.
  24. # For more information, see https://angular.io/guide/workspace-config#alternate-build-configurations.
  25. # Prefix --flags with literal \0, and split on that to get each flag section
  26. for section in ${(s:\0:)"$(ng ${words[2]} --help 2>/dev/null | sed -e '1,/^options/ d;s/^ --/\\0--/')"}; do
  27. # Split by newline and discard extra description lines (lines > 2)
  28. for args description in ${${(@f)section}[1,2]}; do
  29. args=(${(s: :)${${args%% #}## #}//[(),]/})
  30. description=${${description%% #}## #}
  31. flags+=(${^args}":$description")
  32. done
  33. done
  34. _describe flags flags
  35. case "$words[2]" in
  36. b|build|l|lint|t|test)
  37. # Sample output (ng config projects):
  38. # {
  39. # "angular-project-1": {
  40. # ...
  41. # },
  42. # "angular-project-2": {
  43. # ...
  44. # }
  45. # }
  46. # In absence of a proper JSON parser, just grab the lines with
  47. # a 2-space indentation and only the stuff inside quotes
  48. local -a projects
  49. projects=(${(@f)"$(ng config projects 2>/dev/null | sed -n 's/^ "\([^"]\+\)".*$/\1/p')"})
  50. _describe projects projects
  51. ;;
  52. esac
  53. fi