All files / src/resolver resolve.js

94.59% Statements 35/37
87.5% Branches 7/8
95% Functions 19/20
93.54% Lines 29/31

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 602x 2x 2x   2x 110x 104x   6x     44x 130x 127x   3x 2x   1x         44x 142x 35x 107x     44x 5x 2x 3x     44x 23x 19x 4x     44x             2x 2x 8x                  
import resolve from "resolve/async";
import { promisify } from "util";
const resolvePromisify = promisify(resolve);
 
const onError = (error) => {
  if (error && (error.code === "ENOENT" || error.code === "ENOTDIR")) {
    return false;
  }
  return error;
};
 
const realPathCb = (fs) => (file, cb) => {
  fs.realpath(file)
    .then((p) => cb(undefined, p))
    .catch((err) => {
      if (err.code !== "ENOENT") {
        cb(err);
      } else {
        cb(null, file);
      }
    });
};
 
const isFile = (fs) => (file, cb) => {
  fs.stat(file)
    .then((stats) => cb(undefined, stats.isFile()))
    .catch((err) => cb(null, onError(err)));
};
 
const isDir = (fs) => (file, cb) => {
  fs.stat(file)
    .then((stats) => cb(undefined, stats.isDirectory()))
    .catch((err) => cb(null, onError(err)));
};
 
const readFileCb = (fs) => (file, cb) => {
  fs.readFile(file, "utf8")
    .then((data) => cb(undefined, data))
    .catch((err) => cb(err));
};
 
export const createResolveFsOptions = (fs) => ({
  readFile: readFileCb(fs),
  isFile: isFile(fs),
  isDirectory: isDir(fs),
  realpath: realPathCb(fs),
});
 
export const createResolve =
  (fs) =>
  async (target, options = {}) => {
    const resolveOptions = {
      ...createResolveFsOptions(fs),
      includeCoreModules: false,
      preserveSymlinks: false,
      ...options,
    };
    return await resolvePromisify(target, resolveOptions);
  };