Maros Hluska

Back

Avoiding node_modules in Dropbox

Since Dropbox doesn't support ignoring directories programmatically, we can use rsync and crontab to achieve something similar:

If we have many projects with deep node_modules directories, we will want to avoid syncing them directly to Dropbox as it may needlessly block important files from syncing. Instead, we can work from ~/projects/ but periodically sync back to ~/Dropbox/.projects/ with auto-generated directories exluded. A dotfile is used to avoid accidentally working within Dropbox.

Lets rename our existing projects and strip them of noise:

$ mv ~/Dropbox/projects ~/Dropbox/.projects
$ find ~/Dropbox/.projects \( \
  -name 'node_modules' -o \
  -name 'bower_components' -o \
  -name 'tmp' -o \
  -name 'temp' -o \
  -name 'Icon?' -o \
  -name '.DS_Store' \) -print0 | xargs -0 rm -rf

Now lets add a couple of scripts to the projects directory: setup.sh and sync.sh.

Contents of setup.sh:

#!/bin/bash

DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

rsync -avh --delete ~/Dropbox/.projects/ ~/projects
(crontab -l ; echo "*/10 * * * * ${DIR}/sync.sh") | sort - | uniq - | crontab -

We can run setup.sh once as part of a fresh system installation. It will copy over all projects to the home directory and add a crontab entry to sync back to Dropbox every 10 minutes.

Contents of sync.sh:

#!/bin/sh

rsync -avh --delete \
  --exclude=node_modules \
  --exclude=bower_components \
  --exclude=tmp \
  --exclude=temp \
  --exclude=Icon? \
  --exclude=.DS_Store ~/projects/ ~/Dropbox/.projects

The sync.sh script gets called by crontab and simply syncs back to Dropbox with various auto-generated entries excluded.

With a project named foo, your files should now look something like this after running setup.sh:

$ find ~/projects
/Users/mhluska/projects
/Users/mhluska/projects/foo
/Users/mhluska/projects/foo/node_modules
/Users/mhluska/projects/foo/package.json
/Users/mhluska/projects/foo/README.md
/Users/mhluska/projects/setup.sh
/Users/mhluska/projects/sync.sh

$ find ~/Dropbox
/Users/mhluska/Dropbox
/Users/mhluska/Dropbox/.projects
/Users/mhluska/Dropbox/.projects/foo
/Users/mhluska/Dropbox/.projects/foo/package.json
/Users/mhluska/Dropbox/.projects/foo/README.md
/Users/mhluska/Dropbox/.projects/setup.sh
/Users/mhluska/Dropbox/.projects/sync.sh

$ crontab -l
*/10 * * * * /Users/mhluska/Dropbox/.projects/dotfiles/sync