Skip to content

Commit

Permalink
Merge pull request #234 from forcedotcom/mdonnalley/add-fs-fileExists
Browse files Browse the repository at this point in the history
feat: add fs.fileExists
  • Loading branch information
mdonnalley authored Jun 8, 2020
2 parents 1ad8258 + 0c5d0a7 commit 726c421
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/util/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,5 +158,19 @@ export const fs = {
encoding: 'utf8',
mode: fs.DEFAULT_USER_FILE_MODE
});
},

/**
* Checks if a file path exists
*
* @param filePath the file path to check the existence of
*/
fileExists: async (filePath: string): Promise<boolean> => {
try {
await fs.access(filePath);
return true;
} catch (err) {
return false;
}
}
};
13 changes: 13 additions & 0 deletions test/unit/util/fsTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,4 +213,17 @@ describe('util/fs', () => {
});
});
});

describe('fileExists', () => {
it('should return true if the file exists', async () => {
$$.SANDBOX.stub(fs, 'access').returns(Promise.resolve(true));
const exists = await fs.fileExists('foo/bar.json');
expect(exists).to.be.true;
});

it('should return false if the file does not exist', async () => {
const exists = await fs.fileExists('foo/bar.json');
expect(exists).to.be.false;
});
});
});

0 comments on commit 726c421

Please sign in to comment.