UNPKG

2.24 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.memfs = exports.fs = exports.createFsFromVolume = exports.vol = exports.Volume = void 0;
4const Stats_1 = require("./Stats");
5const Dirent_1 = require("./Dirent");
6const volume_1 = require("./volume");
7const constants_1 = require("./constants");
8const fsSynchronousApiList_1 = require("./node/lists/fsSynchronousApiList");
9const fsCallbackApiList_1 = require("./node/lists/fsCallbackApiList");
10const { F_OK, R_OK, W_OK, X_OK } = constants_1.constants;
11exports.Volume = volume_1.Volume;
12// Default volume.
13exports.vol = new volume_1.Volume();
14function createFsFromVolume(vol) {
15 const fs = { F_OK, R_OK, W_OK, X_OK, constants: constants_1.constants, Stats: Stats_1.default, Dirent: Dirent_1.default };
16 // Bind FS methods.
17 for (const method of fsSynchronousApiList_1.fsSynchronousApiList)
18 if (typeof vol[method] === 'function')
19 fs[method] = vol[method].bind(vol);
20 for (const method of fsCallbackApiList_1.fsCallbackApiList)
21 if (typeof vol[method] === 'function')
22 fs[method] = vol[method].bind(vol);
23 fs.StatWatcher = vol.StatWatcher;
24 fs.FSWatcher = vol.FSWatcher;
25 fs.WriteStream = vol.WriteStream;
26 fs.ReadStream = vol.ReadStream;
27 fs.promises = vol.promises;
28 fs._toUnixTimestamp = volume_1.toUnixTimestamp;
29 fs.__vol = vol;
30 return fs;
31}
32exports.createFsFromVolume = createFsFromVolume;
33exports.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 */
44const memfs = (json = {}, cwd = '/') => {
45 const vol = exports.Volume.fromNestedJSON(json, cwd);
46 const fs = createFsFromVolume(vol);
47 return { fs, vol };
48};
49exports.memfs = memfs;
50module.exports = Object.assign(Object.assign({}, module.exports), exports.fs);
51module.exports.semantic = true;
52//# sourceMappingURL=index.js.map
\No newline at end of file