From b7a723fa7e658b61d3f222fe5cf075c3b04495bb Mon Sep 17 00:00:00 2001 From: Florent Vilmart Date: Tue, 22 Mar 2016 18:59:41 -0400 Subject: [PATCH 1/2] Adds bootstrap.sh --- bootstrap.sh | 149 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 149 insertions(+) create mode 100755 bootstrap.sh diff --git a/bootstrap.sh b/bootstrap.sh new file mode 100755 index 00000000..107306b1 --- /dev/null +++ b/bootstrap.sh @@ -0,0 +1,149 @@ +#!/bin/sh +RED='\033[0;31m' +GREEN='\033[0;32m' +NC='\033[0m' +CHECK="${GREEN}\xE2\x9C\x93${NC}" +confirm() { + DEFAULT=$1; + shift + printf "$@" + read -r YN + if [ "$YN" = "" ] && [ "$DEFAULT" = 'N' ]; then + exit 1 + elif [ "$YN" != "" ] && [ "$YN" != "y" ] && [ "$YN" != "Y" ]; then + echo 'Bye Bye!' + exit 1 + fi +} + +genstring() { + local l=$1 + [ "$l" == "" ] && l=40 + LC_ALL=C tr -dc A-Za-z0-9 < /dev/urandom | head -c ${l} +} + +check_node() { + node=`which node 2>&1` + ret=$? + + if [ $ret -eq 0 ] && [ -x "$node" ]; then + echo "${CHECK} node:" $(node -v) + (exit 0) + else + echo "parse-server cannot be installed without Node.js." >&2 + exit 1 + fi +} + +check_npm() { + npm=`which npm 2>&1` + ret=$? + + if [ $ret -eq 0 ] && [ -x "$npm" ]; then + echo "${CHECK} npm:" $(npm -v) + (exit 0) + else + echo "parse-server cannot be installed without npm." >&2 + exit 1 + fi +} + + +echo '' +echo 'This will setup parse-server in the current directory' +confirm 'Y' 'Do you want to continue? (Y/n): ' + +check_node +check_npm + +echo "Setting up parse-server in $PWD" + +if [ -f './package.json' ]; then + echo "\n${RED}package.json exists${NC}" + confirm 'N' "Do you want to continue? \n${RED}this will erase your configuration${NC} (y/N): " +fi + + +if [ -f 'config.json' ]; then + echo "\n${RED}config.json exists${NC}" + confirm 'N' "Do you want to continue? \n${RED}this will erase your configuration${NC} (y/N): " +fi + +APP_NAME='' +i=0 +while [ "$APP_NAME" = "" ] +do + [[ $i != 0 ]] && printf "${RED}An application name is required!${NC}\n" + printf 'Enter your Application Name: ' + read -r APP_NAME + i=$(($i+1)) +done + +printf 'Enter your appId (leave empty to generate): ' +read -r APP_ID + +[[ $APP_ID = '' ]] && APP_ID=$(genstring) && printf "\n$APP_ID\n\n" + +printf 'Enter your masterKey (leave empty to generate): ' +read -r MASTER_KEY + +[[ $MASTER_KEY = '' ]] && MASTER_KEY=$(genstring) && printf "\n$MASTER_KEY\n\n" + +cat > ./config.json << EOF +{ + "appId": "$APP_ID", + "masterKey": "$MASTER_KEY", + "appName": "$APP_NAME", + "cloud": "./cloud/main" +} +EOF +echo "${CHECK} Created config.json" + +# Make a proper npm app name +NPM_APP_NAME=$(echo "$APP_NAME" | tr '[:upper:]' '[:lower:]' | tr ' ' '-') +cat > ./package.json << EOF +{ + "name": "$NPM_APP_NAME", + "scripts": { + "start": "parse-server ./config.json" + }, + "dependencies": { + "parse-server": "^2.0.0" + } +} +EOF +echo "${CHECK} Created package.json" + +if [ -d "./cloud/" ]; then + echo "${CHECK} cloud/ exists" +else + mkdir -p ./cloud + echo "${CHECK} Created cloud/" +fi + +if [ -e "./cloud/main.js" ]; then + echo "${CHECK} cloud/main.js exists" +else + echo "${CHECK} Created cloud/main.js" + cat > ./cloud/main.js << EOF +// Cloud Code entry point + +EOF +fi + +if [ -d "./public/" ]; then + echo "${CHECK} public/ exists" +else + mkdir -p ./public + echo "${CHECK} Created public/" +fi + +echo "\n${CHECK} running npm install\n" + +npm install + +confirm 'Y' '\nDo you want to start the server now? (Y/n): ' + +echo "\n${CHECK} running npm start\n" + +npm start From 0a4cc606c63933e3975bee0a56adc67e4aed53a4 Mon Sep 17 00:00:00 2001 From: Florent Vilmart Date: Tue, 22 Mar 2016 19:51:02 -0400 Subject: [PATCH 2/2] Adds curl test command --- bootstrap.sh | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/bootstrap.sh b/bootstrap.sh index 107306b1..25f9199b 100755 --- a/bootstrap.sh +++ b/bootstrap.sh @@ -2,7 +2,10 @@ RED='\033[0;31m' GREEN='\033[0;32m' NC='\033[0m' +BOLD='\033[1m' CHECK="${GREEN}\xE2\x9C\x93${NC}" +DEFAULT_MONGODB_URI='mongodb://localhost:127.0.0.1:27017/parse' + confirm() { DEFAULT=$1; shift @@ -89,12 +92,18 @@ read -r MASTER_KEY [[ $MASTER_KEY = '' ]] && MASTER_KEY=$(genstring) && printf "\n$MASTER_KEY\n\n" +printf "Enter your mongodbURI (%s): " $DEFAULT_MONGODB_URI +read -r MONGODB_URI + +[[ $MONGODB_URI = '' ]] && MONGODB_URI="$DEFAULT_MONGODB_URI" + cat > ./config.json << EOF { "appId": "$APP_ID", "masterKey": "$MASTER_KEY", "appName": "$APP_NAME", - "cloud": "./cloud/main" + "cloud": "./cloud/main", + "mongodbURI": "$MONGODB_URI" } EOF echo "${CHECK} Created config.json" @@ -142,8 +151,13 @@ echo "\n${CHECK} running npm install\n" npm install -confirm 'Y' '\nDo you want to start the server now? (Y/n): ' +CURL_CMD=$(cat << EOF +curl -X POST -H 'X-Parse-Application-Id: ${APP_ID}' \\ + -H 'Content-Type: application/json' \\ + -d '{"foo":"bar"}' http://localhost:1337/parse/classes/TestObject +EOF) -echo "\n${CHECK} running npm start\n" - -npm start +echo "\n${CHECK} Happy Parsing!\n\n" +echo "${CHECK} Make sure you have ${BOLD}mongo${NC} listening on ${BOLD}${MONGODB_URI}${NC}" +echo "${CHECK} start parse-server by running ${BOLD}npm start${NC}" +echo "${CHECK} Test your setup with:\n\n${CURL_CMD}\n"