Skip to content

Commit

Permalink
feat: 🎸 implement exists() method
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Jun 17, 2023
1 parent c72390b commit 0753937
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
5 changes: 1 addition & 4 deletions src/fsa-to-node/FsaNodeFs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,10 +308,7 @@ export class FsaNodeFs implements FsCallbackApi {
public readonly exists: FsCallbackApi['exists'] = (path: misc.PathLike, callback: (exists: boolean) => void): void => {
const filename = pathToFilename(path);
if (typeof callback !== 'function') throw Error(ERRSTR.CB);
const [folder, name] = pathToLocation(filename);
(async () => {
// const stats = await new Promise();
})().then(() => callback(true), () => callback(false));
this.access(path, AMODE.F_OK, (error) => callback(!error));
};

public readonly access: FsCallbackApi['access'] = (path: misc.PathLike, a: misc.TCallback<void> | number, b?: misc.TCallback<void>) => {
Expand Down
22 changes: 14 additions & 8 deletions src/fsa-to-node/__tests__/FsaNodeFs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -355,14 +355,20 @@ describe('.write()', () => {

describe('.exists()', () => {
test('can works for folders and files', async () => {
// const { fs, mfs } = setup({ folder: { file: 'test' }, 'empty-folder': null, 'f.html': 'test' });
// const exists = async (path: string): Promise<boolean> => {
// return new Promise((resolve) => {
// fs.exists(path, (exists) => resolve(exists));
// });
// };
// expect(await exists('/folder')).toBe(true);

const { fs, mfs } = setup({ folder: { file: 'test' }, 'empty-folder': null, 'f.html': 'test' });
const exists = async (path: string): Promise<boolean> => {
return new Promise((resolve) => {
fs.exists(path, (exists) => resolve(exists));
});
};
expect(await exists('/folder')).toBe(true);
expect(await exists('/folder/file')).toBe(true);
expect(await exists('/folder/not-a-file')).toBe(false);
expect(await exists('/f.html')).toBe(true);
expect(await exists('/empty-folder')).toBe(true);
expect(await exists('/')).toBe(true);
expect(await exists('/asdf')).toBe(false);
expect(await exists('asdf')).toBe(false);
});
});

Expand Down

0 comments on commit 0753937

Please sign in to comment.