fix(directory-fetcher): don't fail if the linked package has no package.json (#4223)

This change was need to support injection in Bit workspace:
https://github.com/teambit/bit/pull/5251
This commit is contained in:
Zoltan Kochan
2022-01-12 16:16:20 +02:00
committed by GitHub
parent 50ee25ae27
commit aa1f9dc195
10 changed files with 65 additions and 13 deletions

View File

@@ -0,0 +1,5 @@
---
"@pnpm/directory-fetcher": patch
---
Don't fail if the linked package has no `package.json` file.

View File

@@ -32,12 +32,13 @@
"homepage": "https://github.com/pnpm/pnpm/blob/master/packages/directory-fetcher#readme",
"dependencies": {
"@pnpm/fetcher-base": "workspace:11.1.4",
"@pnpm/read-project-manifest": "workspace:2.0.10",
"@pnpm/resolver-base": "workspace:8.1.4",
"load-json-file": "^6.2.0",
"npm-packlist": "^3.0.0",
"ramda": "^0.27.1"
},
"devDependencies": {
"@pnpm/directory-fetcher": "workspace:1.0.4"
"@pnpm/directory-fetcher": "workspace:1.0.4",
"@pnpm/test-fixtures": "workspace:*"
}
}

View File

@@ -1,8 +1,8 @@
import path from 'path'
import { Cafs, DeferredManifestPromise } from '@pnpm/fetcher-base'
import { safeReadProjectManifestOnly } from '@pnpm/read-project-manifest'
import { DirectoryResolution } from '@pnpm/resolver-base'
import fromPairs from 'ramda/src/fromPairs'
import loadJsonFile from 'load-json-file'
import packlist from 'npm-packlist'
export interface DirectoryFetcherOptions {
@@ -30,7 +30,11 @@ export async function fetchFromDir (
const files = await packlist({ path: dir })
const filesIndex: Record<string, string> = fromPairs(files.map((file) => [file, path.join(dir, file)]))
if (opts.manifest) {
opts.manifest.resolve(await loadJsonFile(path.join(dir, 'package.json')))
// 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
const manifest = await safeReadProjectManifestOnly(dir) ?? {}
opts.manifest.resolve(manifest as any) // eslint-disable-line @typescript-eslint/no-explicit-any
}
return {
local: true as const,

View File

View File

View File

@@ -0,0 +1,5 @@
{
"name": "simple-pkg",
"version": "0.0.0",
"files": ["index.js"]
}

View File

View File

@@ -1,28 +1,57 @@
/// <reference path="../../../typings/index.d.ts"/>
import path from 'path'
import createFetcher from '@pnpm/directory-fetcher'
import fixtures from '@pnpm/test-fixtures'
const f = fixtures(__dirname)
test('fetch', async () => {
process.chdir(f.find('simple-pkg'))
const fetcher = createFetcher()
// eslint-disable-next-line
const fetchResult = await fetcher.directory({} as any, {
directory: '..',
directory: '.',
type: 'directory',
}, {
lockfileDir: __dirname,
lockfileDir: process.cwd(),
})
expect(fetchResult.local).toBe(true)
expect(fetchResult.packageImportMethod).toBe('hardlink')
expect(fetchResult.filesIndex['package.json']).toBe(path.join(__dirname, '../package.json'))
expect(fetchResult.filesIndex['package.json']).toBe(path.resolve('package.json'))
// Only those files are included which would get published
expect(Object.keys(fetchResult.filesIndex).sort()).toStrictEqual([
'README.md',
'lib/index.d.ts',
'lib/index.js',
'lib/index.js.map',
'index.js',
'package.json',
])
})
test('fetch a directory that has no package.json', async () => {
process.chdir(f.find('no-manifest'))
const fetcher = createFetcher()
const manifest = {
resolve: jest.fn(),
reject: jest.fn(),
}
// eslint-disable-next-line
const fetchResult = await fetcher.directory({} as any, {
directory: '.',
type: 'directory',
}, {
lockfileDir: process.cwd(),
manifest,
})
expect(manifest.resolve).toBeCalledWith({})
expect(fetchResult.local).toBe(true)
expect(fetchResult.packageImportMethod).toBe('hardlink')
expect(fetchResult.filesIndex['index.js']).toBe(path.resolve('index.js'))
// Only those files are included which would get published
expect(Object.keys(fetchResult.filesIndex).sort()).toStrictEqual([
'index.js',
])
})

View File

@@ -9,9 +9,15 @@
"../../typings/**/*.d.ts"
],
"references": [
{
"path": "../../privatePackages/test-fixtures"
},
{
"path": "../fetcher-base"
},
{
"path": "../read-project-manifest"
},
{
"path": "../resolver-base"
}

6
pnpm-lock.yaml generated
View File

@@ -686,18 +686,20 @@ importers:
specifiers:
'@pnpm/directory-fetcher': workspace:1.0.4
'@pnpm/fetcher-base': workspace:11.1.4
'@pnpm/read-project-manifest': workspace:2.0.10
'@pnpm/resolver-base': workspace:8.1.4
load-json-file: ^6.2.0
'@pnpm/test-fixtures': workspace:*
npm-packlist: ^3.0.0
ramda: ^0.27.1
dependencies:
'@pnpm/fetcher-base': link:../fetcher-base
'@pnpm/read-project-manifest': link:../read-project-manifest
'@pnpm/resolver-base': link:../resolver-base
load-json-file: 6.2.0
npm-packlist: 3.0.0
ramda: 0.27.1
devDependencies:
'@pnpm/directory-fetcher': 'link:'
'@pnpm/test-fixtures': link:../../privatePackages/test-fixtures
packages/error:
specifiers: