views:

562

answers:

4

Given a need to write command line utilities to do common tasks like uploading files to a remote FTP site, downloading data from a remote MySQL database etc.

Is it practical to use JavaScript for this sort of thing? I know there are JavaScript interpreters that can be run from the command line, but are there libraries for things like FTP and database access the way there are for e.g. Java? If so, what's the best place to look for them? (Google searches with JavaScript in the keywords always seem to return many pages of browser specific things.)

And is there a way to package a JavaScript program up as a standalone executable on Windows?

Update: I've decided Python is a better tool for this kind of job, but the answers to the original question are still good ones.

+5  A: 

You can use Rhino to compile Javascript into Java byte code, and get access to all Java libraries.

Or you could use JScript.net, and get access to the .net libraries. .net includes a jsc.exe that produces exe-files.

Both of these requires the respective framework to be installed to be able to run.

svinto
+2  A: 

One way is to write these utilities as AIR applications - They can be written in JavaScript and need not have a UI. They have access to the command line, and there are existing ActionScript 3 libraries that can handle FTP etc. These ActionScript APIs can be called from JS, in AIR applications. AIR applications also have access to a sqlite database.

zorder
+5  A: 

Standalone executable?

By the way you ask the question, I'm not sure if you are aware, but the Windows Script Host - included in Windows - allows you to run .js files from the command-line. Your javascript will not be an executable, it will remain a script, a text file. The script runs within cscript.exe, which is provided by WSH. There's no compilation required. Maybe you knew all that.

I use Javascript this way for various utilities on Windows.

I think your instinct is right on the availability of libraries. You are sort of on your own to find all those things. Although, once you find them, it's not hard to package Javascript libraries as COM components and allow re-use from anywhere. See here for an example of packaging the Google Diff/Patch/Match Javascript library in COM.

Addendum: Once a bit of code is available within COM, it can be consumed by any Javascript running on the machine. Some examples of COM objects available to Javascript scripts running in WSH:

Cheeso
+2  A: 

Rhino is bundled with JDK 1.6, jrunscript.exe in the bin directory will allow you to run any Javascript you want. Since it runs under Java you get access to any Java libraries that you may have.

We use it from the command line extensively. It's very good at that.

Tom Hubbard