Last active
May 22, 2016 06:38
-
-
Save Jeff-Russ/9c2c20816f9367648ffe072e96f656c9 to your computer and use it in GitHub Desktop.
`rails new <appname>` + project cloning for Node.js apps
This file contains 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 | |
# node-appgen | |
SD=$(dirname "${BASH_SOURCE[0]}") | |
CALLER_PATH=$(pwd) | |
COPY_SRC="$SD/node-appgen-default" | |
INSTALLER="$SD/node-appgen-default/node-appgen-installs" | |
EMAIL=$(git config user.email || "") | |
AUTHOR=$(git config user.name || id -F || id -un || whoami) | |
NAME=$(id -F || id -un || whoami || git config user.name ) | |
############################################################################### | |
main () { | |
display_welcome | |
check_args | |
if $DIR_MISSING; then | |
fancyfail "Copy Directory Missing" | |
echo_blue "Make sure $COPY_SRC exists. It should also contain 'node-appgen-installs'" | |
elif $INST_MISSING; then | |
fp_color=33 | |
fancyprompt "$INSTALLER missing." '' '' exit | |
fi | |
tasks_before_install "$@" | |
# Run Installers ------------------------------------------------------- | |
if $INST_MISSING; then pipe "Installing Node Packages" | |
install_packages () { RUN_NPM=true; source "$INSTALLER"; run_appgen_installs; } | |
m="About to run installers in:\n" | |
m="${m}\n\t$SRC_DIR_NAME/node-appgen-installs\n\nMake a selection:" | |
fp_color=34 | |
fancyprompt "$m" install_packages install_packages npm_install_cancel | |
fi | |
# Copy Project --------------------------------------------------------- | |
pipe "Copying project files and folders from template" | |
copy_project () { cp -an $COPY_SRC/. $PROJ_PATH/; } | |
m="Are you sure you want to run the npm installers from $SRC_DIR_NAME?\n" | |
m="${m}No files will be overriden. $COPY_SRC is default source." | |
m="${m}Hit enter to proceed or enter a different path and hit enter" | |
fp_color=34 | |
fancyprompt "$m" copy_project copy_project cancel_copy | |
if $RUN_NPM; then # ---------------------------------------------------- | |
line "Running: npm install" | |
cd $PROJ_PATH | |
npm install | |
fi | |
} | |
############################################################################### | |
check_args () { | |
# check arguments_______________________________________________________ | |
if [ "$#" -eq 0 ]; then | |
DIR_MISSING=false | |
DESC='' | |
if [ "$(ls -A $CALLER_PATH)" ]; then | |
m="This directory is not empty! This will create a new app here." | |
fancyprompt "$m" '' '' abort_appgen | |
fi | |
PROJ_PATH=$(abs_path "$CALLER_PATH") | |
elif [ "$#" -eq 2 ]; then | |
mkdir "$1" | |
PROJ_PATH=$(abs_path "${CALLER_PATH}/${1}") | |
if [ -d "$2" ]; then | |
COPY_SRC=$(abs_path "$2") | |
DIR_MISSING=false | |
if [ -f "$2/node-appgen-installs" ]; then | |
INST_MISSING=false | |
INSTALLER="$COPY_SRC/node-appgen-installs" | |
else | |
INSTALLER="$2/node-appgen-installs" # for fail display | |
INST_MISSING=true | |
RUN_NPM=false | |
fi | |
else | |
COPY_SRC="$2" # for fail display | |
DIR_MISSING=true | |
fi | |
else # TODO: add help menu | |
echo_magenta "Incorrect number of arguments" | |
fi | |
} | |
tasks_before_install () { | |
SRC_DIR_NAME=$(basename "$COPY_SRC") | |
echo "gathering project variables..." | |
local HAS_JSON=false; local HAS_GIT=false | |
if [ -d "$PROJ_PATH" ]; then # inspect any pre-existing content | |
echo_blue "found directory with matching name" | |
# store name of readme if found: | |
local README=$(ls "$PROJ_PATH" | grep -i readme) | |
if [ -f "$PROJ_PATH/package.json" ]; then HAS_JSON=true; fi | |
if [ -d "$PROJ_PATH/.git" ]; then HAS_GIT=true; fi | |
else | |
mkdir $PROJ_PATH | |
fi | |
cd $PROJ_PATH | |
if $HAS_GIT; then | |
echo_magenta ".git found" | |
local SSH=`git remote -v | grep -m1 "^origin"` | |
[ -z "$SSH" ] || SSH=`git remote -v | grep -m1 ''` | |
local HOST=$(sed 's/.*@\(.*\):.*/\1/' <<< "$SSH") # get b/w @ and : | |
AUTHOR=$(sed 's/.*:\(.*\)\/.*/\1/' <<< "$SSH") # get b/w : and / | |
PROJ=$(sed 's/.*\/\(.*\)\.git.*/\1/' <<< "$SSH") # get b/w / and .git | |
URL="https://$HOST/$AUTHOR/$PROJ" | |
else | |
PROJ=$(basename "$PROJ_PATH") | |
if $(has_spaces "$AUTHOR"); then AUTHOR=$(stripwhite_downcase "$AUTHOR"); fi | |
URL="https://github.com/$AUTHOR/$PROJ" | |
fi | |
# _______________________________________________________ | |
# package.json | |
declare_string_templates | |
if $HAS_JSON; then | |
echo_magenta "package.json found. Skipping..." | |
else | |
printf "\nCreating package.json file with the following info:\n\n" | |
printf "You: $NAME\nProject: $PROJ $URL\nAuthor: $AUTHOR $EMAIL\n\n" | |
echo "More info will be added when packages are installed" | |
printf "$PACKAGE_JSON" > $PROJ_PATH/package.json | |
fi | |
# ______________________________________________________________________ | |
# README.md -silence warning while installing packages by making non-empty | |
if [ -z "$README" ]; then | |
echo | |
echo "Creating README.md" | |
printf "$README_MD" > $PROJ_PATH/README.md | |
else | |
if [ -s "$PROJ_PATH/$README" ]; then : # file has content so do nothing | |
else echo "# " >> $PROJ_PATH/$README # need something to avoid npm warnings | |
fi | |
fi | |
} | |
############################################################################### | |
# Special Responders | |
npm_install_cancel () { RUN_NPM=false } | |
cancel_msg () { echo "action canceled."; } | |
abort_appgen () { echo "Goodbye"; exit 0; } | |
# ____________________________________________________________________________ | |
# STRING TEMPLATES: | |
declare_string_templates () { | |
PACKAGE_JSON=$(cat <<EOF | |
{ | |
"name": "$PROJ", | |
"version": "0.0.1", | |
"description": "$DESC", | |
"author": "$AUTHOR <$EMAIL>", | |
"contributors": [{ | |
"name": "$NAME", | |
"email": "$EMAIL" | |
}], | |
"main": "app.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"repository": { | |
"type": "git", | |
"url": "$URL" | |
}, | |
"private": "true", | |
"license": "MIT" | |
} | |
EOF | |
) | |
README_MD=$(cat <<EOF | |
# $PROJ | |
[version 0.0.1]($URL) | |
$DESC | |
EOF | |
) | |
} | |
# ___________________________________________________________ | |
# just some utilities for executing this script: | |
echo_blue () { echo "\033[34m$1\033[0m"; } | |
echo_green () { echo "\033[32m$1\033[0m"; } | |
echo_red () { echo "\033[31m$1\033[0m"; } | |
echo_magenta () { echo "\033[35m$1\033[0m"; } | |
bar () { | |
echo | |
echo '###############################################################' | |
echo "######## $1" | |
} | |
pipe () { | |
echo | |
echo '=============================================================' | |
echo "======== $1" | |
} | |
line () { | |
echo | |
echo '___________________________________________________________' | |
echo "________ $1" | |
} | |
display_welcome () { | |
bar "node-appgen ##########################################" | |
} | |
fancyprompt () { | |
if [[ -z $fp_color ]]; then fp_color=33; fi | |
local beg="\033[$fp_color;1m"; local end="\033[0m\n" | |
printf "\033[$fp_color;5;7m\t____/ attention \____\t\t$end" | |
printf "${beg}${1}${end}" | |
printf "$beg - 'c'\t\tto skip or\n - [return]\tto continue$end > " | |
fp_color=33; | |
local reply='' | |
if [[ -z $TMOUT ]]; then TMOUT=10; fi | |
read reply | |
if [ "$#" -eq 2 ]; then | |
if [[ "$reply" != 'c' ]]; then $2 $reply # not 'c'. default or custom | |
fi | |
elif [ "$#" -eq 3 ]; then | |
if [[ -z $reply ]]; then $2 # default (timeout or [enter]) | |
elif [[ "$reply" != 'c' ]]; then $3 $reply # not 'c' with custom reply | |
fi | |
elif [ "$#" -eq 4 ]; then | |
if [[ -z $reply ]]; then $2 # default (timeout or [enter]) | |
elif [[ "$reply" != 'c' ]]; then $3 $reply # not 'c' with custom reply | |
else $4 # reply was 'c' | |
fi | |
fi | |
} | |
fancyfail () { | |
if [[ -z $fail_color ]]; then fail_color=31; fi | |
local beg="\033[$fail_color;3m"; local end="\033[0m\n" | |
printf "\033[$fail_color;1;7m\t____/ attention \____\t\t$end" | |
printf "${beg}${1}${end}" | |
} | |
abs_path () { | |
# use double quotes for paths with spaces etc | |
ABS_PATH=`cd "$1"; pwd` | |
echo "$ABS_PATH" | |
} | |
stripwhite_downcase () { | |
local var=$(printf "$1" | tr "[:upper:]" "[:lower:]") | |
printf "$var" | tr -d '[[:space:]]' | |
} | |
has_spaces () { | |
if [[ "$1" != "${1/ /}" ]] | |
then echo true | |
else echo false | |
fi | |
} | |
is_int () { | |
if [[ $1 =~ ^-?[0-9]+$ ]] | |
then echo true | |
else echo false | |
fi | |
} | |
############################################################################### | |
# execution starts here: | |
main "$@" |
This file contains 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 | |
# node-appgen resource | |
npm_cmdlist=( | |
"npm install --save express" | |
"npm install --save grunt" | |
"npm install --save-dev coffee-script" | |
"npm install --save jade" | |
"npm install --save grunt-sass" | |
"npm install grunt-contrib-watch --save-dev" | |
) | |
WD=$(pwd) | |
clone_src="$WD/node-appgen-src1" |
This file contains 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
echo_blue "Generating project files and folders" | |
cp -an $clone_src/. $PROJ_DIR/ | |
echo_blue "All packages installed." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment