File System
The std/fs module provides asynchronous file read/write operations and directory management.
Import
Section titled “Import”import { fs } from "std/fs"Reading Files
Section titled “Reading Files”fs.readFile(path: string): Task<string>
Section titled “fs.readFile(path: string): Task<string>”Reads a file asynchronously and returns its contents as a string.
const content = await fs.readFile("./config.json")println(content)Writing Files
Section titled “Writing Files”fs.writeFile(path: string, content: string): Task<void>
Section titled “fs.writeFile(path: string, content: string): Task<void>”Writes content to a file asynchronously. Creates the file if it doesn’t exist, or overwrites it if it does.
await fs.writeFile("./log.txt", "Log entry")fs.appendFile(path: string, content: string): Task<void>
Section titled “fs.appendFile(path: string, content: string): Task<void>”Appends content to an existing file.
await fs.appendFile("./log.txt", "\nAnother entry")File & Directory Info
Section titled “File & Directory Info”fs.exists(path: string): bool
Section titled “fs.exists(path: string): bool”Returns true if the given path exists.
if (fs.exists("./config.json")) { println("Config found")}fs.stat(path: string): any
Section titled “fs.stat(path: string): any”Returns file metadata including size, modification time, and whether it’s a directory.
const info = fs.stat("./data.txt")println(info["size"]) // File size in bytesprintln(info["isDir"]) // falseprintln(info["modTime"]) // Last modification timeDirectory Operations
Section titled “Directory Operations”fs.mkdir(path: string): void
Section titled “fs.mkdir(path: string): void”Creates a directory at the given path.
fs.mkdir("./output")fs.readDir(path: string): []string
Section titled “fs.readDir(path: string): []string”Lists the contents of a directory.
const files = fs.readDir("./src")files.forEach(function(f: any, i: any, a: any): any { println(f)})fs.remove(path: string): void
Section titled “fs.remove(path: string): void”Removes a file or directory.
fs.remove("./temp.txt")Path Utilities
Section titled “Path Utilities”fs.abs(path: string): string
Section titled “fs.abs(path: string): string”Returns the absolute path.
const full = fs.abs("./config.json")println(full) // e.g. "/home/user/project/config.json"fs.join(...parts: string): string
Section titled “fs.join(...parts: string): string”Joins path segments together.
const p = fs.join("src", "utils", "helpers.chuks")println(p) // "src/utils/helpers.chuks"Function Reference
Section titled “Function Reference”| Function | Description |
|---|---|
fs.readFile(path) | Read file contents as string |
fs.writeFile(path, content) | Write string to file |
fs.appendFile(path, content) | Append string to file |
fs.exists(path) | Check if path exists |
fs.mkdir(path) | Create directory |
fs.remove(path) | Remove file or directory |
fs.readDir(path) | List directory contents |
fs.stat(path) | File metadata (size, modTime, isDir) |
fs.abs(path) | Absolute path |
fs.join(parts...) | Join path segments |
Example: Read, Transform, Write
Section titled “Example: Read, Transform, Write”import { fs } from "std/fs"import { json } from "std/json"
// Read a JSON configconst raw = await fs.readFile("./config.json")const config = json.parse(raw)
// Modify and write backconfig["version"] = 2await fs.writeFile("./config.json", json.stringify(config))println("Config updated")