-
Notifications
You must be signed in to change notification settings - Fork 576
/
_fs_truncate.ts
33 lines (27 loc) · 1.04 KB
/
_fs_truncate.ts
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
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
import { CallbackWithError } from "./_fs_common.ts";
import { fromFileUrl } from "../path.ts";
import { promisify } from "../internal/util.mjs";
export function truncate(
path: string | URL,
lenOrCallback: number | CallbackWithError,
maybeCallback?: CallbackWithError,
) {
path = path instanceof URL ? fromFileUrl(path) : path;
const len: number | undefined = typeof lenOrCallback === "number"
? lenOrCallback
: undefined;
const callback: CallbackWithError = typeof lenOrCallback === "function"
? lenOrCallback
: maybeCallback as CallbackWithError;
if (!callback) throw new Error("No callback function supplied");
Deno.truncate(path, len).then(() => callback(null), callback);
}
export const truncatePromise = promisify(truncate) as (
path: string | URL,
len?: number,
) => Promise<void>;
export function truncateSync(path: string | URL, len?: number) {
path = path instanceof URL ? fromFileUrl(path) : path;
Deno.truncateSync(path, len);
}