SyncAsyncFileSystemDecorator.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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").ReaddirStringCallback} ReaddirStringCallback */
  8. /** @typedef {import("./Resolver").StringCallback} StringCallback */
  9. /** @typedef {import("./Resolver").SyncFileSystem} SyncFileSystem */
  10. /**
  11. * @param {SyncFileSystem} fs file system implementation
  12. * @constructor
  13. */
  14. function SyncAsyncFileSystemDecorator(fs) {
  15. this.fs = fs;
  16. this.lstat = undefined;
  17. this.lstatSync = undefined;
  18. const lstatSync = fs.lstatSync;
  19. if (lstatSync) {
  20. this.lstat =
  21. /** @type {FileSystem["lstat"]} */
  22. (
  23. (arg, options, callback) => {
  24. let result;
  25. try {
  26. result = /** @type {Function | undefined} */ (callback)
  27. ? lstatSync.call(fs, arg, options)
  28. : lstatSync.call(fs, arg);
  29. } catch (e) {
  30. return (callback || options)(
  31. /** @type {NodeJS.ErrnoException | null} */ (e)
  32. );
  33. }
  34. (callback || options)(null, /** @type {any} */ (result));
  35. }
  36. );
  37. this.lstatSync =
  38. /** @type {SyncFileSystem["lstatSync"]} */
  39. ((arg, options) => lstatSync.call(fs, arg, options));
  40. }
  41. this.stat =
  42. /** @type {FileSystem["stat"]} */
  43. (
  44. (arg, options, callback) => {
  45. let result;
  46. try {
  47. result = /** @type {Function | undefined} */ (callback)
  48. ? fs.statSync(arg, options)
  49. : fs.statSync(arg);
  50. } catch (e) {
  51. return (callback || options)(
  52. /** @type {NodeJS.ErrnoException | null} */ (e)
  53. );
  54. }
  55. (callback || options)(null, /** @type {any} */ (result));
  56. }
  57. );
  58. this.statSync =
  59. /** @type {SyncFileSystem["statSync"]} */
  60. ((arg, options) => fs.statSync(arg, options));
  61. this.readdir =
  62. /** @type {FileSystem["readdir"]} */
  63. (
  64. (arg, options, callback) => {
  65. let result;
  66. try {
  67. result = /** @type {Function | undefined} */ (callback)
  68. ? fs.readdirSync(
  69. arg,
  70. /** @type {Exclude<Parameters<FileSystem["readdir"]>[1], ReaddirStringCallback>} */
  71. (options)
  72. )
  73. : fs.readdirSync(arg);
  74. } catch (e) {
  75. return (callback || options)(
  76. /** @type {NodeJS.ErrnoException | null} */ (e)
  77. );
  78. }
  79. (callback || options)(null, /** @type {any} */ (result));
  80. }
  81. );
  82. this.readdirSync =
  83. /** @type {SyncFileSystem["readdirSync"]} */
  84. (
  85. (arg, options) =>
  86. fs.readdirSync(
  87. arg,
  88. /** @type {Parameters<SyncFileSystem["readdirSync"]>[1]} */ (options)
  89. )
  90. );
  91. this.readFile =
  92. /** @type {FileSystem["readFile"]} */
  93. (
  94. (arg, options, callback) => {
  95. let result;
  96. try {
  97. result = /** @type {Function | undefined} */ (callback)
  98. ? fs.readFileSync(arg, options)
  99. : fs.readFileSync(arg);
  100. } catch (e) {
  101. return (callback || options)(
  102. /** @type {NodeJS.ErrnoException | null} */ (e)
  103. );
  104. }
  105. (callback || options)(null, /** @type {any} */ (result));
  106. }
  107. );
  108. this.readFileSync =
  109. /** @type {SyncFileSystem["readFileSync"]} */
  110. ((arg, options) => fs.readFileSync(arg, options));
  111. this.readlink =
  112. /** @type {FileSystem["readlink"]} */
  113. (
  114. (arg, options, callback) => {
  115. let result;
  116. try {
  117. result = /** @type {Function | undefined} */ (callback)
  118. ? fs.readlinkSync(
  119. arg,
  120. /** @type {Exclude<Parameters<FileSystem["readlink"]>[1], StringCallback>} */
  121. (options)
  122. )
  123. : fs.readlinkSync(arg);
  124. } catch (e) {
  125. return (callback || options)(
  126. /** @type {NodeJS.ErrnoException | null} */ (e)
  127. );
  128. }
  129. (callback || options)(null, /** @type {any} */ (result));
  130. }
  131. );
  132. this.readlinkSync =
  133. /** @type {SyncFileSystem["readlinkSync"]} */
  134. (
  135. (arg, options) =>
  136. fs.readlinkSync(
  137. arg,
  138. /** @type {Parameters<SyncFileSystem["readlinkSync"]>[1]} */ (options)
  139. )
  140. );
  141. this.readJson = undefined;
  142. this.readJsonSync = undefined;
  143. const readJsonSync = fs.readJsonSync;
  144. if (readJsonSync) {
  145. this.readJson =
  146. /** @type {FileSystem["readJson"]} */
  147. (
  148. (arg, callback) => {
  149. let result;
  150. try {
  151. result = readJsonSync.call(fs, arg);
  152. } catch (e) {
  153. return callback(
  154. /** @type {NodeJS.ErrnoException | Error | null} */ (e)
  155. );
  156. }
  157. callback(null, result);
  158. }
  159. );
  160. this.readJsonSync =
  161. /** @type {SyncFileSystem["readJsonSync"]} */
  162. (arg => readJsonSync.call(fs, arg));
  163. }
  164. this.realpath = undefined;
  165. this.realpathSync = undefined;
  166. const realpathSync = fs.realpathSync;
  167. if (realpathSync) {
  168. this.realpath =
  169. /** @type {FileSystem["realpath"]} */
  170. (
  171. (arg, options, callback) => {
  172. let result;
  173. try {
  174. result = /** @type {Function | undefined} */ (callback)
  175. ? realpathSync.call(
  176. fs,
  177. arg,
  178. /** @type {Exclude<Parameters<NonNullable<FileSystem["realpath"]>>[1], StringCallback>} */
  179. (options)
  180. )
  181. : realpathSync.call(fs, arg);
  182. } catch (e) {
  183. return (callback || options)(
  184. /** @type {NodeJS.ErrnoException | null} */ (e)
  185. );
  186. }
  187. (callback || options)(null, /** @type {any} */ (result));
  188. }
  189. );
  190. this.realpathSync =
  191. /** @type {SyncFileSystem["realpathSync"]} */
  192. (
  193. (arg, options) =>
  194. realpathSync.call(
  195. fs,
  196. arg,
  197. /** @type {Parameters<NonNullable<SyncFileSystem["realpathSync"]>>[1]} */
  198. (options)
  199. )
  200. );
  201. }
  202. }
  203. module.exports = SyncAsyncFileSystemDecorator;