fix: adding new dependency in a workspace project

close #2798
PR #2805
This commit is contained in:
Zoltan Kochan
2020-08-28 11:04:59 +03:00
committed by GitHub
parent 1270094669
commit 3feae53425
4 changed files with 48 additions and 25 deletions

View File

@@ -0,0 +1,5 @@
---
"@pnpm/plugin-commands-installation": patch
---
The same code should run when running some command inside a project directory, or when using `--filter` to select a specific workspace project.

View File

@@ -9,7 +9,7 @@ import findWorkspacePackages, { arrayOfWorkspacePackagesToMap } from '@pnpm/find
import { rebuild } from '@pnpm/plugin-commands-rebuild/lib/implementation'
import { requireHooks } from '@pnpm/pnpmfile'
import { createOrConnectStoreController, CreateStoreControllerOptions } from '@pnpm/store-connection-manager'
import { IncludedDependencies } from '@pnpm/types'
import { IncludedDependencies, Project } from '@pnpm/types'
import {
install,
mutateModules,
@@ -20,6 +20,7 @@ import getSaveType from './getSaveType'
import recursive, { createMatcher, matchDependencies } from './recursive'
import updateToLatestSpecsFromManifest, { createLatestSpecs } from './updateToLatestSpecsFromManifest'
import { createWorkspaceSpecs, updateToWorkspacePackagesFromManifest } from './updateWorkspaceDependencies'
import path = require('path')
const OVERWRITE_UPDATE_OPTIONS = {
allowNew: true,
@@ -111,30 +112,34 @@ when running add/update with the --workspace option')
typeof opts.rawLocalConfig['hoist'] !== 'undefined'
const forcePublicHoistPattern = typeof opts.rawLocalConfig['shamefully-hoist'] !== 'undefined' ||
typeof opts.rawLocalConfig['public-hoist-pattern'] !== 'undefined'
if (opts.recursive && opts.allProjects && opts.selectedProjectsGraph && opts.workspaceDir) {
await recursive(opts.allProjects,
params,
{
...opts,
forceHoistPattern,
forcePublicHoistPattern,
selectedProjectsGraph: opts.selectedProjectsGraph,
workspaceDir: opts.workspaceDir,
},
opts.update ? 'update' : (params.length === 0 ? 'install' : 'add')
)
return
const allProjects = opts.allProjects ?? (
opts.workspaceDir ? await findWorkspacePackages(opts.workspaceDir, opts) : []
)
if (opts.workspaceDir) {
const selectedProjectsGraph = opts.selectedProjectsGraph ?? selectProjectByDir(allProjects, opts.dir)
if (selectedProjectsGraph) {
await recursive(allProjects,
params,
{
...opts,
forceHoistPattern,
forcePublicHoistPattern,
selectedProjectsGraph,
workspaceDir: opts.workspaceDir,
},
opts.update ? 'update' : (params.length === 0 ? 'install' : 'add')
)
return
}
}
// `pnpm install ""` is going to be just `pnpm install`
params = params.filter(Boolean)
const dir = opts.dir || process.cwd()
let allProjects = opts.allProjects
let workspacePackages!: WorkspacePackages
if (opts.workspaceDir) {
allProjects = allProjects ?? await findWorkspacePackages(opts.workspaceDir, opts)
workspacePackages = arrayOfWorkspacePackagesToMap(allProjects)
workspacePackages = arrayOfWorkspacePackagesToMap(allProjects!)
}
const store = await createOrConnectStoreController(opts)
@@ -188,12 +193,7 @@ when running add/update with the --workspace option')
params = createWorkspaceSpecs(params, workspacePackages)
}
}
if (!params || !params.length) {
const updatedManifest = await install(manifest, installOpts)
if (opts.update === true && opts.save !== false) {
await writeProjectManifest(updatedManifest)
}
} else {
if (params?.length) {
const [updatedImporter] = await mutateModules([
{
allowNew: opts.allowNew,
@@ -210,10 +210,15 @@ when running add/update with the --workspace option')
if (opts.save !== false) {
await writeProjectManifest(updatedImporter.manifest)
}
return
}
const updatedManifest = await install(manifest, installOpts)
if (opts.update === true && opts.save !== false) {
await writeProjectManifest(updatedManifest)
}
if (opts.linkWorkspacePackages && opts.workspaceDir) {
allProjects = allProjects ?? await findWorkspacePackages(opts.workspaceDir, opts)
const { selectedProjectsGraph } = await filterPkgsBySelectorObjects(allProjects, [
{
excludeSelf: true,
@@ -248,3 +253,9 @@ when running add/update with the --workspace option')
)
}
}
function selectProjectByDir (projects: Project[], searchedDir: string) {
const project = projects.find(({ dir }) => path.relative(dir, searchedDir) === '')
if (!project) return undefined
return { [searchedDir]: { dependencies: [], package: project } }
}

View File

@@ -286,6 +286,13 @@ export default async function recursive (
if (!currentInput.length) return
}
}
if (opts.workspace) {
if (!currentInput || !currentInput.length) {
currentInput = updateToWorkspacePackagesFromManifest(manifest, includeDirect, workspacePackages)
} else {
currentInput = createWorkspaceSpecs(currentInput, workspacePackages)
}
}
let action!: any // eslint-disable-line @typescript-eslint/no-explicit-any
switch (cmdFullName) {

View File

@@ -99,7 +99,7 @@ test('update: fail when package not in dependencies', async (t) => {
err = _err
}
t.equal(err.code, 'ERR_PNPM_NO_PACKAGE_IN_DEPENDENCIES')
t.equal(err.message, 'None of the specified packages were found in the dependencies.')
t.equal(err.message, 'None of the specified packages were found in the dependencies of any of the projects.')
t.end()
})