Skip to content

A fluent folder builder for javascript runtimes

Notifications You must be signed in to change notification settings

ragrag/mayonaka

Repository files navigation

About

mayonaka 🌃 (ma-yo-na-ka) is a flexible folder builder for javascript runtimes. It provides a very minimal yet fluent builder interface on top of the familiar node:fs API with concurrency control

Installing

npm install mayonaka

Examples

Creating a similar directory with mayonaka

.
├── foo
├── bar
│   └── qux
│       ├── quux
│       ├── readable.txt
│       ├── buffer.txt
│       ├── string.txt
│       └── iterable.txt
└── baz
import { Mayonaka } from "mayonaka";
import { Readable } from "node:stream";

await new Mayonaka(__dirname)
  // similar to fs.mkdir
  .addFolder("foo")
  .addFolder("bar", (bar) => {
    bar.addFolder("qux", (qux) => {
      qux
        .addFolder("quux")
        // similar to fs.writeFile but data from an async source
        .addFile(
          "readable.txt",
          async () => Readable.from(["mayonaka"]),
          "utf-8"
        )
        .addFile("buffer.txt", async () => Buffer.from("mayonaka"), "utf-8")
        .addFile("string.txt", async () => "mayonaka", "utf-8")
        .addFile("iterable.txt", async () => iterable(), "utf-8");
    });
  })
  .addFolder("baz")
  .build();

function* iterable() {
  const strings = ["m", "a", "y", "o", "n", "a", "k", "a"];
  for (const str of strings) {
    yield str;
  }
}

Concurrency control

import { Mayonaka } from "mayonaka";

await new Mayonaka(__dirname, { maxConcurrency: 50 })
  .addFolder("foo")
  .addFolder("bar", (bar) => {
    for (let i = 0; i < 1000; i++) {
      bar.addFile(`${i}.txt`, async () => expensive(i), "utf-8");
    }
  })
  .build();

Access permissions

import { MayonakaSync } from "mayonaka";

// global access permissions
new MayonakaSync(__dirname, { dirMode: 0o744, fileMode: 0o766 })
  // local access permissions, overriding global permissions
  .addFolder("foo", { mode: 0o777 })
  .addFolder("bar", (bar) => {
    bar.addFile("mayonaka.txt", () => "mayonaka 🌃", "utf-8");
  })
  .addFile("baz.txt", () => "baz", "utf-8", { mode: 0o777 })
  .build();

About

A fluent folder builder for javascript runtimes

Resources

Stars

Watchers

Forks

Packages

No packages published