Shonzilla, a pattern-seeking animal

Life is a game of patterns and chance, and those who play well will win.

Twitter

Sun Oct 31

Command-line JavaScript beautifier implemented in JavaScript

Intro

In the the world of web programming where more things gets done in JavaScript, most of this JavaScript is directly available online. An exception is the server-side JavaScript like the one used in node.js or, less commonly known, in a OpenSocial container like Apache Shindig. Unless the license says otherwise, all this JavaScript code is free for anyone to download, fiddle with and fork/hack/improve/refactor.

For different reasons, the code is frequently post-processed in a way making it impossible to read. Enter JavaScript beautifiers. There’s plenty of them. However, they’re all on the web. Have you asked yourself “is there’s no command-line JavaScript beautifier?" so that you can most of the work from hacker’s favorite development environment. If you were looking for CLI JavaScript beautifier and haven’t found one, you came to the right page.

Ingredients

Here’s the DIY guide on how to create your own CLI JavaScript beautifier.

First, you’ll need a command-line JavaScript interpreter. Say Mozilla Rhino, JavaScript interpreter implemented in Java. I’ll assume you already have a working Java installation on your machine. Since Rhino works with any JDK 1.4 or higher, if you have any Java on your machine - you’re surely fine already.

Secondly, JavaScript beautifying itself is implemented in JavaScript just as most of the online JavaScript beautifiers rely on client-side process implemented JavaScipt. This JavaScript piece of the puzzle fits perfectly in the previous one - the JavaScript interpreter. I recommend jsbeautifier.org - JavaScript unpacker and beautifier since it’s the only I’ll use here as an example.

The last piece of the puzzle is a shell script to tie it all together.

DIY guide

Follow this steps to DIY:

  1. Download the latest stable Rhino and unpack it somewhere, e.g. ~/dev/javascript/rhino
  2. Download beautify.js which is referenced from aforementioned jsbeautifier.org then copy it somewhere, e.g. ~/dev/javascript/bin/cli-beautifier.js
  3. Add this at the end of beautify.js (using some additional top-level properties to JavaScript):
    // Run the beautifier on the file passed as the first argument. print( j23s_beautify( readFile( arguments[0] )));
  4. Copy-paste the following code in an executable file, e.g. ~/dev/javascript/bin/jsbeautifier.sh:

    #!/bin/sh

    java -cp ~/dev/javascript/rhino/js.jar org.mozilla.javascript.tools.shell.Main ~/dev/web/javascript/bin/cli-beautifier.js $*

  5. (optional)  add the folder with jsbeautifier.js to PATH or moving to some folder already there.

Usage

If you have the above script (jsbeautifier.sh) in PATH like I do, you can run it from any folder to format a JavaScript or JSON file by adding the file as the first argument: 

jsbeautifier.sh some-minified.js

where you replace some-minified.js with some JavaScript or JSON file that no normal web developer wants to read.

Outro

I see no reason why wouldn’t the same technique repurposed for other JavaScript-based services currently only found online. Some ideas come to mind: JavaScript comressing/uglifying… maybe even running YQL from the command-line.

Note that I haven’t actually tried those nor checked if there are stronger dependencies on the client-side requiring more work to repurpose the JavaScript code from the web browser into Rhino runtime.

  1. shonzilla posted this