Skip to content

Commit

Permalink
feat(core): support directory patterns for --projects (#16288)
Browse files Browse the repository at this point in the history
  • Loading branch information
AgentEnder committed Apr 24, 2023
1 parent db32400 commit 6c613ce
Show file tree
Hide file tree
Showing 2 changed files with 254 additions and 63 deletions.
91 changes: 88 additions & 3 deletions packages/nx/src/utils/find-matching-projects.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { findMatchingProjects } from './find-matching-projects';
import {
findMatchingProjects,
getMatchingStringsWithCache,
} from './find-matching-projects';
import minimatch = require('minimatch');
import type { ProjectGraphProjectNode } from '../config/project-graph';

describe('findMatchingProjects', () => {
Expand Down Expand Up @@ -32,13 +36,22 @@ describe('findMatchingProjects', () => {
},
c: {
name: 'c',
type: 'lib',
type: 'app',
data: {
root: 'lib/c',
root: 'apps/c',
files: [],
tags: ['api'],
},
},
nested: {
name: 'nested',
type: 'lib',
data: {
root: 'lib/shared/nested',
files: [],
tags: [],
},
},
};

it('should expand "*"', () => {
Expand All @@ -47,6 +60,7 @@ describe('findMatchingProjects', () => {
'a',
'b',
'c',
'nested',
]);
});

Expand All @@ -55,6 +69,7 @@ describe('findMatchingProjects', () => {
'test-project',
'b',
'c',
'nested',
]);
expect(findMatchingProjects(['!*', 'a'], projectGraph)).toEqual([]);
});
Expand Down Expand Up @@ -99,12 +114,14 @@ describe('findMatchingProjects', () => {
'a',
'b',
'c',
'nested',
]);
});

it('should support negation "!" for tags', () => {
expect(findMatchingProjects(['*', '!tag:api'], projectGraph)).toEqual([
'b',
'nested',
]);
});

Expand All @@ -124,4 +141,72 @@ describe('findMatchingProjects', () => {
findMatchingProjects(['tag:api', '!tag:theme2'], projectGraph)
).toEqual(['test-project', 'c']);
});

it('should support glob patterns for project roots', () => {
expect(findMatchingProjects(['lib/*'], projectGraph)).toEqual([
'test-project',
'a',
'b',
]);
expect(findMatchingProjects(['apps/*'], projectGraph)).toEqual(['c']);
expect(findMatchingProjects(['**/nested'], projectGraph)).toEqual([
'nested',
]);
});
});

const projects = [
'shop-client',
'shop-api',
'cart-api',
'cart-client',
'shop-ui',
'cart-ui',
'shop-e2e',
'cart-e2e',
];

const roots = projects
.map((x) => `apps/${x}`)
.concat(projects.map((x) => `libs/${x}`));

describe.each([
{
items: projects,
pattern: 'cart-*',
},
{
items: projects,
pattern: '*-ui',
},
{
items: roots,
pattern: 'libs/*',
},
])('getMatchingStringsWithCache', ({ items, pattern }) => {
it(`should be faster than using minimatch directly multiple times (${pattern})`, () => {
const iterations = 100;
const cacheTime = time(
() => getMatchingStringsWithCache(pattern, items),
iterations
);
const directTime = time(() => minimatch.match(items, pattern), iterations);
// Using minimatch directly takes at least twice as long than using the cache.
expect(directTime / cacheTime).toBeGreaterThan(2);
});

it(`should be comparable to using minimatch a single time (${pattern})`, () => {
const cacheTime = time(() => getMatchingStringsWithCache(pattern, items));
const directTime = time(() => minimatch.match(items, pattern));
// We are dealing with really small file sets here, with such a small
// difference it time, the system variablility can make this flaky for
// smaller values. If we are within 1ms, we are good.
expect(cacheTime).toBeLessThan(directTime + 1);
});
});

function time(fn: () => void, iterations = 1) {
const start = performance.now();
for (let i = 0; i < iterations; i++) fn();
return performance.now() - start;
}

1 comment on commit 6c613ce

@vercel
Copy link

@vercel vercel bot commented on 6c613ce Apr 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

nx-dev – ./

nx-five.vercel.app
nx-dev-nrwl.vercel.app
nx-dev-git-master-nrwl.vercel.app
nx.dev

Please sign in to comment.