Skip to content

Instantly share code, notes, and snippets.

@DungGramer
Last active July 15, 2021 15:43
Show Gist options
  • Save DungGramer/7eb72add86446c9de73396521cd260bc to your computer and use it in GitHub Desktop.
Save DungGramer/7eb72add86446c9de73396521cd260bc to your computer and use it in GitHub Desktop.
title() {
echo -e "\e[1;97;44m$1\e[0m"
}
warn() {
echo -e "\e[1;97;41m$1\e[0m"
}
dashToCamelCase() {
echo "$1" | sed -r 's/(^|_|-)([a-z])/\U\2/g'
}
getPath() {
echo "$1" | sed -r 's/([^\/])*$/\ /g'
}
# Set up variables
path=$(getPath "$1")
lastFolder=${1##*\/}
fileName=$(dashToCamelCase "$lastFolder")
# Create the folder if it doesn't exist
if [ -d "src/components/$1" ]; then
warn "Directory already exists"
cd "src/components/$1"
else
cd src/components/$path
mkdir $lastFolder
cd $lastFolder
touch $fileName.jsx $fileName.module.scss
fi
# Import the component
echo "import './$fileName.module.scss';" > $fileName.jsx
echo "import PropTypes from 'prop-types';" >> $fileName.jsx
echo "" >> $fileName.jsx
echo "function $fileName(props) {" >> $fileName.jsx
echo " return <div>$fileName</div>;" >> $fileName.jsx
echo "}" >> $fileName.jsx
echo "" >> $fileName.jsx
echo "$fileName.propTypes = {};" >> $fileName.jsx
echo "" >> $fileName.jsx
echo "export default $fileName;" >> $fileName.jsx
title "Component created in src/components/$1"
Using Git Bash for run if using Windows
Ex: addComponent.sh pages/dropdown-menu
--> src/components/pages/dropdown-menu
.
|-- DropdownMenu.jsx
`-- DropdownMenu.module.scss
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment