deploy/deploy-npm

62 lines
1.6 KiB
Bash
Executable File

#!/usr/local/bin/bash
# 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
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 ]] npm($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"
echo "[[ Deploy ]] Deploying npm at '$tmp_dir' into '$tmp_dir/dist'."
cd "$tmp_dir" || exit
npm ci
npm run deploy
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 "dist"
cp -r dist/ "$dst_dir"
echo "[[ Deploy ]] Deleting '$tmp_dir'."
rm -rf "$tmp_dir"