reduxadder.sh 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. actcomments="// action.$(echo $props | sed 's/\s\+/\\n \/\/ action./g')"
  41. objprops=""
  42. if [ ! -z "$props" ]
  43. then
  44. for p in $props
  45. do
  46. objprops="$objprops $p,\n"
  47. done
  48. fi
  49. sed \
  50. -e "s/example/$camelcase/g" \
  51. -e "s/()/($actargs)/g" \
  52. -e "s/^ };/$objprops };/g" \
  53. -e "s/EXAMPLE/$actiontype/g" \
  54. $scriptfolder/exampleAction.js >> actions.js
  55. #TODO: consider adding option to add to epic AND/OR reducer (check for ".js", check for basename, etc) (all following arguments are action parameters)
  56. if [ -e "$reducer" ]
  57. then
  58. sed -i \
  59. -e "s/default:/case '$actiontype':\n return $camelcase(state, action);\n default:/" \
  60. $reducer
  61. sed \
  62. -e "s/name/$camelcase/g" \
  63. -e "s_return_$actcomments\n return_g" \
  64. $scriptfolder/exampleReducerAction.js \
  65. >> $reducer
  66. fi
  67. echo "added action '$actiontype' to [actions.js] and [$reducer]"
  68. ;;
  69. epic)
  70. echo "add epic"
  71. if [ -d "epics" ]
  72. then
  73. sed -e "s/example/$camelcase/g" -e "s/PING/$actiontype/g" $scriptfolder/exampleEpic.js > epics/$camelcase.js
  74. sed -i "s/import { combineEpics } from 'redux-observable';/import { combineEpics } from 'redux-observable';\nimport $camelcase from '.\/$camelcase.js';/g" epics/index.js
  75. sed -i "s/combineEpics(/combineEpics(\n $camelcase,/g" epics/index.js
  76. else
  77. #TODO: add epics folder and template epics/index.js if not present (with user confirmation?)
  78. echo "need epics folder (for now)"
  79. fi
  80. echo "added epic $camelcase to epics/ and epics/index.js"
  81. ;;
  82. component)
  83. if [ -d "components" ]
  84. then
  85. sed "s/Example/$name/g" $scriptfolder/exampleComponent.js > components/$name.js
  86. else
  87. #TODO: add components folder if not present (with user confirmation?)
  88. echo "need components folder (for now)"
  89. fi
  90. echo "added component $name to components/"
  91. ;;
  92. *)
  93. help_message
  94. ;;
  95. esac
  96. # ADD REDUCER
  97. # 1. add reducer template file to reducers/
  98. # 2. import reducer in reducers/index.js
  99. # 3. include name of reducer in combineReducers in reducers/index.js
  100. # ADD ACTION AND INCLUDE IN REDUCER
  101. # 1. append action creator function in actions.js
  102. # 2. insert action case statement into designated reducer
  103. # 3. append function in reducer file
  104. # ADD EPIC
  105. # 1. add epic template file to epics/
  106. # 2. import epic in epics/index.js
  107. # 3. include name of epic in combineEpics in epics/index.js
  108. # ADD COMPONENT
  109. # 1. add component template file to components/