Skip to content

Instantly share code, notes, and snippets.

@mulholio mulholio/new-proj.sh Secret
Created Feb 28, 2019

Embed
What would you like to do?
Creates a new folder with Git and JavaScript optionally setup.
#!/bin/bash
# new-proj
# while number of args > 0
while [[ $# -gt 0 ]]
do
case $1 in
-j | --javascript )
# set the $JS variable to true if the -j or --javascript option is passed
JS=true
shift # change the position of the arguments by 1
;;
-g | --git )
# set the $GIT variable to true if the -g or --git option is passed
GIT=true
shift # change the position of the arguments by 1
;;
*)
# set any other argument as the directory name
DIRNAME=$1
shift # change the position of the arguments by 1
esac; # end switch statement
done
# exit if directory name isn't set
if [ -z ${DIRNAME+x} ]; then
echo "ERROR: Could not find a directory name"
exit 1
fi
setup-git() {
echo "Setting up Git"
if [[ $1 = true ]]; then
# add JS relevant lines to .gitignore
echo "node_modules" >> $DIRNAME/.gitignore
echo "dist" >> $DIRNAME/.gitignore
fi
git init --quiet $DIRNAME
}
setup-js() {
echo "Setting up JavaScript"
# create index.js
touch "$DIRNAME"/index.js
# change working directory to our passed in directoyr name
cd $DIRNAME
# init package.json and yarn, auto-answer yes to all Qs
yarn init --silent --yes
# jump back to level above
cd ..
}
# make a directory for our new project
mkdir $DIRNAME
# make some files
touch "$DIRNAME"/index.html
touch "$DIRNAME"/style.css
if [ "$JS" = true ]; then
setup-js
fi
if [ "$GIT" = true ]; then
setup-git "$JS"
fi
echo "Done!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.