index.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.memfs = exports.fs = exports.vol = exports.Volume = void 0;
  4. exports.createFsFromVolume = createFsFromVolume;
  5. const fs_node_1 = require("@jsonjoy.com/fs-node");
  6. Object.defineProperty(exports, "Volume", { enumerable: true, get: function () { return fs_node_1.Volume; } });
  7. const fs_node_utils_1 = require("@jsonjoy.com/fs-node-utils");
  8. const { F_OK, R_OK, W_OK, X_OK } = fs_node_utils_1.constants;
  9. // Default volume.
  10. exports.vol = new fs_node_1.Volume();
  11. function createFsFromVolume(vol) {
  12. const fs = { F_OK, R_OK, W_OK, X_OK, constants: fs_node_utils_1.constants, Stats: fs_node_1.Stats, Dirent: fs_node_1.Dirent };
  13. // Bind FS methods.
  14. for (const method of fs_node_1.fsSynchronousApiList)
  15. if (typeof vol[method] === 'function')
  16. fs[method] = vol[method].bind(vol);
  17. for (const method of fs_node_1.fsCallbackApiList)
  18. if (typeof vol[method] === 'function')
  19. fs[method] = vol[method].bind(vol);
  20. fs.StatWatcher = vol.StatWatcher;
  21. fs.FSWatcher = vol.FSWatcher;
  22. fs.WriteStream = vol.WriteStream;
  23. fs.ReadStream = vol.ReadStream;
  24. fs.promises = vol.promises;
  25. // Handle realpath and realpathSync with their .native properties
  26. if (typeof vol.realpath === 'function') {
  27. fs.realpath = vol.realpath.bind(vol);
  28. if (typeof vol.realpath.native === 'function') {
  29. fs.realpath.native = vol.realpath.native.bind(vol);
  30. }
  31. }
  32. if (typeof vol.realpathSync === 'function') {
  33. fs.realpathSync = vol.realpathSync.bind(vol);
  34. if (typeof vol.realpathSync.native === 'function') {
  35. fs.realpathSync.native = vol.realpathSync.native.bind(vol);
  36. }
  37. }
  38. fs._toUnixTimestamp = fs_node_1.toUnixTimestamp;
  39. fs.__vol = vol;
  40. return fs;
  41. }
  42. exports.fs = createFsFromVolume(exports.vol);
  43. /**
  44. * Creates a new file system instance.
  45. *
  46. * @param json File system structure expressed as a JSON object.
  47. * Use `null` for empty directories and empty string for empty files.
  48. * @param cwd Current working directory. The JSON structure will be created
  49. * relative to this path.
  50. * @returns A `memfs` file system instance, which is a drop-in replacement for
  51. * the `fs` module.
  52. */
  53. const memfs = (json = {}, cwd = '/') => {
  54. const vol = fs_node_1.Volume.fromNestedJSON(json, cwd);
  55. const fs = createFsFromVolume(vol);
  56. return { fs, vol };
  57. };
  58. exports.memfs = memfs;
  59. module.exports = { ...module.exports, ...exports.fs };
  60. module.exports.semantic = true;
  61. //# sourceMappingURL=index.js.map