From ef6d93235800161343281972e9bdc1b6b69e310e Mon Sep 17 00:00:00 2001 From: Axetroy Date: Tue, 12 Mar 2019 01:14:26 +0800 Subject: [PATCH] add fs/exists (denoland/deno_std#260) Original: https://github.com/denoland/deno_std/commit/142a1c6cf890e5c7626c4c934496fae1eee01715 --- fs/exists.ts | 27 +++++++++++++++++++++++++++ fs/exists_test.ts | 36 ++++++++++++++++++++++++++++++++++++ fs/testdata/0.ts | 0 test.ts | 1 + 4 files changed, 64 insertions(+) create mode 100644 fs/exists.ts create mode 100644 fs/exists_test.ts create mode 100644 fs/testdata/0.ts diff --git a/fs/exists.ts b/fs/exists.ts new file mode 100644 index 00000000000000..aa961037d94db0 --- /dev/null +++ b/fs/exists.ts @@ -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} + */ +export async function exists(filePath: string): Promise { + 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; + } +} diff --git a/fs/exists_test.ts b/fs/exists_test.ts new file mode 100644 index 00000000000000..3d781108e17505 --- /dev/null +++ b/fs/exists_test.ts @@ -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); +}); diff --git a/fs/testdata/0.ts b/fs/testdata/0.ts new file mode 100644 index 00000000000000..e69de29bb2d1d6 diff --git a/test.ts b/test.ts index 09ce823dec9a0f..63e5ded7b227df 100755 --- a/test.ts +++ b/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";