2022-08-23 20:16:47 +02:00
|
|
|
import { promises as fs } from 'fs'
|
|
|
|
import { fileExists } from './file-exists'
|
|
|
|
import { resolveFrom } from './resolve-from'
|
|
|
|
import { dirname, join, relative } from 'path'
|
2022-04-12 16:32:42 +02:00
|
|
|
|
2021-08-04 23:53:15 +02:00
|
|
|
export interface MissingDependency {
|
|
|
|
file: string
|
|
|
|
pkg: string
|
2022-04-12 16:32:42 +02:00
|
|
|
exportsRestrict: boolean
|
2021-08-04 23:53:15 +02:00
|
|
|
}
|
2021-04-30 13:09:07 +02:00
|
|
|
|
2020-05-29 01:39:46 +02:00
|
|
|
export type NecessaryDependencies = {
|
2021-06-15 03:31:40 +02:00
|
|
|
resolved: Map<string, string>
|
2021-08-04 23:53:15 +02:00
|
|
|
missing: MissingDependency[]
|
2020-05-29 01:39:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function hasNecessaryDependencies(
|
2021-04-30 13:09:07 +02:00
|
|
|
baseDir: string,
|
2021-08-04 23:53:15 +02:00
|
|
|
requiredPackages: MissingDependency[]
|
2020-05-29 01:39:46 +02:00
|
|
|
): Promise<NecessaryDependencies> {
|
|
|
|
let resolutions = new Map<string, string>()
|
2022-08-23 20:16:47 +02:00
|
|
|
const missingPackages: MissingDependency[] = []
|
|
|
|
|
|
|
|
await Promise.all(
|
|
|
|
requiredPackages.map(async (p) => {
|
|
|
|
try {
|
|
|
|
const pkgPath = await fs.realpath(
|
|
|
|
resolveFrom(baseDir, `${p.pkg}/package.json`)
|
|
|
|
)
|
|
|
|
const pkgDir = dirname(pkgPath)
|
|
|
|
|
|
|
|
if (p.exportsRestrict) {
|
|
|
|
const fileNameToVerify = relative(p.pkg, p.file)
|
|
|
|
if (fileNameToVerify) {
|
|
|
|
const fileToVerify = join(pkgDir, fileNameToVerify)
|
|
|
|
if (await fileExists(fileToVerify)) {
|
|
|
|
resolutions.set(p.pkg, fileToVerify)
|
|
|
|
} else {
|
|
|
|
return missingPackages.push(p)
|
|
|
|
}
|
2022-04-12 16:32:42 +02:00
|
|
|
} else {
|
2022-08-23 20:16:47 +02:00
|
|
|
resolutions.set(p.pkg, pkgPath)
|
2022-04-12 16:32:42 +02:00
|
|
|
}
|
|
|
|
} else {
|
2022-08-23 20:16:47 +02:00
|
|
|
resolutions.set(p.pkg, resolveFrom(baseDir, p.file))
|
2022-04-12 16:32:42 +02:00
|
|
|
}
|
2022-08-23 20:16:47 +02:00
|
|
|
} catch (_) {
|
|
|
|
return missingPackages.push(p)
|
2022-04-12 16:32:42 +02:00
|
|
|
}
|
2022-08-23 20:16:47 +02:00
|
|
|
})
|
|
|
|
)
|
2020-05-29 01:39:46 +02:00
|
|
|
|
2021-08-04 23:53:15 +02:00
|
|
|
return {
|
|
|
|
resolved: resolutions,
|
|
|
|
missing: missingPackages,
|
2020-05-29 01:39:46 +02:00
|
|
|
}
|
|
|
|
}
|