All files / src/node-fs-adapter index.ts

100% Statements 17/17
100% Branches 2/2
100% Functions 11/11
100% Lines 17/17

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  3x   3x   3x 3x   2x             23x     23x         174x     6x     170x           3x             4x       9x     2x     1x     1x         3x  
import type { IsomorphicRollupFs, Stats } from "../fs-iface";
import nodeFs from "fs";
 
const nodeFsAdapter: IsomorphicRollupFs = {
  access: async function (path: string): Promise<boolean> {
    try {
      return !!(await nodeFs.promises.stat(path));
    } catch (_) {
      return false;
    }
  },
  readFile: function (
    path: string,
    encoding?: "utf8",
  ): Promise<string | Uint8Array> {
    const options: { encoding: "utf-8" } | undefined = encoding
      ? { encoding: "utf-8" }
      : undefined;
    return nodeFs.promises.readFile(path, options) as Promise<
      string | Uint8Array
    >;
  },
  realpath: function (path: string): Promise<string> {
    return nodeFs.promises.realpath(path);
  },
  realpathSync: function (path: string): string {
    return nodeFs.realpathSync(path);
  },
  stat: function (path: string): Promise<Stats> {
    return nodeFs.promises.stat(path) as unknown as Promise<Stats>;
  },
  mkdir: async function (
    path: string,
    options?: { recursive?: boolean },
  ): Promise<void> {
    await nodeFs.promises.mkdir(path, options);
  },
  writeFile: function (
    path: string,
    data: string | Uint8Array,
    encoding?: "utf8",
  ): Promise<void> {
    return nodeFs.promises.writeFile(path, data, encoding);
  },
  promises: {
    lstat(path: string) {
      return nodeFs.promises.stat(path);
    },
    readdir(path: string, options: { withFileTypes: true }) {
      return nodeFs.promises.readdir(path, options);
    },
    readlink(path) {
      return nodeFs.promises.realpath(path);
    },
    realpath(path) {
      return nodeFs.promises.realpath(path);
    },
  },
};
 
export default nodeFsAdapter;