Skip to content

Commit

Permalink
chore(misc): address review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
AgentEnder committed Dec 29, 2022
1 parent 84650b3 commit 8ec97e3
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 73 deletions.
2 changes: 1 addition & 1 deletion packages/nx/src/command-line/run-many.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ describe('run-many', () => {
);
performance.mark('end');
const measure = performance.measure('projects', 'start', 'end');
expect(measure.duration).toBeLessThan(2500);
expect(measure.duration).toBeLessThan(3000);
});
});
});
Expand Down
8 changes: 5 additions & 3 deletions packages/nx/src/command-line/run-many.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ export function projectsToRun(
): ProjectGraphProjectNode[] {
const selectedProjects = new Map<string, ProjectGraphProjectNode>();
const validProjects = runnableForTarget(projectGraph.nodes, nxArgs.targets);
const validProjectNames = Array.from(validProjects.keys());
const invalidProjects: string[] = [];

// --all is default now, if --projects is provided, it'll override the --all
Expand All @@ -67,9 +66,11 @@ export function projectsToRun(
selectedProjects.set(projectName, projectGraph.nodes[projectName]);
}
} else {
const allProjectNames = Object.keys(projectGraph.nodes);
const matchingProjects = findMatchingProjects(
nxArgs.projects,
Object.keys(projectGraph.nodes)
allProjectNames,
new Set(allProjectNames)
);
for (const project of matchingProjects) {
if (!validProjects.has(project)) {
Expand All @@ -93,7 +94,8 @@ export function projectsToRun(

const excludedProjects = findMatchingProjects(
nxArgs.exclude ?? [],
Array.from(selectedProjects.keys())
Array.from(selectedProjects.keys()),
new Set(selectedProjects.keys())
);

for (const excludedProject of excludedProjects) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,70 +1,46 @@
import { ProjectConfiguration } from 'nx/src/config/workspace-json-project-json';
import { normalizeImplicitDependencies } from './workspace-projects';

describe('workspace-projects', () => {
let projectsSet: Set<string>;

beforeEach(() => {
projectsSet = new Set(['test-project', 'a', 'b', 'c']);
});

describe('normalizeImplicitDependencies', () => {
it('should expand "*" implicit dependencies', () => {
expect(
normalizeImplicitDependencies('test-project', ['*'], {
fileMap: {},
filesToProcess: {},
workspace: {
version: 2,
projects: {
...makeProject('test-project'),
...makeProject('a'),
...makeProject('b'),
...makeProject('c'),
},
},
})
normalizeImplicitDependencies(
'test-project',
['*'],
Array.from(projectsSet),
projectsSet
)
).toEqual(['a', 'b', 'c']);
});

it('should return [] for null implicit dependencies', () => {
expect(
normalizeImplicitDependencies('test-project', null, {
fileMap: {},
filesToProcess: {},
workspace: {
version: 2,
projects: {
...makeProject('test-project'),
...makeProject('a'),
...makeProject('b'),
...makeProject('c'),
},
},
})
normalizeImplicitDependencies(
'test-project',
null,
Array.from(projectsSet),
projectsSet
)
).toEqual([]);
});

it('should expand glob based implicit dependencies', () => {
projectsSet.add('b-1');
projectsSet.add('b-2');
expect(
normalizeImplicitDependencies('test-project', ['b*'], {
fileMap: {},
filesToProcess: {},
workspace: {
version: 2,
projects: {
...makeProject('test-project'),
...makeProject('a'),
...makeProject('b'),
...makeProject('b-1'),
...makeProject('b-2'),
...makeProject('c'),
},
},
})
normalizeImplicitDependencies(
'test-project',
['b*'],
Array.from(projectsSet),
projectsSet
)
).toEqual(['b', 'b-1', 'b-2']);
});
});
});

function makeProject(name: string): Record<string, ProjectConfiguration> {
return {
[name]: {
root: `packages/${name}`,
},
};
}
26 changes: 17 additions & 9 deletions packages/nx/src/project-graph/build-nodes/workspace-projects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ export function buildWorkspaceProjectNodes(
nxJson: NxJsonConfiguration
) {
const toAdd = [];
Object.keys(ctx.workspace.projects).forEach((key) => {
const projects = Object.keys(ctx.workspace.projects);
const projectsSet = new Set(projects);

for (const key of projects) {
const p = ctx.workspace.projects[key];
const projectRoot = join(workspaceRoot, p.root);

Expand Down Expand Up @@ -56,7 +59,8 @@ export function buildWorkspaceProjectNodes(
p.implicitDependencies = normalizeImplicitDependencies(
key,
p.implicitDependencies,
ctx
projects,
projectsSet
);

p.targets = mergePluginTargetsWithNxTargets(
Expand All @@ -83,7 +87,7 @@ export function buildWorkspaceProjectNodes(
files: ctx.fileMap[key],
},
});
});
}

// Sort by root directory length (do we need this?)
toAdd.sort((a, b) => {
Expand Down Expand Up @@ -149,12 +153,16 @@ function normalizeProjectTargets(
export function normalizeImplicitDependencies(
source: string,
implicitDependencies: ProjectConfiguration['implicitDependencies'],
context: ProjectGraphProcessorContext
projectNames: string[],
projectsSet: Set<string>
) {
return findMatchingProjects(
implicitDependencies || [],
Object.keys(context.workspace.projects).filter(
(projectName) => projectName !== source
)
if (!implicitDependencies?.length) {
return implicitDependencies ?? [];
}
const matches = findMatchingProjects(
implicitDependencies,
projectNames,
projectsSet
);
return matches.filter((x) => x !== source);
}
22 changes: 14 additions & 8 deletions packages/nx/src/utils/assert-workspace-validity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ export function assertWorkspaceValidity(
workspaceJson,
nxJson: NxJsonConfiguration
) {
const workspaceJsonProjects = Object.keys(workspaceJson.projects);
const projectNames = Object.keys(workspaceJson.projects);
const projectNameSet = new Set(projectNames);

const projects = {
...workspaceJson.projects,
Expand Down Expand Up @@ -50,12 +51,14 @@ export function assertWorkspaceValidity(
map,
filename,
projectNames,
projects
projects,
projectNames,
projectNameSet
);
return map;
}, invalidImplicitDependencies);

workspaceJsonProjects
projectNames
.filter((projectName) => {
const project = projects[projectName];
return !!project.implicitDependencies;
Expand All @@ -66,7 +69,9 @@ export function assertWorkspaceValidity(
map,
projectName,
project.implicitDependencies,
projects
projects,
projectNames,
projectNameSet
);
return map;
}, invalidImplicitDependencies);
Expand All @@ -91,17 +96,18 @@ function detectAndSetInvalidProjectGlobValues(
map: Map<string, string[]>,
sourceName: string,
desiredImplicitDeps: string[],
validProjects: ProjectsConfigurations['projects']
projectConfigurations: ProjectsConfigurations['projects'],
projectNames: string[],
projectNameSet: Set<string>
) {
const validProjectNames = Object.keys(validProjects);
const invalidProjectsOrGlobs = desiredImplicitDeps.filter((implicit) => {
const projectName = implicit.startsWith('!')
? implicit.substring(1)
: implicit;

return !(
validProjects[projectName] ||
findMatchingProjects([implicit], validProjectNames).length
projectConfigurations[projectName] ||
findMatchingProjects([implicit], projectNames, projectNameSet).length
);
});

Expand Down
30 changes: 30 additions & 0 deletions packages/nx/src/utils/find-matching-projects.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { findMatchingProjects } from './find-matching-projects';

describe('findMatchingProjects', () => {
let projectsSet: Set<string>;

beforeEach(() => {
projectsSet = new Set(['test-project', 'a', 'b', 'c']);
});

it('should expand "*"', () => {
expect(
findMatchingProjects(['*'], Array.from(projectsSet), projectsSet)
).toEqual(['test-project', 'a', 'b', 'c']);
});

it('should expand generic glob patterns', () => {
projectsSet.add('b-1');
projectsSet.add('b-2');

expect(
findMatchingProjects(['b*'], Array.from(projectsSet), projectsSet)
).toEqual(['b', 'b-1', 'b-2']);
});

it('should support projectNames', () => {
expect(
findMatchingProjects(['a', 'b'], Array.from(projectsSet), projectsSet)
).toEqual(['a', 'b']);
});
});
19 changes: 17 additions & 2 deletions packages/nx/src/utils/find-matching-projects.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,33 @@
import minimatch = require('minimatch');

const globCharacters = ['*', '|', '{', '}', '(', ')'];

/**
* Find matching project names given a list of potential project names or globs.
*
* @param patterns A list of project names or globs to match against.
* @param projectNames An array containing the list of project names.
* @param projectNamesSet A set containing the list of project names.
* @returns
*/
export function findMatchingProjects(
patterns: string[],
projectNames: string[]
projectNames: string[],
projectNamesSet: Set<string>
): string[] {
const selectedProjects: Set<string> = new Set();
const excludedProjects: Set<string> = new Set();

for (const nameOrGlob of patterns) {
if (projectNames.includes(nameOrGlob)) {
if (projectNamesSet.has(nameOrGlob)) {
selectedProjects.add(nameOrGlob);
continue;
}

if (!globCharacters.some((c) => nameOrGlob.includes(c))) {
continue;
}

const exclude = nameOrGlob.startsWith('!');
const pattern = exclude ? nameOrGlob.substring(1) : nameOrGlob;

Expand Down

0 comments on commit 8ec97e3

Please sign in to comment.