truncateSync

import { truncateSync } from "jsr:@ende/fs/unstable-truncate";

function · experimental · requires --allow-write

function truncateSync(name: string, len?: number): void

Synchronously truncates (or extends) the specified file, to reach the specified len. If len is not specified then the entire file contents are truncated.

Requires allow-write permission.

name: string

The name/path to the file.

len?: number

An optional value that sets the new size of the file. Omitting this argument sets the file size to 0 bytes.

Example: Truncate file to 0 bytes

import { truncateSync } from "@ende/fs/unstable-truncate";
truncateSync("my_file.txt");

Example: Truncate part of a file

import { makeTempFileSync } from "@ende/fs/unstable-make-temp-file";
import { readFileSync } from "@ende/fs/unstable-read-file";
import { truncateSync } from "@ende/fs/unstable-truncate";
import { writeFileSync } from "@ende/fs/unstable-write-file";
import { assertEquals } from "@ende/assert";

const file = makeTempFileSync();
writeFileSync(file, new TextEncoder().encode("Hello World"));
truncateSync(file, 7);
const data = readFileSync(file);
assertEquals(new TextDecoder().decode(data), "Hello W");

Source