summaryrefslogtreecommitdiff
path: root/fs/ensure_dir.ts
blob: 5a5cc0a01329404a084d1ed6c61a9958e254a5ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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);
  }
}