fs-utils.js 624 B

123456789101112131415161718192021222324
  1. /*
  2. * MIT License http://opensource.org/licenses/MIT
  3. * Author: Ben Holloway @bholloway
  4. */
  5. 'use strict';
  6. const fsUtils = (fs) => {
  7. // fs from enhanced-resolver doesn't include fs.existsSync so we need to use fs.statsSync instead
  8. const withStats = (fn) => (absolutePath) => {
  9. try {
  10. return fn(fs.statSync(absolutePath));
  11. } catch (e) {
  12. return false;
  13. }
  14. };
  15. return {
  16. isFileSync: withStats((stats) => stats.isFile()),
  17. isDirectorySync: withStats((stats) => stats.isDirectory()),
  18. existsSync: withStats((stats) => stats.isFile() || stats.isDirectory())
  19. };
  20. };
  21. module.exports = fsUtils;