forked from denoland/std
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix permission errors are swallowed by fs.copy() (denoland/deno#3504)
- Loading branch information
Showing
3 changed files
with
71 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters