Last active
April 25, 2024 06:24
-
-
Save szhu/827c15a27de4c7ec7e0afb7fac70425d to your computer and use it in GitHub Desktop.
This file contains 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
#!/bin/sh | |
# File formatted with shell-format. | |
# | |
# This tool creates a disk image to put node_modules in. This makes it so that | |
# when the disk image is not mounted, it is treated as a single file by | |
# Spotlight and other tools to do expensive tasks on a per-file basis. | |
set -e | |
# Utils | |
self_name() { | |
if [ "$(dirname "$0")" = "$PWD" ]; then | |
echo "./$(basename "$0")" | |
else | |
echo "$0" | |
fi | |
} | |
test_line_exists_in_file() { | |
local line="$1" | |
local file="$2" | |
grep -qFx "$line" "$file" | |
} | |
test_has_dir_x_access() { | |
local dir="$1" | |
ls -1 "$dir" >/dev/null 2>/dev/null | |
} | |
# Commands | |
init() { | |
test -f 'node_modules.sparseimage' || | |
hdiutil create -size '1G' -type 'SPARSE' -volname 'node_modules' 'node_modules.sparseimage' | |
test_line_exists_in_file '/node_modules.sparseimage' '.git/info/exclude' || | |
echo >>'.git/info/exclude' '/node_modules.sparseimage' | |
echo 'Initialized node_modules.sparseimage.' | |
create_placeholder | |
} | |
mount() { | |
hdiutil attach 'node_modules.sparseimage' -mountpoint 'node_modules' -nobrowse -quiet | |
} | |
create_placeholder() { | |
if ! test -e 'node_modules'; then | |
mkdir 'node_modules' && | |
chmod -x 'node_modules' | |
echo 'Created placeholder node_modules dir.' | |
fi | |
} | |
migrate() { | |
unmount || true | |
if test_has_dir_x_access 'node_modules'; then | |
mv 'node_modules' 'node_modules-being-migrated' && | |
mount && | |
ditto 'node_modules-being-migrated' 'node_modules' && | |
rm -rf 'node_modules-being-migrated' && | |
unmount | |
else | |
echo "There is no node_modules dir to migrate." | |
fi | |
create_placeholder | |
} | |
unmount() { | |
hdiutil detach 'node_modules' -quiet | |
} | |
case "$1" in | |
init) init ;; | |
mount) mount ;; | |
migrate) migrate ;; | |
unmount) unmount ;; | |
*) echo "Usage: $(self_name) [init | mount | migrate | unmount]" ;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Moved to https://github.com/szhu/nmg.