writeFileSync
writeFileSync escreve dados em um arquivo. Se o arquivo não existir, é criado; se existir, é sobrescrito.
Assinatura
writeFileSync(path: string, data: string): Result<void, FsError>tyrus
import { writeFileSync, type FsError } from "tyrus:fs";
match (writeFileSync("message.txt", "hello from tyrus")) {
Ok(_) => { print("file written"); }
Err(error) => { printError(error.code); // ERR_FS_WRITE_FAILED
}
}Tratamento de erros
Se o arquivo não puder ser escrito, writeFileSync retorna Err(FsError) com um campo code estável. Inspecione o resultado com match ou propague com ?:
error
tyrus
import { writeFileSync, type FsError } from "tyrus:fs";
match (writeFileSync("message.txt", "hello from tyrus")) {
Ok(_) => { print("file written"); }
Err(error) => { printError(error.code); // ERR_FS_WRITE_FAILED
}
}Copiar um arquivo
tyrus
import { readFileSync, writeFileSync, type FsError } from "tyrus:fs";
const main = (): void => {
match (readFileSync("README.md")) {
Ok(source) => {
match (writeFileSync("target/readme-copy.md", source)) {
Ok() => print("file copied");
Err(error) => printError(error.message);
}
}
Err(error) => printError(error.message);
}
};