std/fs/empty_dir.ts
Lino Le Van c46143f0ac
chore: update copyright year (#4046)
* chore: update copyright year

* fix

---------

Co-authored-by: Asher Gomez <ashersaupingomez@gmail.com>
2024-01-02 08:11:32 +11:00

73 lines
2.0 KiB
TypeScript

// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import { join } from "../path/join.ts";
import { toPathString } from "./_to_path_string.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.
*
* @example
* ```ts
* import { emptyDir } from "https://deno.land/std@$STD_VERSION/fs/mod.ts";
*
* emptyDir("./foo"); // returns a promise
* ```
*/
export async function emptyDir(dir: string | URL) {
try {
const items = await Array.fromAsync(Deno.readDir(dir));
await Promise.all(items.map((item) => {
if (item && item.name) {
const filepath = join(toPathString(dir), item.name);
return 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.
*
* @example
* ```ts
* import { emptyDirSync } from "https://deno.land/std@$STD_VERSION/fs/mod.ts";
*
* emptyDirSync("./foo"); // void
* ```
*/
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 });
}
}