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.exists (denoland/deno#3493)
- Loading branch information
Showing
4 changed files
with
132 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,35 @@ | ||
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. | ||
|
||
const { lstat, lstatSync, DenoError, ErrorKind } = Deno; | ||
/** | ||
* Test whether or not the given path exists by checking with the file system | ||
*/ | ||
export async function exists(filePath: string): Promise<boolean> { | ||
return Deno.lstat(filePath) | ||
return lstat(filePath) | ||
.then((): boolean => true) | ||
.catch((): boolean => false); | ||
.catch((err: Error): boolean => { | ||
if (err instanceof DenoError) { | ||
if (err.kind === ErrorKind.NotFound) { | ||
return false; | ||
} | ||
} | ||
|
||
throw err; | ||
}); | ||
} | ||
|
||
/** | ||
* Test whether or not the given path exists by checking with the file system | ||
*/ | ||
export function existsSync(filePath: string): boolean { | ||
try { | ||
Deno.lstatSync(filePath); | ||
lstatSync(filePath); | ||
return true; | ||
} catch { | ||
return false; | ||
} catch (err) { | ||
if (err instanceof DenoError) { | ||
if (err.kind === ErrorKind.NotFound) { | ||
return false; | ||
} | ||
} | ||
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
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { exists } from "../exists.ts"; | ||
|
||
exists(Deno.args[1]) | ||
.then(isExist => { | ||
Deno.stdout.write(new TextEncoder().encode(isExist ? 'exist' :'not exist')) | ||
}) | ||
.catch((err) => { | ||
Deno.stdout.write(new TextEncoder().encode(err.message)) | ||
}) | ||
|
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { existsSync } from "../exists.ts"; | ||
|
||
try { | ||
const isExist = existsSync(Deno.args[1]) | ||
Deno.stdout.write(new TextEncoder().encode(isExist ? 'exist' :'not exist')) | ||
} catch (err) { | ||
Deno.stdout.write(new TextEncoder().encode(err.message)) | ||
} | ||
|
||
|