fs.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. 'use strict';
  2. const fs = require('fs');
  3. const call = require('../call');
  4. /**
  5. * A facade around {@link fs.readdirSync} that allows it to be called
  6. * the same way as {@link fs.readdir}.
  7. *
  8. * @param {string} dir
  9. * @param {function} callback
  10. */
  11. exports.readdir = function (dir, callback) {
  12. // Make sure the callback is only called once
  13. callback = call.once(callback);
  14. try {
  15. let items = fs.readdirSync(dir);
  16. callback(null, items);
  17. }
  18. catch (err) {
  19. callback(err);
  20. }
  21. };
  22. /**
  23. * A facade around {@link fs.statSync} that allows it to be called
  24. * the same way as {@link fs.stat}.
  25. *
  26. * @param {string} path
  27. * @param {function} callback
  28. */
  29. exports.stat = function (path, callback) {
  30. // Make sure the callback is only called once
  31. callback = call.once(callback);
  32. try {
  33. let stats = fs.statSync(path);
  34. callback(null, stats);
  35. }
  36. catch (err) {
  37. callback(err);
  38. }
  39. };
  40. /**
  41. * A facade around {@link fs.lstatSync} that allows it to be called
  42. * the same way as {@link fs.lstat}.
  43. *
  44. * @param {string} path
  45. * @param {function} callback
  46. */
  47. exports.lstat = function (path, callback) {
  48. // Make sure the callback is only called once
  49. callback = call.once(callback);
  50. try {
  51. let stats = fs.lstatSync(path);
  52. callback(null, stats);
  53. }
  54. catch (err) {
  55. callback(err);
  56. }
  57. };