Javascript for command line utilities

Given the need to write command line utilities to perform common tasks, such as uploading files to a remote FTP site, downloading data from a remote MySQL database, etc.

How practical is using JavaScript for this kind of thing? I know that there are JavaScript interpreters that can be launched from the command line, but are there libraries for things like FTP and access to the database as it is, for example. Java? If so, what is the best place to find them? (Google search queries using JavaScript in keywords always return many pages of specific browsers.)

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

Update: I decided that Python is the best tool for this kind of work, but the answers to the original question are still good.

+6
javascript command-line
source share
6 answers

Node.js is the best environment for running non-browser JS. I used Rhino and SpiderMonkey, and there is a huge difference in everything, starting with the basics, for example, how errors are handled with community size using this tool. Node is broken down into server-side JS server-side applications in JS. This is great for that. But it works equally well for creating command line tools.

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

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

There are packages such as commander.js for args analysis. I am currently using a highly enhanced version of Commander in my underscore-cli command line tool.

For messaging with JSON or for doing JS command line work (similar to "perl -pe"), underscore-cli - This is a really powerful tool for processing JSON data, processing underscore patterns, and running JS expressions from the command line. I use it for 1001 different things that otherwise would be really unpleasant to achieve.

+6
source share

Standalone executable file?

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

I use Javascript this way for various utilities on Windows.

I think your instinct is right to access the libraries. You yourself can find all these things. Although, once you find them, it’s easy to pack the Javascript libraries as COM components and allow reuse from anywhere. See here an example of packaging the Google Diff / Patch / Match Javascript library in COM.

Addition . Once the COM code is available in COM, it can be used by any Javascript running on the computer. Some examples of COM objects available for Javascript running in WSH:

+8
source share

You can use Rhino to compile Javascript into Java bytecode and access all Java libraries.

Or you can use JScript.net and access the .net..net libraries including jsc.exe, which creates exe files.

Both of these require that the appropriate infrastructure be installed to be able to run.

+7
source share

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

We use it from the command line. It is very good.

+4
source share

One way is to write these utilities as AIR applications. They can be written in JavaScript and should not have a user interface. They have command line access, 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 the sqlite database.

+1
source share

jslibs is a good stand-alone JavaScript runtime that supports many third-party open source libraries such as zlib, SQLite, NSPR, libiconv, libTomCrypt, OpenGL, ...

+1
source share

All Articles