diff options
Diffstat (limited to 'config/build.sh')
| -rwxr-xr-x | config/build.sh | 135 |
1 files changed, 135 insertions, 0 deletions
diff --git a/config/build.sh b/config/build.sh new file mode 100755 index 0000000..4277143 --- /dev/null +++ b/config/build.sh @@ -0,0 +1,135 @@ +#!/usr/bin/env bash +# +# Build +# Build all Angular & React projects. + +############################################################################### +# 1.0. Helpers +############################################################################### +_COLOR_RESET=$(printf '\e[0m') +_COLOR_BLUE=$(printf '\e[34m') +_COLOR_GREEN=$(printf '\e[32m') +_COLOR_RED=$(printf '\e[31m') +_COLOR_YELLOW=$(printf '\e[33m') + +# Print an error prefixed by a colorized "Error:". +error() { + printf "%sError:%s %s\n" "$_COLOR_RED" "$_COLOR_RESET" "$1" +} + +# Print an info prefixed by a colorized "Info:". +info() { + printf "%sInfo:%s %s\n" "$_COLOR_BLUE" "$_COLOR_RESET" "$1" +} + +# Print a success prefixed by a colorized "Success:". +success() { + printf "%sSuccess:%s %s\n" "$_COLOR_GREEN" "$_COLOR_RESET" "$1" +} + +# Print a warning prefixed by a colorized "Warning:". +warning() { + printf "%sWarning:%s %s\n" "$_COLOR_YELLOW" "$_COLOR_RESET" "$1" +} + +# Check if the provided command is installed. +is_command_installed() { + [ -x "$(command -v "$1")" ] +} + +# Display an error in case of an unplanned scenario then exit. +error_unexpected() { + error "An unexpected error occurred." + printf "Exit.\n" + exit 1 +} + +############################################################################### +# 2.0. Angular +############################################################################### + +install_angular_dependencies() { + if [ ! -d "./projects/angular-small-apps" ]; then + error "Angular projects are missing." + printf "Exit.\n" + exit 1 + fi + + cd "./projects/angular-small-apps" || exit + yarn + cd "$OLDPWD" || exit +} + +build_angular_app() { + [ $# -ne 1 ] && error_unexpected + + cd "./projects/angular-small-apps/" || exit + yarn --cwd "apps/${1}" run build --base-href="/projects/angular-small-apps/apps/${1}/dist/${1}/" + info "${1} built." + cd "$OLDPWD" || exit +} + +setup_angular_projects() { + install_angular_dependencies + build_angular_app "recipes" + success "Angular apps are ready." +} + +############################################################################### +# 3.0. React +############################################################################### + +install_react_dependencies() { + if [ ! -d "./projects/react-small-apps" ]; then + error "React projects are missing." + printf "Exit.\n" + exit 1 + fi + + cd "./projects/react-small-apps" || exit + yarn + cd "$OLDPWD" || exit +} + +build_react_app() { + [ $# -ne 1 ] && error_unexpected + + cd "./projects/react-small-apps/" || exit + PUBLIC_URL="/projects/react-small-apps/apps/${1}/build/" yarn --cwd "apps/${1}" run build + info "${1} built." + cd "$OLDPWD" || exit +} + +setup_react_projects() { + install_react_dependencies + build_react_app "meme-generator" + build_react_app "notebook" + build_react_app "todos" + success "React apps are ready." +} + +############################################################################### +# 4.0. Main +############################################################################### + +# Check if Yarn is installed. +is_yarn_installed() { + printf "Checking if Yarn is installed...\n" + if is_command_installed "yarn"; then + info "Yarn is installed." + else + error "This program needs Yarn to proceed. Please install it." + printf "Exit.\n" + exit 1 + fi +} + +# Build Angular & React projects. +build_all_projects() { + is_yarn_installed + setup_angular_projects + setup_react_projects + success "All projects have been built." +} + +build_all_projects |
