aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/build-unix.sh
blob: 24ab0f73e0c812a22e33c30eaef9cc41165c4373 (plain) (blame)
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
142
#!/bin/bash

# INTRO:
# This file is used to build ferdium-server on both x64 and arm-based for macos and linux (not tested on arm for linux).
# It also handles any corrupted node modules with the 'CLEAN' env var (set it to 'true' for cleaning)
# It will install the system dependencies except for node and python (which are still verified)
# I sometimes symlink my 'recipes' folder so that any changes that I need to do in it can also be committed and pushed independently
# This file can live anywhere in your PATH

set -e

export CI=true

# -----------------------------------------------------------------------------
#                  Utility functions
fail_with_docs() {
  printf "\n*************** FAILING ***************\n"
  echo "$1"
  echo ""
  echo "Please read the developer documentation in CONTRIBUTING.md"
  exit 1
}

command_exists() {
  type "$1" &>/dev/null 2>&1
}

# -----------------------------------------------------------------------------
#                  Checking the developer environment
# Check for installed programmes
command_exists node || fail_with_docs "Node is not installed"
command_exists python || fail_with_docs "python is not installed"

# Check node version
EXPECTED_NODE_VERSION=$(cat .nvmrc)
ACTUAL_NODE_VERSION=$(node -v)
if [ "v$EXPECTED_NODE_VERSION" != "$ACTUAL_NODE_VERSION" ]; then
  fail_with_docs "You are not running the expected version of node!
    expected: [v$EXPECTED_NODE_VERSION]
    actual  : [$ACTUAL_NODE_VERSION]"
fi

# Check if the 'recipes' folder is present either as a git submodule or a symbolic link
if ! [ -f "recipes/package.json" ]; then
  fail_with_docs "'recipes' folder is missing or submodule has not been checked out"
fi

# This log statement is only to remind me which 'recipes' folder I am using (symlink or git submodule)
if [[ -L recipes ]]; then
  printf "\n*************** CONTINUING for 'recipes' symlinked ***************\n"
else
  printf "\n*************** CONTINUING for 'recipes' submodule ***************\n"
fi

# -----------------------------------------------------------------------------
# If you are moving to a new version of node or any other system dependency, then cleaning is recommended
# so that there's no irregular results due to cached modules
if [ "$CLEAN" != "true" ]; then
  printf "\n*************** SKIPPING Cleaning ***************\n"
else
  printf "\n*************** Cleaning!!!!!! ***************\n"

  if [[ -s 'pnpm-lock.yaml' ]]; then
    pnpm store prune || true # in case the pnpm executable itself is not present
    rm -rf ~/.pnpm-store ~/.pnpm-state
  fi

  npm cache clean --force
  rm -rf ~/.npm ~/.node-gyp ~/.asdf/installs/nodejs/*/.npm/

  git -C recipes clean -fxd # Clean recipes folder/submodule
  git clean -fxd            # Note: This will blast away the 'recipes' folder if you have symlinked it
fi

# -----------------------------------------------------------------------------
# Ensure that the system dependencies are at the correct version - fail if not
# Check python version
EXPECTED_PYTHON_VERSION=$(node -p 'require("./package.json").engines.python')
ACTUAL_PYTHON_VERSION=$(python --version | sed -e "s/Python //")
if [[ "$ACTUAL_PYTHON_VERSION" != "$EXPECTED_PYTHON_VERSION" ]]; then
  fail_with_docs "You are not running the expected version of Python!
    expected: [$EXPECTED_PYTHON_VERSION]
    actual  : [$ACTUAL_PYTHON_VERSION]"
fi

# -----------------------------------------------------------------------------
# Ensure that the system dependencies are at the correct version - recover if not
# If 'asdf' is installed, reshim for new nodejs if necessary
command_exists asdf && asdf reshim nodejs

# Ensure that the system dependencies are at the correct version
# Check npm version
EXPECTED_NPM_VERSION=$(node -p 'require("./package.json").engines.npm')
ACTUAL_NPM_VERSION=$(npm --version)
if [[ "$ACTUAL_NPM_VERSION" != "$EXPECTED_NPM_VERSION" ]]; then
  npm i -gf npm@$EXPECTED_NPM_VERSION
fi

# Check pnpm version
EXPECTED_PNPM_VERSION=$(node -p 'require("./recipes/package.json").engines.pnpm')
ACTUAL_PNPM_VERSION=$(pnpm --version || true) # in case the pnpm executable itself is not present
if [[ "$ACTUAL_PNPM_VERSION" != "$EXPECTED_PNPM_VERSION" ]]; then
  npm i -gf pnpm@$EXPECTED_PNPM_VERSION
fi

# If 'asdf' is installed, reshim for new nodejs if necessary
command_exists asdf && asdf reshim nodejs

# -----------------------------------------------------------------------------
# This is useful if we move from 'npm' to 'pnpm' for the main repo as well
if [[ -s 'pnpm-lock.yaml' ]]; then
  BASE_CMD=pnpm
else
  BASE_CMD=npm
fi

ENV_FILE=".env"
if [[ ! -s $ENV_FILE ]]; then
  APP_KEY=`cat /dev/urandom | LC_ALL=C tr -dc "[:alnum:]" | fold -w ${1:-32} | head -n 1`
  # SAVE APP_KEY TO .env FILE
  sed "s/APP_KEY\=/APP_KEY=$APP_KEY/" .env.example > $ENV_FILE
fi

mkdir -p data

# -----------------------------------------------------------------------------
printf "\n*************** Building recipes ***************\n"
# Note: 'recipes' is already using only pnpm - can switch to $BASE_CMD AFTER both repos are using pnpm
pushd recipes
pnpm i
pnpm package
popd

# Now the meat.....
$BASE_CMD i
node ace migration:refresh

# -----------------------------------------------------------------------------
printf "\n*************** Building app ***************\n"
$BASE_CMD start --dev

printf "\n*************** App successfully stopped! ***************\n"