summaryrefslogtreecommitdiff
path: root/std/fs/ensure_dir.ts
diff options
context:
space:
mode:
Diffstat (limited to 'std/fs/ensure_dir.ts')
-rw-r--r--std/fs/ensure_dir.ts40
1 files changed, 20 insertions, 20 deletions
diff --git a/std/fs/ensure_dir.ts b/std/fs/ensure_dir.ts
index dfc02f35c..de0cba333 100644
--- a/std/fs/ensure_dir.ts
+++ b/std/fs/ensure_dir.ts
@@ -1,49 +1,49 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import { getFileInfoType } from "./utils.ts";
+const { lstat, lstatSync, mkdir, mkdirSync, ErrorKind } = Deno;
+
/**
* Ensures that the directory exists.
* If the directory structure does not exist, it is created. Like mkdir -p.
+ * Requires the `--allow-read` and `--alow-write` flag.
*/
export async function ensureDir(dir: string): Promise<void> {
- let pathExists = false;
try {
- // if dir exists
- const stat = await Deno.stat(dir);
- pathExists = true;
- if (!stat.isDirectory()) {
+ const fileInfo = await lstat(dir);
+ if (!fileInfo.isDirectory()) {
throw new Error(
- `Ensure path exists, expected 'dir', got '${getFileInfoType(stat)}'`
+ `Ensure path exists, expected 'dir', got '${getFileInfoType(fileInfo)}'`
);
}
} catch (err) {
- if (pathExists) {
- throw err;
+ if (err instanceof Deno.DenoError && err.kind === ErrorKind.NotFound) {
+ // if dir not exists. then create it.
+ await mkdir(dir, true);
+ return;
}
- // if dir not exists. then create it.
- await Deno.mkdir(dir, true);
+ throw err;
}
}
/**
* Ensures that the directory exists.
* If the directory structure does not exist, it is created. Like mkdir -p.
+ * Requires the `--allow-read` and `--alow-write` flag.
*/
export function ensureDirSync(dir: string): void {
- let pathExists = false;
try {
- // if dir exists
- const stat = Deno.statSync(dir);
- pathExists = true;
- if (!stat.isDirectory()) {
+ const fileInfo = lstatSync(dir);
+ if (!fileInfo.isDirectory()) {
throw new Error(
- `Ensure path exists, expected 'dir', got '${getFileInfoType(stat)}'`
+ `Ensure path exists, expected 'dir', got '${getFileInfoType(fileInfo)}'`
);
}
} catch (err) {
- if (pathExists) {
- throw err;
+ if (err instanceof Deno.DenoError && err.kind == ErrorKind.NotFound) {
+ // if dir not exists. then create it.
+ mkdirSync(dir, true);
+ return;
}
- // if dir not exists. then create it.
- Deno.mkdirSync(dir, true);
+ throw err;
}
}