Building scripts and CLIs with Deno
Why Deno?
Writing scripts in Deno is fast, easy, and productive. There’s no separate step to install dependencies, configure TypeScript, setup a linter, formatter, test runner, or any other part of your toolchain.
For example, you can write TypeScript, install a module and its type definitions right when you run the script for the first time:
import cowsay from "npm:cowsay@1.5.0";
let output: string = cowsay.say({ text: "Hello from typescript!" });
console.log(output);
And when we run it the first time, it installs all dependencies and caches them:
$ deno run --allow-read main.ts
________________________
< Hello from typescript! >
------------------------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
Web Platform APIs
Scripting with Deno is easier with its built-in Web Platform APIs. This makes accessing the network, parsing HTTP responses, and more simple and intuitive.
const res = await fetch("https://examples.deno.land/hello-world.ts");
const text = await res.text();
console.log(text);
And running this script we see the body of the script:
$ deno run --allow-net main.ts
console.log("Hello, World!")
Sharing and distributing
Deno can execute programs and scripts via URL, so sharing your script is as simple as hosting your script on gist.github.com. An example of a script is https://examples.deno.land/hello-world.ts, which you can run from a URL:
$ deno run https://examples.deno.land/hello-world.ts
Hello, World!
If you want to share your JavaScript or TypeScript program as a binary, Deno’s
all-in-one modern toolchain includes
deno compile
, which
makes it easy to compile your script into a single, portable binary that you can
share on all major platforms.
$ deno compile --allow-net ./main.ts
Compile file:///deno/main to main
$ ./main
console.log("Hello, World!")
Additional Resources
Here are some examples, blog posts, and videos about writing scripts or CLIs with Deno.
- How the creator of Homebrew simplifies distributing software with tea and Deno
- How immutable scripts in Deno allow Windmill.dev (YC S22) to build production-grade ops
- dax — Cross platform shell tools for Deno inspired by zx