Appearance
std/fs
From the spec
This page is spec/std/fs.md, the specification the compiler and the backends are written from. It is rebuilt from the spec with every deploy (how).
Files and directories, for the programs weft run runs: scripts, command-line tools, servers (core/crates/weft-check/src/std/fs.weft declares it; the server runtime implements it on its I/O threads, server.md). It is an I/O module: a package that imports it has the fs capability (docs/packages.md), and sandboxed user code can't import it (sandbox.md). Relative paths are from the working directory.
weft
import { readText, writeText, list, join } from "std/fs"
import { args } from "std/process"
let dir = args()[0] ?? "."
let mut total = 0
for entry of try await list(dir) {
if entry.kind == "file" && entry.name.endsWith(".md") {
let text = try await readText(join(dir, entry.name))
total += text.split("\n").length
}
}
try await writeText(join(dir, "lines.txt"), `${total}\n`)readText(path): Task<string> | the file as UTF-8 (InvalidData when it isn't) |
readBytes(path): Task<Bytes> | the file |
writeText(path, text, options?), writeBytes(path, data, options?): Task<void> | creates the file or replaces what it held; WriteOptions = { append?, createDirs? }: add to the end, make missing parent directories |
exists(path): Task<boolean> | whether something is there |
stat(path): Task<FileInfo?> | { kind, size, modified, created, readonly } of what is there (through symlinks), null for nothing; kind is "file" | "dir" | "symlink" | "other", times in ms since the epoch (null where the system keeps none) |
list(path): Task<DirEntry[]> | a directory's entries { name, kind } (the entry's own kind), sorted by name |
mkdir(path): Task<void> | the directory and its missing parents (nothing when it exists) |
remove(path, options?: { recursive? }): Task<void> | a file, or a directory: empty, or with everything in it when recursive |
rename(from, to), copy(from, to): Task<void> | move (replacing a file at to), copy a file |
tempDir(prefix = "weft-"): Task<string> | a new empty directory under the system's temporary directory |
cwd(): string | the working directory |
Every operation but exists, cwd and the paths is Task<R ! FsError>: FsError { code, path, message }, code (FsCode) one of NotFound, PermissionDenied, AlreadyExists, NotADirectory, IsADirectory, DirectoryNotEmpty, InvalidData, Other.
Paths, as Node's path on POSIX (no I/O): join(...parts) (one / between, . and .. resolved, "." for nothing), resolve(...parts) (absolute, from the working directory, the last absolute part winning), dirname(path), basename(path, ext = ""), extname(path) (with its dot; "" for none and for a leading-dot name).
Not yet: watching for changes, file handles and streamed reads and writes (readBytes and writeBytes hold the whole file), permissions and symlinks made by code.