Created
March 6, 2025 12:25
-
-
Save wazeerc/2d9321d1e5382ac0c864e9c81e6d51e0 to your computer and use it in GitHub Desktop.
zsh Script to create new Excalidraw in VS Code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Create new Excalidraw file in a "designs" folder | |
function new_excalidraw() { | |
local filename="${1:-diagram}" # Use 'diagram' as default if no name provided | |
# Validate filename | |
if [[ ! "$filename" =~ ^[a-zA-Z0-9_-]+$ ]]; then | |
echo -e "\033[0;31mError: Filename can only contain letters, numbers, underscores, and hyphens\033[0m" | |
return 1 | |
fi | |
# Initial Excalidraw JSON content | |
local initial_content='{ | |
"type": "excalidraw", | |
"version": 2, | |
"source": "https://excalidraw.com", | |
"elements": [], | |
"appState": { | |
"viewBackgroundColor": "#ffffff" | |
} | |
}' | |
# Determine path: inside Git repo or current directory | |
if git rev-parse --is-inside-work-tree >/dev/null 2>&1; then | |
local designs_path="$(git rev-parse --show-toplevel)/designs" | |
else | |
local designs_path="$(pwd)/designs" | |
fi | |
# Create "designs" folder if it doesn't exist | |
if ! mkdir -p "$designs_path"; then | |
echo -e "\033[0;31mError: Failed to create designs directory\033[0m" | |
return 1 | |
fi | |
# Define file path and create file | |
local path="$designs_path/$filename.excalidraw" | |
echo "$initial_content" > "$path" | |
if [ ! -f "$path" ]; then | |
echo -e "\033[0;31mError: Failed to create file\033[0m" | |
return 1 | |
fi | |
# Print success message | |
echo -e "\033[0;32mSuccessfully created $path\033[0m" | |
echo -e "\033[0;33m[!] Consider adding 'designs/' to .gitignore.\033[0m" | |
echo -e "\033[0;34mOpen the file in VS Code with Excalidraw extension\033[0m" | |
} | |
# Add alias for the new_excalidraw function | |
alias excalidraw='new_excalidraw' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment