Skip to content

Commit

Permalink
feat(core): also update run-many and show project
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysoo committed Jan 8, 2025
1 parent c18b730 commit c5a0e0f
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 4 deletions.
6 changes: 5 additions & 1 deletion packages/nx/src/command-line/run/run-one.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,11 @@ function getProjects(
};
} else if (matchingProjects.length > 1) {
output.error({
title: `Multiple projects matched:\n- ${matchingProjects.join('\n- ')}`,
title: `Multiple projects matched:`,
bodyLines:
matchingProjects.length > 100
? [...matchingProjects.slice(0, 100), '...']
: matchingProjects,
});
process.exit(1);
}
Expand Down
21 changes: 18 additions & 3 deletions packages/nx/src/command-line/show/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,25 @@ export async function showProjectHandler(
performance.mark('code-loading:end');
performance.measure('code-loading', 'init-local', 'code-loading:end');
const graph = await createProjectGraphAsync();
const node = graph.nodes[args.projectName];
let node = graph.nodes[args.projectName];
if (!node) {
console.log(`Could not find project ${args.projectName}`);
process.exit(1);
const matchingProjects = Object.keys(graph.nodes).filter((name) =>
name.includes(args.projectName)
);
if (matchingProjects.length === 1) {
node = graph.nodes[matchingProjects[0]];
} else if (matchingProjects.length > 1) {
console.log(
`Multiple projects matched:\n ${(matchingProjects.length > 100
? [...matchingProjects.slice(0, 100), '...']
: matchingProjects
).join(' \n')}`
);
process.exit(1);
} else {
console.log(`Could not find project ${args.projectName}`);
process.exit(1);
}
}
if (args.json) {
console.log(JSON.stringify(node.data));
Expand Down
7 changes: 7 additions & 0 deletions packages/nx/src/utils/find-matching-projects.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,13 @@ describe('findMatchingProjects', () => {
findMatchingProjects(['!tag:api', 'test-project'], projectGraph)
).toEqual(['b', 'nested', 'test-project']);
});

it('should match on substring of names', () => {
expect(findMatchingProjects(['test', 'nest'], projectGraph)).toEqual([
'test-project',
'nested',
]);
});
});

const projects = [
Expand Down
14 changes: 14 additions & 0 deletions packages/nx/src/utils/find-matching-projects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,20 @@ function addMatchingProjectsByName(
}

if (!isGlobPattern(pattern.value)) {
// Only matching substrings when string is long enough, otherwise there will be too many matches.
// This is consistent with the behavior of run-one.ts.
if (pattern.value.length > 1) {
const matchingProjects = Object.keys(projects).filter((name) =>
name.includes(pattern.value)
);
for (const projectName of matchingProjects) {
if (pattern.exclude) {
matchedProjects.delete(projectName);
} else {
matchedProjects.add(projectName);
}
}
}
return;
}

Expand Down

0 comments on commit c5a0e0f

Please sign in to comment.