-
Notifications
You must be signed in to change notification settings - Fork 28k
/
Copy pathhas-necessary-dependencies.ts
72 lines (66 loc) · 2.26 KB
/
has-necessary-dependencies.ts
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
60
61
62
63
64
65
66
67
68
69
70
71
72
import { existsSync, promises as fs } from 'fs'
import { resolveFrom } from './resolve-from'
import { dirname, join, relative } from 'path'
export interface MissingDependency {
file: string
/**
* The package's package.json (e.g. require(`${pkg}/package.json`)) MUST resolve.
* If `exportsRestrict` is false, `${file}` MUST also resolve.
*/
pkg: string
/**
* If true, the pkg's package.json needs to be resolvable.
* If true, will resolve `file` relative to the real path of the package.json.
*
* For example, `{ file: '@types/react/index.d.ts', pkg: '@types/react', exportsRestrict: true }`
* will try to resolve '@types/react/package.json' first and then assume `@types/react/index.d.ts`
* resolves to `path.join(dirname(resolvedPackageJsonPath), 'index.d.ts')`.
*
* If false, will resolve `file` relative to the baseDir.
* ForFor example, `{ file: '@types/react/index.d.ts', pkg: '@types/react', exportsRestrict: true }`
* will try to resolve `@types/react/index.d.ts` directly.
*/
exportsRestrict: boolean
}
export type NecessaryDependencies = {
resolved: Map<string, string>
missing: MissingDependency[]
}
export async function hasNecessaryDependencies(
baseDir: string,
requiredPackages: MissingDependency[]
): Promise<NecessaryDependencies> {
let resolutions = new Map<string, string>()
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 (existsSync(fileToVerify)) {
resolutions.set(p.pkg, fileToVerify)
} else {
return missingPackages.push(p)
}
} else {
resolutions.set(p.pkg, pkgPath)
}
} else {
resolutions.set(p.pkg, resolveFrom(baseDir, p.file))
}
} catch (_) {
return missingPackages.push(p)
}
})
)
return {
resolved: resolutions,
missing: missingPackages,
}
}