All files / fs / unstable_truncate.ts

33.33% Branches 1/3
41.67% Lines 10/24
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
 
 
x1
x1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x1
x7
x7
 
 
 
 
 
 
 
x7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x1
x7
x7
 
 
 
 
 
 
 
x7






































I











































I






// Copyright 2018-2025 the Deno authors. MIT license.

import { getNodeFs, isDeno } from "./_utils.ts";
import { mapError } from "./_map_error.ts";

/**
 * 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.
 *
 * @example Truncate file to 0 bytes
 * ```ts ignore
 * import { truncate } from "@std/fs/unstable-truncate";
 * await truncate("my_file.txt");
 * ```
 *
 * @example Truncate part of a file
 * ```ts ignore
 * import { makeTempFile } from "@std/fs/unstable-make-temp-file";
 * import { readFile } from "@std/fs/unstable-read-file";
 * import { truncate } from "@std/fs/unstable-truncate";
 * import { writeTextFile } from "@std/fs/unstable-write-text-file";
 *
 * const file = await makeTempFile();
 * await writeTextFile(file, "Hello World");
 * await truncate(file, 7);
 * const data = await readFile(file);
 * console.log(new TextDecoder().decode(data));  // "Hello W"
 * ```
 *
 * @tags allow-write
 *
 * @param name The name/path to the file.
 * @param len An optional value that sets the new size of the file. Omitting this argument sets the file size to 0 bytes.
 */
export async function truncate(name: string, len?: number): Promise<void> {
  if (isDeno) {
    await Deno.truncate(name, len);
  } else {
    try {
      await getNodeFs().promises.truncate(name, len);
    } catch (error) {
      throw mapError(error);
    }
  }
}

/**
 * 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.
 *
 * @example Truncate file to 0 bytes
 * ```ts ignore
 * import { truncateSync } from "@std/fs/unstable-truncate";
 * truncateSync("my_file.txt");
 * ```
 *
 * @example Truncate part of a file
 * ```ts ignore
 * import { makeTempFileSync } from "@std/fs/unstable-make-temp-file";
 * import { readFileSync } from "@std/fs/unstable-read-file";
 * import { truncateSync } from "@std/fs/unstable-truncate";
 * import { writeFileSync } from "@std/fs/unstable-write-file";
 *
 * const file = makeTempFileSync();
 * writeFileSync(file, new TextEncoder().encode("Hello World"));
 * truncateSync(file, 7);
 * const data = readFileSync(file);
 * console.log(new TextDecoder().decode(data));
 * ```
 *
 * @tags allow-write
 *
 * @param name The name/path to the file.
 * @param len An optional value that sets the new size of the file. Omitting this argument sets the file size to 0 bytes.
 */
export function truncateSync(name: string, len?: number): void {
  if (isDeno) {
    Deno.truncateSync(name, len);
  } else {
    try {
      getNodeFs().truncateSync(name, len);
    } catch (error) {
      throw mapError(error);
    }
  }
}