Skip to content

Commit

Permalink
feat(angular): use addExplicitTargets for Angular projects to avoid p…
Browse files Browse the repository at this point in the history
…artial project config
  • Loading branch information
jaysoo committed Feb 16, 2024
1 parent 219ba8b commit 07bcf88
Show file tree
Hide file tree
Showing 12 changed files with 108 additions and 22 deletions.
2 changes: 1 addition & 1 deletion packages/angular/src/generators/add-linting/add-linting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export async function addLintingGenerator(
skipFormat: true,
rootProject: rootProject,
addPlugin: options.addPlugin,
addExplicitTargets: !options.addPlugin,
addExplicitTargets: true,
});
tasks.push(lintTask);

Expand Down
1 change: 1 addition & 0 deletions packages/angular/src/generators/add-linting/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ export interface AddLintingGeneratorSchema {
skipPackageJson?: boolean;
unitTestRunner?: string;
addPlugin?: boolean;
addExplicitTargets?: boolean;
}
8 changes: 3 additions & 5 deletions packages/angular/src/generators/application/lib/add-e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ import { getInstalledAngularVersionInfo } from '../../utils/version-utils';
import type { NormalizedSchema } from './normalized-schema';

export async function addE2e(tree: Tree, options: NormalizedSchema) {
// check for explicit false, separate e2e projects infer targets by default
const addPlugin = process.env.NX_ADD_PLUGINS !== 'false';

if (options.e2eTestRunner === 'cypress') {
// TODO: This can call `@nx/web:static-config` generator when ready
addFileServerTarget(tree, options, 'serve-static');
Expand All @@ -37,7 +34,8 @@ export async function addE2e(tree: Tree, options: NormalizedSchema) {
devServerTarget: `${options.name}:serve:development`,
baseUrl: 'http://localhost:4200',
rootProject: options.rootProject,
addPlugin: addPlugin,
addPlugin: options.addPlugin,
addExplicitTargets: false, // since e2e is a separate project, use inferred targets
});
} else if (options.e2eTestRunner === 'playwright') {
const { configurationGenerator: playwrightConfigurationGenerator } =
Expand Down Expand Up @@ -65,7 +63,7 @@ export async function addE2e(tree: Tree, options: NormalizedSchema) {
}`,
webServerAddress: `http://localhost:${options.port ?? 4200}`,
rootProject: options.rootProject,
addPlugin: addPlugin,
addPlugin: options.addPlugin,
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export async function normalizeOptions(
});
options.rootProject = appProjectRoot === '.';
options.projectNameAndRootFormat = projectNameAndRootFormat;
options.addPlugin ??= process.env.NX_ADD_PLUGINS === 'true';
options.addPlugin ??= process.env.NX_ADD_PLUGINS !== 'false';

const e2eProjectName = options.rootProject ? 'e2e' : `${appProjectName}-e2e`;
const e2eProjectRoot = options.rootProject ? 'e2e' : `${appProjectRoot}-e2e`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ export async function cypressComponentConfigurationInternal(
tree: Tree,
options: CypressComponentConfigSchema
) {
options.addPlugin ??= process.env.NX_ADD_PLUGINS === 'true';
options.addPlugin ??= process.env.NX_ADD_PLUGINS !== 'false';

const projectConfig = readProjectConfiguration(tree, options.project);
const installTask = await baseCyCTConfig(tree, {
project: options.project,
skipFormat: true,
addPlugin: options.addPlugin,
addExplicitTargets: !options.addPlugin,
addExplicitTargets: true,
});

await configureCypressCT(tree, options);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,16 @@ exports[`workspace move to nx layout should create nx.json 1`] = `
],
"sharedGlobals": [],
},
"plugins": [
{
"options": {
"targetName": "lint",
},
"plugin": "@nx/eslint/plugin",
},
],
"targetDefaults": {
"@nx/eslint:lint": {
"cache": true,
"inputs": [
"default",
"{workspaceRoot}/.eslintrc.json",
"{workspaceRoot}/.eslintignore",
"{workspaceRoot}/eslint.config.js",
],
},
"build": {
"cache": true,
"dependsOn": [
Expand Down
2 changes: 1 addition & 1 deletion packages/angular/src/generators/utils/add-jest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export async function addJest(
skipPackageJson: options.skipPackageJson,
skipFormat: true,
addPlugin: options.addPlugin,
addExplicitTargets: !options.addPlugin,
addExplicitTargets: true,
});

const setupFile = joinPathFragments(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,48 @@ export default defineConfig({
"
`);
});

it('should support generating explicit targets', async () => {
mockedInstalledCypressVersion.mockReturnValue(undefined); // ensure init is called
addProject(tree, { name: 'explicit-lib', type: 'apps' });
addProject(tree, { name: 'inferred-lib', type: 'apps' });

await cypressE2EConfigurationGenerator(tree, {
project: 'explicit-lib',
baseUrl: 'http://localhost:4200',
addPlugin: true,
addExplicitTargets: true,
});
await cypressE2EConfigurationGenerator(tree, {
project: 'inferred-lib',
baseUrl: 'http://localhost:4200',
addPlugin: true,
addExplicitTargets: false,
});

expect(readProjectConfiguration(tree, 'explicit-lib').targets.e2e)
.toMatchInlineSnapshot(`
{
"configurations": {
"ci": {
"devServerTarget": "explicit-lib:serve-static",
},
"production": {
"devServerTarget": "explicit-lib:serve:production",
},
},
"executor": "@nx/cypress:cypress",
"options": {
"cypressConfig": "apps/explicit-lib/cypress.config.ts",
"devServerTarget": "explicit-lib:serve",
"testingType": "e2e",
},
}
`);
expect(
readProjectConfiguration(tree, 'inferred-lib').targets.e2e
).toBeUndefined();
});
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export interface CypressE2EConfigSchema {
webServerCommands?: Record<string, string>;
ciWebServerCommand?: string;
addPlugin?: boolean;
addExplicitTargets?: boolean;
}

type NormalizedSchema = ReturnType<typeof normalizeOptions>;
Expand All @@ -56,7 +57,10 @@ export function configurationGenerator(
tree: Tree,
options: CypressE2EConfigSchema
) {
return configurationGeneratorInternal(tree, { addPlugin: false, ...options });
return configurationGeneratorInternal(tree, {
addPlugin: false,
...options,
});
}

export async function configurationGeneratorInternal(
Expand Down Expand Up @@ -86,14 +90,15 @@ export async function configurationGeneratorInternal(
);

await addFiles(tree, opts, projectGraph, hasPlugin);
if (!hasPlugin) {
if (!hasPlugin || options.addExplicitTargets) {
addTarget(tree, opts);
}

const linterTask = await addLinterToCyProject(tree, {
...opts,
cypressDir: opts.directory,
addPlugin: opts.addPlugin,
addExplicitTargets: opts.addExplicitTargets,
});
tasks.push(linterTask);

Expand Down
2 changes: 2 additions & 0 deletions packages/cypress/src/utils/add-linter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export interface CyLinterOptions {
**/
overwriteExisting?: boolean;
addPlugin?: boolean;
addExplicitTargets?: boolean;
}

export async function addLinterToCyProject(
Expand All @@ -65,6 +66,7 @@ export async function addLinterToCyProject(
skipPackageJson: options.skipPackageJson,
rootProject: options.rootProject,
addPlugin: options.addPlugin,
addExplicitTargets: options.addExplicitTargets,
})
);
}
Expand Down
39 changes: 37 additions & 2 deletions packages/eslint/src/generators/lint-project/lint-project.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {
addProjectConfiguration,
readJson,
readProjectConfiguration,
updateJson,
Tree,
readJson,
updateJson,
} from '@nx/devkit';

import { Linter } from '../utils/linter';
Expand Down Expand Up @@ -298,4 +298,39 @@ describe('@nx/eslint:lint-project', () => {
"
`);
});

it('should support generating explicit targets on project config', async () => {
addProjectConfiguration(tree, 'explicit-lib', {
root: 'libs/explicit-lib',
projectType: 'library',
targets: {},
});
addProjectConfiguration(tree, 'inferred-lib', {
root: 'libs/inferred-lib',
projectType: 'library',
targets: {},
});

await lintProjectGenerator(tree, {
...defaultOptions,
linter: Linter.EsLint,
project: 'explicit-lib',
addExplicitTargets: true,
});
await lintProjectGenerator(tree, {
...defaultOptions,
linter: Linter.EsLint,
project: 'inferred-lib',
addExplicitTargets: false,
});

const explicitCOnfig = readProjectConfiguration(tree, 'explicit-lib');
expect(explicitCOnfig.targets.lint).toMatchInlineSnapshot(`
{
"executor": "@nx/eslint:lint",
}
`);
const inferredConfig = readProjectConfiguration(tree, 'inferred-lib');
expect(inferredConfig.targets.lint).toBeUndefined();
});
});
2 changes: 2 additions & 0 deletions packages/workspace/src/generators/preset/preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ async function createPreset(tree: Tree, options: Schema) {
e2eTestRunner: options.e2eTestRunner ?? 'cypress',
bundler: options.bundler,
ssr: options.ssr,
addPlugin,
});
} else if (options.preset === Preset.AngularStandalone) {
const {
Expand All @@ -52,6 +53,7 @@ async function createPreset(tree: Tree, options: Schema) {
e2eTestRunner: options.e2eTestRunner ?? 'cypress',
bundler: options.bundler,
ssr: options.ssr,
addPlugin,
});
} else if (options.preset === Preset.ReactMonorepo) {
const { applicationGenerator: reactApplicationGenerator } = require('@nx' +
Expand Down

0 comments on commit 07bcf88

Please sign in to comment.