All files / src index.ts

100% Statements 41/41
100% Branches 8/8
100% Functions 3/3
100% Lines 36/36

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 8561x 61x 61x   61x     61x 61x 61x   122x     61x                           61x 416x     17056x 20384x   416x 416x 416x 416x 416x     416x 416x 416x 416x     416x 416x 416x 416x       416x 416x   416x     61x                       61x 291x 291x 291x           61x 61x  
import Stats from './node/Stats';
import Dirent from './node/Dirent';
import { Volume, StatWatcher, FSWatcher, toUnixTimestamp, IWriteStream } from './node/volume';
import { DirectoryJSON, NestedDirectoryJSON } from './core';
import { constants } from './constants';
import type { FsPromisesApi } from './node/types';
import type * as misc from './node/types/misc';
import { fsSynchronousApiList } from './node/lists/fsSynchronousApiList';
import { fsCallbackApiList } from './node/lists/fsCallbackApiList';
const { F_OK, R_OK, W_OK, X_OK } = constants;
 
export { DirectoryJSON, NestedDirectoryJSON, Volume };
 
// Default volume.
export const vol = new Volume();
 
export interface IFs extends Volume {
  constants: typeof constants;
  Stats: new (...args) => Stats;
  Dirent: new (...args) => Dirent;
  StatWatcher: new () => StatWatcher;
  FSWatcher: new () => FSWatcher;
  ReadStream: new (...args) => misc.IReadStream;
  WriteStream: new (...args) => IWriteStream;
  promises: FsPromisesApi;
  _toUnixTimestamp;
}
 
export function createFsFromVolume(vol: Volume): IFs {
  const fs = { F_OK, R_OK, W_OK, X_OK, constants, Stats, Dirent } as any as IFs;
 
  // Bind FS methods.
  for (const method of fsSynchronousApiList) if (typeof vol[method] === 'function') fs[method] = vol[method].bind(vol);
  for (const method of fsCallbackApiList) if (typeof vol[method] === 'function') fs[method] = vol[method].bind(vol);
 
  fs.StatWatcher = vol.StatWatcher;
  fs.FSWatcher = vol.FSWatcher;
  fs.WriteStream = vol.WriteStream;
  fs.ReadStream = vol.ReadStream;
  fs.promises = vol.promises;
 
  // Handle realpath and realpathSync with their .native properties
  if (typeof vol.realpath === 'function') {
    fs.realpath = vol.realpath.bind(vol);
    if (typeof vol.realpath.native === 'function') {
      fs.realpath.native = vol.realpath.native.bind(vol);
    }
  }
  if (typeof vol.realpathSync === 'function') {
    fs.realpathSync = vol.realpathSync.bind(vol);
    if (typeof vol.realpathSync.native === 'function') {
      fs.realpathSync.native = vol.realpathSync.native.bind(vol);
    }
  }
 
  fs._toUnixTimestamp = toUnixTimestamp;
  (fs as any).__vol = vol;
 
  return fs;
}
 
export const fs: IFs = createFsFromVolume(vol);
 
/**
 * Creates a new file system instance.
 *
 * @param json File system structure expressed as a JSON object.
 *        Use `null` for empty directories and empty string for empty files.
 * @param cwd Current working directory. The JSON structure will be created
 *        relative to this path.
 * @returns A `memfs` file system instance, which is a drop-in replacement for
 *          the `fs` module.
 */
export const memfs = (json: NestedDirectoryJSON = {}, cwd: string = '/'): { fs: IFs; vol: Volume } => {
  const vol = Volume.fromNestedJSON(json, cwd);
  const fs = createFsFromVolume(vol);
  return { fs, vol };
};
 
export type IFsWithVolume = IFs & { __vol: Volume };
 
declare let module;
module.exports = { ...module.exports, ...fs };
module.exports.semantic = true;