In yesterday’s post Getting started with the CLI and backing up your scripts (Day 4) we looked at how you can use the new Azure Cross-Platform CLI on Windows, Mac and Linux to work interactively using the command line and automate certain scenarios.
In today’s post I want to push that scenario a little harder and understand how this might change our workflow when writing Mobile Services scripts. The post yesterday showed how to use bash to backup your server scripts, how to install Node and use NPM. Today, we’re going to use a small node program to allow us to work on scripts using our favorite text editor and automatically upload my scripts whenever I make changes to the files locally.
The idea is simple. We create a node script that pulls all scripts down to a given directory (like yesterday, but we’ll use NodeJS) and then watches that file system for any changes. If a .js file changes we’ll upload it back to the server.
Before you do this, you’ll need to install node and import your .publishsettings file so you’re ready to start scripting – the steps are detailed in yesterday’s post.
Here’s the complete node script that we’ll be using today:
In order to run this node program you’ll need to pull in Mikael’s watch module. Of course, you can use NPM to do this: npm install –g watch
The –g switch will install globally so you can easily use the module anywhere. Then, you’re ready to go. Save the app.js file somewhere and launch it with the following command line arguments:
node app.js servicename /Directory/To/Files
The two arguments are
- The name of the Mobile Service
- The folder, into which the table scripts should be downloaded and monitored.
Note this also works identically on Windows and Mac:
Once the files have been downloaded (they will be downloaded each time and will replace any existing files) you can open the file up in your favourite text editor, like Sublime 2:
And now, whenever I save a change to this file it will be automatically uploaded to the Mobile Service ready for testing. This is cool, and a big boost for those who want to edit files locally.
As an exercise for the reader, it would also be very easy to setup git with a bare repository and a hook that pulls the latest files into another directory. You could watch this directory in much the same way as I demonstrated with this script, giving you yet another workflow:
Work on files => push to git repo => automatically push to Mobile Service
I'd think of this code as nothing more than experimental in quality so use at your own risk, of course.
Enjoy! I hope this wasn’t the best Christmas present you received today (for those to whom that particular holiday applies) but I hope you like this all the same. Join us again tomorrow for another post as part of the twelve days of ZUMO.