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.