2023-01-03 10:47:44 +00:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2023-08-14 07:08:42 +00:00
|
|
|
import { join } from "../path/join.ts";
|
2022-08-30 03:58:48 +00:00
|
|
|
import { toPathString } from "./_util.ts";
|
2020-06-12 19:23:38 +00:00
|
|
|
|
2019-03-11 18:19:52 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
2020-05-16 18:36:13 +00:00
|
|
|
* Requires the `--allow-read` and `--allow-write` flag.
|
2022-11-25 11:40:23 +00:00
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* ```ts
|
|
|
|
* import { emptyDir } from "https://deno.land/std@$STD_VERSION/fs/mod.ts";
|
|
|
|
*
|
|
|
|
* emptyDir("./foo"); // returns a promise
|
|
|
|
* ```
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
2020-05-16 18:36:13 +00:00
|
|
|
* Requires the `--allow-read` and `--allow-write` flag.
|
2022-11-25 11:40:23 +00:00
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* ```ts
|
|
|
|
* import { emptyDirSync } from "https://deno.land/std@$STD_VERSION/fs/mod.ts";
|
|
|
|
*
|
|
|
|
* emptyDirSync("./foo"); // void
|
|
|
|
* ```
|
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
|
|
|
}
|
|
|
|
}
|