Skip to content

Commit

Permalink
chore(core): refactor -> minimatch
Browse files Browse the repository at this point in the history
  • Loading branch information
AgentEnder committed Jun 10, 2024
1 parent 3e153f3 commit a9dd434
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 34 deletions.
77 changes: 46 additions & 31 deletions packages/nx/src/tasks-runner/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down Expand Up @@ -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<string, ProjectGraphProjectNode>
): { 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(
Expand Down
9 changes: 6 additions & 3 deletions packages/nx/src/utils/find-matching-projects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit a9dd434

Please sign in to comment.