fs
Install the required plugin:
tynd add fsimport { os, path } from "@tynd/core/client";import { fs } from "@tynd/plugin-fs/client";
// The app's own data directory: inside the default scope, created on first call.const dir = await os.appDataDir();Cross-OS filesystem operations. Binary I/O uses the zero-copy binary channel. The examples below build their paths from dir; a path outside the scope is refused.
Text I/O
Section titled “Text I/O”await fs.writeText(path.join(dir, "data.json"), JSON.stringify(state), { createDirs: true });const text = await fs.readText(path.join(dir, "data.json"));readText(path): Promise<string>
Section titled “readText(path): Promise<string>”Reads the file as UTF-8.
writeText(path, content, opts?): Promise<void>
Section titled “writeText(path, content, opts?): Promise<void>”Writes UTF-8 text.
createDirs?: boolean— create parent directories if missing.
Binary I/O
Section titled “Binary I/O”const bytes = await fs.readBinary(path.join(dir, "image.png")); // Uint8Arrayawait fs.writeBinary(path.join(dir, "copy.png"), bytes, { createDirs: true });Uses the tynd-bin:// channel — no base64, zero-copy. Accepts Uint8Array | ArrayBuffer. See the Binary Data guide.
Metadata
Section titled “Metadata”exists(path): Promise<boolean>
Section titled “exists(path): Promise<boolean>”if (await fs.exists(path.join(dir, "data.json"))) { /* … */ }stat(path): Promise<FileStat>
Section titled “stat(path): Promise<FileStat>”interface FileStat { size: number; isFile: boolean; isDir: boolean; isSymlink: boolean; mtime: number | null; // ms since epoch; null if unsupported}
const info = await fs.stat(path.join(dir, "data.json"));Directories
Section titled “Directories”readDir(path): Promise<DirEntry[]>
Section titled “readDir(path): Promise<DirEntry[]>”interface DirEntry { name: string; isFile: boolean; isDir: boolean; isSymlink: boolean;}
const entries = await fs.readDir(dir);Join with the parent path for an absolute path: path.join(dir, entry.name).
mkdir(path, opts?): Promise<void>
Section titled “mkdir(path, opts?): Promise<void>”await fs.mkdir(path.join(dir, "nested/dir"), { recursive: true });remove(path, opts?): Promise<void>
Section titled “remove(path, opts?): Promise<void>”Works on files and directories.
await fs.remove(path.join(dir, "old.json"));await fs.remove(path.join(dir, "old-cache"), { recursive: true });rename(from, to): Promise<void>
Section titled “rename(from, to): Promise<void>”await fs.rename(path.join(dir, "a.txt"), path.join(dir, "b.txt"));copy(from, to): Promise<void>
Section titled “copy(from, to): Promise<void>”await fs.copy(path.join(dir, "src.json"), path.join(dir, "dst.json"));copyDir(from, to): Promise<void>
Section titled “copyDir(from, to): Promise<void>”Recursive — files are copied, symlinks are left alone (call fs.symlink yourself if you need them).
await fs.copyDir(path.join(dir, "src-assets"), path.join(dir, "dist/assets"));trash(path): Promise<void>
Section titled “trash(path): Promise<void>”Move to the OS recycle bin / Trash — reversible, unlike remove(). Backed by the trash crate (the freedesktop.org Trash spec on Linux).
await fs.trash(path.join(dir, "old.log"));Symbolic + hard links
Section titled “Symbolic + hard links”const data = path.join(dir, "data.db");await fs.symlink(data, path.join(dir, "data-link"));const target = await fs.readlink(path.join(dir, "data-link")); // the data.db pathawait fs.hardlink(data, path.join(dir, "data.bak"));Windows chooses file vs dir symlink based on what the target currently is (falls back to file when the target doesn’t exist).
Stateful file handles
Section titled “Stateful file handles”Open a file once, then seek / read / write by chunk — handy for large or partially-read files. Binary travels base64 over JSON; for multi-MB I/O prefer fs.readBinary / fs.writeBinary (zero-copy via tynd-bin://).
const h = await fs.open(path.join(dir, "big.bin"), { read: true, write: true, create: true });await h.seek(1024); // "start" | "current" | "end"const { bytes, read } = await h.read(256); // read up to 256 Bconst written = await h.write(new Uint8Array([1, 2, 3]));await h.close(); // always release the handleseek(offset, from?) resolves to the new position; from is "start" (default), "current" or "end". Handles are AsyncDisposable, so await using h = await fs.open(...) closes them at scope exit.
A handle is usable only from the webview that opened it (or the backend) and is closed when that webview reloads, navigates or closes; another webview, or a frame of another origin in the same webview (a remote iframe in a local page), gets the same answer as for a handle that is not open (fs: handle <id> not open; close returns false). If the page navigates or reloads while open is still running, the new handle is closed and the call fails with “fs.open: the document that requested it is gone”.
Watcher
Section titled “Watcher”interface FsChangeEvent { id: number; kind: "create" | "modify" | "delete" | "rename" | "error" | "other"; path?: string; error?: string;}
const watcher = await fs.watch(path.join(dir, "notes"), { recursive: true }, (event) => { console.log(event.kind, event.path);});
// laterawait watcher.unwatch(); // false if the watch was already gone- Uses
ReadDirectoryChangesW(Windows),FSEvents(macOS),inotify(Linux). - Event coalescing behavior differs by OS —
modifymay fire multiple times for a single logical save. Debounce on your side if needed. kind: "error"events include anerrorfield; watcher remains active.- Change events go only to the webview that called
watch. Other webviews, including other app-local ones, do not receive them. A watch started from the backend reaches app-local webviews only. - Watches and file handles belong to the webview that created them: another webview’s
unwatch/ handle id answers like an unknown id, and they are released when that webview reloads, navigates or closes. Awatchstill being created when the page navigates or reloads is removed and fails with “fs.watch: the document that requested it is gone”. See security.
- Paths accept both absolute and relative. Relative paths resolve against the app’s current working directory at launch, which is in scope under
tynd devonly. Build absolute paths fromos.appDataDir(),os.appConfigDir(),os.appCacheDir()oros.appTempDir(). - By default a path must sit in the app’s own directories:
<dataDir>/<app>,<configDir>/<app>,<cacheDir>/<app>or<tmpDir>/<app>(<app>isbundle.identifier, the executable name when unset;os.appDataDir()and its siblings return them), or the working directory undertynd devonly. The home directory and the shared data and temp directories are not in scope. Anything else, including a symlink whose target escapes them, is refused (fs: path '…' is outside capability roots) unless anfs.allowrule starting with an absolute directory above it or a runtime grant (for example a dialog opened withgrantAccess: true) names it; a rule such as**/*.pdfreaches nothing outside. See default filesystem scope. - Credential stores (
~/.ssh,~/.aws,~/.git-credentials,~/.npmrc, keychains…), browser profiles and Tynd’s grants file are always refused (fs: '…' is a protected location), whatever the policy. See protected locations. - All methods run on the host’s worker pool — the JS event loop is never blocked.
Related
Section titled “Related”- path — path helpers.
- os —
dataDir,configDir,cacheDir,tmpDir. - Binary Data guide.