Second, download and install The Mozilla group's Java based Javascript engine, Rhino. "Install" is a little bit misleading; Download the zip file, extract everything, place js.jar in your Java classpath (or Library/Java/Extensions on OS X). You can then run scripts with an invocation similar to this
Use the Pretty Print/Beautifier from step 1 to write a small shell script that will read in your javascript file and run it through the Pretty Print/Beautifier from step one. For example
Rhino gives javascript a few extra useful functions that don't necessarily make sense in a browser context, but do in a console context. The function print does what you'd expect, and prints out a string. The function readFile accepts a file path string as an argument and returns the contents of that file.
You can mix and match Java and Javascript in your Rhino run scripts, so if you know a little Java it shouldn't be too hard to get this running with text-streams as well.
The beautifier has been rewritten since I answered this in 2010. There is now a python module in there, an npm Package for nodejs, and the jar file is gone. Please read the project page on github.com.
Python style:
$ pip install jsbeautifier
NPM style:
$ npm -g install js-beautify
to use it (this will return the beatified js file on the terminal, the main file remains unchanged):
$ js-beautify file.js
To make the changes take effect on the file, you should use this command:
$ js-beautify -r file.js
Original answer
Adding to Answer of @Alan Storm
the command line beautifier based on http://jsbeautifier.org/ has gotten a bit easier to use, because it is now (alternatively) based on the V8 javascript engine (c++ code) instead of rhino (java-based JS engine, packaged as "js.jar"). So you can use V8 instead of rhino.
I'm not able to add a comment to the accepted answer so that's why you see a post that should have not existed in the first place.
Basically I also needed a javascript beautifier in a java code and to my surprise none is available as far as I could find. So I coded one myself entirely based on the accepted answer (it wraps the jsbeautifier.org beautifier .js script but is callable from java or the command line).
On Linux or Mac, assuming you already have nodejs installed, you can install uglify with:
sudo npm install -g uglify-js
And then get the options:
uglifyjs -h
So if I have a source file foo.js which looks like this:
// foo.js -- minified
function foo(bar,baz){console.log("something something");return true;}
I can beautify it like so:
uglifyjs foo.js --beautify --output cutefoo.js
uglify uses spaces for indentation by default so if I want to convert the 4-space-indentation to tabs I can run it through unexpand which Ubuntu 12.04 comes with:
I believe when you asked about command line tool you just wanted to beautify all your js files in batch.
In this case Intellij IDEA (tested with 11.5) can do this.
You just need to select any of your project files and select "Code"->"Reformat code.." in main IDE menu. Then in the dialog select "all files in directory ..." and press "enter".
Just make sure you dedicated enough memory for the JVM.
In the console, you can use Artistic Style (a.k.a. AStyle) with --mode=java.
It works great and it's free, open-source and cross-platform (Linux, Mac OS X, Windows).
You can install everything easily into your dev environment using npm.
All you will need is set up a Gruntfile.js with the appropriate tasks, which can also involve file concatenation, lint, uglify, minify etc, and run the grunt command.