From a9dd43488a3ec0aca41a4916bb9d7765b55216ef Mon Sep 17 00:00:00 2001 From: Craigory Coppola Date: Wed, 5 Jun 2024 13:30:07 -0400 Subject: [PATCH] chore(core): refactor -> minimatch --- packages/nx/src/tasks-runner/utils.ts | 77 +++++++++++-------- .../nx/src/utils/find-matching-projects.ts | 9 ++- 2 files changed, 52 insertions(+), 34 deletions(-) diff --git a/packages/nx/src/tasks-runner/utils.ts b/packages/nx/src/tasks-runner/utils.ts index bd3721e418aa59..566a4f9f8e8e3e 100644 --- a/packages/nx/src/tasks-runner/utils.ts +++ b/packages/nx/src/tasks-runner/utils.ts @@ -15,6 +15,8 @@ import { splitByColons } from '../utils/split-target'; import { getExecutorInformation } from '../command-line/run/executor-utils'; import { CustomHasher, ExecutorConfig } from '../config/misc-interfaces'; import { readProjectsConfigurationFromProjectGraph } from '../project-graph/project-graph'; +import { GLOB_CHARACTERS } from '../utils/find-matching-projects'; +import { minimatch } from 'minimatch'; export function getDependencyConfigs( { project, target }: { project: string; target: string }, @@ -65,48 +67,61 @@ export function expandDependencyConfigSyntaxSugar( ]; } + const { projects, target } = readProjectAndTargetFromTargetString( + targetString, + graph.nodes + ); + + return GLOB_CHARACTERS.some((char) => target.indexOf(char) >= 0) + ? expandWildcardTargetConfiguration(target, projects, currentProject, graph) + : [ + { + target, + projects, + }, + ]; +} + +function expandWildcardTargetConfiguration( + target: string, + projects: string[], + currentProject: string, + graph: ProjectGraph +) { + const matches: TargetDependencyConfig[] = []; + const matcher = minimatch.filter(target); + const projectsToCheck = projects ? projects : [currentProject]; + for (const project of projectsToCheck) { + const projectTargets = graph.nodes[project].data?.targets ?? {}; + + for (const target in projectTargets) { + if (matcher(target)) { + matches.push({ target, projects: [project] }); + } + } + } + return matches; +} + +export function readProjectAndTargetFromTargetString( + targetString: string, + projects: Record +): { projects?: string[]; target: string } { // Support for both `project:target` and `target:with:colons` syntax const [maybeProject, ...segments] = splitByColons(targetString); - let target, projects; if (!segments.length) { // if no additional segments are provided, then the string references // a target of the same project - target = maybeProject; - } else if (maybeProject in graph.nodes) { + return { target: maybeProject }; + } else if (maybeProject in projects) { // Only the first segment could be a project. If it is, the rest is a target. // If its not, then the whole targetString was a target with colons in its name. - target = segments.join(':'); - projects = [maybeProject]; + return { projects: [maybeProject], target: segments.join(':') }; } else { // If the first segment is a project, then we have a specific project. Otherwise, we don't. - target = targetString; + return { target: targetString }; } - - // handle target wildcards - if (target.indexOf('*') >= 0) { - const matches: TargetDependencyConfig[] = []; - const targetMatch = new RegExp('^' + target.replaceAll('*', '.*') + '$'); - const projectsToCheck = projects ? projects : [currentProject]; - for (const project of projectsToCheck) { - const projectTargets = graph.nodes[project].data?.targets; - if (projectTargets) { - for (const target in projectTargets) { - if (target.match(targetMatch)) { - matches.push({ target, projects: [project] }); - } - } - } - } - return matches; - } - - return [ - { - target, - projects, - }, - ]; } export function getOutputs( diff --git a/packages/nx/src/utils/find-matching-projects.ts b/packages/nx/src/utils/find-matching-projects.ts index 497e2ab13ee1a4..de76ec1e39294f 100644 --- a/packages/nx/src/utils/find-matching-projects.ts +++ b/packages/nx/src/utils/find-matching-projects.ts @@ -18,7 +18,10 @@ interface ProjectPattern { value: string; } -const globCharacters = ['*', '|', '{', '}', '(', ')']; +/** + * The presence of these characters in a string indicates that it might be a glob pattern. + */ +export const GLOB_CHARACTERS = ['*', '|', '{', '}', '(', ')']; /** * Find matching project names given a list of potential project names or globs. @@ -166,7 +169,7 @@ function addMatchingProjectsByName( return; } - if (!globCharacters.some((c) => pattern.value.includes(c))) { + if (!GLOB_CHARACTERS.some((c) => pattern.value.includes(c))) { return; } @@ -201,7 +204,7 @@ function addMatchingProjectsByTag( continue; } - if (!globCharacters.some((c) => pattern.value.includes(c))) { + if (!GLOB_CHARACTERS.some((c) => pattern.value.includes(c))) { continue; }