10
0

index.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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("./Stats");
  6. const Dirent_1 = require("./Dirent");
  7. const volume_1 = require("./volume");
  8. const constants_1 = require("./constants");
  9. const fsSynchronousApiList_1 = require("./node/lists/fsSynchronousApiList");
  10. const fsCallbackApiList_1 = require("./node/lists/fsCallbackApiList");
  11. const { F_OK, R_OK, W_OK, X_OK } = constants_1.constants;
  12. exports.Volume = volume_1.Volume;
  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. fs._toUnixTimestamp = volume_1.toUnixTimestamp;
  30. fs.__vol = vol;
  31. return fs;
  32. }
  33. exports.fs = createFsFromVolume(exports.vol);
  34. /**
  35. * Creates a new file system instance.
  36. *
  37. * @param json File system structure expressed as a JSON object.
  38. * Use `null` for empty directories and empty string for empty files.
  39. * @param cwd Current working directory. The JSON structure will be created
  40. * relative to this path.
  41. * @returns A `memfs` file system instance, which is a drop-in replacement for
  42. * the `fs` module.
  43. */
  44. const memfs = (json = {}, cwd = '/') => {
  45. const vol = exports.Volume.fromNestedJSON(json, cwd);
  46. const fs = createFsFromVolume(vol);
  47. return { fs, vol };
  48. };
  49. exports.memfs = memfs;
  50. module.exports = Object.assign(Object.assign({}, module.exports), exports.fs);
  51. module.exports.semantic = true;
  52. //# sourceMappingURL=index.js.map