index.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.memfs = exports.fs = exports.createFsFromVolume = exports.vol = exports.Volume = void 0;
  4. const Stats_1 = require("./Stats");
  5. const Dirent_1 = require("./Dirent");
  6. const volume_1 = require("./volume");
  7. const { fsSyncMethods, fsAsyncMethods } = require('fs-monkey/lib/util/lists');
  8. const constants_1 = require("./constants");
  9. const { F_OK, R_OK, W_OK, X_OK } = constants_1.constants;
  10. exports.Volume = volume_1.Volume;
  11. // Default volume.
  12. exports.vol = new volume_1.Volume();
  13. function createFsFromVolume(vol) {
  14. const fs = { F_OK, R_OK, W_OK, X_OK, constants: constants_1.constants, Stats: Stats_1.default, Dirent: Dirent_1.default };
  15. // Bind FS methods.
  16. for (const method of fsSyncMethods)
  17. if (typeof vol[method] === 'function')
  18. fs[method] = vol[method].bind(vol);
  19. for (const method of fsAsyncMethods)
  20. if (typeof vol[method] === 'function')
  21. fs[method] = vol[method].bind(vol);
  22. fs.StatWatcher = vol.StatWatcher;
  23. fs.FSWatcher = vol.FSWatcher;
  24. fs.WriteStream = vol.WriteStream;
  25. fs.ReadStream = vol.ReadStream;
  26. fs.promises = vol.promises;
  27. fs._toUnixTimestamp = volume_1.toUnixTimestamp;
  28. fs.__vol = vol;
  29. return fs;
  30. }
  31. exports.createFsFromVolume = createFsFromVolume;
  32. exports.fs = createFsFromVolume(exports.vol);
  33. /**
  34. * Creates a new file system instance.
  35. *
  36. * @param json File system structure expressed as a JSON object.
  37. * Use `null` for empty directories and empty string for empty files.
  38. * @param cwd Current working directory. The JSON structure will be created
  39. * relative to this path.
  40. * @returns A `memfs` file system instance, which is a drop-in replacement for
  41. * the `fs` module.
  42. */
  43. const memfs = (json = {}, cwd = '/') => {
  44. const volume = exports.Volume.fromJSON(json, cwd);
  45. const fs = createFsFromVolume(volume);
  46. return fs;
  47. };
  48. exports.memfs = memfs;
  49. module.exports = Object.assign(Object.assign({}, module.exports), exports.fs);
  50. module.exports.semantic = true;