6

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.

rwallace
  • 31,405
  • 40
  • 123
  • 242

6 Answers6

8

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:

Community
  • 1
  • 1
Cheeso
  • 189,189
  • 101
  • 473
  • 713
7

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.

Svante Svenson
  • 12,315
  • 4
  • 41
  • 45
6

Node.js is by far the best environment for running non-browser JS. I've used Rhino and SpiderMonkey, and there's a pretty huge difference in everything from the basics like how errors are handled to the size of the community using the tool. Node is pitched for "server-side" JS - building server apps in JS. It's great for this. But it works equally well for building command line tools.

The NPM package manager (bundled with Node) provides a nice global directory for finding and installing packages. It works much better than other language equivalents like PECL / Pear / CPAN / etc. Several high quality tools like JSHint, The Jade templating language, and the CoffeeScript compiler are all already available through NPM/Node:

npm install -g jshint, coffee-script, jade
jshint my_code.js
jade < my.jade > my.html

For args parsing, there are packages like commander.js. I currently use a heavily extended version of Commander in my underscore-cli command-line tool.

For messing with JSON or for doing command-line JS work (similar to "perl -pe"), check out underscore-cli - It's a really powerful tool for processing JSON data, processing underscore templates, and running JS expressions from the command-line. I use it for 1001 different things that would otherwise be really annoying to accomplish.

Dave Dopson
  • 41,600
  • 19
  • 95
  • 85
  • Why do you say npm is better than cpan? – Gringo Suave Dec 26 '12 at 22:36
  • Specific to CPAN, one gripe is that it wants me to walk through a 15 step "setup" prior to use ... just silly. From a usability / CLI perspective, npm is more pleasant. As for more substantial philosophical deltas... npm handles recursive dependencies quite differently. With PERL (and ruby and most other scripting languages), you have a global module path and it gets tricky to use modules A and B if they each require a different version of module C. npm handles this well, though it downloads full transitive dependency closure each time you install a module (also silly). – Dave Dopson Dec 31 '12 at 02:07
  • Ok, thanks for the background. I've only used python pip extensively, npm once or twice. cpan was always well regarded by the slashdot crowd however. – Gringo Suave Jan 01 '13 at 21:42
  • How does npm "work much better than CPAN"? npmjs.org is a mess (try picking a CSV module - you'll get a ton of search results, out of which 80% are irrelevant or just crap) because it has no ratings or comments feature. I filed an [issue about that on GitHub](https://github.com/isaacs/npm-www/issues/298). To address your gripe about the setup steps: use [cpanm](http://cpanratings.perl.org/dist/App-cpanminus). No setup required. – Dan Dascalescu Apr 29 '13 at 02:21
4

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
  • 15,820
  • 14
  • 59
  • 86
1

jslibs is a good standalone JavaScript runtime that support many 3rd party open source libraries like zlib, SQLite, NSPR, libiconv, libTomCrypt, OpenGL, ...

Franck Freiburger
  • 26,310
  • 20
  • 70
  • 95
1

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
  • 11
  • 2