Skip to content

Instantly share code, notes, and snippets.

@luifiller
Created October 26, 2024 03:46
Show Gist options
  • Save luifiller/0462e8b7b361f635bf5230aea3bb97e8 to your computer and use it in GitHub Desktop.
Save luifiller/0462e8b7b361f635bf5230aea3bb97e8 to your computer and use it in GitHub Desktop.
Bash version @joshuamorony: Alias for creating playground applications w/ specific Angular versions and sharing to GitHub/StackBlitz
# References:
# - YouTube Joshua Morony: https://www.youtube.com/watch?v=J4PWmOqpzME&ab_channel=JoshuaMorony
# - .zshrc joshuamorony GH Gist: https://gist.github.com/joshuamorony/9a73a2c033014c754c81d8dc3e49ba54
# Prerequisites:
# - Ensure Git is installed and configured with your GitHub account.
# - Install GitHub CLI (gh) for managing GitHub repositories directly from the terminal.
# - Install Node.js and npm (Node Package Manager) to manage Angular CLI and its dependencies.
# - Ensure Angular CLI is installed globally or accessible via npx.
# - Install Visual Studio Code for opening projects directly from the terminal.
# - Optionally, configure StackBlitz for seamless integration with your projects.
# - Verify and adapt the command paths to fit your directory structure and operating system.
# - Ensure you have the necessary permissions to create directories and files in the specified locations.
# Launches Nano to edit your bash_profile file for environment configuration
nano ~/.bash_profile
# Creates a new Angular project with the specified version after user confirmation and prompts for the project name.
ng-v() {
if [ -z "$1" ]; then
echo "Please specify the Angular version (example: ng-v 10)"
return 1
fi
local version=$1
read -p "Are you sure you want to create an Angular project with version $version? (y/n) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
read -p "Enter the project name: " projectName
npx --legacy-peer-deps -p @angular/cli@$version ng new "$projectName"
if [ $? -eq 0 ]; then
read -p "Do you want to open the project in a new VS Code window? (y/n) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
cd "$projectName" && code .
else
echo "Project created. You can manually navigate to $projectName if needed."
fi
else
echo "Project creation failed."
fi
else
echo "Project creation canceled."
fi
}
# [WARNING] - Remember to manually create aliases for any new projects you add.
# Aliases for navigating to specific Angular project directories, resetting the Git state, and starting the development server.
alias play-angular="cd ~/Dev/templates/angular && resetAndOpen && ng serve"
alias play-ng-mat="cd ~/Dev/templates/angular-material && resetAndOpen && ng serve"
# [WARNING] - Change the name on the switch command to "main", "master," or another name used in your context.
# Resets the current Git repository to a clean state and opens the project in Visual Studio Code.
resetAndOpen() {
git switch main --discard-changes
git reset --hard
git clean -fd
code .
}
# Shares a Git repository by prompting for a name if not provided and then calls createGitRepo to create the repository.
share() {
if [ -z "$1" ]; then
read -p "What would you like to call this repo? " NAME
echo
createGitRepo "$NAME"
else
createGitRepo "$1"
fi
}
# Creates a new Git repository, optionally sharing it publicly and opening a StackBlitz link (if the user is logged in and uses the same GitHub username).
createGitRepo() {
read -p "Are you sure you want to share this publicly? (y/n) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
local sourcePath=$(pwd)
rsync -r --exclude=.git --exclude=node_modules . ~/Dev/playground/shared/$1
cd ~/Dev/playground/shared/$1
git init
git add .
git commit -m 'initial commit'
gh repo create "$1" --public --push --source=. --remote=upstream
read -p "Do you also want to create a StackBlitz? (y/n) " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
xdg-open "https://stackblitz.com/github/YOUR-GITHUB-USERNAME/$1"
else
gh repo view -w
fi
echo "Copy created at $(pwd), returning to the original directory."
cd "$sourcePath"
fi
}
# Reloads your Bash profile and applies any changes made to your environment variables, aliases, or functions.
source ~/.bash_profile
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment