{"version":3,"sources":["../../../src/lib/has-necessary-dependencies.ts"],"sourcesContent":["import { existsSync, realpathSync } from 'fs'\nimport { resolveFrom } from './resolve-from'\nimport { dirname, join, relative } from 'path'\n\nexport interface MissingDependency {\n  file: string\n  /**\n   * The package's package.json (e.g. require(`${pkg}/package.json`)) MUST resolve.\n   * If `exportsRestrict` is false, `${file}` MUST also resolve.\n   */\n  pkg: string\n  /**\n   * If true, the pkg's package.json needs to be resolvable.\n   * If true, will resolve `file` relative to the real path of the package.json.\n   *\n   * For example, `{ file: '@types/react/index.d.ts', pkg: '@types/react', exportsRestrict: true }`\n   * will try to resolve '@types/react/package.json' first and then assume `@types/react/index.d.ts`\n   * resolves to `path.join(dirname(resolvedPackageJsonPath), 'index.d.ts')`.\n   *\n   * If false, will resolve `file` relative to the baseDir.\n   * ForFor example, `{ file: '@types/react/index.d.ts', pkg: '@types/react', exportsRestrict: true }`\n   * will try to resolve `@types/react/index.d.ts` directly.\n   */\n  exportsRestrict: boolean\n}\n\nexport type NecessaryDependencies = {\n  resolved: Map<string, string>\n  missing: MissingDependency[]\n}\n\nexport function hasNecessaryDependencies(\n  baseDir: string,\n  requiredPackages: MissingDependency[]\n): NecessaryDependencies {\n  let resolutions = new Map<string, string>()\n  const missingPackages: MissingDependency[] = []\n\n  for (const p of requiredPackages) {\n    try {\n      const pkgPath = realpathSync(\n        resolveFrom(baseDir, `${p.pkg}/package.json`)\n      )\n      const pkgDir = dirname(pkgPath)\n\n      resolutions.set(join(p.pkg, 'package.json'), pkgPath)\n\n      if (p.exportsRestrict) {\n        const fileNameToVerify = relative(p.pkg, p.file)\n        if (fileNameToVerify) {\n          const fileToVerify = join(pkgDir, fileNameToVerify)\n          if (existsSync(fileToVerify)) {\n            resolutions.set(p.pkg, fileToVerify)\n          } else {\n            missingPackages.push(p)\n            continue\n          }\n        } else {\n          resolutions.set(p.pkg, pkgPath)\n        }\n      } else {\n        resolutions.set(p.pkg, resolveFrom(baseDir, p.file))\n      }\n    } catch (_) {\n      missingPackages.push(p)\n      continue\n    }\n  }\n\n  return {\n    resolved: resolutions,\n    missing: missingPackages,\n  }\n}\n"],"names":["existsSync","realpathSync","resolveFrom","dirname","join","relative","hasNecessaryDependencies","baseDir","requiredPackages","resolutions","Map","missingPackages","p","pkgPath","pkg","pkgDir","set","exportsRestrict","fileNameToVerify","file","fileToVerify","push","_","resolved","missing"],"mappings":"AAAA,SAASA,UAAU,EAAEC,YAAY,QAAQ,KAAI;AAC7C,SAASC,WAAW,QAAQ,iBAAgB;AAC5C,SAASC,OAAO,EAAEC,IAAI,EAAEC,QAAQ,QAAQ,OAAM;AA6B9C,OAAO,SAASC,yBACdC,OAAe,EACfC,gBAAqC;IAErC,IAAIC,cAAc,IAAIC;IACtB,MAAMC,kBAAuC,EAAE;IAE/C,KAAK,MAAMC,KAAKJ,iBAAkB;QAChC,IAAI;YACF,MAAMK,UAAUZ,aACdC,YAAYK,SAAS,GAAGK,EAAEE,GAAG,CAAC,aAAa,CAAC;YAE9C,MAAMC,SAASZ,QAAQU;YAEvBJ,YAAYO,GAAG,CAACZ,KAAKQ,EAAEE,GAAG,EAAE,iBAAiBD;YAE7C,IAAID,EAAEK,eAAe,EAAE;gBACrB,MAAMC,mBAAmBb,SAASO,EAAEE,GAAG,EAAEF,EAAEO,IAAI;gBAC/C,IAAID,kBAAkB;oBACpB,MAAME,eAAehB,KAAKW,QAAQG;oBAClC,IAAIlB,WAAWoB,eAAe;wBAC5BX,YAAYO,GAAG,CAACJ,EAAEE,GAAG,EAAEM;oBACzB,OAAO;wBACLT,gBAAgBU,IAAI,CAACT;wBACrB;oBACF;gBACF,OAAO;oBACLH,YAAYO,GAAG,CAACJ,EAAEE,GAAG,EAAED;gBACzB;YACF,OAAO;gBACLJ,YAAYO,GAAG,CAACJ,EAAEE,GAAG,EAAEZ,YAAYK,SAASK,EAAEO,IAAI;YACpD;QACF,EAAE,OAAOG,GAAG;YACVX,gBAAgBU,IAAI,CAACT;YACrB;QACF;IACF;IAEA,OAAO;QACLW,UAAUd;QACVe,SAASb;IACX;AACF","ignoreList":[0]}