I finally got fed-up today with having to use online javascript beautifiers whenever I needed to re-format a minified javascript file to make it readable. I often need to reformat a minified script to have a look at some part of it and thought it would be great if a utility can quickly format the script through command-line.

So, I went out today searching for such a utility supporting command-line mode of operation for the beautification process. However everything I came across provided an online option for beautification (optionally with the source code, that could be used offline also from the local machine). But none supported the command-line way of doing things I was looking for. So, I decided to use one of the existing beautification utilities and create a wrapper for it supporting both interactive as well as command-line mode.

I found js-beautify (https://github.com/einars/js-beautify) a popular utility for the task and decided to use it to create my wrapper for the command-line. Although the beautifier itself ships with various ways of executing it from the command-line, but all those ways needed (a bit hefty) installations and configuration, before you can use them from the command-line.

I ended up creating jsBeautifier.net (https://github.com/rhoney/jsBeautifier.net).

jsBeautifier.net is a simple wrapper created in .NET 2.0 for js-beautify. js-beautify does all its work in javascript and provides some handy options to control the format of the beautified script. My .Net wrapper allows you regular interactive access with exactly the same User Interface as the original beautifier.

But additionally, it supports a zero-configuration command-line mode for automating the beautification tasks from command-line. An example command-line invocation of the wrapper is as follows:

 

jsBeautifier.net "sourceFile=C:\TextToJs.txt" "destinationFile=C:\TextToJs1.txt" "indent=2" "bracesInNewLine=false" "preserveEmptyLines=true" "detectPackers=false" "keepArrayIndent=false"

The attached file contains examples of executing jsBeautifier.net from regular command line we well as from PowerShell, together with the full source and binary for the jsBeautifier.net.

The wrapper supports all options of the original js-beautify to be passed as command-line parameters.

It has been created in .NET 2.0 (and has no external dependency that you need to download separately), so should readily run in all versions of Windows supported by .Net 2.0 (i.e. Windows 98 and later). If you are on Windows Vista or later, you only need to download the attached file(Vista and later come with .Net 2.0) and run jsBeautifier.net.exe with appropriate parameters from command-line or execute it as a normal Desktop application for interactive use. Windows XP and earlier users might need to verify if they have .NET 2.0 installed, and would need to install it first if not available already.

If you are a Mono user on a non-Windows OS, then jsBeautifier.net would not work for you as it has dependency on components not available in Mono on non-Windows platforms (but should work in Mono on Windows platform although I haven’t tested this).

The 1.0 release of this utility is attached with this blog post. To keep up with the changes for this utility, you can watch it on its GitHub page here.