index.js 2.8 KB

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