JavaScript on the Command Line

QuickJS gains new cmake build system for Windows, Linux and MacOS

Programmer holding a big coffee mug

Image based on Open Doodles.

HOLLYWOOD, CA (goshrobin.com) 2022/11/29 – QuickJS is a JavaScript engine primarily intended for embedding in other programs. It also offers a command-line interpreter and a JS compiler that makes scripts into executables. To evaluate JavaScript as a scripting engine for CinePaint, I’ve forked three JavaScript engines: QuickJS, mJS and TinyJS.

QuickJS was originally on Linux then ported to Windows. That fork uses premake. I prefer using cmake, so I created a new cmake build system for QuickJS.

QuickJS running in Win64 gitbash terminal

QuickJS running in Win64 gitbash terminal

With a 1MB executable, QuickJS is bigger than mJS which is intended for micro-controllers or TinyJS. An interesting extension in QuickJS is support for C-style stdio file operations. That capability makes QuickJS an interesting candidate for file handling tasks that we’d typically program in bash, Perl or Python scripts.

QuickJS enables Web programmers familiar with JavaScript to avoid needing another programming language for command-line tasks. Although I’ve used 25 programming languages professionally, including bash, when I need to write a quick bash script I never quite remember the syntax and must pause to look it up.

One of my colleagues is going to write a QuickJS script to do some book edits for me.

Command-line Scripting with JavaScript

As a test of using QuickJS, produce a command-line file manipulation script that would typically be done using Python, Perl or bash. To do that, we’ll take some badly formatted HTML files and reformat them like so…

  • git clone https://github.com/jalbertbowden/grokking-the-gimp
  • Rename annoying image original/background.gif to anything else (to block displaying it in html)
  • Write JS script to cat append from node1.html to node90.html to create a single grokking.html file that combines contents of all the nodeN.html files (See std module for file handling calls https://bellard.org/quickjs/quickjs.html#Extensions)
  • Write JS script that reads one.html file line by line and outputs grokking-edit.html file that has removed any text between these lines:
     <!--Navigation Panel-->
     <!--End of Navigation Panel-->

Because the QuickJS engine isn’t embedded in HTML, there is no DOM to call like with the example https://linuxhint.com/read-file-line-line-javascript/. Instead, we use std library calls in JavaScript, similar to how file handling is done in C with https://stackoverflow.com/questions/3501338/c-read-file-line-by-line.

Will see what happens…

About Robin Rowe

Robin Rowe

I’m Robin Rowe. People call me Robin or Rob, sometimes professor. I work in advanced product design and innovation management. AI, metaverse, robotics, safety-critical real-time systems, visual effects software for major motion pictures, animation software for a hit cartoon series and guide other engineering projects. Such as the United Nations…

The UN World Health Organization faced a problem with certifying doctors. COVID-19 was blocking WHO doctor-instructors from traveling to local hospitals around the world to train and certify their doctors. Working for the UN WHO, Robin Rowe designed MCARE, a medical metaverse, a digital twins simulation of the world’s hospitals.

“Hospitals, the factory floor, airports, anywhere that has real world complexity and logistics, that’s the obvious business case for building a metaverse,” says Rowe, who led the WHO Augmented Reality Group and continues to oversee MCARE as metaverse director at Heroic Robots. “There’s a great future in healthcare for hospital digital twins, where doctors can do training and new procedures can be safely researched and tested on simulated patients.”

In the future, medical suppliers will introduce doctors to new lifesaving equipment and pharmaceutical products in the MCARE metaverse where doctors can safely learn.