Replacing Shell Scripts with Cross-Platform TypeScript

Rate this content
Bookmark

Shell scripts serve a useful purpose, but they have some downsides. What if we could overcome these issues and make our scripts more powerful by utilizing familiar cross-platform TypeScript? In this talk, we'll discuss why you should switch your shell scripts to TypeScript and demonstrate a possible approach.

8 min
21 Sep, 2023

Video Summary and Transcription

The speaker discusses the benefits of replacing shell scripts with TypeScript, highlighting the limitations of shell scripts and the advantages of TypeScript such as cross-platform compatibility and better tooling. Deno is presented as the ideal platform for single file scripting, with its built-in support for TypeScript, automatic dependency installation, and sandboxing. The dax library is mentioned as a useful tool for scripting, providing a cross-platform shell and other APIs. Overall, the Talk emphasizes the power and flexibility of using TypeScript and Deno for scripting purposes.

Available in Español

1. Why Replace Shell Scripts with TypeScript

Short description:

I'm David Sherrit, a developer at Deno, and today I'm going to talk about why you should replace your shell scripts with cross platform TypeScript. TypeScript is not my primary language, but I use it in almost all my projects, and this talk will outline how and why you should also consider doing that. We often use shell scripts because they work out of the box on the system we're on, but there are downsides. Shell scripts are system dependent, can't be run on every platform, and don't work on Windows out of the box. TypeScript, on the other hand, is familiar, has great tooling, and allows for cross-platform scripting in Deno.

Hi, everyone. I'm David Sherrit, a developer at Deno, and today I'm going to talk about why you should replace your shell scripts with cross platform TypeScript. This is not a talk just for JavaScript developers, but for developers from any language and background. Personally, TypeScript is not my primary language, but I use it in almost all my projects, and this talk will outline how and why you should also consider doing that.

So why do we use shell scripts? We often use them because they work out of the box on the system we're on, so that's convenient. They are easy to spawn. They have concise syntax, and they execute similarly to what we write on the command line. So if you have a command you often run in your terminal, you can often just copy and paste it into your shell script. There's some downsides, though. The first major downside is it's system dependent. Shell scripts can't be taken and run on every platform. Mostly, they won't run out of the box on Windows. Sometimes this is even the case across Unix-based systems, depending on how the shell script is written. Also, basic Unix commands don't work on Windows out of the box. And telling Windows developers to use WSL is not a great solution. That can require them having to set up a new development environment within WSL just to run the script. Shell scripts are great if you're executing a few commands. But it's difficult to increase functionality without drastically increasing complexity. Another point specifically for JavaScript developers, is shell scripts present code we need to maintain in yet another language. We also can't pull in and use our favorite JavaScript libraries. Finally, there's no type checking. We don't get code completion to help us develop, and all the other nice tooling that TypeScript provides.

Now, why should you use TypeScript instead of shell scripts? First of all, it's familiar, and TypeScript and JavaScript are popular languages with a large community. Many developers have at least touched JavaScript or could at least understand its syntax. Second is type checking and tooling. A very large benefit of TypeScript is that its tooling is great, even if we're just using JavaScript. We get features like code completion, go to definition, and type checking errors in the editor. Now, this talk is not just about why you should replace shell scripts with TypeScript, but also why you should use Deno while doing that. First of all, it's far easier to write cross-platform scripts in Deno than shell scripts, because most APIs work cross-platform. Obviously, there's some platform-specific APIs, if you need them, but usually we don't have to reach for those.

2. Advantages of Deno for Single File Scripting

Short description:

Deno is the best platform for writing single file scripts. It supports TypeScript out of the box, auto-installs dependencies based on the code, and is sandboxed by default. Deno also allows you to define dependencies in the script file itself, making it easy to host scripts on a web server. Additionally, the dax library provides a cross-platform shell with common Unix commands and other useful APIs for scripting. While launching commands may be slightly more verbose than shell scripts, the advantages of writing scripts in JavaScript outweigh this. Overall, Deno offers a powerful and flexible environment for single file scripting.

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.

Check out more articles and videos

We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career

React Day Berlin 2023React Day Berlin 2023
21 min
React's Most Useful Types
Top Content
We don't think of React as shipping its own types. But React's types are a core part of the framework - overseen by the React team, and co-ordinated with React's major releases.In this live coding talk, we'll look at all the types you've been missing out on. How do you get the props type from a component? How do you know what ref a component takes? Should you use React.FC? And what's the deal with JSX.Element?You'll walk away with a bunch of exciting ideas to take to your React applications, and hopefully a new appreciation for the wonders of React and TypeScript working together.
Vue.js London 2023Vue.js London 2023
30 min
Stop Writing Your Routes
The more you keep working on an application, the more complicated its routing becomes, and the easier it is to make a mistake. ""Was the route named users or was it user?"", ""Did it have an id param or was it userId?"". If only TypeScript could tell you what are the possible names and params. If only you didn't have to write a single route anymore and let a plugin do it for you. In this talk we will go through what it took to bring automatically typed routes for Vue Router.
TypeScript Congress 2023TypeScript Congress 2023
31 min
Making Magic: Building a TypeScript-First Framework
I'll dive into the internals of Nuxt to describe how we've built a TypeScript-first framework that is deeply integrated with the user's IDE and type checking setup to offer end-to-end full-stack type safety, hints for layouts, middleware and more, typed runtime configuration options and even typed routing. Plus, I'll highlight what I'm most excited about doing in the days to come and how TypeScript makes that possible not just for us but for any library author.
React Advanced Conference 2021React Advanced Conference 2021
6 min
Full-stack & typesafe React (+Native) apps with tRPC.io
Top Content
Why are we devs so obsessed with decoupling things that are coupled nature? tRPC is a library that replaces the need for GraphQL or REST for internal APIs. When using it, you simply write backend functions whose input and output shapes are instantly inferred in your frontend without any code generation; making writing API schemas a thing of the past. It's lightweight, not tied to React, HTTP-cacheable, and can be incrementally adopted. In this talk, I'll give a glimpse of the DX you can get from tRPC and how (and why) to get started.
TypeScript Congress 2023TypeScript Congress 2023
24 min
Faster TypeScript builds with --isolatedDeclarations
Top Content
Type-checking a TypeScript codebase can be slow, especially for monorepos containing lots of projects that each need to use the type checker to generate type declaration files. In this talk, we introduce — for the very first time — a new TypeScript feature we are working on called “Isolated Declarations” that allows DTS files to be generated without using the type checker at all! This opens the door to faster declaration generation in TypeScript itself, as well as in external tools written in other languages such as ESBuild and swc. You'll see how to use this new option, and maybe (just maybe) you’ll be convinced about the benefits of explicit return types! Most importantly, we will show how Isolated Declarations enables parallel builds to spread work across your CPU cores to significantly improve the build speed of your TypeScript projects.

Workshops on related topic

React Advanced Conference 2021React Advanced Conference 2021
174 min
React, TypeScript, and TDD
Top Content
Featured WorkshopFree
ReactJS is wildly popular and thus wildly supported. TypeScript is increasingly popular, and thus increasingly supported.

The two together? Not as much. Given that they both change quickly, it's hard to find accurate learning materials.

React+TypeScript, with JetBrains IDEs? That three-part combination is the topic of this series. We'll show a little about a lot. Meaning, the key steps to getting productive, in the IDE, for React projects using TypeScript. Along the way we'll show test-driven development and emphasize tips-and-tricks in the IDE.
React Advanced Conference 2022React Advanced Conference 2022
148 min
Best Practices and Advanced TypeScript Tips for React Developers
Top Content
Featured Workshop
Are you a React developer trying to get the most benefits from TypeScript? Then this is the workshop for you.In this interactive workshop, we will start at the basics and examine the pros and cons of different ways you can declare React components using TypeScript. After that we will move to more advanced concepts where we will go beyond the strict setting of TypeScript. You will learn when to use types like any, unknown and never. We will explore the use of type predicates, guards and exhaustive checking. You will learn about the built-in mapped types as well as how to create your own new type map utilities. And we will start programming in the TypeScript type system using conditional types and type inferring.
Node Congress 2024Node Congress 2024
83 min
Deep TypeScript Tips & Tricks
Top Content
Workshop
TypeScript has a powerful type system with all sorts of fancy features for representing wild and wacky JavaScript states. But the syntax to do so isn't always straightforward, and the error messages aren't always precise in telling you what's wrong. Let's dive into how many of TypeScript's more powerful features really work, what kinds of real-world problems they solve, and how to wrestle the type system into submission so you can write truly excellent TypeScript code.
TypeScript Congress 2023TypeScript Congress 2023
131 min
Practice TypeScript Techniques Building React Server Components App
Workshop
In this hands-on workshop, Maurice will personally guide you through a series of exercises designed to empower you with a deep understanding of React Server Components and the power of TypeScript. Discover how to optimize your applications, improve performance, and unlock new possibilities.
 
During the workshop, you will:
- Maximize code maintainability and scalability with advanced TypeScript practices
- Unleash the performance benefits of React Server Components, surpassing traditional approaches
- Turbocharge your TypeScript with the power of Mapped Types
- Make your TypeScript types more secure with Opaque Types
- Explore the power of Template Literal Types when using Mapped Types
 
Maurice will virtually be by your side, offering comprehensive guidance and answering your questions as you navigate each exercise. By the end of the workshop, you'll have mastered React Server Components, armed with a newfound arsenal of TypeScript knowledge to supercharge your React applications.
 
Don't miss this opportunity to elevate your React expertise to new heights. Join our workshop and unlock the potential of React Server Components with TypeScript. Your apps will thank you.
TypeScript Congress 2022TypeScript Congress 2022
116 min
Advanced TypeScript types for fun and reliability
Workshop
If you're looking to get the most out of TypeScript, this workshop is for you! In this interactive workshop, we will explore the use of advanced types to improve the safety and predictability of your TypeScript code. You will learn when to use types like unknown or never. We will explore the use of type predicates, guards and exhaustive checking to make your TypeScript code more reliable both at compile and run-time. You will learn about the built-in mapped types as well as how to create your own new type map utilities. And we will start programming in the TypeScript type system using conditional types and type inferring.
Are you familiar with the basics of TypeScript and want to dive deeper? Then please join me with your laptop in this advanced and interactive workshop to learn all these topics and more.
You can find the slides, with links, here: http://theproblemsolver.nl/docs/ts-advanced-workshop.pdf
And the repository we will be using is here: https://github.com/mauricedb/ts-advanced
TestJS Summit 2023TestJS Summit 2023
78 min
Mastering Node.js Test Runner
Workshop
Node.js test runner is modern, fast, and doesn't require additional libraries, but understanding and using it well can be tricky. You will learn how to use Node.js test runner to its full potential. We'll show you how it compares to other tools, how to set it up, and how to run your tests effectively. During the workshop, we'll do exercises to help you get comfortable with filtering, using native assertions, running tests in parallel, using CLI, and more. We'll also talk about working with TypeScript, making custom reports, and code coverage.