Skip to content

Instantly share code, notes, and snippets.

@senki
Last active July 25, 2016 03:09
Show Gist options
  • Save senki/a37172794bdedd3f479b to your computer and use it in GitHub Desktop.
Save senki/a37172794bdedd3f479b to your computer and use it in GitHub Desktop.
Notify of global npm updates via Notification Center on Mac OS X

Notify of global npm updates via Notification Center on Mac OS X

Make sure the terminal notifier is installed. Install it wia brew install terminal-notifier command.
The script assumes the iTerm is installed. Go get it, or edit the npm-update-notifier.sh for your need.

  1. Put notify-update-npm.sh to your ~/bin/ directory
  2. Put notify.update.npm.plist to your ~/Library/LaunchAgents/ directory
  3. Run launchctl load ~/Library/LaunchAgents/notify.update.npm.plist

This script is run twice a day at 11 AM & 4 PM.
Based on Based on Chris Streeters' brew-update-notifier.sh work from https://gist.github.com/streeter/3254906

#!/bin/bash
#
# Notify of npm updates via Notification Center on Mac OS X
#
# Author Csaba Maulis @_senki
# Based on Chris Streeters (http://www.chrisstreeter.com) `brew-update-notifier.sh`
# Requires: terminal-notifier. Install with:
# brew install terminal-notifier
#
NPM_EXEC='/usr/local/bin/npm'
TERMINAL_NOTIFIER='/usr/local/bin/terminal-notifier'
NOTIF_ARGS="-activate com.googlecode.iterm2 -sound NotificationCenter"
# Remove pinned package from the list of outdated package
outdated=`$NPM_EXEC outdated -g | awk '{if (NR!=1) {print $1}}'`
if [ -z "$outdated" ] ; then
if [ -e $TERMINAL_NOTIFIER ]; then
# No updates available
$TERMINAL_NOTIFIER $NOTIF_ARGS \
-title "NPM: No Updates Available" \
-message "No updates available yet for any global package."
fi
else
# We've got an outdated package or two
# Nofity via Notification Center
if [ -e $TERMINAL_NOTIFIER ]; then
lc=$((`echo "$outdated" | wc -l`))
if [ $lc -gt 1 ]; then
title="NPM: $lc Updates Available"
else
title="NPM: 1 Updates Available"
fi
outdated=`echo "$outdated" | tail -$lc`
message=`echo "$outdated" | head -5`
if [ "$outdated" != "$message" ]; then
message="Some of the globally outdated package are:
$message"
else
message="The following globally package are outdated:
$message"
fi
# Send to the Nofication Center
$TERMINAL_NOTIFIER $NOTIF_ARGS \
-title "$title" -message "$message"
fi
fi
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>EnableGlobbing</key>
<false/>
<key>Label</key>
<string>notify.update.npm</string>
<key>ProgramArguments</key>
<array>
<string>/bin/bash</string>
<string>/Users/USERNAME/bin/notify-update-npm.sh</string>
</array>
<key>EnvironmentVariables</key>
<dict>
<key>PATH</key>
<string>/usr/local/bin:/usr/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin</string>
</dict>
<key>StartInterval</key>
<true/>
<key>StandardErrorPath</key>
<string>/tmp/notify.update.npm.err</string>
<key>StandardOutPath</key>
<string>/tmp/notify.update.npm.out</string>
<key>StartCalendarInterval</key>
<array>
<dict>
<key>Hour</key>
<integer>15</integer>
<key>Minute</key>
<integer>0</integer>
</dict>
</array>
</dict>
</plist>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment