Second is single file scripts. I'm obviously biased, but I think Deno is the best platform for writing single file scripts. For example, in Node, using TypeScript requires extra setup. You need to have a package.json file to define dependencies. This also requires anyone running your scripts to have to remember to run npm install first and also after any changes to the dependencies. That process then creates a Node modules folder in the directory tree. So right there you have two extra file system entries, so it's not a single file script.
Whereas in Deno, TypeScript is supported out of the box, so you can execute TypeScript code directly. It's optional to use a config file to define your dependencies, and it's also optional to have a Node modules folder for vendoring npm dependencies. It's important to note that you can still use that model if you want, but it's opt-in. Third, dependencies are auto-installed based on the code, so no separate npm install setup command is necessary. Finally, Deno is sandboxed by default. Node recently got an experimental permission system, but it's opt-in rather than opt-out.
Another great thing about Deno for single file scripting is it's possible to define the dependencies in the script file itself. For example, you can pull in packages from npm or use https imports to easily host scripts on a web server and reference those directly. One bad part about this is it's verbose to launch sub-processes using a low-level process API available in both Node and Deno. But that's not a big deal because we can pull in a dependency that helps make it more like shell scripts.
There's a library I wrote called dax. Dax is a cross-platform shell written in JavaScript that has common cross-platform Unix commands built right in. It also has other useful APIs to help out with scripting. Here's some examples of running commands. The shell syntax it supports is a common syntax found in shell scripts. For anything more complex, like loops, you probably just want to use JavaScript for that instead. Again, to emphasize, all the code shown here works on Windows. Launching commands is slightly more verbose than shell scripts for simple cases, but in my opinion, the advantages of being able to write these scripts mostly in JavaScript outweighs the slight extra verbosity when launching commands. Also, it could be argued that this is less verbose in complex cases. For example, as shown on the last line of this code, accessing properties on the JSON output of a command is quite easy.
Finally, DAX has logging APIs, a path API. For example, here we create a path ref object for the source directory, get if it's a directory, then call mkdir on it to create the directory. Then we get a reference to a text file within that directory, write some text to it, or we could even read from that text file. Or we could write some object as JSON to a file, or read from it deserializing to a JSON value. There's also APIs for doing prompts, making selections, and showing progress. Finally, there's a request API built in that's similar to the fetch API, but less error-prone, and with some additional helpers on it. Anyway, there's a lot more to this, and I'd recommend checking out the documentation for more details.
Comments