From fde7fa424b1a7b80064e74bfc6923dae0db5b914 Mon Sep 17 00:00:00 2001 From: zongor Date: Sun, 4 Aug 2024 14:38:48 -0400 Subject: [PATCH] Add class related scripts --- bin/sh/class_scripts.sh | 60 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 bin/sh/class_scripts.sh diff --git a/bin/sh/class_scripts.sh b/bin/sh/class_scripts.sh new file mode 100644 index 0000000..892cae1 --- /dev/null +++ b/bin/sh/class_scripts.sh @@ -0,0 +1,60 @@ +function generate_env() { + DB_NAME=${1} + ENV_PATH=${2-$HOME/.sql.env} + + cp $ENV_PATH ./.env + echo "DB_NAME='$DB_NAME'" >> ./.env +} + +function install_nodemon() { + FILE=${1-package.json} + + if grep -q nodemon "$FILE"; then + echo 'nodemon exists;' + else + bun i -D nodemon + sed -i '/\"scripts\":\ {/a\ \ \ \ \"watch\":\"nodemon server.js\",' $FILE + fi +} + +function swap_localhost_home() { + grep -r --exclude-dir=node_modules -l "localhost". | xargs awk -i inplace '{gsub("localhost", "127.0.0.1")} 1' +} + +function setup() { + ENV_PATH=${1-$HOME/.sql.env} + SCHEMA_PATH=${2-db/schema.sql} + DB_NAME=$(grep -oP '(?<=CREATE DATABASE )(.*)(?=;)' $SCHEMA_PATH) + + generate_env $DB_NAME + + bun install + install_nodemon + + PGPASSWORD=$(grep -oP '(?<=DB_PASSWORD=)(.*)' $ENV_PATH) psql -U postgres -a -f $SCHEMA_PATH + + if [[ "$OSTYPE" == "darwin"* ]]; then + swap_localhost_home + elif [[ "$OSTYPE" == "linux-gnu"* ]]; then + swap_localhost_home + elif [[ "$OSTYPE" == "freebsd"* ]]; then + swap_localhost_home + else + echo "no need to localhost, not unix" + fi + + if [ -f "seeds/index.js" ]; then + node seeds/index.js + elif [ -f "seeds/seed.js" ]; then + node seeds/seed.js + else + echo "no seed file" + fi +} + +alias gfc="~/git-file-cleanup.sh" +alias dircp="cp -r" +alias clean_node_modules="find . -name 'node_modules' -type d -prune -exec rm -rf '{}' +" +alias md2org="find . -name \\*.md -type f -exec pandoc -f markdown -t org -o {}.org {} \\;" +alias icat="kitten icat" +