SyncAsyncFileSystemDecorator.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. function SyncAsyncFileSystemDecorator(fs) {
  7. this.fs = fs;
  8. if (fs.statSync) {
  9. this.stat = function(arg, callback) {
  10. let result;
  11. try {
  12. result = fs.statSync(arg);
  13. } catch (e) {
  14. return callback(e);
  15. }
  16. callback(null, result);
  17. };
  18. }
  19. if (fs.readdirSync) {
  20. this.readdir = function(arg, callback) {
  21. let result;
  22. try {
  23. result = fs.readdirSync(arg);
  24. } catch (e) {
  25. return callback(e);
  26. }
  27. callback(null, result);
  28. };
  29. }
  30. if (fs.readFileSync) {
  31. this.readFile = function(arg, callback) {
  32. let result;
  33. try {
  34. result = fs.readFileSync(arg);
  35. } catch (e) {
  36. return callback(e);
  37. }
  38. callback(null, result);
  39. };
  40. }
  41. if (fs.readlinkSync) {
  42. this.readlink = function(arg, callback) {
  43. let result;
  44. try {
  45. result = fs.readlinkSync(arg);
  46. } catch (e) {
  47. return callback(e);
  48. }
  49. callback(null, result);
  50. };
  51. }
  52. if (fs.readJsonSync) {
  53. this.readJson = function(arg, callback) {
  54. let result;
  55. try {
  56. result = fs.readJsonSync(arg);
  57. } catch (e) {
  58. return callback(e);
  59. }
  60. callback(null, result);
  61. };
  62. }
  63. }
  64. module.exports = SyncAsyncFileSystemDecorator;