diff options
author | Ryan Dahl <ry@tinyclouds.org> | 2019-10-09 17:18:08 -0400 |
---|---|---|
committer | Ryan Dahl <ry@tinyclouds.org> | 2019-10-09 17:18:08 -0400 |
commit | 28293acd9c12a94f5d769706291032e844c7b92b (patch) | |
tree | 1fec6a3cd8d7c9e8bc9b1486f5c8438eb906a595 /std/fs/ensure_dir.ts | |
parent | 5c6835efd82c298df99ce71c4a36ca23515333a3 (diff) | |
parent | 151ce0266eb4de2c8fc600c81c192a5f791b6169 (diff) |
Merge branch 'std_modified' into merge_std3
Diffstat (limited to 'std/fs/ensure_dir.ts')
-rw-r--r-- | std/fs/ensure_dir.ts | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/std/fs/ensure_dir.ts b/std/fs/ensure_dir.ts new file mode 100644 index 000000000..dfc02f35c --- /dev/null +++ b/std/fs/ensure_dir.ts @@ -0,0 +1,49 @@ +// 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 + 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); + } +} + +/** + * Ensures that the directory exists. + * 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 + 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); + } +} |