How to Create a macOS Menu Bar App for Netlify

In case you don’t already know, I like Netlify. And I build on it quite a lot.

A while back, I was scrolling through my twitter feed and saw this project tweeted about by Phil Hawksworth:

Screenshot of lekoarts_de’s Netlify status board

I really like this. I've been meaning to make my own dashboard showing the status any of my sites that I care about by just including a list of @Netlify deploy badges.

https://www.netlify.com/blog/2019/01/2...

But @lekoarts_de beat me to it with this nice example.

Similar to Phil, a tool of some sort showing the build status of all my sites on Netlify was a fun project idea I’d had in the back of my mind for a couple months. It was cool to see somebody else execute on it so beautifully.

Not only can you checkout a live demo of the project, but you can also read/view/fork the code behind it to get your own dashboard! So why try and make anything better than that?

Well I tried to make something a little different. If you’re a Mac user, what if you had a menu bar app that could show you similar information? Namely, what’s the status of the latest build for my sites on Netlify?

That’s what I set out to do.

A Netlify Menu Bar App for Mac

First of all, it’s worth noting that I built this on top of BitBar. So what does it look like?

By default you get a nice little Netlify logo in your menu bar.

Screenshot of Netlify app in my Mac’s menu bar

What’s neat is BitBar has instructions on how to build your menu bar app so it accommodates things like retina screens and dark mode.

Screenshot of Netlify app in my Mac’s menu bar in dark mode

BitBar also has a very simple API for indicating how often your script should run: via the file name! I set mine to run every minute (but I could make it every 30, 15, or 5 seconds if I wanted). Each time it runs, it asks Netlify for all my sites and the latest build status of each. If there’s a build in progress (or one failed), I get a number next to the menu bar icon, a kind of notification if you will.

Screenshot of Netlify app in my Mac’s menu bar with a notification

When I click on the menu bar app, I get a native dropdown which displays all my sites (I decided to group them by domain) along with a little status light indicator to show the build status:

Screenshot of Netlify menu bar app dropdown

Clicking on any of the sites listed in the dropdown will take you to that particular site in Netlify.

So putting it all together, you get something like this:

Animated gif depicting how the Netlify menu bar app works

What’s really neat about is how flexible BitBar actually is. If you wanted, you have each site be its own flyout menu in the menu bar and have it display all kinds of meta info—build status, build time, site url, netlify site admin url, etc—you could! The sky’s the limit.

Under the Hood

So how does all of this work exactly? Good question.

Because I wanted a native menu bar app for my Netlify sites, I thought for a second about jumping into Xcode. But I just didn’t have the time for that.

“There’s got to be a way to write JavaScript for OS X” I thought, “like an Electron app but for the Mac’s menubar.” Then I found BitBar: an app that let’s you “put the output from any script or program in your Mac OS X Menu Bar”. Boom! Precisely what I was looking for. “Netlify has an API” I thought, “I bet I could write a node script that talked to Netlify and translated the response to something BitBar expects in order to render a menu bar app!” A little while later and I had exactly what I wanted. Here’s how it works.

First you have to install BitBar on your Mac. Once installed, you’ll get a directory where you can put individual scripts (of all kinds might I add, like PHP, JavaScript, Ruby, Go, etc). Those scripts get turned into individual menu bar apps.

In my case, I have a netlify.1m.js file in my BitBar scripts folder (the 1m in the filename tells bitbar how often to refresh my script, in this case every 1 minute). In addition, since my script is JavaScript (node) I have to put a shebang at the front of my file denoting where my node executable is.

So, to get started, you have a file like myFile.5m.js which looks like this:

#!/usr/bin/env /path/to/the/node/executable

/*
  Write your javascript here.
  Anything you console.log() out is what ends up in your menubar.
*/

BitBar has an API you can work with to console.log() a string of text which will render your menu bar app. There’s also a bitbar module that allows to construct your bitbar app with a nice API vs. concatenating a big string. While it’s a cool idea, I wanted to write my BitBar script such that it had zero dependencies. So I didn’t use that module (or any npm modules for that matter).

So what does my script look like? Well my script is pretty custom to my setup in Netlify. I grouped my Netlify sites in the menu bar based on their individual characteristics and what made sense to me, which may not make sense to you.

So, instead of just giving you my script, I put together this simple script to get you started. It has zero dependencies and just calls the Netlify API to pull all your sites and the latest build for each of those sites.

Screenshot of Netlify menu bar app using the example script

Note: this script accesses the Netlify API and therefore requires an access token. You’ll have to follow their instructions to get one and put in the script below where it says YOUR_TOKEN_HERE.

Second Note: this script probably has issues. I am imperfect and so is my code. I may not even be using the Netlify API correctly. If for any reason you find an issue with this script and want to notify me of it, feel free to reach out. This is not meant to be a product. It’s meant to be a hacky starting point for your use in building things that are much better.

#!/usr/bin/env /usr/local/bin/node
const https = require("https");
const url = require("url");

// All my images as base64 strings
const imgLogo =
  "iVBORw0KGgoAAAANSUhEUgAAACQAAAAkCAYAAADhAJiYAAAAAXNSR0IAr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hwYWNrZXQgZW5kPSJyIj8+7SjG7QAAAgpJREFUeNokkU1oE0EYht+Zne0mm3SzJtmQphJiC6Vp1RaF+lNB/AcPnqSleuxRPIiU0pvXHtSD1FtPiiJ48uBJC2JF8CD1UFoEE9KsIbFJ2jTs5m9/nN0MDHy83/vM984McV0X3nqx+kje2nz/IEDNeVHojHtaz5Z22478bvrS3bWHy89NTyMcII8Xr56wDn59GE0Yk+mYBVV2/EMOTYq9GsOff6FtdmzqztP1jbxw9mRK0bfefp7JNCevzJ7H6VtPkLm4hPSZRWSylzEcqEBo5RKVauNmoyO9FtLBwtK0Vpk/l+4hcWoBtGfAOcr5m3QOEFLTUBtfYDuO9nO7YLCYWJ+b0GxEJAJ7Zx2OPAwiyn4kt2fCNUt+Lxu3UWzW51g8YGSTig2RMhC7Axg6QEUfIE4P4BrlvaRigXvHWZjTQZGBiSFgQAEZUAHWnwDLgNttAN0jBBwTssRsBlnLdYXKmBSKgIRSIMEhDqpeIG489CO5pvfENogc3WOITW3UrE9jkQgHBodAwqOAFOtPaO/DlTgqmqjVD0G4l167/+xV3tSK1bYJqqgcioGGExzU+rUSwX7bQN7Qip6XRrXjhZHrayu7ZaqX9K/odvNwaYtfvO3XJX0Tv8uCPnLj5Yrn9X46wIenzGZ94sfH5Xus9X1WotWkl6jjxMtW8MK3mdurb+TB6A6X/v4XYABO7c//Tbpc4wAAAABJRU5ErkJggg==";

// First fetch all my sites
fetchNetlify("sites")
  // Then fetch the build info for each of my sites and append the latest build
  // info into what netlify returns
  .then(sites => {
    return Promise.all(
      sites.map(site =>
        fetchNetlify(`sites/${site.site_id}/builds`).then(builds => ({
          ...site,
          ...(builds && builds[0] && builds[0].id
            ? { __latestBuild__: builds[0] }
            : {})
        }))
      )
    );
  })
  /*
    [
      {
        // `/sites` endpoint data from netlify
        id: String,
        site_id: String,
        admin_url: <String> links to netlify admin page
        url: <String> ,
        updated_at: <String> ISO8601 format

        // `/sites/:id/builds` data from netlify (the latest object)
        __latestBuild__: {
          "id": "5ceff7f99c0ff20174f10387",
          "deploy_id": "5ceff7f99c0ff20174f10386",
          "sha": null,
          "done": false,
          "error": null,
          "created_at": "2019-05-30T15:34:17.629Z"
        }
      }
    ]
  */
  .then(sites => {
    // Ok now we have the data how we want, let's loop over it and concatenate
    // a big string for bitbar
    let log = "";
    const addToLog = str => (log = log + str + "\n");
    let notifications = 0;

    sites.forEach(site => {
      const hostname = new URL(site.url).hostname;

      let img = imgClear;
      if (site.__latestBuild__) {
        const { done, error } = site.__latestBuild__;
        if (error) {
          notifications += 1;
          img = imgRed;
        } else if (done) {
          img = imgGreen;
        } else {
          notifications += 1;
          img = imgYellow;
        }
      }
      addToLog(`${hostname} | href=${site.admin_url} image=${img}`);
    });

    // Log it all out
    logNetlifyLogo(notifications);
    console.log(log);
  })
  .catch(e => {
    logNetlifyLogo();
    console.log("Whoops, caught an error.");
    console.log(e);
  });

/**
 * Outputs just the netlify logo in the menubar
 */
function logNetlifyLogo(notifications = 0) {
  console.log(
    `${notifications > 0 ? notifications : ""}|templateImage=${imgLogo}`
  );
  console.log("---");
}

/**
 * Take a path to a Netlify API endpoint and return a promise which resolves
 * to the data of that endpoint.
 * @param {String} endpoint
 */
function fetchNetlify(endpoint) {
  return new Promise((resolve, reject) => {
    const uri = `https://api.netlify.com/api/v1/${endpoint}/?access_token=YOUR_TOKEN_HERE`;

    https
      .get(uri, res => {
        let data = "";
        // A chunk of data has been recieved.
        res.on("data", chunk => {
          data += chunk;
        });
        // The whole response has been received. Resolve it.
        res.on("end", () => {
          resolve(JSON.parse(data));
        });
      })
      .on("error", err => {
        reject("Error: " + err.message);
      });
  });
}