#!/bin/bash scriptfolder=$(dirname "$0") addwhat="$1" name="$2" camelcase=$(echo $name | sed -e 's/\b\(.\)/\u\1/g' -e 's/\(.*\)/\l\1/' -e 's/\s//g') actiontype=$(echo $name | sed -e 's/\(.*\)/\U\1/' -e 's/\s/-/g') help_message () { echo "possible actions:" echo "- component name" echo "- reducer name" echo "- action name-string [reducer-file] [prop1 prop2 ...]" echo "- epic name-string" } if [ -z "$name" ] then help_message exit fi case "$addwhat" in reducer) if [ -d "reducers" ] then sed "s/example/$name/g" $scriptfolder/exampleReducer.js > reducers/$name.js sed -i "s/import { combineReducers } from 'redux';/import { combineReducers } from 'redux';\nimport $name from '.\/$name.js';/g" reducers/index.js sed -i "s/combineReducers({/combineReducers({\n $name,/g" reducers/index.js else #TODO: add reducers folder and template reducers/index.js if not present (with user confirmation?) echo "need reducers folder (for now)" fi echo "added reducer $name to reducers/ and reducers/index.js" ;; action) if [ ! -e "actions.js" ] then echo "creating actions.js (don't forget to import in other files)" fi reducer="$3" props="${@:4}" actargs=$(echo $props | sed 's/\s\+/, /g') actcomments="// action.$(echo $props | sed 's/\s\+/\\n \/\/ action./g')" objprops="" if [ ! -z "$props" ] then for p in $props do objprops="$objprops $p,\n" done fi sed \ -e "s/example/$camelcase/g" \ -e "s/()/($actargs)/g" \ -e "s/^ };/$objprops };/g" \ -e "s/EXAMPLE/$actiontype/g" \ $scriptfolder/exampleAction.js >> actions.js #TODO: consider adding option to add to epic AND/OR reducer (check for ".js", check for basename, etc) (all following arguments are action parameters) if [ -e "$reducer" ] then sed -i \ -e "s/default:/case '$actiontype':\n return $camelcase(state, action);\n default:/" \ $reducer sed \ -e "s/name/$camelcase/g" \ -e "s_return_$actcomments\n return_g" \ $scriptfolder/exampleReducerAction.js \ >> $reducer fi echo "added action '$actiontype' to [actions.js] and [$reducer]" ;; epic) echo "add epic" if [ -d "epics" ] then sed -e "s/example/$camelcase/g" -e "s/PING/$actiontype/g" $scriptfolder/exampleEpic.js > epics/$camelcase.js sed -i "s/import { combineEpics } from 'redux-observable';/import { combineEpics } from 'redux-observable';\nimport $camelcase from '.\/$camelcase.js';/g" epics/index.js sed -i "s/combineEpics(/combineEpics(\n $camelcase,/g" epics/index.js else #TODO: add epics folder and template epics/index.js if not present (with user confirmation?) echo "need epics folder (for now)" fi echo "added epic $camelcase to epics/ and epics/index.js" ;; component) if [ -d "components" ] then sed "s/Example/$name/g" $scriptfolder/exampleComponent.js > components/$name.js else #TODO: add components folder if not present (with user confirmation?) echo "need components folder (for now)" fi echo "added component $name to components/" ;; *) help_message ;; esac # ADD REDUCER # 1. add reducer template file to reducers/ # 2. import reducer in reducers/index.js # 3. include name of reducer in combineReducers in reducers/index.js # ADD ACTION AND INCLUDE IN REDUCER # 1. append action creator function in actions.js # 2. insert action case statement into designated reducer # 3. append function in reducer file # ADD EPIC # 1. add epic template file to epics/ # 2. import epic in epics/index.js # 3. include name of epic in combineEpics in epics/index.js # ADD COMPONENT # 1. add component template file to components/