Skip to content

Commit 2c8eeeb

Browse files
committed
feat(core): allow disabling project inference from config file
1 parent 90b3aab commit 2c8eeeb

File tree

4 files changed

+41
-11
lines changed

4 files changed

+41
-11
lines changed

packages/core/src/graph/infer-project.spec.ts

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
1+
import type { NxDotnetConfig } from '@nx-dotnet/utils';
2+
13
import * as fs from 'fs';
24

3-
import * as config from '@nx-dotnet/utils/src/lib/utility-functions/config';
5+
let configValues: NxDotnetConfig = {
6+
nugetPackages: {},
7+
};
8+
9+
jest.mock(
10+
'@nx-dotnet/utils/src/lib/utility-functions/config',
11+
() =>
12+
({
13+
readConfig: () => configValues,
14+
} as typeof import('@nx-dotnet/utils/src/lib/utility-functions/config')),
15+
);
416

517
import { registerProjectTargets } from './infer-project';
618

@@ -10,19 +22,19 @@ describe('infer-project', () => {
1022
});
1123

1224
it('should obey inferProjectTargets: false', () => {
13-
jest.spyOn(config, 'readConfig').mockReturnValue({
25+
configValues = {
1426
nugetPackages: {},
1527
inferProjectTargets: false,
16-
});
28+
};
1729
jest.spyOn(fs, 'readFileSync').mockReturnValueOnce('<project></project>');
1830

1931
expect(registerProjectTargets('libs/api/my.csproj')).toEqual({});
2032
});
2133

2234
it('should generate build, lint, serve targets for projects', () => {
23-
jest.spyOn(config, 'readConfig').mockReturnValue({
35+
configValues = {
2436
nugetPackages: {},
25-
});
37+
};
2638
jest.spyOn(fs, 'readFileSync').mockReturnValueOnce('<project></project>');
2739

2840
const targets = registerProjectTargets('libs/api/my.csproj');
@@ -65,9 +77,9 @@ describe('infer-project', () => {
6577
});
6678

6779
it('should generate test target for test projects', () => {
68-
jest.spyOn(config, 'readConfig').mockReturnValue({
80+
configValues = {
6981
nugetPackages: {},
70-
});
82+
};
7183
jest
7284
.spyOn(fs, 'readFileSync')
7385
.mockReturnValueOnce('<project ref=Microsoft.NET.Test.Sdk></project>');

packages/core/src/graph/infer-project.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@ import {
1212
GetTestExecutorConfig,
1313
} from '../models';
1414

15-
export const projectFilePatterns = ['*.csproj', '*.fsproj', '*.vbproj'];
15+
export const projectFilePatterns = readConfig().inferProjects
16+
? ['*.csproj', '*.fsproj', '*.vbproj']
17+
: [];
1618

1719
export const registerProjectTargets = (projectFile: string) => {
18-
const { inferProjectTargets } = readConfig();
1920
const targets: Record<string, TargetConfiguration> = {};
21+
const { inferProjectTargets } = readConfig();
2022
if (inferProjectTargets ?? true) {
2123
const projectFileContents = readFileSync(
2224
resolve(workspaceRoot, projectFile),

packages/utils/src/lib/models/nx-dotnet-config.interface.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,10 @@ export interface NxDotnetConfig {
2323
* @default true
2424
*/
2525
inferProjectTargets?: boolean;
26+
27+
/**
28+
* Set to false to skip project inference
29+
* @default true
30+
*/
31+
inferProjects?: boolean;
2632
}

packages/utils/src/lib/utility-functions/config.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,26 @@ import {
99
import { CONFIG_FILE_PATH } from '../constants';
1010
import { NxDotnetConfig } from '../models';
1111

12-
export const DefaultConfigValues: Partial<NxDotnetConfig> = {
12+
export const DefaultConfigValues: NxDotnetConfig = {
1313
solutionFile: '{npmScope}.nx-dotnet.sln',
14+
inferProjects: true,
15+
inferProjectTargets: true,
16+
nugetPackages: {},
1417
};
1518

1619
let cachedConfig: NxDotnetConfig;
1720
export function readConfig(host?: Tree): NxDotnetConfig {
1821
if (host) {
1922
return readJson(host, CONFIG_FILE_PATH);
2023
} else {
21-
cachedConfig ??= readJsonFile(`${workspaceRoot}/${CONFIG_FILE_PATH}`);
24+
try {
25+
cachedConfig ??= {
26+
...DefaultConfigValues,
27+
...readJsonFile(`${workspaceRoot}/${CONFIG_FILE_PATH}`),
28+
};
29+
} catch {
30+
return DefaultConfigValues;
31+
}
2232
return cachedConfig;
2333
}
2434
}

0 commit comments

Comments
 (0)