const { dirname } = require('path');
const { pkgUp } = require('@umijs/utils');

const pkgPathCache = {};

function getPkgPath(filePath) {
  const dir = dirname(filePath);
  if (dir in pkgPathCache) return pkgPathCache[dir];
  pkgPathCache[dir] = pkgUp.sync({ cwd: filePath });
  return pkgPathCache[dir];
}

function shouldTransform(pkgPath, include) {
  const { name } = require(pkgPath);
  return name === include;
}

module.exports = {
    getPkgPath,
    shouldTransform
}