entbit. by niklasmtj.de logo

entbit. by niklasmtj.de

Subscribe
Archives
October 16, 2024

Surf your filesystem with Deno πŸ¦•

Hey!

First of all: Great to see all the new subscribers! Thanks for subscribing (also to the old subscribers) 🫢🏼 - I really appreciate your support.

I'm currently working on the next chapter of "Everything I know about Deno". We are taking a deep dive into interacting with the file system via Deno. How can we create files, read them or check what's in our directory? You'll get the answers with examples in the next chapter.

I'm going on holiday for the next two weeks πŸ„πŸ»β€β™‚οΈ. But because of the waiting time for you in between. Here's a sneak peek at an example you'll see in the new chapter. Let's surf the file system together.

Walk directories

For example, if we want to build an application that displays the file system and its contents, we have the option of traversing the file system from a starting point. This method is provided by the @std/fs module from the standard library. Using an additional function from the @std/path library, we can create a kind of tree application.

import { walk } from "jsr:@std/fs/walk";
import { relative, SEPARATOR } from "jsr:@std/path";

const path = ".";

for await (const entry of walk(path, { includeDirs: true })) {
  const relativePath = relative(path, entry.path);
  const depth = relativePath.split(SEPARATOR).length - 1;
  const indent = "-->".repeat(depth);
  const prefix = entry.isDirectory ? "πŸ“ " : "πŸ“„ ";
  console.log(`${prefix}${indent} ${entry.name}`);
}

An example output would be the following, which shows my testing project:

➜ deno run -A walk.ts
πŸ“  .
πŸ“  .zed
πŸ“„ --> settings.json
πŸ“„  main.ts
πŸ“„  deno.json
πŸ“„  walk.ts
πŸ“„  deno.lock
πŸ“„  .env
πŸ“„  flags.ts
πŸ“  my-dir
πŸ“ --> sub-dir

That's it for now, thanks for reading!
Niklas

PS: I'm taking a look at Gleam at the moment. I know about that language for a while now, but it comes up more often in the last weeks. After I learnt, that Gleam can output Javascript I got more intrigued to check it out.

But in the mean time I'll surf, drink some coconut water and just enjoy life.

PPS: What could I write about that would be of value to you? Reply to this mail or write me at hi[at]entbit.de.

Don't miss what's next. Subscribe to entbit. by niklasmtj.de:
GitHub niklasmtj.de My free Deno guide
This email brought to you by Buttondown, the easiest way to start and grow your newsletter.