fs
Files and directories.
Import
ts
import { FileStat, Files } from "fs";Needs the "fs" permission in thicket.json.
FileStat class
ts
class FileStatMetadata 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: float64Creation time in milliseconds since the Unix epoch.
FileStat.isDirectory
ts
isDirectory: booleanTrue when the entry is a directory.
FileStat.isFile
ts
isFile: booleanTrue when the entry is a regular file.
FileStat.modifiedMs
ts
modifiedMs: float64Modification time in milliseconds since the Unix epoch.
FileStat.size
ts
size: uintSize in bytes.
Files static class
ts
static class FilesFilesystem 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: stringThe path separator on this platform.
Always "/" on macOS and Linux, "\\" on Windows.
Files.delimiter
ts
static readonly delimiter: stringThe PATH environment variable delimiter on this platform.
":" on macOS and Linux, ";" on Windows.
Files.appendBytes
ts
static appendBytes(path: string, data: any): anyAppend raw bytes to path.
Creates the file if it does not exist.
Files.appendFile
ts
static appendFile(path: string, content: string): anyAppend content to path as UTF-8.
Creates the file if it does not exist.
Files.copy
ts
static copy(src: string, dest: string): anyCopy 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): anyResolve 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): anyTrue when the entry is a directory.
Files.isEmbedded
ts
static isEmbedded(path: string): booleanTrue 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): anyTrue when the entry is a regular file.
Files.mkdir
ts
static mkdir(path: string, recursive?: boolean): anyCreate 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): anyResolve 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): anyRead 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); // 3Files.readText
ts
static readText(path: string): anyRead 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")); // helloFiles.remove
ts
static remove(path: string, recursive?: boolean): anyRemove 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): anyRename 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): anySize in bytes.
Files.stat
ts
static stat(path: string): anyResolve to a FileStat describing path.
Throws when path does not exist.
Files.writeFile
ts
static writeFile(path: string, data: any): anyWrite 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): anyWrite 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): stringReturn 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): stringReturn the directory portion of path.
Returns "." when path has no directory component.
Files.extname
ts
static extname(path: string): stringReturn 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): stringBuild 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): booleanTrue when path is absolute on the host platform.
Files.join
ts
static join(...parts: string[]): stringJoin 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.pngFiles.normalize
ts
static normalize(path: string): stringCollapse . and .. segments and redundant separators.
Returns "." for the empty string.
Files.parsePath
ts
static parsePath(path: string): anySplit 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): stringCompute 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[]): stringResolve 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.