forked from denoland/deno
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Original: denoland/std@142a1c6
- Loading branch information
Showing
4 changed files
with
64 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. | ||
/** | ||
* Test whether or not the given path exists by checking with the file system | ||
* @export | ||
* @param {string} filePath | ||
* @returns {Promise<boolean>} | ||
*/ | ||
export async function exists(filePath: string): Promise<boolean> { | ||
return Deno.stat(filePath) | ||
.then(() => true) | ||
.catch(() => false); | ||
} | ||
|
||
/** | ||
* Test whether or not the given path exists by checking with the file system | ||
* @export | ||
* @param {string} filePath | ||
* @returns {boolean} | ||
*/ | ||
export function existsSync(filePath: string): boolean { | ||
try { | ||
Deno.statSync(filePath); | ||
return true; | ||
} catch { | ||
return false; | ||
} | ||
} |
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,36 @@ | ||
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. | ||
import { test } from "../testing/mod.ts"; | ||
import { assertEquals } from "../testing/asserts.ts"; | ||
import { exists, existsSync } from "./exists.ts"; | ||
import * as path from "./path/mod.ts"; | ||
|
||
const testdataDir = path.resolve("fs", "testdata"); | ||
|
||
test(async function existsFile() { | ||
assertEquals( | ||
await exists(path.join(testdataDir, "not_exist_file.ts")), | ||
false | ||
); | ||
assertEquals(await existsSync(path.join(testdataDir, "0.ts")), true); | ||
}); | ||
|
||
test(function existsFileSync() { | ||
assertEquals(existsSync(path.join(testdataDir, "not_exist_file.ts")), false); | ||
assertEquals(existsSync(path.join(testdataDir, "0.ts")), true); | ||
}); | ||
|
||
test(async function existsDirectory() { | ||
assertEquals( | ||
await exists(path.join(testdataDir, "not_exist_directory")), | ||
false | ||
); | ||
assertEquals(existsSync(testdataDir), true); | ||
}); | ||
|
||
test(function existsDirectorySync() { | ||
assertEquals( | ||
existsSync(path.join(testdataDir, "not_exist_directory")), | ||
false | ||
); | ||
assertEquals(existsSync(testdataDir), true); | ||
}); |
Empty file.
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