mirror of
https://github.com/pnpm/pnpm.git
synced 2026-03-24 18:11:39 -04:00
fix(link): prefer local packages in a workspace
This commit is contained in:
@@ -8,9 +8,10 @@ import {
|
||||
InstallOptions,
|
||||
link,
|
||||
linkToGlobal,
|
||||
LocalPackages,
|
||||
} from 'supi'
|
||||
import { cached as createStoreController } from '../createStoreController'
|
||||
import findWorkspacePackages from '../findWorkspacePackages'
|
||||
import findWorkspacePackages, { arrayOfLocalPackagesToMap } from '../findWorkspacePackages'
|
||||
import getConfigs from '../getConfigs'
|
||||
import { PnpmOptions } from '../types'
|
||||
|
||||
@@ -23,9 +24,18 @@ export default async (
|
||||
const cwd = opts && opts.prefix || process.cwd()
|
||||
|
||||
const storeControllerCache = new Map<string, Promise<{path: string, ctrl: StoreController}>>()
|
||||
let workspacePackages
|
||||
let localPackages!: LocalPackages
|
||||
if (opts.linkWorkspacePackages && opts.workspacePrefix) {
|
||||
workspacePackages = await findWorkspacePackages(opts.workspacePrefix)
|
||||
localPackages = arrayOfLocalPackagesToMap(workspacePackages)
|
||||
} else {
|
||||
localPackages = {}
|
||||
}
|
||||
|
||||
const store = await createStoreController(storeControllerCache, opts)
|
||||
const linkOpts = Object.assign(opts, {
|
||||
localPackages,
|
||||
store: store.path,
|
||||
storeController: store.ctrl,
|
||||
})
|
||||
@@ -41,9 +51,9 @@ export default async (
|
||||
if (pkgNames.length) {
|
||||
let globalPkgNames!: string[]
|
||||
if (opts.workspacePrefix) {
|
||||
const pkgs = await findWorkspacePackages(opts.workspacePrefix)
|
||||
workspacePackages = await findWorkspacePackages(opts.workspacePrefix)
|
||||
|
||||
const pkgsFoundInWorkspace = pkgs.filter((pkg) => pkgNames.indexOf(pkg.manifest.name) !== -1)
|
||||
const pkgsFoundInWorkspace = workspacePackages.filter((pkg) => pkgNames.indexOf(pkg.manifest.name) !== -1)
|
||||
pkgsFoundInWorkspace.forEach((pkgFromWorkspace) => pkgPaths.push(pkgFromWorkspace.path))
|
||||
|
||||
if (pkgsFoundInWorkspace.length && !linkOpts.saveDev && !linkOpts.saveProd && !linkOpts.saveOptional) {
|
||||
@@ -63,6 +73,7 @@ export default async (
|
||||
const s = await createStoreController(storeControllerCache, opts)
|
||||
await install({
|
||||
...await getConfigs({ ...opts.cliArgs, prefix }, { excludeReporter: true }),
|
||||
localPackages,
|
||||
store: s.path,
|
||||
storeController: s.ctrl,
|
||||
} as InstallOptions)
|
||||
|
||||
@@ -459,3 +459,30 @@ test('recursive installation with shared-workspace-shrinkwrap and a readPackage
|
||||
|
||||
await projects['project-1'].hasNot('project-1')
|
||||
})
|
||||
|
||||
test('local packages should be preferred when running "pnpm link" inside a workspace', async (t) => {
|
||||
const projects = preparePackages(t, [
|
||||
{
|
||||
name: 'project-1',
|
||||
version: '1.0.0',
|
||||
dependencies: {
|
||||
'is-positive': '1.0.0',
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'is-positive',
|
||||
version: '1.0.0',
|
||||
},
|
||||
])
|
||||
|
||||
await writeYamlFile('pnpm-workspace.yaml', { packages: ['**', '!store/**'] })
|
||||
await fs.writeFile('.npmrc', 'link-workspace-packages = true', 'utf8')
|
||||
|
||||
process.chdir('project-1')
|
||||
|
||||
await execPnpm('link', '.')
|
||||
|
||||
const shr = await projects['project-1'].loadShrinkwrap()
|
||||
|
||||
t.equal(shr && shr.dependencies && shr.dependencies['is-positive'], 'link:../is-positive')
|
||||
})
|
||||
|
||||
@@ -41,6 +41,8 @@ export { UninstallOptions } from './uninstall/extendUninstallOptions'
|
||||
import * as packageRequesterLogs from '@pnpm/package-requester'
|
||||
import * as supiLogs from './loggers'
|
||||
|
||||
export { LocalPackages } from '@pnpm/resolver-base'
|
||||
|
||||
export type ProgressLog = ProgressLog | packageRequesterLogs.ProgressLog
|
||||
export type Log = supiLogs.Log
|
||||
| packageRequesterLogs.Log
|
||||
|
||||
Reference in New Issue
Block a user