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 all 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
3 changes: 2 additions & 1 deletion libs/nx/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ npx @traf/nx@latest affected <action> [options]
### **Options**

| Option | Description | Default |
| -------------------- | ------------------------------------------------------------------------------------ | -------------------- |
|----------------------|--------------------------------------------------------------------------------------| -------------------- |
| `--cwd` | The current working directory | `process.cwd()` |
| `--all` | Outputs all available projects regardless of changes | `false` |
| `--base` | The base branch to compare against | `origin/main` |
| `--tsConfigFilePath` | The path to the root tsconfig file | `tsconfig.base.json` |
| `--action` | The action to perform. Can be any command | `log` |
| `--json` | Output the result as JSON | `false` |
| `--includeFiles` | Comma separated list of glob patterns to include (relative to projects' source root) | |
| `--target` | Comma separated list of targets to filter affected projects by | |
50 changes: 48 additions & 2 deletions libs/nx/src/cli.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ describe('cli', () => {
json: false,
restArgs: [],
tsConfigFilePath: 'tsconfig.base.json',
target: [],
});
});

Expand All @@ -93,6 +94,7 @@ describe('cli', () => {
json: false,
restArgs: [],
tsConfigFilePath: 'tsconfig.json',
target: [],
});
});
});
Expand Down Expand Up @@ -125,6 +127,7 @@ describe('cli', () => {
json: false,
restArgs: [],
tsConfigFilePath: 'tsconfig.base.json',
target: [],
});

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

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

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

expect(consoleSpy).toHaveBeenCalledWith('["proj1"]');
Expand All @@ -206,6 +212,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 +226,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 +283,7 @@ describe('cli', () => {
json: false,
restArgs: [],
tsConfigFilePath: 'tsconfig.base.json',
target: [],
});

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

expect(logSpy).toHaveBeenCalledWith(
Expand Down
23 changes: 22 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,14 @@ 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',
default: [],
coerce: (array: string[]) => {
return array.flatMap((v) => v.split(',')).map((v) => v.trim());
},
},
},
handler: async ({
cwd,
Expand All @@ -128,6 +147,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 +162,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
Loading