diff options
author | Axetroy <axetroy.dev@gmail.com> | 2019-12-18 22:45:19 +0800 |
---|---|---|
committer | Ry Dahl <ry@tinyclouds.org> | 2019-12-18 09:45:19 -0500 |
commit | ef30d376db94bda24ed31947a1cc264b827705dc (patch) | |
tree | aba58a06c797efd513a1a83ba056487e18674d6f /std/fs/ensure_dir.ts | |
parent | 1c09cc63c8b9c33842ee302c93aebc6593be8b15 (diff) |
fix permission errors are swallowed by fs.copy() (#3504)
Diffstat (limited to 'std/fs/ensure_dir.ts')
-rw-r--r-- | std/fs/ensure_dir.ts | 40 |
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; } } |