2024-01-01 21:11:32 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2024-04-29 02:57:30 +00:00
|
|
|
import { join } from "@std/path/join";
|
2023-12-21 07:39:51 +00:00
|
|
|
import { toPathString } from "./_to_path_string.ts";
|
2020-06-12 19:23:38 +00:00
|
|
|
|
2019-03-11 18:19:52 +00:00
|
|
|
/**
|
2024-06-07 03:54:50 +00:00
|
|
|
* Asynchronously ensures that a directory is empty.
|
2024-03-27 06:28:06 +00:00
|
|
|
*
|
2024-06-07 03:54:50 +00:00
|
|
|
* If the directory does not exist, it is created. The directory itself is not
|
|
|
|
* deleted.
|
|
|
|
*
|
|
|
|
* Requires `--allow-read` and `--allow-write` permissions.
|
|
|
|
*
|
|
|
|
* @see {@link https://docs.deno.com/runtime/manual/basics/permissions#file-system-access}
|
|
|
|
* for more information on Deno's permissions system.
|
2022-11-25 11:40:23 +00:00
|
|
|
*
|
2024-03-27 06:28:06 +00:00
|
|
|
* @param dir The path of the directory to empty, as a string or URL.
|
2024-06-07 03:54:50 +00:00
|
|
|
*
|
2024-03-27 06:28:06 +00:00
|
|
|
* @returns A void promise that resolves once the directory is empty.
|
|
|
|
*
|
2024-06-07 03:54:50 +00:00
|
|
|
* @example Usage
|
|
|
|
* ```ts no-eval
|
2024-04-29 02:57:30 +00:00
|
|
|
* import { emptyDir } from "@std/fs/empty-dir";
|
2022-11-25 11:40:23 +00:00
|
|
|
*
|
2024-03-27 06:28:06 +00:00
|
|
|
* await emptyDir("./foo");
|
2022-11-25 11:40:23 +00:00
|
|
|
* ```
|
2019-03-11 18:19:52 +00:00
|
|
|
*/
|
2022-08-30 03:58:48 +00:00
|
|
|
export async function emptyDir(dir: string | URL) {
|
2019-03-11 18:19:52 +00:00
|
|
|
try {
|
2023-11-10 19:00:28 +00:00
|
|
|
const items = await Array.fromAsync(Deno.readDir(dir));
|
2019-12-18 10:12:36 +00:00
|
|
|
|
2023-10-12 06:16:53 +00:00
|
|
|
await Promise.all(items.map((item) => {
|
2019-12-18 10:12:36 +00:00
|
|
|
if (item && item.name) {
|
2022-08-30 03:58:48 +00:00
|
|
|
const filepath = join(toPathString(dir), item.name);
|
2023-10-12 06:16:53 +00:00
|
|
|
return Deno.remove(filepath, { recursive: true });
|
2019-12-18 10:12:36 +00:00
|
|
|
}
|
2023-10-12 06:16:53 +00:00
|
|
|
}));
|
2019-12-18 10:12:36 +00:00
|
|
|
} catch (err) {
|
2020-02-24 20:48:35 +00:00
|
|
|
if (!(err instanceof Deno.errors.NotFound)) {
|
2019-12-18 10:12:36 +00:00
|
|
|
throw err;
|
2019-03-11 18:19:52 +00:00
|
|
|
}
|
2019-12-18 10:12:36 +00:00
|
|
|
|
|
|
|
// if not exist. then create it
|
2020-06-12 19:23:38 +00:00
|
|
|
await Deno.mkdir(dir, { recursive: true });
|
2019-03-11 18:19:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-03-27 06:28:06 +00:00
|
|
|
* Synchronously ensures that a directory is empty deletes the directory
|
2024-06-07 03:54:50 +00:00
|
|
|
* contents it is not empty.
|
2024-03-27 06:28:06 +00:00
|
|
|
*
|
2024-06-07 03:54:50 +00:00
|
|
|
* If the directory does not exist, it is created. The directory itself is not
|
|
|
|
* deleted.
|
|
|
|
*
|
|
|
|
* Requires `--allow-read` and `--allow-write` permissions.
|
|
|
|
*
|
|
|
|
* @see {@link https://docs.deno.com/runtime/manual/basics/permissions#file-system-access}
|
|
|
|
* for more information on Deno's permissions system.
|
2022-11-25 11:40:23 +00:00
|
|
|
*
|
2024-03-27 06:28:06 +00:00
|
|
|
* @param dir The path of the directory to empty, as a string or URL.
|
2024-06-07 03:54:50 +00:00
|
|
|
*
|
2024-03-27 06:28:06 +00:00
|
|
|
* @returns A void value that returns once the directory is empty.
|
|
|
|
*
|
2024-06-07 03:54:50 +00:00
|
|
|
* @example Usage
|
|
|
|
* ```ts no-eval
|
2024-04-29 02:57:30 +00:00
|
|
|
* import { emptyDirSync } from "@std/fs/empty-dir";
|
2022-11-25 11:40:23 +00:00
|
|
|
*
|
2024-03-27 06:28:06 +00:00
|
|
|
* emptyDirSync("./foo");
|
2022-11-25 11:40:23 +00:00
|
|
|
* ```
|
2019-03-11 18:19:52 +00:00
|
|
|
*/
|
2022-08-30 03:58:48 +00:00
|
|
|
export function emptyDirSync(dir: string | URL) {
|
2019-03-11 18:19:52 +00:00
|
|
|
try {
|
2020-06-12 19:23:38 +00:00
|
|
|
const items = [...Deno.readDirSync(dir)];
|
2019-12-18 10:12:36 +00:00
|
|
|
|
2020-05-17 17:24:39 +00:00
|
|
|
// If the directory exists, remove all entries inside it.
|
2019-12-18 10:12:36 +00:00
|
|
|
while (items.length) {
|
|
|
|
const item = items.shift();
|
|
|
|
if (item && item.name) {
|
2022-08-30 03:58:48 +00:00
|
|
|
const filepath = join(toPathString(dir), item.name);
|
2020-06-12 19:23:38 +00:00
|
|
|
Deno.removeSync(filepath, { recursive: true });
|
2019-12-18 10:12:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (err) {
|
2020-02-24 20:48:35 +00:00
|
|
|
if (!(err instanceof Deno.errors.NotFound)) {
|
2019-12-18 10:12:36 +00:00
|
|
|
throw err;
|
|
|
|
}
|
2019-03-11 18:19:52 +00:00
|
|
|
// if not exist. then create it
|
2020-06-12 19:23:38 +00:00
|
|
|
Deno.mkdirSync(dir, { recursive: true });
|
2019-03-11 18:19:52 +00:00
|
|
|
}
|
|
|
|
}
|