mirror of
https://github.com/denoland/std.git
synced 2024-11-21 20:50:22 +00:00
add fs/exists (#260)
This commit is contained in:
parent
5852cd251b
commit
142a1c6cf8
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
Loading…
Reference in New Issue
Block a user