Skip to content

Instantly share code, notes, and snippets.

@vkuprin
Last active January 6, 2025 23:30
Show Gist options
  • Save vkuprin/8fbc75f2a144bb76533a96240291f899 to your computer and use it in GitHub Desktop.
Save vkuprin/8fbc75f2a144bb76533a96240291f899 to your computer and use it in GitHub Desktop.
Shell - FrontHelpers. This script is designed for front-end microservices based on javascript and nodejs.
#!/bin/sh
#
# 1. Check package manager (npm or yarn)
# 2. Bump version
# 3. Set project ID in HTML (based on package.json version)
# 4. Get/set values in .env
# 5. Make the script globally accessible in the system
#
# Usage:
# myscript.sh <command> [args...]
#
# Commands:
# checkPackageManager
# bump_ver
# setProjectID
# get <key>
# set <key> <value>
# makeScriptGlobalSystem
#
# Dependencies: jq (for setProjectID) and git (for commits)
###############################################################################
# COLOR CONSTANTS FOR COOLNESS
###############################################################################
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
CYAN='\033[0;36m'
BOLD='\033[1m'
RESET='\033[0m'
###############################################################################
# PRINT USAGE
###############################################################################
print_usage() {
cat <<EOF
${BOLD}Usage:${RESET} ${0} <command> [options]
${BOLD}Commands:${RESET}
${CYAN}checkPackageManager${RESET} Check which package manager (npm or yarn) is used.
${CYAN}bump_ver${RESET} Bump version using the detected package manager.
${CYAN}setProjectID${RESET} Set the project ID in index.html based on package.json version.
${CYAN}get <key>${RESET} Retrieve value from .env file.
${CYAN}set <key> <value>${RESET} Update value in .env file.
${CYAN}makeScriptGlobalSystem${RESET} Make this script globally accessible on the system.
Example:
${0} bump_ver
EOF
}
###############################################################################
# UTILITY FUNCTION: Check if required commands exist
###############################################################################
require_command() {
command -v "$1" >/dev/null 2>&1 || {
echo "${RED}Error:${RESET} Required command '$1' not found. Please install it."
exit 1
}
}
###############################################################################
# checkPackageManager
# Detects whether npm or yarn is being used in this project.
###############################################################################
checkPackageManager() {
if [ -f "package-lock.json" ]; then
echo "npm"
elif [ -f "yarn.lock" ]; then
echo "yarn"
else
echo "unknown"
fi
}
###############################################################################
# bump_ver
# Bumps the version (patch) of the project based on package manager.
###############################################################################
bump_ver() {
echo "${YELLOW}Bumping version...${RESET}"
local package_manager
package_manager=$(checkPackageManager)
case "$package_manager" in
npm)
require_command "npm"
npm version patch
;;
yarn)
require_command "yarn"
yarn version --patch
;;
*)
echo "${RED}Unknown package manager. Please use npm or yarn.${RESET}"
exit 1
;;
esac
echo "${GREEN}Version bumped successfully using $package_manager!${RESET}"
}
###############################################################################
# setProjectID
# Sets the project ID in index.html based on the "version" field in package.json.
###############################################################################
setProjectID() {
require_command "jq"
require_command "git"
# Ensure package.json and index.html exist
if [ ! -f package.json ]; then
echo "${RED}Error:${RESET} package.json not found!"
exit 1
fi
if [ ! -f index.html ]; then
echo "${RED}Error:${RESET} index.html not found!"
exit 1
fi
local project_id
project_id=$(jq -r '.version' package.json)
# Use sed to replace the existing meta content attribute
sed -i.bak "s/<meta name=\"project-id\" content=\".*\" \/>/<meta name=\"project-id\" content=\"$project_id\" \/>/" index.html
# Clean up backup file created by sed (-i.bak)
[ -f index.html.bak ] && rm -f index.html.bak
git add index.html
git commit -m "Set project id to $project_id"
echo "${GREEN}Project ID set to $project_id in index.html and changes committed.${RESET}"
}
###############################################################################
# get <key>
# Retrieves a value from the .env file for the given key.
###############################################################################
get() {
local key="$1"
if [ ! -f .env ]; then
echo "${RED}Error:${RESET} .env file not found!"
exit 1
fi
local value
value=$(grep -E "^${key}=" .env | cut -d '=' -f 2-)
if [ -z "$value" ]; then
echo "${RED}Key not found or empty:${RESET} $key"
else
echo "$value"
fi
}
###############################################################################
# set <key> <value>
# Updates (or sets) a given key-value pair in the .env file.
###############################################################################
set() {
local key="$1"
local value="$2"
require_command "git"
# If .env doesn't exist, create an empty one
if [ ! -f .env ]; then
touch .env
echo "${YELLOW}Note:${RESET} .env file created."
fi
# If the key already exists, update it; otherwise, append
if grep -qE "^${key}=" .env; then
sed -i.bak "s|^${key}=.*|${key}=${value}|" .env
[ -f .env.bak ] && rm -f .env.bak
else
echo "${key}=${value}" >> .env
fi
git add .env
git commit -m "Set $key to $value"
echo "${GREEN}Successfully set $key to $value in .env and committed changes.${RESET}"
}
###############################################################################
# makeScriptGlobalSystem
# Creates a symbolic link to this script in /usr/local/bin, allowing it to be
# called from anywhere on the system.
###############################################################################
makeScriptGlobalSystem() {
require_command "sudo"
local script_name
local script_path
local script_path_global
script_name="$(basename "$0")"
script_path="$(cd "$(dirname "$0")" && pwd)/$script_name"
script_path_global="/usr/local/bin/$script_name"
if [ -f "$script_path_global" ]; then
echo "${RED}Error:${RESET} Script already exists at $script_path_global"
exit 1
fi
sudo ln -s "$script_path" "$script_path_global"
echo "${GREEN}Script is now globally accessible at $script_path_global${RESET}"
}
###############################################################################
# MAIN
###############################################################################
case "$1" in
checkPackageManager)
checkPackageManager
;;
bump_ver)
bump_ver
;;
setProjectID)
setProjectID
;;
get)
get "$2"
;;
set)
set "$2" "$3"
;;
makeScriptGlobalSystem)
makeScriptGlobalSystem
;;
*)
print_usage
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment