deploy/deploy-npm

62 lines
1.6 KiB
Plaintext
Raw Normal View History

2021-04-14 18:28:56 +02:00
#!/usr/local/bin/bash
2022-03-16 09:02:27 +01:00
# Usage: `deploy-npm [--delete-target] $target`
#
# Builds the repository in the current directory with `npm run deploy` and copies the contents of `dist/` into
# `$target`.
# If `--delete-target` is set, then `$target` is deleted before copying, except for hidden files and folders.
if [ "$1" == "--delete-target" ]; then
2021-04-21 23:49:21 +02:00
delete_target=true
dst_dir="$2"
else
2021-04-21 23:49:21 +02:00
delete_target=false
dst_dir="$1"
fi
2021-04-14 18:28:56 +02:00
2021-04-14 20:05:45 +02:00
repo_dir="$(pwd)"
repo_name="$(basename -s .git "$repo_dir")"
tmp_dir="$HOME/tmp/git/$repo_name-$(date +%s)"
2021-04-14 18:28:56 +02:00
2021-04-15 19:00:29 +02:00
echo "[[ Deploy ]] npm($repo_name)"
echo "[[ Deploy ]] src: '$repo_dir'"
echo "[[ Deploy ]] dst: '$dst_dir'"
echo "[[ Deploy ]] tmp: '$tmp_dir'"
2021-04-14 19:24:11 +02:00
if test -e "$tmp_dir"; then
echo "[[ Deploy ]] Directory '$tmp_dir' already exists, stopping deployment."
exit
fi
2021-04-14 18:28:56 +02:00
echo "[[ Deploy ]] Creating directories '$dst_dir' and '$tmp_dir'."
mkdir -p "$dst_dir" "$tmp_dir"
2021-04-14 18:28:56 +02:00
echo "[[ Deploy ]] Cloning repository at '$repo_dir' into '$tmp_dir'."
git clone "$repo_dir" "$tmp_dir"
2021-04-14 18:28:56 +02:00
echo "[[ Deploy ]] Deploying npm at '$tmp_dir' into '$tmp_dir/dist'."
cd "$tmp_dir" || exit
npm ci
2021-04-14 18:28:56 +02:00
npm run deploy
2021-04-14 19:24:11 +02:00
if [ ! -d dist/ ]; then
echo "[[ Deploy ]] FATAL ERROR: 'dist' directory was not created."
exit;
fi
2021-04-21 23:49:21 +02:00
if [ "$delete_target" = true ]; then
echo "[[ Deploy ]] Deleting then recreating '$dst_dir'."
# shellcheck disable=SC2086
rm -rf ${dst_dir:?}/..?* ${dst_dir:?}/.[!.]* ${dst_dir:?}/**
chmod g+w "$dst_dir"
fi
echo "[[ Deploy ]] Copying files from '$tmp_dir/dist' to '$dst_dir'."
chmod -R g+w "dist"
cp -r dist/ "$dst_dir"
2021-04-14 18:28:56 +02:00
echo "[[ Deploy ]] Deleting '$tmp_dir'."
rm -rf "$tmp_dir"