Last active
September 6, 2021 13:44
-
-
Save criography/d2b4926f22f829888299 to your computer and use it in GitHub Desktop.
tiny shell script for creating new project/client folder structure
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/sh | |
| # Help: | |
| # ------ | |
| # nu help | |
| ################################## HELPERS ################################## | |
| # _mkdir | |
| # --------------------------------------------- | |
| # check if project or client already exists | |
| # before setting it up | |
| # | |
| # @param {string} dir_name Name of the project/client directory. | |
| # If it's a project, the date is optional | |
| # @param {string} dir_type Type of dir structure : [client/project]. | |
| # Defaults to project | |
| # --------------------------------------------- | |
| function _mkdir { | |
| if [ -d "$1" ]; then | |
| TYPE="project" | |
| if [ "$2" = "client" ]; then | |
| TYPE="client" | |
| fi | |
| echo "This $TYPE already exists! You n00b..." | |
| exit | |
| else | |
| mkdir -p "${1}" && cd "${1}" | |
| fi | |
| } | |
| # createProject | |
| # --------------------------------------------- | |
| # generates new project folder structure | |
| # | |
| # @param {string} dir_name Name of the project directory. | |
| # If it doesn't start with a YYYY.MM.DD, | |
| # today's date will be used. | |
| # --------------------------------------------- | |
| function createProject { | |
| # check if similar project exists | |
| SIMILAR_PROJECT=$(ls -d */ | grep -E "${1}/$" ) | |
| if [ "$SIMILAR_PROJECT" ] ; then | |
| echo "Project of same name, but a different date already exists:" | |
| echo "$SIMILAR_PROJECT" | |
| read -p "Do you want to proceed [y/n]? " PROCEED | |
| if [ $PROCEED = 'n' ]; then | |
| exit | |
| fi | |
| fi | |
| # Check if given project name starts with date, | |
| # if so use it instead today's date | |
| # (more compact regex seems to be failing it gitbash) | |
| PREFIX=$(date +"%Y.%m.%d")" - " | |
| if echo "$1" | grep -E "^[0-9][0-9][0-9][0-9].[0-9][0-9].[0-9][0-9]" > /dev/null; then | |
| PREFIX="" | |
| fi | |
| # all OK, generate folders | |
| _mkdir "${PREFIX}${1}" project | |
| mkdir -p "_Assets/"{Branding/{Fonts,Logos},Photography,Freelancers} | |
| mkdir -p Admin/{Account,Legal,Specs,Finance} | |
| mkdir -p Design/{Components,Flats,Links} | |
| mkdir -p {Content,Work} | |
| } | |
| function printHelp { | |
| cat << EOM | |
| # Usage: | |
| # ====== | |
| # | |
| # To create new client | |
| # -------------------- | |
| # nu client "client name" | |
| # nu c "client name" | |
| # | |
| # | |
| # To create project with today's date: | |
| # ------------------------------------ | |
| # nu project "project name" | |
| # nu p "project name" | |
| # | |
| # | |
| # To create project with custom date: | |
| # ------------------------------------ | |
| # nu project "YYYY.MM.DD - project name" | |
| # nu p "YYYY.MM.DD - project name" | |
| # | |
| # | |
| # You can combine both attributes | |
| # -------------------------- | |
| # nu c "client name" p "project name" | |
| # | |
| # | |
| # To show this help: | |
| # ------------------ | |
| # nu help | |
| # nu -h | |
| EOM | |
| } | |
| ############################### ENDOF: HELPERS ############################### | |
| ############################# PROCESS ARGUMENTS ############################## | |
| while [[ $# > 0 ]]; do | |
| key="$1" | |
| case $key in | |
| client|c) | |
| CLIENT_NAME="$2" | |
| shift | |
| ;; | |
| project|p) | |
| PROJECT_NAME="$2" | |
| shift | |
| ;; | |
| help|-h|--help) | |
| printHelp | |
| ;; | |
| *) | |
| printHelp | |
| ;; | |
| esac | |
| shift # past argument or value | |
| done | |
| ########################## ENDOF: PROCESS ARGUMENTS ########################## | |
| ############################### GENERATE DIRS ################################ | |
| # Generate Client Dirs | |
| if [ ${#CLIENT_NAME} -gt 2 ] ; then | |
| _mkdir "${CLIENT_NAME}" client | |
| mkdir -p "_Assets Master/"{Branding/{Fonts,Logos},Photography,Freelancers} | |
| mkdir -p Admin/{Account,Legal,Specs,Finance,Deployment} | |
| mkdir -p Projects/{Archive,Emails} | |
| # optionally create new project dir | |
| if [ "${#PROJECT_NAME}" -gt 2 ] ; then | |
| cd "Projects" | |
| createProject "${PROJECT_NAME}" | |
| fi | |
| fi | |
| # Generate Project Dirs | |
| if [ ${#CLIENT_NAME} = 0 ] && [ ${#PROJECT_NAME} -gt 2 ]; then | |
| createProject "${PROJECT_NAME}" | |
| fi | |
| ############################ ENDOF: GENERATE DIRS ############################# |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Installation
curl -L https://gist.githubusercontent.com/criography/d2b4926f22f829888299/raw/bd790a02b276f35dc8934193291cf4cc2d3753bf/nu.sh -o /usr/local/bin/nu && sudo chmod +x /usr/local/bin/nuPlease note
If you're getting
Warning: Failed to create the file /usr/local/bin/nu: No such file or directoryerror, it means that your/usr/local/bindirectory doesn't exist. You can either create it by installing latest node or similar package, or by hand, by running:Usage
First
cdto wherever you want to create new client or project and:To create new client
To create project with today's date
To create project with custom date
You can combine both arguments
To show this help
nu help nu -hExpected Dir Structure