Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Loading