-
-
Notifications
You must be signed in to change notification settings - Fork 944
/
getPkgInfo.ts
359 lines (329 loc) · 9.86 KB
/
getPkgInfo.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
import path from 'path'
import pathAbsolute from 'path-absolute'
import { readFile } from 'fs/promises'
import { readPackageJson } from '@pnpm/read-package-json'
import { depPathToFilename } from 'dependency-path'
import pLimit from 'p-limit'
import { PackageManifest, Registries } from '@pnpm/types'
import {
getFilePathByModeInCafs,
getFilePathInCafs,
PackageFileInfo,
PackageFilesIndex,
} from '@pnpm/cafs'
import loadJsonFile from 'load-json-file'
import { PnpmError } from '@pnpm/error'
import { LicensePackage } from './licenses'
import { DirectoryResolution, PackageSnapshot, pkgSnapshotToResolution, Resolution } from '@pnpm/lockfile-utils'
import { fetchFromDir } from '@pnpm/directory-fetcher'
const limitPkgReads = pLimit(4)
export async function readPkg (pkgPath: string) {
return limitPkgReads(async () => readPackageJson(pkgPath))
}
/**
* @const
* List of typical names for license files
*/
const LICENSE_FILES = [
'LICENSE',
'LICENCE',
'LICENSE.md',
'LICENCE.md',
'LICENSE.txt',
'LICENCE.txt',
'MIT-LICENSE.txt',
'MIT-LICENSE.md',
'MIT-LICENSE',
]
export interface LicenseInfo {
name: string
licenseFile?: string
}
/**
* Coerce the given value to a string or a null value
* @param field the string to be converted
* @returns string | null
*/
function coerceToString (field: unknown): string | null {
const string = String(field)
return typeof field === 'string' || field === string ? string : null
}
/**
* Parses the value of the license-property of a
* package manifest and return it as a string
* @param field the value to parse
* @returns string
*/
function parseLicenseManifestField (field: unknown) {
if (Array.isArray(field)) {
const licenses = field
const licenseTypes = licenses.reduce((listOfLicenseTypes, license) => {
const type = coerceToString(license.type) ?? coerceToString(license.name)
if (type) {
listOfLicenseTypes.push(type)
}
return listOfLicenseTypes
}, [])
if (licenseTypes.length > 1) {
const combinedLicenseTypes = licenseTypes.join(' OR ') as string
return `(${combinedLicenseTypes})`
}
return licenseTypes[0] ?? null
} else {
return (field as { type: string })?.type ?? coerceToString(field)
}
}
/**
* Reads the license field or LICENSE file from
* the directory of the given package manifest
*
* If the package.json file is missing the `license`-property
* the root of the manifest directory will be scanned for
* files named listed in the array LICENSE_FILES and the
* contents will be returned.
*
* @param {*} pkg the package to check
* @param {*} opts the options for parsing licenses
* @returns Promise<LicenseInfo>
*/
async function parseLicense (
pkg: {
manifest: PackageManifest
files:
| { local: true, files: Record<string, string> }
| { local: false, files: Record<string, PackageFileInfo> }
},
opts: { cafsDir: string }
): Promise<LicenseInfo> {
let licenseField: unknown = pkg.manifest.license
if ('licenses' in pkg.manifest) {
licenseField = (
pkg.manifest as PackageManifest & {
licenses: unknown
}
).licenses
}
const license = parseLicenseManifestField(licenseField)
// check if we discovered a license, if not attempt to parse the LICENSE file
if (!license || /see license/i.test(license)) {
const { files: pkgFileIndex } = pkg.files
for (const filename of LICENSE_FILES) {
// check if the a file with the expected name exists in the file index
if (!(filename in pkgFileIndex)) {
continue
}
const licensePackageFileInfo = pkgFileIndex[filename]
let licenseContents: Buffer | undefined
if (pkg.files.local) {
licenseContents = await readFile(licensePackageFileInfo as string)
} else {
licenseContents = await readLicenseFileFromCafs(opts.cafsDir, licensePackageFileInfo as PackageFileInfo)
}
return {
name: 'Unknown',
licenseFile: licenseContents?.toString('utf-8'),
}
}
}
return { name: license ?? 'Unknown' }
}
/**
* Fetch a file by integrity id from the content-addressable store
* @param cafsDir the cafs directory
* @param opts the options for reading file
* @returns Promise<Buffer>
*/
async function readLicenseFileFromCafs (cafsDir: string, { integrity, mode }: PackageFileInfo) {
const fileName = getFilePathByModeInCafs(cafsDir, integrity, mode)
const fileContents = await readFile(fileName)
return fileContents
}
/**
* Returns the index of files included in
* the package identified by the integrity id
* @param packageResolution the resolution package information
* @param depPath the package reference
* @param opts options for fetching package file index
*/
export async function readPackageIndexFile (
packageResolution: Resolution,
depPath: string,
opts: { cafsDir: string, storeDir: string, lockfileDir: string }
): Promise<
| {
local: false
files: Record<string, PackageFileInfo>
}
| {
local: true
files: Record<string, string>
}
> {
const isPackageWithIntegrity = 'integrity' in packageResolution
let pkgIndexFilePath
if (isPackageWithIntegrity) {
// Retrieve all the index file of all files included in the package
pkgIndexFilePath = getFilePathInCafs(
opts.cafsDir,
packageResolution.integrity as string,
'index'
)
} else if (!packageResolution.type && packageResolution.tarball) {
// If the package resolution has a tarball then we need to clean up
// the call to depPathToFilename as it adds '_[hash]' part to the
// directory for the package in the content-addressable store
const packageDirInStore = depPathToFilename(depPath.split('_')[0])
pkgIndexFilePath = path.join(
opts.storeDir,
packageDirInStore,
'integrity.json'
)
} else {
throw new PnpmError(
'UNSUPPORTED_PACKAGE_TYPE',
`Unsupported package resolution type for ${depPath}`
)
}
// If the package resolution is of type directory we need to do things
// differently and generate our own package index file
const isLocalPkg = packageResolution.type === 'directory'
if (isLocalPkg) {
const localInfo = await fetchFromDir(
path.join(opts.lockfileDir, (packageResolution as DirectoryResolution).directory),
{}
)
return {
local: true,
files: localInfo.filesIndex,
}
} else {
try {
const { files } = await loadJsonFile<PackageFilesIndex>(pkgIndexFilePath)
return {
local: false,
files,
}
} catch (err: any) { // eslint-disable-line
if (err.code === 'ENOENT') {
throw new PnpmError(
'MISSING_PACKAGE_INDEX_FILE',
`Failed to find package index file for ${depPath}, please consider running 'pnpm install'`
)
}
throw err
}
}
}
export interface PackageInfo {
name?: string
version?: string
depPath: string
snapshot: PackageSnapshot
registries: Registries
}
export interface GetPackageInfoOptions {
storeDir: string
virtualStoreDir: string
dir: string
modulesDir: string
}
/**
* Returns the package manifest information for a give package name and path
* @param pkg the package to fetch information for
* @param opts the fetching options
* @returns Promise<{ from: string; description?: string } & Omit<LicensePackage, 'belongsTo'>>
*/
export async function getPkgInfo (
pkg: PackageInfo,
opts: GetPackageInfoOptions
): Promise<
{
from: string
description?: string
} & Omit<LicensePackage, 'belongsTo'>
> {
const cafsDir = path.join(opts.storeDir, 'files')
// Retrieve file index for the requested package
const packageResolution = pkgSnapshotToResolution(
pkg.depPath,
pkg.snapshot,
pkg.registries
)
const packageFileIndexInfo = await readPackageIndexFile(
packageResolution as Resolution,
pkg.depPath,
{
cafsDir,
storeDir: opts.storeDir,
lockfileDir: opts.dir,
}
)
// Fetch the package manifest
let packageManifestDir!: string
if (packageFileIndexInfo.local) {
packageManifestDir = packageFileIndexInfo.files['package.json']
} else {
const packageFileIndex = packageFileIndexInfo.files as Record<
string,
PackageFileInfo
>
const packageManifestFile = packageFileIndex['package.json']
packageManifestDir = getFilePathByModeInCafs(
cafsDir,
packageManifestFile.integrity,
packageManifestFile.mode
)
}
let manifest
try {
manifest = await readPkg(packageManifestDir)
} catch (err: any) { // eslint-disable-line
if (err.code === 'ENOENT') {
throw new PnpmError(
'MISSING_PACKAGE_MANIFEST',
`Failed to find package manifest file at ${packageManifestDir}`
)
}
throw err
}
// Determine the path to the package as known by the user
const modulesDir = opts.modulesDir ?? 'node_modules'
const virtualStoreDir = pathAbsolute(
opts.virtualStoreDir ?? path.join(modulesDir, '.pnpm'),
opts.dir
)
// TODO: fix issue that path is only correct when using node-linked=isolated
const packageModulePath = path.join(
virtualStoreDir,
depPathToFilename(pkg.depPath),
modulesDir,
manifest.name
)
const licenseInfo = await parseLicense(
{ manifest, files: packageFileIndexInfo },
{ cafsDir }
)
const packageInfo = {
from: manifest.name,
path: packageModulePath,
name: manifest.name,
version: manifest.version,
description: manifest.description,
license: licenseInfo.name,
licenseContents: licenseInfo.licenseFile,
author:
(manifest.author &&
(typeof manifest.author === 'string'
? manifest.author
: (manifest.author as { name: string }).name)) ??
undefined,
homepage: manifest.homepage,
repository:
(manifest.repository &&
(typeof manifest.repository === 'string'
? manifest.repository
: manifest.repository.url)) ??
undefined,
}
return packageInfo
}