mirror of
https://github.com/denoland/deno.git
synced 2024-12-27 17:49:08 -05:00
add fs/exists (#260)
This commit is contained in:
parent
5852cd251b
commit
142a1c6cf8
4 changed files with 64 additions and 0 deletions
27
fs/exists.ts
Normal file
27
fs/exists.ts
Normal file
|
@ -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;
|
||||
}
|
||||
}
|
36
fs/exists_test.ts
Normal file
36
fs/exists_test.ts
Normal file
|
@ -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);
|
||||
});
|
0
fs/testdata/0.ts
vendored
Normal file
0
fs/testdata/0.ts
vendored
Normal file
1
test.ts
1
test.ts
|
@ -14,6 +14,7 @@ import "./fs/path/test.ts";
|
|||
import "./fs/walk_test.ts";
|
||||
import "./fs/globrex_test.ts";
|
||||
import "./fs/glob_test.ts";
|
||||
import "./fs/exists_test.ts";
|
||||
import "./io/test.ts";
|
||||
import "./http/server_test.ts";
|
||||
import "./http/file_server_test.ts";
|
||||
|
|
Loading…
Reference in a new issue