Replacing Shell Scripts with Cross-Platform TypeScript

Rate this content
Bookmark
This talk discusses the benefits of replacing shell scripts with cross-platform TypeScript, especially using Deno. TypeScript shell scripts offer cross-platform compatibility, making them ideal for running on different operating systems without additional setup. Deno supports TypeScript out of the box, automatically installs dependencies, and provides a sandboxed environment for secure scripting. The DAX library enhances Deno's capabilities by offering a cross-platform shell with common Unix commands and useful APIs. This makes scripting more versatile and powerful. Unlike system-dependent shell scripts, TypeScript scripts can be run on any platform, including Windows. Additionally, TypeScript's familiar syntax and enhanced tooling, such as code completion and type checking, improve developer productivity and script maintainability. Deno's ability to define dependencies directly in the script file simplifies hosting and sharing scripts online. Overall, Deno and TypeScript together provide a robust solution for modern scripting needs.

From Author:

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.

This talk has been presented at TypeScript Congress 2023, check out the latest edition of this Tech Conference.

FAQ

The main reasons to replace shell scripts with TypeScript include cross-platform compatibility, familiar syntax for JavaScript developers, enhanced tooling like code completion and type checking, and the ability to use JavaScript libraries directly in the scripts.

Shell scripts are system dependent primarily because they often do not run consistently across different operating systems, especially between Unix-based systems and Windows. Commands and scripts that work on Unix might not work on Windows without additional setup like Windows Subsystem for Linux (WSL).

Deno offers several advantages over Node for TypeScript scripting, including out-of-the-box TypeScript support without extra setup, automatic dependency installation, sandboxed environment by default, and the ability to write single-file scripts without the need for package.json or node_modules.

In Deno, dependencies are automatically installed based on the code, eliminating the need for separate npm install commands. Developers can also define dependencies directly in the script file using HTTPS imports, simplifying script management and execution.

Yes, you can use npm packages with Deno. While Deno supports HTTP imports for dependencies, it is also possible to opt-in to a traditional npm model if needed, allowing developers to utilize npm packages alongside Deno's features.

DAX is a cross-platform shell written in JavaScript, designed to integrate common Unix commands and provide additional scripting APIs. It simplifies scripting tasks by allowing more JavaScript-like syntax for loops and complex operations, while still supporting shell script features.

David Sherret
David Sherret
8 min
21 Sep, 2023

Comments

Sign in or register to post your comment.

Video Transcription

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's Most Useful Types
React Day Berlin 2023React Day Berlin 2023
21 min
React's Most Useful Types
Top Content
Watch video: React's Most Useful Types
Today's Talk focuses on React's best types and JSX. It covers the types of JSX and React components, including React.fc and React.reactnode. The discussion also explores JSX intrinsic elements and react.component props, highlighting their differences and use cases. The Talk concludes with insights on using React.componentType and passing components, as well as utilizing the react.element ref type for external libraries like React-Select.
TypeScript and React: Secrets of a Happy Marriage
React Advanced Conference 2022React Advanced Conference 2022
21 min
TypeScript and React: Secrets of a Happy Marriage
Top Content
React and TypeScript have a strong relationship, with TypeScript offering benefits like better type checking and contract enforcement. Failing early and failing hard is important in software development to catch errors and debug effectively. TypeScript provides early detection of errors and ensures data accuracy in components and hooks. It offers superior type safety but can become complex as the codebase grows. Using union types in props can resolve errors and address dependencies. Dynamic communication and type contracts can be achieved through generics. Understanding React's built-in types and hooks like useState and useRef is crucial for leveraging their functionality.
Making Magic: Building a TypeScript-First Framework
TypeScript Congress 2023TypeScript Congress 2023
31 min
Making Magic: Building a TypeScript-First Framework
Top Content
Daniel Rowe discusses building a TypeScript-first framework at TypeScript Congress and shares his involvement in various projects. Nuxt is a progressive framework built on Vue.js, aiming to reduce friction and distraction for developers. It leverages TypeScript for inference and aims to be the source of truth for projects. Nuxt provides type safety and extensibility through integration with TypeScript. Migrating to TypeScript offers long-term maintenance benefits and can uncover hidden bugs. Nuxt focuses on improving existing tools and finds inspiration in frameworks like TRPC.
Stop Writing Your Routes
Vue.js London 2023Vue.js London 2023
30 min
Stop Writing Your Routes
Designing APIs is a challenge, and it's important to consider the language used and different versions of the API. API ergonomics focus on ease of use and trade-offs. Routing is a misunderstood aspect of API design, and file-based routing can simplify it. Unplugging View Router provides typed routes and eliminates the need to pass routes when creating the router. Data loading and handling can be improved with data loaders and predictable routes. Handling protected routes and index and ID files are also discussed.
Faster TypeScript builds with --isolatedDeclarations
TypeScript Congress 2023TypeScript Congress 2023
24 min
Faster TypeScript builds with --isolatedDeclarations
Top Content
This talk discusses the performance issues in TypeScript builds and introduces a new feature called isolated declarations. By running the compiler in parallel and using isolated modules, significant performance gains can be achieved. Isolated declarations improve build speed, compatibility with other tools, and require developers to write types in code. This feature has the potential to further increase performance and may be available in TypeScript soon.
Full-stack & typesafe React (+Native) apps with tRPC.io
React Advanced Conference 2021React Advanced Conference 2021
6 min
Full-stack & typesafe React (+Native) apps with tRPC.io
Top Content
Alex introduces tRPC, a toolkit for making end-to-end type-safe APIs easily, with auto-completion of API endpoints and inferred data from backend to frontend. tRPC works the same way in React Native and can be adopted incrementally. The example showcases backend communication with a database using queries and validators, with types inferred to the frontend and data retrieval done using Prisma ORM.

Workshops on related topic

React, TypeScript, and TDD
React Advanced Conference 2021React Advanced Conference 2021
174 min
React, TypeScript, and TDD
Top Content
Featured WorkshopFree
Paul Everitt
Paul Everitt
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.
Mastering advanced concepts in TypeScript
React Summit US 2023React Summit US 2023
132 min
Mastering advanced concepts in TypeScript
Top Content
Featured WorkshopFree
Jiri Lojda
Jiri Lojda
TypeScript is not just types and interfaces. Join this workshop to master more advanced features of TypeScript that will make your code bullet-proof. We will cover conditional types and infer notation, template strings and how to map over union types and object/array properties. Each topic will be demonstrated on a sample application that was written with basic types or no types at all and we will together improve the code so you get more familiar with each feature and can bring this new knowledge directly into your projects.
You will learn:- - What are conditional types and infer notation- What are template strings- How to map over union types and object/array properties.
Deep TypeScript Tips & Tricks
Node Congress 2024Node Congress 2024
83 min
Deep TypeScript Tips & Tricks
Top Content
Featured Workshop
Josh Goldberg
Josh Goldberg
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.
Best Practices and Advanced TypeScript Tips for React Developers
React Advanced Conference 2022React Advanced Conference 2022
148 min
Best Practices and Advanced TypeScript Tips for React Developers
Top Content
Featured Workshop
Maurice de Beijer
Maurice de Beijer
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.
Building Your Own Custom Type System
React Summit 2024React Summit 2024
38 min
Building Your Own Custom Type System
Featured Workshop
Kunal Dubey
Kunal Dubey
I'll introduce the audience to a concept where they can have end-to-end type systems that helps ensure typesafety across the teams Such a system not only improves communication between teams but also helps teams collaborate effectively and ship way faster than they used to before. By having a custom type system, teams can also identify the errors and modify the API contracts on their IDE, which contributes to a better Developer Experience. The workshop would primarily leverage TS to showcase the concept and use tools like OpenAPI to generate the typesystem on the client side. 
Frictionless Development With Unified Type System
JSNation 2024JSNation 2024
113 min
Frictionless Development With Unified Type System
Featured Workshop
Ejiro Asiuwhu
Ejiro Asiuwhu
Imagine developing where frontend and backend sing in harmony, types dance in perfect sync, and errors become a distant memory. That's the magic of TypeScript Nirvana!
Join me on a journey to unveil the secrets of unified type definitions, the key to unlocking frictionless development. We'll dive into:
- Shared language, shared love: Define types once, share them everywhere. Consistency becomes your BFF, errors your worst nightmare (one you'll rarely see).- Effortless coding: Ditch the manual grind of type checking. TypeScript's got your back, freeing you to focus on building awesomeness.- Maintainability magic: With crystal-clear types guiding your code, maintaining it becomes a walk in the park. More time innovating, less time debugging.- Security fortress: TypeScript's type system shields your app from common vulnerabilities, making it a fortress against security threats.