Monday, May 1, 2017

How to Write Shell Scripts with JavaScript

This week I had to upgrade a client's website to use SSL. This wasn't a difficult task in itself — installing the certificate was just the click of a button — yet once I had made the switch, I was left with a lot of mixed content warnings. Part of fixing these meant that I had to go through the theme directory (it was a WordPress site) and identify all of the files in which assets were being included via HTTP.

Previously, I would have used a small Ruby script to automate this. Ruby was the first programming language I learned and is ideally suited to such tasks. However, we recently published an article on using Node to create a command-line interface. This article served to remind me that JavaScript has long since grown beyond the browser and can (amongst many other things) be used to great effect for desktop scripting.

In the rest of this post, I'll explain how to use JavaScript to recursively iterate over the files in a directory and to identify any occurrences of a specified string. I'll also offer a gentle introduction to writing shell scripts in JavaScript and put you on the road to writing your own.

Set Up

The only prerequisite here is Node.js. If you don't have this installed already, you can head over to their website and download one of the binaries. Alternatively, you can use a version manager such as nvm. We've got a tutorial on that here.

Getting Started

So where to begin? The first thing we need to do is iterate over all of the files in the theme directory. Luckily Node's native File System module comes with a readdir method we can use for that. It takes the directory path and a callback function as parameters. The callback gets two arguments (err and entries) where entries is an array of the names of the entries in the directory excluding . and .. — the current directory and the parent directory, respectively.

const fs = require('fs');

function buildTree(startPath) {
  fs.readdir(startPath, (err, entries) => {
    console.log(entries);
  });
}

buildTree('/home/jim/Desktop/theme');

If you're following along with this, save the above in a file named search_and_replace.js and run it from the command line using node search_and_replace.js. You'll also need to adjust the path to whichever directory you are using.

Adding Recursion

So far so good! The above script logs the directory's top level entries to the console, but my theme folder contained subdirectories which also had files that needed processing. That means that we need to iterate over the array of entries and have the function call itself for any directories it encounters.

To do this, we first need to work out if we are dealing with a directory. Luckily the File System module has a method for that, too: lstatSync. This returns an fs.Stats object, which itself has an isDirectory method. This method returns true or false accordingly.

Continue reading %How to Write Shell Scripts with JavaScript%


by James Hibbard via SitePoint

No comments:

Post a Comment