Skip to content

Instantly share code, notes, and snippets.

@NOMADE55
Last active July 21, 2020 19:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save NOMADE55/116bcc13e3da83ff5e9fdcd8c2449f15 to your computer and use it in GitHub Desktop.
Save NOMADE55/116bcc13e3da83ff5e9fdcd8c2449f15 to your computer and use it in GitHub Desktop.
Adds a React Function Component in the supplied directory.
#!/bin/zsh
#
# React Function Component Maker made with ❤ by Lucas G. Terracino
#
# Help
show_help() {
# Display help
echo "Usage: pls_react_function_component [OPTION]... COMPONENT..."
echo "Adds a React Function Component in the supplied directory."
echo ""
echo "If subfolder doesn't exist it won't be created unless -p is provided."
echo "Optional parameters"
echo " -e Changes the extension of the created files to the one provided."
echo " In case of choosing ts, semicolons are removed from files."
echo " Default value is js."
echo " -p Creates parent folders if they don't exist."
echo " -h Shows this message"
echo ""
}
# Main Script
pls_react_function_component() {
extension="js"
semicolon=";"
create_parents="false"
OPTIND=1 # Reset in case getopts has been used previously in the shell.
# Parsing options
while getopts "h?p?e:" OPTION; do
case "${OPTION}" in
h|\?)
show_help
return
;;
p)
create_parents="true"
;;
e)
extension=${OPTARG}
if [ $extension = "ts" ]; then
semicolon=""
fi
;;
: )
echo "Invalid option: $OPTARG requires an argument" 1>&2
;;
esac
done
# Shifting variable $1 from options to params
shift $((OPTIND-1))
[ "${1:-}" = "--" ] && shift
# Exit if no arguments!
if [ $# -eq 0 ]; then
echo "👎 No arguments supplied"
return 0
fi
# If the final directory doesn't exist function continues
if [ -d $1 ]; then
echo "👎 Directory already exists, I won't overwrite it"
return 0
fi
# Getting the path and name
component_path="$@"
component_name="`basename $component_path`"
# Preparing the data for the files
index_data="export { default } from \"./$component_name\"$semicolon"
component_data="import React from 'react'$semicolon
function $component_name() {
return (
<React.Fragment></React.Fragment>
)$semicolon
}
export default $component_name$semicolon" # End of component_data
# Checking if directory exists
if [ ! -d $component_path ]; then
# Creating the Directory
if [ "$create_parents" = "true" ]; then
mkdir --parents "${component_path}"
else
mkdir "${component_path}"
fi
# Creating files with the data arranged previously
if [ -d $component_path ]; then
[ ! -f /etc/resolv.conf ] && echo $component_data > "$component_path/$component_name.$extension"
[ ! -f /etc/resolv.conf ] && echo $index_data > "$component_path/index.$extension"
echo "⚛️ Function component $component_name created!"
else
# Errors creating the folders
echo " └ cannot create the folder."
if [ "$create_parents" = "false" ]; then
echo " Use -p To also create parents folders."
fi
return 0
fi
return 0
fi
}
pls_react_function_component $*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment