Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript for command line utilities

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.

like image 644
rwallace Avatar asked May 13 '09 09:05

rwallace


1 Answers

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:

  • MSXML2.XMLHTTP object - used in AJAX, but can be used for any HTTP communication. There also an object for the XSLT engine so you can do transforms from script.
  • Excel.Application - allows you to open up Excel spreadsheets and automate them from Javascript.
  • Communicator.UIAutomation - automate MS Communicator (send IM's via script)
  • COM objects for Google Earth.
  • SlowAES - an all-Javascript implementation of AES encryption.
like image 161
Cheeso Avatar answered Oct 02 '22 18:10

Cheeso