Skip to content

Commit

Permalink
fix: fixed minor bug with utils/file exists() function
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahmed Osama Ibrahim committed Jan 13, 2025
1 parent 1efa63d commit 7eb0864
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/utils/file.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import { readdir, lstat, stat } from 'fs/promises';
import { readdir, lstat, access } from 'fs/promises';
import type { Dirent } from 'fs';
import * as path from 'path';

export const exists = async (path: string): Promise<boolean> => {
return !!(await stat(path));
try {
await access(path);

return true;
} catch {
return false;
}
};

export function mapAsync<T, U>(
Expand Down
15 changes: 15 additions & 0 deletions tests/utils/file.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { join } from 'node:path';

import { exists } from '../../src/utils/file';

describe('file util tests', () => {
describe('exists() tests', () => {
it('should return true if the file exists', async () => {
await expect(exists(join(__dirname, 'test-file.txt'))).resolves.toBe(true);
});

it('should return false if the file does not exist', async () => {
await expect(exists(join(__dirname, 'not-test-file.txt'))).resolves.toBe(false);
});
});
});
Empty file added tests/utils/test-file.txt
Empty file.

0 comments on commit 7eb0864

Please sign in to comment.