reduxadder.sh 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #!/bin/bash
  2. scriptfolder=$(dirname "$0")
  3. addwhat="$1"
  4. name="$2"
  5. camelcase=$(echo $name | sed -e 's/\b\(.\)/\u\1/g' -e 's/\(.*\)/\l\1/' -e 's/\s//g')
  6. actiontype=$(echo $name | sed -e 's/\(.*\)/\U\1/' -e 's/\s/-/g')
  7. help_message () {
  8. echo "possible actions:"
  9. echo "- component name"
  10. echo "- reducer name"
  11. echo "- action name-string [reducer-file] [prop1 prop2 ...]"
  12. echo "- epic name-string"
  13. }
  14. if [ -z "$name" ]
  15. then
  16. help_message
  17. exit
  18. fi
  19. case "$addwhat" in
  20. reducer)
  21. if [ -d "reducers" ]
  22. then
  23. sed "s/example/$name/g" $scriptfolder/exampleReducer.js > reducers/$name.js
  24. sed -i "s/import { combineReducers } from 'redux';/import { combineReducers } from 'redux';\nimport $name from '.\/$name.js';/g" reducers/index.js
  25. sed -i "s/combineReducers({/combineReducers({\n $name,/g" reducers/index.js
  26. else
  27. #TODO: add reducers folder and template reducers/index.js if not present (with user confirmation?)
  28. echo "need reducers folder (for now)"
  29. fi
  30. echo "added reducer $name to reducers/ and reducers/index.js"
  31. ;;
  32. action)
  33. if [ ! -e "actions.js" ]
  34. then
  35. echo "creating actions.js (don't forget to import in other files)"
  36. fi
  37. reducer="$3"
  38. props="${@:4}"
  39. actargs=$(echo $props | sed 's/\s\+/, /g')
  40. objprops=""
  41. if [ ! -z "$props" ]
  42. then
  43. for p in $props
  44. do
  45. objprops="$objprops $p,\n"
  46. done
  47. fi
  48. sed \
  49. -e "s/example/$camelcase/g" \
  50. -e "s/()/($actargs)/g" \
  51. -e "s/^ };/$objprops };/g" \
  52. -e "s/EXAMPLE/$actiontype/g" \
  53. $scriptfolder/exampleAction.js >> actions.js
  54. #TODO: consider adding option to add to epic AND/OR reducer (check for ".js", check for basename, etc) (all following arguments are action parameters)
  55. if [ -e "$reducer" ]
  56. then
  57. sed -i \
  58. -e "s/default:/case '$actiontype':\n return $camelcase(state, action);\n default:/" \
  59. $reducer
  60. sed "s/name/$camelcase/g" $scriptfolder/exampleReducerAction.js >> $reducer
  61. fi
  62. echo "added action '$actiontype' to [actions.js] and [$reducer]"
  63. ;;
  64. epic)
  65. echo "add epic"
  66. if [ -d "epics" ]
  67. then
  68. sed -e "s/example/$camelcase/g" -e "s/PING/$actiontype/g" $scriptfolder/exampleEpic.js > epics/$camelcase.js
  69. sed -i "s/import { combineEpics } from 'redux-observable';/import { combineEpics } from 'redux-observable';\nimport $camelcase from '.\/$camelcase.js';/g" epics/index.js
  70. sed -i "s/combineEpics(/combineEpics(\n $camelcase,/g" epics/index.js
  71. else
  72. #TODO: add epics folder and template epics/index.js if not present (with user confirmation?)
  73. echo "need epics folder (for now)"
  74. fi
  75. echo "added epic $camelcase to epics/ and epics/index.js"
  76. ;;
  77. component)
  78. if [ -d "components" ]
  79. then
  80. sed "s/Example/$name/g" $scriptfolder/exampleComponent.js > components/$name.js
  81. else
  82. #TODO: add components folder if not present (with user confirmation?)
  83. echo "need components folder (for now)"
  84. fi
  85. echo "added component $name to components/ and components/index.js"
  86. ;;
  87. *)
  88. help_message
  89. ;;
  90. esac
  91. # ADD REDUCER
  92. # 1. add reducer template file to reducers/
  93. # 2. import reducer in reducers/index.js
  94. # 3. include name of reducer in combineReducers in reducers/index.js
  95. # ADD ACTION AND INCLUDE IN REDUCER
  96. # 1. append action creator function in actions.js
  97. # 2. insert action case statement into designated reducer
  98. # 3. append function in reducer file
  99. # ADD EPIC
  100. # 1. add epic template file to epics/
  101. # 2. import epic in epics/index.js
  102. # 3. include name of epic in combineEpics in epics/index.js
  103. # ADD COMPONENT
  104. # 1. add component template file to components/