Skip to content

Object Oriented Shell for Web Devs

Table of contents
Table of contents

shell@brandon_j369

If you are familiar with JavaScript, you must have noticed that this language has spread in many domains. JS was initially designed for the web frontends, and quickly became parts of backend systems, with the arrival of NodeJS. Frontend developers can today use their expertise in JavaScript to contribute to the backend as well without having to learn a new programming language. The convergence of language also made it possible to easily connect backend and frontend using a common format: JSON. The simplicity and readability of JSON combined with its native support by JavaScript produced the perfect combo for the growth of the Web/API we know today.

Meanwhile, Bash, and some variants such as dash, csh, tcsh, zsh, oh-my-zsh, fish, etc., have been around for the last decades, deeply rooted in the world of system administration. Although missing from most of the countless “Most Used Programming Languages” charts, shell (Bash to be more precise) is the language that many, if not all, programmers must use from time to time. It is a unique language in that we all use it, by necessity for minor tasks (installing packages, changing permissions, etc.) although very few are experts (do you remember the syntax of an inline for loop?).

Nacre

Nacre is an open source project which combines these two worlds to provide an intuitive shell based on JavaScript. The strategy is to attract the web dev community, by offering an alternative to the shells landscape. While Bash (and Bash-like) has proven to be particularly efficient at manipulating text by encouraging the use of sed, grep, cut, awk, or format flag, Nacre has been designed to manipulate objects. These objects encourage the use of the simplest and most common formats for data exchange: JSON.

Commands returns JSONCommands returns JavaScript Object

The web community, familiar with JS, will find its feet very quickly without the need to learn a new language. In addition to this ease of adoption, a bunch of tools from the JavaScript ecosystem become instantly available. Some of which are giant projects in themselves:

  • TypeScript: make large projects more sustainable by making code more readable and robust within a rich IDE environment and by eliminating bugs quickly;
  • NPM: leverage years of contribution with thousands of packages available like lodash, chalk or acorn;
  • Chrome DevTools enable step by step debugging;
  • In addition with countless test frameworks, highlighters, linters, and so on.

So far, the product focuses a few specific aspects. First, a minimal set of builtins which facilitate the navigation within the system and provide building blocks for scripting. The usual linux commands are available, such as cat, cd, chmod, ls, mkdir, stat, etc. but embellished with preview and completion.

Use of builtinsUse of builtins

The second aspect focuses on the auto loading of JavaScript modules. Whether you are writing code or using someone else’s, having an interactive environment lets you play with functions providing the fastest feedback possible. Just by typing the name of the module, Nacre will load it to make it accessible without further effort. Such mechanisms enable web devs to reach a more “direct connection with a creation” in the words of Bret Victor. Also, the auto loading of modules extends the capabilities of Nacre with several hundred thousand modules available via NPM.

Interactive use of path module with previewInteractive use of path module with preview

Finally, Nacre emphasizes on portability by opting for JSON, a format understood by many programmers and that many programming languages can handle. It gives a more UNIX/Linux experience for those who are reluctant to use PowerShell with .NET objects. Here is an example of how you can benefit from Nacre’s commands while remaining in your favorite shell:

$ nacre --evaluate "stat('Dockerfile')"
{"name":"Dockerfile","type":"file","size":54,"createdAt":"2022-06-15T15:11:17.041Z","modifiedAt":"2022-06-15T15:11:17.041Z","owner":"arnauddebec","group":"wheel"}
$ nacre --evaluate "stat('Dockerfile').size"
54
# for those who prefer using jq
$ nacre --evaluate "stat('/tmp/Dockerfile')" | jq '.size'
54

Philosophy

The shell and its alternatives have not changed much since the 60’s compared to the rest of the computer industry. Could you count the number of languages and frameworks that you have seen born (and die) since the beginning of your career? Many alternatives of shell have chosen to be similar syntactically to facilitate their adoption. The success of Bash may be due to the fact that everything that works with Bash works similarly with sh. The benefits of being a superset are innumerable but limit some [r]evolutions, starting with backward compatibility reason.

Compact syntax is widely used in UNIX/Linux shells to speed up the writing of a command line at the expense of its readability. Nacre on the other hand favors a more explicit and readable syntax. To keep the writing smooth, energy has been invested in making the environment ergonomic. For example, ls -R is replaced by ls.recursive(), a longer version but enhanced by a completion. No more need for the programmer to mentally map flags. Since the advent of Infrastructure as Code, and more generally of the DevOps culture, system commands have a greater chance of ending up in a code base. Readability becomes an increasingly important factor as commands are read, re-read, maintained and operated by multiple contributors on critical infrastructures.

Readers shouldn’t have to mentally translate your names into other names they already know. […] In general programmers are pretty smart people. Smart people sometimes like to show off their smarts by demonstrating their mental juggling abilities. […] One difference between a smart programmer and a professional programmer is that the professional understands that clarity is king. Professionals use their powers for good and write code that others can understand.

Clean Code - Robert C.Martin

Nacre released a Beta version in April 2022 with a Getting Started guide.