summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--fs/exists.ts27
-rw-r--r--fs/exists_test.ts36
-rw-r--r--fs/testdata/0.ts0
-rwxr-xr-xtest.ts1
4 files changed, 64 insertions, 0 deletions
diff --git a/fs/exists.ts b/fs/exists.ts
new file mode 100644
index 000000000..aa961037d
--- /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<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;
+ }
+}
diff --git a/fs/exists_test.ts b/fs/exists_test.ts
new file mode 100644
index 000000000..3d781108e
--- /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 000000000..e69de29bb
--- /dev/null
+++ b/fs/testdata/0.ts
diff --git a/test.ts b/test.ts
index 09ce823de..63e5ded7b 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";