fix(directory-fetcher): return the fetched package's manifest (#7052)

This commit is contained in:
Zoltan Kochan
2023-09-05 11:57:18 +03:00
committed by GitHub
parent 0da8703063
commit 4a1a9431dc
4 changed files with 20 additions and 20 deletions

View File

@@ -0,0 +1,7 @@
---
"@pnpm/directory-fetcher": major
"@pnpm/fetcher-base": patch
"@pnpm/cafs-types": patch
---
Breakin change to the directory-fetcher API.

View File

@@ -4,7 +4,7 @@ import { createExportableManifest } from '@pnpm/exportable-manifest'
import type { DirectoryFetcher, DirectoryFetcherOptions } from '@pnpm/fetcher-base'
import { logger } from '@pnpm/logger'
import { safeReadProjectManifestOnly } from '@pnpm/read-project-manifest'
import type { ProjectManifest } from '@pnpm/types'
import { type DependencyManifest } from '@pnpm/types'
import { writeProjectManifest } from '@pnpm/write-project-manifest'
import equal from 'fast-deep-equal'
import packlist from 'npm-packlist'
@@ -51,14 +51,12 @@ async function fetchAllFilesFromDir (
opts: FetchFromDirOpts
) {
const filesIndex = await _fetchAllFilesFromDir(readFileStat, dir)
const manifest = await safeReadProjectManifestAndMakeExportable(dir, filesIndex) ?? {}
if (opts.manifest) {
opts.manifest.resolve(manifest as any) // eslint-disable-line @typescript-eslint/no-explicit-any
}
const manifest = await safeReadProjectManifestAndMakeExportable(dir, filesIndex)
return {
local: true as const,
filesIndex,
packageImportMethod: 'hardlink' as const,
manifest,
}
}
@@ -129,26 +127,24 @@ async function fetchPackageFilesFromDir (
) {
const files = await packlist({ path: dir })
const filesIndex: Record<string, string> = Object.fromEntries(files.map((file) => [file, path.join(dir, file)]))
const manifest = await safeReadProjectManifestAndMakeExportable(dir, filesIndex) ?? {}
if (opts.manifest) {
opts.manifest.resolve(manifest as any) // eslint-disable-line @typescript-eslint/no-explicit-any
}
const manifest = await safeReadProjectManifestAndMakeExportable(dir, filesIndex)
return {
local: true as const,
filesIndex,
packageImportMethod: 'hardlink' as const,
manifest,
}
}
async function safeReadProjectManifestAndMakeExportable (
dir: string,
filesIndex: Record<string, string>
): Promise<ProjectManifest | null> {
const manifest = await safeReadProjectManifestOnly(dir)
): Promise<DependencyManifest | undefined> {
const manifest = await safeReadProjectManifestOnly(dir) as DependencyManifest
// In a regular pnpm workspace it will probably never happen that a dependency has no package.json file.
// Safe read was added to support the Bit workspace in which the components have no package.json files.
// Related PR in Bit: https://github.com/teambit/bit/pull/5251
if (!manifest) return null
if (!manifest) return undefined
const exportableManifest = await createExportableManifest(dir, manifest)
if (equal(manifest, exportableManifest)) return manifest
const manifestPathOverride = path.join(dir, 'node_modules/.pnpm/package.json')

View File

@@ -63,10 +63,6 @@ test('fetch including all files', async () => {
test('fetch a directory that has no package.json', async () => {
process.chdir(f.find('no-manifest'))
const fetcher = createDirectoryFetcher()
const manifest = {
resolve: jest.fn(),
reject: jest.fn(),
}
// eslint-disable-next-line
const fetchResult = await fetcher.directory({} as any, {
@@ -74,10 +70,10 @@ test('fetch a directory that has no package.json', async () => {
type: 'directory',
}, {
lockfileDir: process.cwd(),
manifest,
readManifest: true,
})
expect(manifest.resolve).toBeCalledWith({})
expect(fetchResult.manifest).toEqual(undefined)
expect(fetchResult.local).toBe(true)
expect(fetchResult.packageImportMethod).toBe('hardlink')
expect(fetchResult.filesIndex['index.js']).toBe(path.resolve('index.js'))

View File

@@ -1,5 +1,5 @@
import { type Resolution, type GitResolution, type DirectoryResolution } from '@pnpm/resolver-base'
import { type DeferredManifestPromise, type Cafs } from '@pnpm/cafs-types'
import { type Cafs } from '@pnpm/cafs-types'
import { type DependencyManifest } from '@pnpm/types'
export interface PkgNameVersion {
@@ -38,13 +38,14 @@ export type GitFetcher = FetchFunction<GitResolution, GitFetcherOptions, { files
export interface DirectoryFetcherOptions {
lockfileDir: string
manifest?: DeferredManifestPromise
readManifest?: boolean
}
export interface DirectoryFetcherResult {
local: true
filesIndex: Record<string, string>
packageImportMethod: 'hardlink'
manifest?: DependencyManifest
}
export type DirectoryFetcher = FetchFunction<DirectoryResolution, DirectoryFetcherOptions, DirectoryFetcherResult>