Created
May 31, 2020 19:25
-
-
Save shikaan/378f7c36bf996b04e137a3a0b455d1f2 to your computer and use it in GitHub Desktop.
Bootstrap a simple and opinionated frontend application
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/bash | |
# CONFIGURATION | |
readonly SCRIPT="$(basename $0)" | |
readonly APP_NAME=$1 | |
readonly TIMEOUT=.5 | |
# FILES | |
readonly GITIGNORE=""" | |
node_modules | |
.cache | |
dist | |
""" | |
readonly INDEX_HTML=""" | |
<!doctype html> | |
<html lang=\"en\"> | |
<head> | |
<meta charset=\"utf-8\"> | |
<meta name=\"viewport\" content=\"width=device-width, user-scalable=no\"> | |
<link rel=\"manifest\" href=\"./manifest.json\"> | |
<link rel=\"shortcut icon\" href=\"../static/favicon.ico\"> | |
<title>$APP_NAME</title> | |
</head> | |
<body> | |
<noscript> | |
You need to enable JavaScript to run this app. | |
</noscript> | |
<h1> | |
Hello $APP_NAME! | |
</h1> | |
<script src=\"../src/index.js\"></script> | |
</body> | |
</html> | |
""" | |
readonly MANIFEST_JSON=""" | |
{ | |
\"name\": \"${APP_NAME}\", | |
\"short_name\": \"${APP_NAME}\", | |
\"theme_color\": \"#2196f3\", | |
\"background_color\": \"#2196f3\", | |
\"display\": \"browser\", | |
\"scope\": \"/\", | |
\"start_url\": \"/\" | |
} | |
""" | |
readonly INDEX_JS="console.log('Hello ${APP_NAME}!');" | |
readonly INDEX_TEST_JS=""" | |
import assert from 'assert'; | |
suite('index.js', () => { | |
describe('when test is dummy', () => { | |
it('passes', () => { | |
assert.ok(true); | |
}) | |
}) | |
}) | |
""" | |
# UTILS | |
function help { | |
printf """ | |
Usage: | |
%s app_name | |
Bootstraps a simple frontend application (with parcel, titef) in current folder. | |
""" $SCRIPT | |
} | |
readonly HIGHLIGHT_START="\033[31m\033[1m" | |
readonly HIGHLIGHT_END="\033[0m" | |
readonly UNDERLINE_START="\033[4m" | |
readonly UNDERLINE_END="\033[0m" | |
if [ -z $APP_NAME ] | |
then | |
help | |
exit 0 | |
fi | |
printf """ | |
Bootstraping ${HIGHLIGHT_START}$APP_NAME${HIGHLIGHT_END} at ${UNDERLINE_START}$(pwd)/${APP_NAME}${UNDERLINE_END}\n | |
""" | |
printf " π Create folder structure\n" | |
mkdir $APP_NAME | |
cd $APP_NAME | |
mkdir src | |
mkdir public | |
mkdir static | |
printf " π³ Setup git repository\n" | |
printf " β Initializing repository...\r" | |
git init &> /dev/null | |
printf "\033[2K β Initialize repository\n" | |
printf " β Creating .gitignore...\r" | |
printf "${GITIGNORE}" > .gitignore | |
sleep $TIMEOUT | |
printf "\033[2K β Create .gitignore\n" | |
printf " π¦ Setup NPM project\n" | |
npm init -y &> /dev/null | |
printf " β Installing dependencies...\r" | |
npm i -D parcel titef &> /dev/null | |
printf "\033[2K β Install dependencies\n" | |
printf " β Setting up scripts...\r" | |
cat package.json | jq '. + {scripts: {test: "titef -e spec.js,test.js ./src", start: "parcel ./public/index.html"}}' > package.tmp.json | |
mv package.tmp.json package.json | |
sleep $TIMEOUT | |
printf "\033[2K β Setup scripts\n" | |
printf " β Setting up metadata...\r" | |
cat package.json | jq ". + {description: \"${APP_NAME} setup with ${SCRIPT}\", version: \"0.0.1\", author: \"$(git config user.name) <$(git config user.email)>\"}" > package.tmp.json | |
mv package.tmp.json package.json | |
sleep $TIMEOUT | |
printf "\033[2K β Setup metadata\n" | |
printf " β¨ Create example files\n" | |
printf "\033[2K β Creating public/index.html...\r" | |
printf "${INDEX_HTML}" > ./public/index.html | |
sleep $TIMEOUT | |
printf "\033[2K β Create public/index.html\n" | |
printf " β Creating public/manifest.json...\r" | |
printf "${MANIFEST_JSON}" > ./public/manifest.json | |
sleep $TIMEOUT | |
printf "\033[2K β Create public/manifest.json\n" | |
printf " β Creating static/favicon.ico...\r" | |
touch ./static/favicon.ico | |
sleep $TIMEOUT | |
printf "\033[2K β Create static/favicon.ico\n" | |
printf " β Creating src/index.js...\r" | |
printf "${INDEX_JS}" > ./src/index.js | |
sleep $TIMEOUT | |
printf "\033[2K β Create src/index.js\n" | |
printf " β Creating src/index.test.js...\r" | |
printf "${INDEX_TEST_JS}" > ./src/index.test.js | |
sleep $TIMEOUT | |
printf "\033[2K β Create src/index.test.js\n" | |
printf " π Create initial commit" | |
git add . &> /dev/null | |
git commit -m "chore: init" &> /dev/null | |
printf """ | |
Bootstrap of ${HIGHLIGHT_START}${APP_NAME}${HIGHLIGHT_END} complete! | |
You can now run you application by | |
cd $APP_NAME | |
npm start | |
or run the tests via | |
npm test | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment