Skip to content

Commit

Permalink
add fs/exists (denoland/std#260)
Browse files Browse the repository at this point in the history
  • Loading branch information
axetroy authored and ry committed Mar 11, 2019
1 parent 8127cfb commit ef6d932
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 0 deletions.
27 changes: 27 additions & 0 deletions fs/exists.ts
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;
}
}
36 changes: 36 additions & 0 deletions fs/exists_test.ts
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 added fs/testdata/0.ts
Empty file.
1 change: 1 addition & 0 deletions test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down

0 comments on commit ef6d932

Please sign in to comment.