Skip to content

Instantly share code, notes, and snippets.

@FirePanther
Created December 1, 2022 00:17
Show Gist options
  • Save FirePanther/c903d1d218fce4be24dd28134c488294 to your computer and use it in GitHub Desktop.
Save FirePanther/c903d1d218fce4be24dd28134c488294 to your computer and use it in GitHub Desktop.
Automatically (macOS-Finder) tag folders, hide them, or exclude them from spotlight and iCloud sync (e.g. node_modules)
#!/bin/bash
# requires: brew install tag
# you can change the source, for me the script is run by my Mac Mini server which only has to tag iCloud Drive documents
source="$HOME/Library/Mobile Documents/com~apple~CloudDocs"
# delete all tags once per day to clean up (if a project doesn't fit anymore)
if [ `date +"%H"` = 0 ]; then
tags=( "Git" "Node" "React" "VueJS" )
for tag in ${tags[@]}; do
/opt/homebrew/bin/tag --find $tag | xargs -I {} sh -c "/opt/homebrew/bin/tag --remove $tag '{}'"
done
fi
# auto "Git" tag
find $source -type d \
\( -path "*/.*/*" \
-o -path "*/node_modules*/*" \
-o -path "*/vendor/*" \
-o -path "*/*.noindex/*" \
\) -prune -o \
-type d -name ".git" -exec dirname "{}" \; | while read line; do \
/opt/homebrew/bin/tag -a Git "$line"; done
# exclude from spotlight index and from iCloud sync
# add more tags ("React", "VueJS", "Node")
find $source -type d \
\( -path "*/.*/*" \
-o -path "*/node_modules*/*" \
-o -path "*/vendor/*" \
-o -path "*/localhost/*" \
-o -path "*/*.noindex/*" \
\) -prune -o -type f -name "package.json" \
-exec sh -c 'dir="$(dirname "{}")" && [ -d "$dir/node_modules" ] && touch "$dir/node_modules/.nosync" && touch "$dir/node_modules/.metadata_never_index" && chflags -h hidden "$dir/node_modules" "$dir/package-lock.json"' \; \
-exec sh -c '/opt/homebrew/bin/tag -a Node "$(dirname "{}")"' \; \
-exec sh -c 'grep -q "\bvue\b" "{}" && '/opt/homebrew/bin/tag' -a VueJS "$(dirname "{}")"' \
-exec sh -c 'grep -q "\breact\-dom[^-]" "{}" && '/opt/homebrew/bin/tag' -a React "$(dirname "{}")"' \;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment