writeFile
writeFile writes string data to a file asynchronously. If the file does not exist, it is created; if it exists, it is overwritten.
Signature
writeFile(path: string, data: string): Promise<Result<void, FsError>>tyrus
import { writeFile, FsError } from "tyrus:fs";
match (await writeFile("message.txt", "hello from tyrus")) {
Ok(_) => { print("write complete"); } // write complete
Err(error) => { printError(error.code); } // ERR_FS_WRITE_FAILED
}Error Handling
If the file cannot be written, the fulfilled promise contains Err(FsError). Match the returned result:
error
tyrus
import { FsError, writeFile } from "tyrus:fs";
match (await writeFile("message.txt", "hello from tyrus")) {
Ok(_) => { print("write complete"); } // write complete
Err(error) => { printError(error.code); } // ERR_FS_WRITE_FAILED
}Copy a File
tyrus
import { FsError, readFile, writeFile } from "tyrus:fs";
match (await readFile("README.md")) {
Ok(source) => {
match (await writeFile("target/readme-copy.md", source)) {
Ok(_) => { print("copy complete"); } // copy complete
Err(error) => { printError(error.code); } // ERR_FS_WRITE_FAILED
}
}
Err(error) => { printError(error.code); } // ERR_FS_READ_FAILED
}