Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(nx): add the ability to filter truly affected by target(s) #23

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions libs/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export interface TrueAffectedProject {
sourceRoot: string;
tsConfig?: string;
implicitDependencies?: string[];
targets?: string[];
}

export interface TrueAffected {
Expand Down
48 changes: 46 additions & 2 deletions libs/nx/src/cli.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ describe('cli', () => {
json: false,
restArgs: [],
tsConfigFilePath: 'tsconfig.base.json',
target: [],
});

expect(getNxTrueAffectedProjectsSpy).toBeCalledWith(process.cwd());
Expand All @@ -150,6 +151,7 @@ describe('cli', () => {
json: false,
restArgs: [],
tsConfigFilePath: 'tsconfig.base.json',
target: [],
});

expect(logSpy).toHaveBeenCalledWith('No affected projects');
Expand All @@ -167,6 +169,7 @@ describe('cli', () => {
json: false,
restArgs: [],
tsConfigFilePath: 'tsconfig.base.json',
target: [],
});

expect(logSpy).toHaveBeenCalledWith('Affected projects:\n - proj1');
Expand All @@ -186,6 +189,7 @@ describe('cli', () => {
json: true,
restArgs: [],
tsConfigFilePath: 'tsconfig.base.json',
target: [],
});

expect(consoleSpy).toHaveBeenCalledWith('["proj1"]');
Expand All @@ -206,6 +210,7 @@ describe('cli', () => {
json: false,
restArgs: [],
tsConfigFilePath: 'tsconfig.base.json',
target: [],
});

const expectedCommand = `npx nx run-many --target=build --projects=proj1 `;
Expand All @@ -219,13 +224,50 @@ describe('cli', () => {
);
});

describe('`target` option', () => {
beforeEach(() => {
trafSpy.mockResolvedValueOnce(['proj1']);

getNxTrueAffectedProjectsSpy.mockResolvedValueOnce([
{
name: 'proj1',
sourceRoot: 'mock',
tsConfig: 'mock',
targets: ['build'],
},
{
name: 'proj2',
sourceRoot: 'mock',
tsConfig: 'mock',
targets: ['test'],
},
]);
});

it('should only return projects with a build target', async () => {
await cli.affectedAction({
action: 'log',
all: false,
base: 'origin/main',
cwd: process.cwd(),
includeFiles: [],
json: false,
restArgs: [],
tsConfigFilePath: 'tsconfig.base.json',
target: ['build'],
});

expect(logSpy).toHaveBeenCalledWith('Affected projects:\n - proj1');
});
});

describe('`all` option', () => {
beforeEach(() => {
trafSpy.mockResolvedValueOnce(['proj1']);

getNxTrueAffectedProjectsSpy.mockResolvedValueOnce([
{ name: 'proj1', sourceRoot: 'mock', tsConfig: 'mock' },
{ name: 'proj2', sourceRoot: 'mock', tsConfig: 'mock' },
{ name: 'proj1', sourceRoot: 'mock', tsConfig: 'mock', targets: [] },
{ name: 'proj2', sourceRoot: 'mock', tsConfig: 'mock', targets: [] },
]);
});

Expand All @@ -239,6 +281,7 @@ describe('cli', () => {
json: false,
restArgs: [],
tsConfigFilePath: 'tsconfig.base.json',
target: [],
});

expect(logSpy).toHaveBeenCalledWith('Affected projects:\n - proj1');
Expand All @@ -254,6 +297,7 @@ describe('cli', () => {
json: false,
restArgs: [],
tsConfigFilePath: 'tsconfig.base.json',
target: [],
});

expect(logSpy).toHaveBeenCalledWith(
Expand Down
22 changes: 21 additions & 1 deletion libs/nx/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,18 @@ export const affectedAction = async ({
restArgs,
tsConfigFilePath,
includeFiles,
target,
}: AffectedOptions) => {
const projects = await getNxTrueAffectedProjects(cwd);
let projects = await getNxTrueAffectedProjects(cwd);

if (target.length) {
projects = projects.filter(
(project) =>
!!project.targets?.some((projectTarget) =>
target.includes(projectTarget)
)
);
}

const affected = all
? projects.map((p) => p.name)
Expand Down Expand Up @@ -80,6 +90,7 @@ interface AffectedOptions {
json: boolean;
includeFiles: string[];
restArgs: string[];
target: string[];
}

const affectedCommand: CommandModule<unknown, AffectedOptions> = {
Expand Down Expand Up @@ -119,6 +130,13 @@ const affectedCommand: CommandModule<unknown, AffectedOptions> = {
return array.flatMap((v) => v.split(','));
},
},
target: {
desc: 'Comma separate list of targets to filter affected projects by',
type: 'array',
coerce: (array: string[]) => {
return array.flatMap((v) => v.split(',')).map((v) => v.trim());
},
},
},
handler: async ({
cwd,
Expand All @@ -128,6 +146,7 @@ const affectedCommand: CommandModule<unknown, AffectedOptions> = {
base,
json,
includeFiles,
target,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
$0,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
Expand All @@ -142,6 +161,7 @@ const affectedCommand: CommandModule<unknown, AffectedOptions> = {
base,
json,
includeFiles,
target,
restArgs: Object.entries(rest).map(
/* istanbul ignore next */
([key, value]) => `--${key}=${value}`
Expand Down
1 change: 1 addition & 0 deletions libs/nx/src/nx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ export async function getNxTrueAffectedProjects(
sourceRoot: project.sourceRoot,
implicitDependencies: project.implicitDependencies ?? [],
tsConfig,
targets: Object.keys(project.targets ?? {}),
};
});
}
2 changes: 1 addition & 1 deletion tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"paths": {
"@traf/core": ["libs/core/src/index.ts"],
"@traf/nx": ["libs/nx/src/cli.ts"],
"@traf/turbo": ["libs/turbo/src/cli.ts"],
yharaskrik marked this conversation as resolved.
Show resolved Hide resolved
"@traf/turbo": ["libs/turbo/src/cli.ts"]
}
},
"exclude": ["node_modules", "tmp"]
Expand Down