refactor: projects finding/filtering/sorting

ref #5338
This commit is contained in:
Zoltan Kochan
2022-09-26 04:17:22 +03:00
parent abb41a6263
commit 2e830c0cb7
3 changed files with 47 additions and 28 deletions

View File

@@ -0,0 +1,5 @@
---
"@pnpm/filter-workspace-packages": minor
---
New function exported: `filterPackagesFromDir`.

View File

@@ -51,18 +51,33 @@ export async function readProjects (
return { allProjects, selectedProjectsGraph }
}
export interface FilterPackagesOptions {
linkWorkspacePackages?: boolean
prefix: string
workspaceDir: string
testPattern?: string[]
changedFilesIgnorePattern?: string[]
useGlobDirFiltering?: boolean
}
export async function filterPackagesFromDir (
workspaceDir: string,
filter: WorkspaceFilter[],
opts: FilterPackagesOptions & { engineStrict?: boolean, patterns: string[] }
) {
const allProjects = await findWorkspacePackages(workspaceDir, { engineStrict: opts?.engineStrict, patterns: opts.patterns })
return {
allProjects,
...(await filterPackages(allProjects, filter, opts)),
}
}
export async function filterPackages<T> (
pkgs: Array<Package & T>,
filter: WorkspaceFilter[],
opts: {
linkWorkspacePackages?: boolean
prefix: string
workspaceDir: string
testPattern?: string[]
changedFilesIgnorePattern?: string[]
useGlobDirFiltering?: boolean
}
opts: FilterPackagesOptions
): Promise<{
allProjectsGraph: PackageGraph<T>
selectedProjectsGraph: PackageGraph<T>
unmatchedFilters: string[]
}> {
@@ -82,6 +97,7 @@ export async function filterPkgsBySelectorObjects<T> (
useGlobDirFiltering?: boolean
}
): Promise<{
allProjectsGraph: PackageGraph<T>
selectedProjectsGraph: PackageGraph<T>
unmatchedFilters: string[]
}> {
@@ -89,9 +105,9 @@ export async function filterPkgsBySelectorObjects<T> (
if ((allPackageSelectors.length > 0) || (prodPackageSelectors.length > 0)) {
let filteredGraph: FilteredGraph<T> | undefined
const { graph } = createPkgGraph<T>(pkgs, { linkWorkspacePackages: opts.linkWorkspacePackages })
if (allPackageSelectors.length > 0) {
const { graph } = createPkgGraph<T>(pkgs, { linkWorkspacePackages: opts.linkWorkspacePackages })
filteredGraph = await filterGraph(graph, allPackageSelectors, {
workspaceDir: opts.workspaceDir,
testPattern: opts.testPattern,
@@ -112,7 +128,8 @@ export async function filterPkgsBySelectorObjects<T> (
})
}
return Promise.resolve({
return {
allProjectsGraph: graph,
selectedProjectsGraph: {
...prodFilteredGraph?.selectedProjectsGraph,
...filteredGraph?.selectedProjectsGraph,
@@ -121,10 +138,10 @@ export async function filterPkgsBySelectorObjects<T> (
...(prodFilteredGraph !== undefined ? prodFilteredGraph.unmatchedFilters : []),
...(filteredGraph !== undefined ? filteredGraph.unmatchedFilters : []),
],
})
}
} else {
const { graph } = createPkgGraph<T>(pkgs, { linkWorkspacePackages: opts.linkWorkspacePackages })
return Promise.resolve({ selectedProjectsGraph: graph, unmatchedFilters: [] })
return { allProjectsGraph: graph, selectedProjectsGraph: graph, unmatchedFilters: [] }
}
}

View File

@@ -5,8 +5,7 @@ import {
Config,
} from '@pnpm/config'
import { scopeLogger } from '@pnpm/core-loggers'
import { filterPackages } from '@pnpm/filter-workspace-packages'
import findWorkspacePackages from '@pnpm/find-workspace-packages'
import { filterPackagesFromDir } from '@pnpm/filter-workspace-packages'
import logger from '@pnpm/logger'
import { ParsedCliArgs } from '@pnpm/parse-cli-args'
import { node } from '@pnpm/plugin-commands-env'
@@ -167,18 +166,6 @@ export default async function run (inputArgv: string[]) {
if (cliOptions['recursive']) {
const wsDir = workspaceDir ?? process.cwd()
const allProjects = await findWorkspacePackages(wsDir, {
engineStrict: config.engineStrict,
patterns: cliOptions['workspace-packages'],
})
if (allProjects.length === 0) {
if (printLogs) {
console.log(`No projects found in "${wsDir}"`)
}
process.exitCode = 0
return
}
config.filter = config.filter ?? []
config.filterProd = config.filterProd ?? []
@@ -194,7 +181,9 @@ export default async function run (inputArgv: string[]) {
filters.push({ filter: `!{${relativeWSDirPath()}}`, followProdDepsOnly: false })
}
const filterResults = await filterPackages(allProjects, filters, {
const filterResults = await filterPackagesFromDir(wsDir, filters, {
engineStrict: config.engineStrict,
patterns: cliOptions['workspace-packages'],
linkWorkspacePackages: !!config.linkWorkspacePackages,
prefix: process.cwd(),
workspaceDir: wsDir,
@@ -202,6 +191,14 @@ export default async function run (inputArgv: string[]) {
changedFilesIgnorePattern: config.changedFilesIgnorePattern,
useGlobDirFiltering: !config.legacyDirFiltering,
})
if (filterResults.allProjects.length === 0) {
if (printLogs) {
console.log(`No projects found in "${wsDir}"`)
}
process.exitCode = 0
return
}
config.selectedProjectsGraph = filterResults.selectedProjectsGraph
if (isEmpty(config.selectedProjectsGraph)) {
if (printLogs) {
@@ -213,7 +210,7 @@ export default async function run (inputArgv: string[]) {
if (filterResults.unmatchedFilters.length !== 0 && printLogs) {
console.log(`No projects matched the filters "${filterResults.unmatchedFilters.join(', ')}" in "${wsDir}"`)
}
config.allProjects = allProjects
config.allProjects = filterResults.allProjects
config.workspaceDir = wsDir
}