deno.land / std@0.157.0 / fs / empty_dir.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
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
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.import { join } from "../path/mod.ts";import { toPathString } from "./_util.ts";
/** * Ensures that a directory is empty. * Deletes directory contents if the directory is not empty. * If the directory does not exist, it is created. * The directory itself is not deleted. * Requires the `--allow-read` and `--allow-write` flag. */export async function emptyDir(dir: string | URL) { try { const items = []; for await (const dirEntry of Deno.readDir(dir)) { items.push(dirEntry); }
while (items.length) { const item = items.shift(); if (item && item.name) { const filepath = join(toPathString(dir), item.name); await Deno.remove(filepath, { recursive: true }); } } } catch (err) { if (!(err instanceof Deno.errors.NotFound)) { throw err; }
// if not exist. then create it await Deno.mkdir(dir, { recursive: true }); }}
/** * Ensures that a directory is empty. * Deletes directory contents if the directory is not empty. * If the directory does not exist, it is created. * The directory itself is not deleted. * Requires the `--allow-read` and `--allow-write` flag. */export function emptyDirSync(dir: string | URL) { try { const items = [...Deno.readDirSync(dir)];
// If the directory exists, remove all entries inside it. while (items.length) { const item = items.shift(); if (item && item.name) { const filepath = join(toPathString(dir), item.name); Deno.removeSync(filepath, { recursive: true }); } } } catch (err) { if (!(err instanceof Deno.errors.NotFound)) { throw err; } // if not exist. then create it Deno.mkdirSync(dir, { recursive: true }); return; }}
std

Version Info

Tagged at
a year ago