diff options
| author | Axetroy <troy450409405@gmail.com> | 2019-03-12 13:52:43 +0800 |
|---|---|---|
| committer | Ryan Dahl <ry@tinyclouds.org> | 2019-03-12 01:52:43 -0400 |
| commit | 1bfc46bbd2d3cd362488034e4a2b8c4d5a4f1f16 (patch) | |
| tree | 642ca8439c6dcd61ec601b27e4758a887cc4031c /fs/ensure_dir.ts | |
| parent | f124e645263099132025256df8594ca0dc5cc83a (diff) | |
feat: add ensureDir/ensureFile for fs modules (denoland/deno_std#264)
Original: https://github.com/denoland/deno_std/commit/9ae941338aea09adf0d4f48d40f2b09131ae3d5f
Diffstat (limited to 'fs/ensure_dir.ts')
| -rw-r--r-- | fs/ensure_dir.ts | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/fs/ensure_dir.ts b/fs/ensure_dir.ts new file mode 100644 index 000000000..5a5cc0a01 --- /dev/null +++ b/fs/ensure_dir.ts @@ -0,0 +1,32 @@ +// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. +/** + * Ensures that the directory exists. If the directory structure does not exist, it is created. Like mkdir -p. + * @export + * @param {string} dir + * @returns {Promise<void>} + */ +export async function ensureDir(dir: string): Promise<void> { + try { + // if dir exists + await Deno.stat(dir); + } catch { + // 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 + * @param {string} dir + * @returns {void} + */ +export function ensureDirSync(dir: string): void { + try { + // if dir exists + Deno.statSync(dir); + } catch { + // if dir not exists. then create it. + Deno.mkdirSync(dir, true); + } +} |
