aboutsummaryrefslogtreecommitdiffstats
path: root/config/build.sh
blob: 60f4ccbce59c1a93e118fb186aad53b1dd7a5b76 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/usr/bin/env bash
#
# Build
# Build all Angular & React projects.

###############################################################################
# 1.0. Config
###############################################################################
_ANGULAR_PROJECTS="projects/angular-small-apps"
_REACT_PROJECTS="projects/react-small-apps"

###############################################################################
# 2.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
}

###############################################################################
# 3.0. Angular
###############################################################################

install_angular_dependencies() {
  if [ ! -d "./public/${_ANGULAR_PROJECTS}" ]; then
    error "Angular projects are missing."
    printf "Exit.\n"
    exit 1
  fi

  cd "./public/${_ANGULAR_PROJECTS}" || exit
  yarn
  cd "$OLDPWD" || exit
}

build_angular_app() {
  [ $# -ne 1 ] && error_unexpected

  cd "./public/${_ANGULAR_PROJECTS}" || exit
  yarn --cwd "apps/${1}" run build --base-href="/${_ANGULAR_PROJECTS}/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."
}

###############################################################################
# 4.0. React
###############################################################################

install_react_dependencies() {
  if [ ! -d "./public/${_REACT_PROJECTS}" ]; then
    error "React projects are missing."
    printf "Exit.\n"
    exit 1
  fi

  cd "./public/${_REACT_PROJECTS}" || exit
  yarn
  cd "$OLDPWD" || exit
}

build_react_app() {
  [ $# -ne 1 ] && error_unexpected

  cd "./public/${_REACT_PROJECTS}" || exit
  PUBLIC_URL="/${_REACT_PROJECTS}/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."
}

###############################################################################
# 5.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