Skip to content

Instantly share code, notes, and snippets.

@shikaan
Created May 31, 2020 19:25
Show Gist options
  • Save shikaan/378f7c36bf996b04e137a3a0b455d1f2 to your computer and use it in GitHub Desktop.
Save shikaan/378f7c36bf996b04e137a3a0b455d1f2 to your computer and use it in GitHub Desktop.
Bootstrap a simple and opinionated frontend application
#!/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