/
licensesDepsOfProjects.ts
67 lines (63 loc) · 2.2 KB
/
licensesDepsOfProjects.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
import path from 'path'
import {
readCurrentLockfile,
readWantedLockfile,
} from '@pnpm/lockfile-file'
import { createMatcher } from '@pnpm/matcher'
import { readModulesManifest } from '@pnpm/modules-yaml'
import {
IncludedDependencies,
ProjectManifest,
Registries,
} from '@pnpm/types'
import unnest from 'ramda/src/unnest'
import { licences, LicensePackage } from './licenses'
import { ClientOptions } from '@pnpm/client'
interface GetManifestOpts {
dir: string
lockfileDir: string
virtualStoreDir: string
rawConfig: object
registries: Registries
}
export type ManifestGetterOptions = Omit<ClientOptions, 'authConfig'>
& GetManifestOpts
& { fullMetadata: boolean, rawConfig: Record<string, string> }
export async function licensesDepsOfProjects (
pkgs: Array<{ dir: string, manifest: ProjectManifest }>,
args: string[],
opts: Omit<ManifestGetterOptions, 'fullMetadata' | 'lockfileDir' | 'virtualStoreDir'> & {
compatible?: boolean
ignoreDependencies?: Set<string>
include: IncludedDependencies
} & Partial<Pick<ManifestGetterOptions, 'fullMetadata' | 'lockfileDir' | 'virtualStoreDir'>>
): Promise<LicensePackage[][]> {
if (!opts.lockfileDir) {
return unnest(await Promise.all(
pkgs.map(async (pkg) => {
return licensesDepsOfProjects([pkg], args, { ...opts, lockfileDir: pkg.dir })
}
)
))
}
const lockfileDir = opts.lockfileDir ?? opts.dir
const modules = await readModulesManifest(path.join(lockfileDir, 'node_modules'))
const virtualStoreDir = modules?.virtualStoreDir ?? path.join(lockfileDir, 'node_modules/.pnpm')
const currentLockfile = await readCurrentLockfile(virtualStoreDir, { ignoreIncompatible: false })
const wantedLockfile = await readWantedLockfile(lockfileDir, { ignoreIncompatible: false }) ?? currentLockfile
return Promise.all(pkgs.map(async ({ dir, manifest }) => {
const match = (args.length > 0) && createMatcher(args) || undefined
return licences({
compatible: opts.compatible,
currentLockfile,
ignoreDependencies: opts.ignoreDependencies,
include: opts.include,
lockfileDir,
manifest,
match,
prefix: dir,
registries: opts.registries,
wantedLockfile,
})
}))
}