From 99f6499b8a6ab56f792065c8e0d12efc686ca425 Mon Sep 17 00:00:00 2001 From: Weyert de Boer Date: Thu, 10 Nov 2022 00:27:42 +0000 Subject: [PATCH] fix:; remove the ability to pass `getPkgInfo` --- packages/license-scanner/src/getPkgInfo.ts | 58 +++++++++++-------- packages/license-scanner/src/licenses.ts | 24 +------- .../src/lockfileToLicenseNodeTree.ts | 7 +-- .../license-scanner/test/licenses.spec.ts | 50 +++++++++------- 4 files changed, 64 insertions(+), 75 deletions(-) diff --git a/packages/license-scanner/src/getPkgInfo.ts b/packages/license-scanner/src/getPkgInfo.ts index dbe234d2ce2..380a17032a7 100644 --- a/packages/license-scanner/src/getPkgInfo.ts +++ b/packages/license-scanner/src/getPkgInfo.ts @@ -4,7 +4,7 @@ import { readFile } from 'fs/promises' import { readPackageJson } from '@pnpm/read-package-json' import { depPathToFilename } from 'dependency-path' import pLimit from 'p-limit' -import { PackageManifest } from '@pnpm/types' +import { PackageManifest, Registries } from '@pnpm/types' import { getFilePathByModeInCafs, getFilePathInCafs, @@ -13,8 +13,8 @@ import { } from '@pnpm/cafs' import loadJsonFile from 'load-json-file' import { PnpmError } from '@pnpm/error' -import { GetPackageInfoFunction, LicensePackage } from './licenses' -import { pkgSnapshotToResolution, Resolution } from '@pnpm/lockfile-utils' +import { LicensePackage } from './licenses' +import { DirectoryResolution, PackageSnapshot, pkgSnapshotToResolution, Resolution } from '@pnpm/lockfile-utils' import { fetchFromDir } from '@pnpm/directory-fetcher' const limitPkgReads = pLimit(4) @@ -160,12 +160,12 @@ async function readLicenseFileFromCafs (cafsDir: string, fileIntegrity: string) * Returns the index of files included in * the package identified by the integrity id * @param packageResolution the resolution package information - * @param packageRef the package reference + * @param depPath the package reference * @param opts options for fetching package file index */ export async function readPackageIndexFile ( packageResolution: Resolution, - packageRef: string, + depPath: string, opts: { cafsDir: string, storeDir: string, lockfileDir: string } ): Promise< | { @@ -187,18 +187,11 @@ export async function readPackageIndexFile ( packageResolution.integrity as string, 'index' ) - } else if (packageResolution.type === 'directory') { - // If the package resolution is of type 'directory' it means - // this is a workspace project and we need to do things differently - const targetRelative = depPathToFilename(packageRef) - const target = path.join(opts.storeDir, targetRelative) - pkgIndexFilePath = path.join(target, 'integrity.json') } 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 pathToDependency = depPathToFilename(packageRef) - const [packageDirInStore] = pathToDependency.split('_') + const packageDirInStore = depPathToFilename(depPath.split('_')[0]) pkgIndexFilePath = path.join( opts.storeDir, packageDirInStore, @@ -207,7 +200,7 @@ export async function readPackageIndexFile ( } else { throw new PnpmError( 'UNSUPPORTED_PACKAGE_TYPE', - `Unsupported package resolution type for ${packageRef}` + `Unsupported package resolution type for ${depPath}` ) } @@ -216,7 +209,7 @@ export async function readPackageIndexFile ( const isLocalPkg = packageResolution.type === 'directory' if (isLocalPkg) { const localInfo = await fetchFromDir( - path.join(opts.lockfileDir, packageResolution.directory), + path.join(opts.lockfileDir, (packageResolution as DirectoryResolution).directory), {} ) return { @@ -234,7 +227,7 @@ export async function readPackageIndexFile ( if (err.code === 'ENOENT') { throw new PnpmError( 'MISSING_PACKAGE_INDEX_FILE', - `Failed to find package index file for ${packageRef}, please consider running 'pnpm install'` + `Failed to find package index file for ${depPath}, please consider running 'pnpm install'` ) } @@ -243,21 +236,36 @@ export async function readPackageIndexFile ( } } +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> */ -export const getPkgInfo: GetPackageInfoFunction = async ( - pkg, - opts +export async function getPkgInfo ( + pkg: PackageInfo, + opts: GetPackageInfoOptions ): Promise< -{ - from: string - description?: string -} & Omit -> => { + { + from: string + description?: string + } & Omit + > { const cafsDir = path.join(opts.storeDir, 'files') // Retrieve file index for the requested package @@ -278,7 +286,7 @@ export const getPkgInfo: GetPackageInfoFunction = async ( ) // Fetch the package manifest - let packageManifestDir: string = '' + let packageManifestDir!: string if (packageFileIndexInfo.local) { packageManifestDir = packageFileIndexInfo.files['package.json'] } else { diff --git a/packages/license-scanner/src/licenses.ts b/packages/license-scanner/src/licenses.ts index 46e3cf96e3a..8a2f79080da 100644 --- a/packages/license-scanner/src/licenses.ts +++ b/packages/license-scanner/src/licenses.ts @@ -1,12 +1,11 @@ import { PnpmError } from '@pnpm/error' -import { Lockfile, PackageSnapshot } from '@pnpm/lockfile-file' +import { Lockfile } from '@pnpm/lockfile-file' import { DependenciesField, IncludedDependencies, ProjectManifest, Registries, } from '@pnpm/types' -import { getPkgInfo } from './getPkgInfo' import { LicenseNode, lockfileToLicenseNodeTree, @@ -24,25 +23,6 @@ export interface LicensePackage { path?: string } -export type GetPackageInfoFunction = ( - pkg: { - name?: string - version?: string - depPath: string - snapshot: PackageSnapshot - registries: Registries - }, - opts: { - storeDir: string - virtualStoreDir: string - dir: string - modulesDir: string - } -) => Promise<{ - from: string - description?: string -} & Omit> - /** * @private * Returns an array of LicensePackages from the given LicenseNode @@ -82,7 +62,6 @@ function getDependenciesFromLicenseNode ( } export async function findDependencyLicenses (opts: { - getPackageInfo?: GetPackageInfoFunction ignoreDependencies?: Set include?: IncludedDependencies lockfileDir: string @@ -107,7 +86,6 @@ export async function findDependencyLicenses (opts: { virtualStoreDir: opts.virtualStoreDir, include: opts.include, registries: opts.registries, - getPackageInfo: opts.getPackageInfo ?? getPkgInfo, }) const licensePackages = new Map() diff --git a/packages/license-scanner/src/lockfileToLicenseNodeTree.ts b/packages/license-scanner/src/lockfileToLicenseNodeTree.ts index 610c0df3fff..76238422ff3 100644 --- a/packages/license-scanner/src/lockfileToLicenseNodeTree.ts +++ b/packages/license-scanner/src/lockfileToLicenseNodeTree.ts @@ -6,7 +6,7 @@ import { LockfileWalkerStep, } from '@pnpm/lockfile-walker' import { DependenciesField, Registries } from '@pnpm/types' -import { GetPackageInfoFunction } from './licenses' +import { getPkgInfo } from './getPkgInfo' export interface LicenseNode { name?: string @@ -34,7 +34,6 @@ export interface LicenseExtractOptions { modulesDir?: string dir: string registries: Registries - getPackageInfo: GetPackageInfoFunction } export async function lockfileToLicenseNode ( @@ -63,7 +62,7 @@ export async function lockfileToLicenseNode ( continue } - const packageInfo = await options.getPackageInfo( + const packageInfo = await getPkgInfo( { name, version, @@ -116,7 +115,6 @@ export async function lockfileToLicenseNode ( export async function lockfileToLicenseNodeTree ( lockfile: Lockfile, opts: { - getPackageInfo: GetPackageInfoFunction include?: { [dependenciesField in DependenciesField]: boolean } } & LicenseExtractOptions ): Promise { @@ -134,7 +132,6 @@ export async function lockfileToLicenseNodeTree ( modulesDir: opts.modulesDir, dir: opts.dir, registries: opts.registries, - getPackageInfo: opts.getPackageInfo, }) const depName = importerWalker.importerId diff --git a/packages/license-scanner/test/licenses.spec.ts b/packages/license-scanner/test/licenses.spec.ts index 69c5eeeb248..3a606320bc9 100644 --- a/packages/license-scanner/test/licenses.spec.ts +++ b/packages/license-scanner/test/licenses.spec.ts @@ -2,29 +2,36 @@ import { findDependencyLicenses } from '@pnpm/license-scanner' import { LOCKFILE_VERSION } from '@pnpm/constants' import { ProjectManifest, Registries } from '@pnpm/types' import { Lockfile } from '@pnpm/lockfile-file' -import { GetPackageInfoFunction, LicensePackage } from '../lib/licenses' +import { LicensePackage } from '../lib/licenses' +import { GetPackageInfoOptions, PackageInfo } from '../lib/getPkgInfo' -const getPackageInfo: GetPackageInfoFunction = async (pkg, _opts): Promise< -{ - from: string - description?: string -} & Omit -> => { - const packageInfo = { - from: pkg.name!, - name: pkg.name!, - version: pkg.version!, - description: 'Package Description', - license: pkg.name === 'bar' ? 'MIT' : 'Unknown', - licenseContents: pkg.name === 'bar' ? undefined : 'The MIT License', - author: 'Package Author', - homepage: 'Homepage', - repository: 'Repository', - path: `/path/to/package/${pkg.name!}@${pkg.version!}/node_modules`, - } +jest.mock('../lib/getPkgInfo', () => { + const actualModule = jest.requireActual('../lib/getPkgInfo') + return { + ...actualModule, + getPkgInfo: async (pkg: PackageInfo, _opts: GetPackageInfoOptions): Promise< + { + from: string + description?: string + } & Omit + > => { + const packageInfo = { + from: pkg.name!, + name: pkg.name!, + version: pkg.version!, + description: 'Package Description', + license: pkg.name === 'bar' ? 'MIT' : 'Unknown', + licenseContents: pkg.name === 'bar' ? undefined : 'The MIT License', + author: 'Package Author', + homepage: 'Homepage', + repository: 'Repository', + path: `/path/to/package/${pkg.name!}@${pkg.version!}/node_modules`, + } - return packageInfo -} + return packageInfo + }, + } +}) describe('licences', () => { test('findDependencyLicenses()', async () => { @@ -63,7 +70,6 @@ describe('licences', () => { virtualStoreDir: '/.pnpm', registries: {} as Registries, wantedLockfile: lockfile, - getPackageInfo, storeDir: '/opt/.pnpm', })