SyncAsyncFileSystemDecorator.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. /** @typedef {import("./Resolver").FileSystem} FileSystem */
  7. /** @typedef {import("./Resolver").SyncFileSystem} SyncFileSystem */
  8. /**
  9. * @param {SyncFileSystem} fs file system implementation
  10. * @constructor
  11. */
  12. function SyncAsyncFileSystemDecorator(fs) {
  13. this.fs = fs;
  14. /** @type {FileSystem["lstat"] | undefined} */
  15. this.lstat = undefined;
  16. /** @type {SyncFileSystem["lstatSync"] | undefined} */
  17. this.lstatSync = undefined;
  18. const lstatSync = fs.lstatSync;
  19. if (lstatSync) {
  20. this.lstat = (arg, options, callback) => {
  21. let result;
  22. try {
  23. result = lstatSync.call(fs, arg);
  24. } catch (e) {
  25. // @ts-ignore
  26. return (callback || options)(e);
  27. }
  28. // @ts-ignore
  29. (callback || options)(null, result);
  30. };
  31. this.lstatSync = (arg, options) => lstatSync.call(fs, arg, options);
  32. }
  33. // @ts-ignore
  34. this.stat = (arg, options, callback) => {
  35. let result;
  36. try {
  37. result = callback ? fs.statSync(arg, options) : fs.statSync(arg);
  38. } catch (e) {
  39. return (callback || options)(e);
  40. }
  41. (callback || options)(null, result);
  42. };
  43. /** @type {SyncFileSystem["statSync"]} */
  44. this.statSync = (arg, options) => fs.statSync(arg, options);
  45. // @ts-ignore
  46. this.readdir = (arg, options, callback) => {
  47. let result;
  48. try {
  49. result = fs.readdirSync(arg);
  50. } catch (e) {
  51. return (callback || options)(e);
  52. }
  53. (callback || options)(null, result);
  54. };
  55. /** @type {SyncFileSystem["readdirSync"]} */
  56. this.readdirSync = (arg, options) => fs.readdirSync(arg, options);
  57. // @ts-ignore
  58. this.readFile = (arg, options, callback) => {
  59. let result;
  60. try {
  61. result = fs.readFileSync(arg);
  62. } catch (e) {
  63. return (callback || options)(e);
  64. }
  65. (callback || options)(null, result);
  66. };
  67. /** @type {SyncFileSystem["readFileSync"]} */
  68. this.readFileSync = (arg, options) => fs.readFileSync(arg, options);
  69. // @ts-ignore
  70. this.readlink = (arg, options, callback) => {
  71. let result;
  72. try {
  73. result = fs.readlinkSync(arg);
  74. } catch (e) {
  75. return (callback || options)(e);
  76. }
  77. (callback || options)(null, result);
  78. };
  79. /** @type {SyncFileSystem["readlinkSync"]} */
  80. this.readlinkSync = (arg, options) => fs.readlinkSync(arg, options);
  81. /** @type {FileSystem["readJson"] | undefined} */
  82. this.readJson = undefined;
  83. /** @type {SyncFileSystem["readJsonSync"] | undefined} */
  84. this.readJsonSync = undefined;
  85. const readJsonSync = fs.readJsonSync;
  86. if (readJsonSync) {
  87. this.readJson = (arg, options, callback) => {
  88. let result;
  89. try {
  90. result = readJsonSync.call(fs, arg);
  91. } catch (e) {
  92. // @ts-ignore
  93. return (callback || options)(e);
  94. }
  95. (callback || options)(null, result);
  96. };
  97. this.readJsonSync = (arg, options) => readJsonSync.call(fs, arg, options);
  98. }
  99. }
  100. module.exports = SyncAsyncFileSystemDecorator;