Command line JavaScript code that runs on Windows and Linux

I am looking for code that supports JavaScript and works on both Windows and Linux and can be used in batch scripts. Any recommendations?

+82
javascript command-line-interface pretty-print beautifier multiplatform
Aug 20 '08 at 22:29
source share
17 answers

First, select your favorite Pretty Print / Beautifier JScript code. I prefer one in http://jsbeautifier.org/ because this is what I found first. Loads its file https://github.com/beautify-web/js-beautify/blob/master/js/lib/beautify.js

Second, download and install the Java-based Mozilla Javascript module, Rhino . โ€œInstallโ€ is a bit misleading; Download the zip file, extract everything, put js.jar in your path to Java (or Library / Java / Extensions in OS X). Then you can run scripts with a call like this

java -cp js.jar org.mozilla.javascript.tools.shell.Main name-of-script.js 

Use Pretty Print / Beautifier from step 1 to write a small shell script that will be read in your javascript file and run it through Pretty Print / Beautifier from the first step. for example

 //original code (function() { ... js_beautify code ... }()); //new code print(global.js_beautify(readFile(arguments[0]))); 

Rhino provides javascript with several additional useful functions that do not necessarily make sense in the context of the browser, but are executed in the context of the console. The print function does what you expect and prints a line. The readFile function takes a file path string as an argument and returns the contents of this file.

Would you call something like

 java -cp js.jar org.mozilla.javascript.tools.shell.Main beautify.js file-to-pp.js 

You can mix and match Java and Javascript in Rhino startup scripts, so if you know a little Java, it is not too difficult to get this to work with text streams.

+59
Aug 26 '08 at 3:22
source share

If you are using nodejs try uglify-js

In Ubuntu 12.04, if you have already installed nodejs, you can install uglify with:

sudo npm install -g uglify-js

And then get the following options:

uglifyjs -h

So, if I have the source file foo.js that looks like this:

 // foo.js -- minified function foo(bar,baz){console.log("something something");return true;} 

I can decorate it like this:

uglifyjs foo.js --beautify --output cutefoo.js

uglify uses spaces for indentation by default, so if I want to convert 4-space indentation to tabs, I can run it through unexpand that Ubuntu 12.04 comes with:

unexpand --tabs=4 cutefoo.js > cuterfoo.js

Or you can do it all in one go:

uglifyjs foo.js --beautify | unexpand --tabs=4 > cutestfoo.js

You can learn more about unpand here.

so after all this I end up with a file that looks like this:

 function foo(bar, baz) { console.log("something something"); return true; } 

update 2016-06-07

It seems that supporting uglify-js is now working on version 2 , although the installation is the same.

+21
May 12 '13 at 23:55
source share

UPDATE April 2014 :

The designer has been rewritten since I answered this in 2010. There is a python module, the npm package for nodejs and the jar file has disappeared. Please read the project page on github.com .

Python style:

  $ pip install jsbeautifier 

NPM Style:

  $ npm -g install js-beautify 

to use it:

  $ js-beautify file.js 

Original answer

Adding to @Alan Storm's answer

decryption of the command line based on http://jsbeautifier.org/ has become a little easier to use, because now it (alternatively) is based on the V8 javascript engine (C ++ code) instead of the rhino (java-JS engine, packaged as "js. jar "). So you can use the V8 instead of the rhino.

How to use:

download jsbeautifier.org zip file from http://github.com/einars/js-beautify/zipball/master

(this is the download URL associated with the zip file, for example http://download.github.com/einars-js-beautify-10384df.zip )

old (no longer working, the jar file is missing)

  java -jar js.jar name-of-script.js 

new (alternative)

install / compile v8 lib FROM svn, see v8 / README.txt in the above zip file

  ./jsbeautify somefile.js 

- has slightly different command line options than the rhino version,

- and works great in Eclipse when configured as an "external tool"

+13
Oct 28 '10 at 9:38 on
source share

My Pretty Diff tool is fully written in JavaScript, so it works equally well on all operating systems. It supports decorating and minimizing JavaScript, CSS, and any markup language that uses XML style delimiters, including HTML.

http://prettydiff.com/?m=beautify

+11
Aug 22 '09 at 12:33
source share

So far I have found a couple of online services:

Still looking for something that I can run from the command line.

+6
Aug 22 '08 at 0:20
source share

I cannot add a comment to the accepted answer, so why do you see a message that should not have existed in the first place.

Basically, I also need a javascript beautifier in java code, and, to my surprise, none of them are available as far as I could find. Therefore, I myself encoded it completely based on the accepted answer (it wraps a jsbeautifier.org beautifier.js script, but can be called from java or the command line).

The code is at https://github.com/belgampaul/JsBeautifier

I used rhino and beautifier.js

USAGE from console: java -jar jsbeautifier.jar script indentation

example: java -jar jsbeautifier.jar "function ff () {return;}" 2

USE from java code: public static String jsBeautify (String jsCode, int indentSize)

You can extend the code. In my case, I needed only an indent so that I could check the generated javascript during development.

In the hope that this will save you some time in your project.

+1
Jan 27 '13 at 7:31
source share

I wrote an article explaining how to build a JavaScript command line constructor implemented in JavaScript in less than 5 minutes. YMMV.

  • Download the latest stable Rhino and unzip it somewhere, for example. ~ / Dev / javascript / rhino
  • Download beautify.js, referenced by the above jsbeautifier.org, then copy it somewhere, for example. ~ / DEV / JavaScript / bin / CLI-beautifier.js
  • Add this to the end of beautify.js (using some additional top-level properties for JavaScript):

     // Run the beautifier on the file passed as the first argument. print( j23s_beautify( readFile( arguments[0] ))); 
  • Copy paste the following code into the executable, for example. ~ / 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 $* 
  • (optional) Add a folder with jsbeautifier.js in PATH or move to some folder already there.

0
Oct 31 '10 at 22:08
source share

This is a very useful website to decorate / set aside your js files. I often use it. Check this out: http://jsbeautifier.org/

0
Nov 26 '10 at 6:03
source share

Here, the javascript beautifier is written in .NET, which supports the command line, as well as the interactive mode: http://www.rahulsingla.com/blog/2010/12/jsbeautifier-net-javascript-beautifier-in-net

No external dependency other than .Net 2.0.

0
Dec 23 '10 at 6:16
source share

I believe that when you asked about the command line tool, you just wanted to decorate all your js files in batch mode.

In this case, Intellij IDEA (tested since 11.5) can do this.

You just need to select any of your project files and select "Code" โ†’ "Convert Code .." in the IDE main menu. Then in the dialog box, select "all files in the directory ..." and press "enter". Just make sure you allocate enough memory for the JVM.

0
Aug 20 '13 at 19:20
source share

In the console, you can use Artistic Style (aka AStyle) with --mode=java .
It works great, it's free, open source and cross-platform (Linux, Mac OS X, Windows).

0
Feb 16 '14 at 2:05
source share

Use the modern JavaScript method:

Use Grunt in conjunction with the jsbeautifier plugin for Grunt

You can easily install everything in your environment using npm .

All you need to do is set up Gruntfile.js with related tasks, which may also include merging files, lint, uglify, minify, etc. and executing the grunt command.

0
Sep 15 '14 at 19:49
source share

The problem with highlighting jewelry is the choice of output data and filtering, as well as the absence of serious command line clarifiers. I recently started modifying the YUI compressor to decorate the code. If you want to make your own decorator, I should highly recommend this approach. YUI is open source and comes with an ant build file, making it easy to modify and compile.

-one
Jan 19 '10 at 19:36
source share

I am using this ( http://jsbeautifier.atomproject.net/ ) beautifier. Its online and free.

-one
Jun 11. '12 at 19:50
source share

If you use notepad ++, here is the plugin for notepad ++ http://www.sunjw.us/jsminnpp/

-one
Jul 10 '13 at 7:23
source share

Try the notepad ++ JSMinNpp plugin (Changed the name for JSTool from version 1.15)

http://www.sunjw.us/jsminnpp/

-one
Jun 27 '15 at 10:37
source share

This is a free standalone decoder (and much more):

http://www.yaldex.com/Free_JavaScript_Editor.htm

-3
Jul 21 '09 at 14:10
source share



All Articles