Replacing Shell Scripts with Cross-Platform TypeScript

Rate this content

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


Sign in or register to post your comment.

AI Generated Video Summary

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.

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

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
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 2022TypeScript Congress 2022
10 min
How to properly handle URL slug changes in Next.js
If you're using a headless CMS for storing content, you also work with URL slugs, the last parts of any URL. The problem is, content editors are able to freely change the slugs which can cause 404 errors, lost page ranks, broken links, and in the end confused visitors on your site. In this talk, I will present a solution for keeping a history of URL slugs in the CMS and explain how to implement a proper redirect mechanism (using TypeScript!) for dynamically generated pages on a Next.js website.
Add to the talk notes: 
React Summit 2023React Summit 2023
19 min
7 TypeScript Patterns You Should Be Using
In this talk, we will be going over a number of common useful and best-practice-proven TypeScript patterns to use in React 18. This includes how to correctly type component properties, children and return types, using React's built-in types, typing contexts, and the usual enum rant (but constructively).

Workshops on related topic

React Advanced Conference 2021React Advanced Conference 2021
174 min
React, TypeScript, and TDD
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
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.
TypeScript Congress 2022TypeScript Congress 2022
116 min
Advanced TypeScript types for fun and reliability
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:
And the repository we will be using is here:
TypeScript Congress 2023TypeScript Congress 2023
131 min
Practice TypeScript Techniques Building React Server Components App
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.
Node Congress 2021Node Congress 2021
245 min
Building Serverless Applications on AWS with TypeScript
This workshop teaches you the basics of serverless application development with TypeScript. We'll start with a simple Lambda function, set up the project and the infrastructure-as-a-code (AWS CDK), and learn how to organize, test, and debug a more complex serverless application.
Table of contents:
        - How to set up a serverless project with TypeScript and CDK
        - How to write a testable Lambda function with hexagonal architecture
        - How to connect a function to a DynamoDB table
        - How to create a serverless API
        - How to debug and test a serverless function
        - How to organize and grow a serverless application
Materials referred to in the workshop:,HYgVepLIpfxrK4EQNclQ9w
DynamoDB blog Alex DeBrie:
Excellent book for the DynamoDB:

TypeScript Congress 2022TypeScript Congress 2022
118 min
Crash Course into TypeScript for content from headless CMS
In this workshop, I’ll first show you how to create a new project in a headless CMS, fill it with data, and use the content in your project. Then, we’ll spend the rest of time in code, we will:
- Generate strongly typed models and structure for the fetched content.
- Use the content in components
- Resolve content from rich text fields into React components
- Touch on deployment pipelines and possibilities for discovering content-related issues before hitting production