Home DEVELOPER TypeScript takes over the command line: end of bash?

TypeScript takes over the command line: end of bash?

0


JavaScript and its type-safe sister language TypeScript seem to be unstoppable in popularity. Whether in frontend or backend development, languages ​​are now the first choice for many developers. So it’s not surprising that developers are increasingly creating CLI scripts using TypeScript – which are typically written in Bash, PowerShell, or Python.

Advertisement





Cosori Dual Blaze TwinFry Air Fryer

Timo Zander studied applied mathematics and computer science and works as a software developer. He is interested in open source, JavaScript universe, and emerging technologies.

Using a web scripting language to create a shell application seems absurd. But thanks to the Node runtime environment, TypeScript is platform-independent, widely used and has an extensive standard library. The ecosystem also has many packages that make it easier to create CLI applications. The barrier to entry is therefore low – unlike Bash, a scripting language that is repeatedly criticized for its idiosyncrasies, difficulty, and unreadable syntax.

Large software projects often use separate scripts for their workflow: for example for setting up the environment, building or generating code. When these projects are written in JavaScript or TypeScript, such scripts are increasingly written in the same language. This means developers don’t have to switch between different languages ​​and contexts and can also transfer the knowledge they learn about JavaScript to the required CLI scripts. Projects like Pulumi, an open source tool for infrastructure as code, highlight these advantages: Instead of learning their own languages ​​that are used only for this purpose, developers can use Pulumi to build their favorite common You can use the Purpose language to write your infrastructure as code. ,

Additionally, scripts written in JavaScript are platform independent. Almost all developers have Node.js or a Node.js-compatible runtime environment on their computer, whether it’s on Windows, Mac, or Linux. A Bash script, on the other hand, causes problems for Windows users and requires tools such as MinGW (a port of the developer tools GNU Compiler Collection and GNU Debugger for Windows) or Windows Subsystem for Linux (WSL). .

Alternatively, the collaborative language TypeScript can also be used. Node.js competitors Deno and Bun also natively support the execution of TypeScript code – and Node.js also has built-in support for TypeScript since version 22.7, although it is still experimental. To make TypeScript CLI applications available to a wider target group, packages such as tsx Or ts nodeThese combine the build and run steps so that TypeScript code can be executed using a single command. The npx tool included with Node.js installation can invoke these packages without manual installation. A script can be created through npx tsx ./script.ts carry out. Or the file is saved with the shebang (#!) (Listing 1), so that it can be executed directly (via ). ./script.ts,

Listing 1: You can use shebang to execute .ts file directly

#!/usr/bin/env -S npx tsx
import { Command } from "@commander-js/extra-typings";
// ...

This shebang indicates that it is running using a script npx tsxOrders are executed. In addition, the file still requires execution rights and there must be a Node.js installation on the computer so that npx is installed and available in the environment. However, not all Unix or Unix-like systems allow arguments in the shebang: in these cases, the execution is somewhat more complex.

Joint Analytics and CRM: Salesforce Introduces Data Cloud One

NO COMMENTS

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Exit mobile version