deploy/deploy-static

57 lines
1.5 KiB
Bash
Executable File

#!/usr/local/bin/bash
# Usage: `deploy-static [--delete-target] $target`
#
# Takes the repository in the current directory, 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
delete_target=true
dst_dir="$2"
else
delete_target=false
dst_dir="$1"
fi
repo_dir="$(pwd)"
repo_name="$(basename -s .git "$repo_dir")"
tmp_dir="$HOME/tmp/git/$repo_name-$(date +%s)"
echo "[[ Deploy ]] static($repo_name)"
echo "[[ Deploy ]] src: '$repo_dir'"
echo "[[ Deploy ]] dst: '$dst_dir'"
echo "[[ Deploy ]] tmp: '$tmp_dir'"
if test -e "$tmp_dir"; then
echo "[[ Deploy ]] Directory '$tmp_dir' already exists, stopping deployment."
exit
fi
echo "[[ Deploy ]] Creating directories '$dst_dir' and '$tmp_dir'."
mkdir -p "$dst_dir" "$tmp_dir"
echo "[[ Deploy ]] Cloning repository at '$repo_dir' into '$tmp_dir'."
git clone "$repo_dir" "$tmp_dir"
cd "$tmp_dir" || exit
if [ ! -d dist/ ]; then
echo "[[ Deploy ]] FATAL ERROR: 'dist' directory was not created."
exit;
fi
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 "$tmp_dir/dist"
cp -r "$tmp_dir/dist/" "$dst_dir"
echo "[[ Deploy ]] Deleting '$tmp_dir'."
rm -rf "$tmp_dir"