All files / src index.ts

100% Statements 33/33
100% Branches 4/4
100% Functions 2/2
100% Lines 28/28

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 7944x 44x 44x                 44x     44x 44x 44x     44x     44x                           44x 603x     22914x 25929x   603x 603x 603x 603x 603x   603x 603x   603x     44x                       44x 552x 552x 552x           44x 44x  
import Stats from './Stats';
import Dirent from './Dirent';
import {
  Volume as _Volume,
  StatWatcher,
  FSWatcher,
  toUnixTimestamp,
  IWriteStream,
  DirectoryJSON,
  NestedDirectoryJSON,
} from './volume';
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 };
export const Volume = _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;
 
  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;