summaryrefslogtreecommitdiff
path: root/fs/ensure_dir.ts
diff options
context:
space:
mode:
Diffstat (limited to 'fs/ensure_dir.ts')
-rw-r--r--fs/ensure_dir.ts30
1 files changed, 25 insertions, 5 deletions
diff --git a/fs/ensure_dir.ts b/fs/ensure_dir.ts
index e7e4f69a2..dfc02f35c 100644
--- a/fs/ensure_dir.ts
+++ b/fs/ensure_dir.ts
@@ -1,14 +1,24 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
-
+import { getFileInfoType } from "./utils.ts";
/**
* Ensures that the directory exists.
* If the directory structure does not exist, it is created. Like mkdir -p.
*/
export async function ensureDir(dir: string): Promise<void> {
+ let pathExists = false;
try {
// if dir exists
- await Deno.stat(dir);
- } catch {
+ const stat = await Deno.stat(dir);
+ pathExists = true;
+ if (!stat.isDirectory()) {
+ throw new Error(
+ `Ensure path exists, expected 'dir', got '${getFileInfoType(stat)}'`
+ );
+ }
+ } catch (err) {
+ if (pathExists) {
+ throw err;
+ }
// if dir not exists. then create it.
await Deno.mkdir(dir, true);
}
@@ -19,10 +29,20 @@ export async function ensureDir(dir: string): Promise<void> {
* If the directory structure does not exist, it is created. Like mkdir -p.
*/
export function ensureDirSync(dir: string): void {
+ let pathExists = false;
try {
// if dir exists
- Deno.statSync(dir);
- } catch {
+ const stat = Deno.statSync(dir);
+ pathExists = true;
+ if (!stat.isDirectory()) {
+ throw new Error(
+ `Ensure path exists, expected 'dir', got '${getFileInfoType(stat)}'`
+ );
+ }
+ } catch (err) {
+ if (pathExists) {
+ throw err;
+ }
// if dir not exists. then create it.
Deno.mkdirSync(dir, true);
}