Skip to content

Commit

Permalink
fix:; remove the ability to pass getPkgInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
tapico-weyert committed Nov 10, 2022
1 parent 362f85a commit 99f6499
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 75 deletions.
58 changes: 33 additions & 25 deletions packages/license-scanner/src/getPkgInfo.ts
Expand Up @@ -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,
Expand All @@ -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)
Expand Down Expand Up @@ -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<
| {
Expand All @@ -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,
Expand All @@ -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}`
)
}

Expand All @@ -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 {
Expand All @@ -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'`
)
}

Expand All @@ -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<LicensePackage, 'belongsTo'>>
*/
export const getPkgInfo: GetPackageInfoFunction = async (
pkg,
opts
export async function getPkgInfo (
pkg: PackageInfo,
opts: GetPackageInfoOptions
): Promise<
{
from: string
description?: string
} & Omit<LicensePackage, 'belongsTo'>
> => {
{
from: string
description?: string
} & Omit<LicensePackage, 'belongsTo'>
> {
const cafsDir = path.join(opts.storeDir, 'files')

// Retrieve file index for the requested package
Expand All @@ -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 {
Expand Down
24 changes: 1 addition & 23 deletions 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,
Expand All @@ -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<LicensePackage, 'belongsTo'>>

/**
* @private
* Returns an array of LicensePackages from the given LicenseNode
Expand Down Expand Up @@ -82,7 +62,6 @@ function getDependenciesFromLicenseNode (
}

export async function findDependencyLicenses (opts: {
getPackageInfo?: GetPackageInfoFunction
ignoreDependencies?: Set<string>
include?: IncludedDependencies
lockfileDir: string
Expand All @@ -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<string, LicensePackage>()
Expand Down
7 changes: 2 additions & 5 deletions packages/license-scanner/src/lockfileToLicenseNodeTree.ts
Expand Up @@ -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
Expand Down Expand Up @@ -34,7 +34,6 @@ export interface LicenseExtractOptions {
modulesDir?: string
dir: string
registries: Registries
getPackageInfo: GetPackageInfoFunction
}

export async function lockfileToLicenseNode (
Expand Down Expand Up @@ -63,7 +62,7 @@ export async function lockfileToLicenseNode (
continue
}

const packageInfo = await options.getPackageInfo(
const packageInfo = await getPkgInfo(
{
name,
version,
Expand Down Expand Up @@ -116,7 +115,6 @@ export async function lockfileToLicenseNode (
export async function lockfileToLicenseNodeTree (
lockfile: Lockfile,
opts: {
getPackageInfo: GetPackageInfoFunction
include?: { [dependenciesField in DependenciesField]: boolean }
} & LicenseExtractOptions
): Promise<LicenseNodeTree> {
Expand All @@ -134,7 +132,6 @@ export async function lockfileToLicenseNodeTree (
modulesDir: opts.modulesDir,
dir: opts.dir,
registries: opts.registries,
getPackageInfo: opts.getPackageInfo,
})

const depName = importerWalker.importerId
Expand Down
50 changes: 28 additions & 22 deletions packages/license-scanner/test/licenses.spec.ts
Expand Up @@ -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<LicensePackage, 'belongsTo'>
> => {
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<LicensePackage, 'belongsTo'>
> => {
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 () => {
Expand Down Expand Up @@ -63,7 +70,6 @@ describe('licences', () => {
virtualStoreDir: '/.pnpm',
registries: {} as Registries,
wantedLockfile: lockfile,
getPackageInfo,
storeDir: '/opt/.pnpm',
})

Expand Down

0 comments on commit 99f6499

Please sign in to comment.