pkgMatch.js 477 Bytes
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
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
}