Skip to content

fs ​

Files and directories.

Import

ts
import { FileStat, Files } from "fs";

Needs the "fs" permission in thicket.json.

FileStat class ​

ts
class FileStat

Metadata describing a filesystem entry, returned by fs.stat.

Times are milliseconds since the Unix epoch as float64 so the values survive a round-trip through JSON.stringify without precision loss.

FileStat.createdMs ​

ts
createdMs: float64

Creation time in milliseconds since the Unix epoch.

FileStat.isDirectory ​

ts
isDirectory: boolean

True when the entry is a directory.

FileStat.isFile ​

ts
isFile: boolean

True when the entry is a regular file.

FileStat.modifiedMs ​

ts
modifiedMs: float64

Modification time in milliseconds since the Unix epoch.

FileStat.size ​

ts
size: uint

Size in bytes.

Files static class ​

ts
static class Files

Filesystem subsystem. Imported as import { Files } from "fs".

Every async method returns Promise<T>; await it from an async function or the top-level entry point. Errors come back as rejected promises. The path-utility methods at the bottom of this class are synchronous and require no permission.

Files.sep ​

ts
static readonly sep: string

The path separator on this platform.

Always "/" on macOS and Linux, "\\" on Windows.

Files.delimiter ​

ts
static readonly delimiter: string

The PATH environment variable delimiter on this platform.

":" on macOS and Linux, ";" on Windows.

Files.appendBytes ​

ts
static appendBytes(path: string, data: any): any

Append raw bytes to path.

Creates the file if it does not exist.

Files.appendFile ​

ts
static appendFile(path: string, content: string): any

Append content to path as UTF-8.

Creates the file if it does not exist.

Files.copy ​

ts
static copy(src: string, dest: string): any

Copy src to dest, replacing dest if it exists.

src may be an embedded file (a plain path or embed://); its bytes are written to dest on disk. dest can never be embedded.

Copies file contents; does not preserve extended attributes or hard-link relationships.

Files.exists ​

ts
static exists(path: string): any

Resolve to true when path exists, false otherwise.

Does not distinguish files from directories — combine with isFile / isDirectory when the kind matters.

Files.isDirectory ​

ts
static isDirectory(path: string): any

True when the entry is a directory.

Files.isEmbedded ​

ts
static isEmbedded(path: string): boolean

True when path is an embedded file or a folder inside the program's embedded files. A plain path answers in a built program only; an embed:// path also answers in thicket run, from the project files the embed entries of assets would ship. Synchronous.

Files.isFile ​

ts
static isFile(path: string): any

True when the entry is a regular file.

Files.mkdir ​

ts
static mkdir(path: string, recursive?: boolean): any

Create directory path.

When recursive is true, intermediate directories are created as needed. Without it, missing parents cause an error.

Files.readDir ​

ts
static readDir(path: string): any

Resolve to the entries of directory path as an array of names.

Names are returned in implementation-defined order, except in a built program where embedded files are merged in (they shadow a disk entry of the same name, and implied folders appear): that listing is sorted by name. embed://<path> lists the embedded files only. Use Files.join to build full paths from the entries.

Files.readFile ​

ts
static readFile(path: string): any

Read the entire contents of path as raw bytes.

Resolves to a Buffer. Use readText when the file is known to be UTF-8.

Example

ts
import { Files } from "fs";
await Files.writeFile("data.bin", ByteArray.from("abc"));
const bytes: any = await Files.readFile("data.bin");
console.log(bytes.length); // 3

Files.readText ​

ts
static readText(path: string): any

Read the entire contents of path as UTF-8.

Throws if the file is missing, unreadable, or contains invalid UTF-8.

Example:

ts
const cfg = await Files.readText("config.json");

Example

ts
import { Files } from "fs";
await Files.writeText("notes.txt", "hello");
console.log(await Files.readText("notes.txt")); // hello

Files.remove ​

ts
static remove(path: string, recursive?: boolean): any

Remove path.

For directories, pass recursive: true to remove the tree. Without it, only empty directories can be removed.

Files.rename ​

ts
static rename(oldPath: string, newPath: string): any

Rename or move oldPath to newPath.

Both paths must reside on the same filesystem; cross-device moves require copy + delete.

Files.size ​

ts
static size(path: string): any

Size in bytes.

Files.stat ​

ts
static stat(path: string): any

Resolve to a FileStat describing path.

Throws when path does not exist.

Files.writeFile ​

ts
static writeFile(path: string, data: any): any

Write raw bytes to path, replacing any existing file.

data is a Buffer or a string (written as its bytes). Like writeText but does not assume UTF-8.

Example

ts
import { Files } from "fs";
await Files.writeFile("data.bin", ByteArray.from("abc"));

Files.writeText ​

ts
static writeText(path: string, data: string): any

Write data to path as UTF-8, replacing any existing file.

Creates the file if it does not exist. Parent directories must already exist; use mkdir first if needed.

Files.basename ​

ts
static basename(path: string, suffix?: string): string

Return the last segment of path.

When suffix is given and the segment ends with it, the suffix is stripped from the result.

Files.dirname ​

ts
static dirname(path: string): string

Return the directory portion of path.

Returns "." when path has no directory component.

Files.extname ​

ts
static extname(path: string): string

Return the extension of path, including the leading dot.

Returns the empty string when there is no extension or the file name begins with a dot (e.g. .gitignore).

Files.formatPath ​

ts
static formatPath(parts: any): string

Build a string from a parsed-path object produced by parsePath.

base takes precedence over name + ext when both are provided, matching Node.js semantics.

Files.isAbsolute ​

ts
static isAbsolute(path: string): boolean

True when path is absolute on the host platform.

Files.join ​

ts
static join(...parts: string[]): string

Join all segments with the platform separator and normalize.

An absolute segment resets the accumulated path. Empty input returns ".".

Example

ts
import { Files } from "fs";
console.log(Files.join("assets", "img", "hero.png")); // assets/img/hero.png

Files.normalize ​

ts
static normalize(path: string): string

Collapse . and .. segments and redundant separators.

Returns "." for the empty string.

Files.parsePath ​

ts
static parsePath(path: string): any

Split path into { root, dir, base, name, ext }.

The shape mirrors Node.js's path.parse. Pass the result back through formatPath to round-trip.

Files.relative ​

ts
static relative(from: string, to: string): string

Compute the relative path from from to to.

Both inputs are resolved against the current working directory before the difference is computed.

Files.resolve ​

ts
static resolve(...parts: string[]): string

Resolve parts to an absolute path.

Segments are processed left to right; an absolute segment discards everything before it. Relative results are anchored at the current working directory.


Generated from lang/types/fs.d.tks in the ThicketScript repository.