Skip to content

Instantly share code, notes, and snippets.

@vkuprin
Last active October 26, 2022 20:02
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
print_usage () {
cat <<PRINT
Usage: ${0} <command>
Commands:
checkPackageManager Check if npm or yarn is used
bump_ver bump version
setProjectID set project id to html meta tag
get <key> get value from .env file
set <key> <value> set value to .env file
makeScriptGlobalSystem make script global in OS system for calling script from anywhere
PRINT
}
checkPackageManager() {
if [ -f "package-lock.json" ]; then
echo "npm"
elif [ -f "yarn.lock" ]; then
echo "yarn"
else
echo "unknown"
fi
}
bump_ver () {
echo "Bumping version..."
local package_manager=$(checkPackageManager)
if [ "$package_manager" = "npm" ]; then
npm version patch
elif [ "$package_manager" = "yarn" ]; then
yarn version --patch
else
echo "Unknown package manager"
exit 1
fi
}
setProjectID() {
local project_id=$(cat package.json | jq -r '.version')
local index_html=$(cat index.html)
local new_index_html=$(echo "$index_html" | sed "s/<meta name=\"project-id\" content=\".*\" \/>/<meta name=\"project-id\" content=\"$project_id\" \/>/")
echo "$new_index_html" > index.html
git add index.html
git commit -m "Set project id to $project_id"
}
get() {
local key=$1
local value=$(cat .env | grep "$key" | cut -d '=' -f 2)
echo "$value"
}
set() {
local key=$1
local value=$2
local new_env=$(cat .env | sed "s/$key=.*/$key=$value/")
echo "$new_env" > .env
git add .env
git commit -m "Set $key to $value"
}
makeScriptGlobalSystem() {
local script_name=$(basename "$0")
local script_path=$(pwd)/"$script_name"
local script_path_global="/usr/local/bin/$script_name"
if [ -f "$script_path_global" ]; then
echo "Script already exists in $script_path_global"
exit 1
fi
sudo ln -s "$script_path" "$script_path_global"
echo "Script created in $script_path_global"
}
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