#!/usr/local/bin/bash # Usage: `deploy-npm-versioned $dst_dir` # # Builds the repository in the current directory with `npm run deploy` and copies the contents of `dist/` into multiple # sub-directories in `$dst_dir`. # The sub-directories are named after the version number listed in the `package.json`. # If the version is `A.B.C`, then the repository is deployed into sub-directories `A.B.C`, `A.B.x`, and `A.x.x`, where # `x` is literal. repo_dir="$(pwd)" repo_name="$(basename -s .git "$repo_dir")" dst_dir="$1" tmp_dir="$HOME/tmp/git/$repo_name-$(date +%s)" echo "[[ Deploy ]] npm-versioned($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 echo "[[ Deploy ]] Extracting version info." read -r major minor patch <<< "$(node -pe "require('./package.json').version" | awk '{split($0, a, "[\.\+\-]"); print a[1], a[2], a[3]}')" dst_dirs="$dst_dir/$major.$minor.$patch $dst_dir/$major.$minor.x $dst_dir/$major.x.x" echo "[[ Deploy ]] Copying files from '$tmp_dir/dist' to the following directories: $dst_dirs" echo "$dst_dirs" | xargs -n 1 rm -rf echo "$dst_dirs" | xargs -n 1 cp -r dist/ echo "$dst_dirs" | xargs -n 1 chmod -R g+w echo "[[ Deploy ]] Deleting '$tmp_dir'." rm -rf "$tmp_dir"