Skip to content

Commit

Permalink
feat(angular): force explicit targets when NX_ADD_PLUGINS is not expl…
Browse files Browse the repository at this point in the history
…icitly true (#21852)

Co-authored-by: Jack Hsu <[email protected]>
  • Loading branch information
leosvelperez and jaysoo committed Feb 16, 2024
1 parent 4c8c24b commit 5b6d1b4
Show file tree
Hide file tree
Showing 23 changed files with 145 additions and 26 deletions.
1 change: 1 addition & 0 deletions packages/angular/src/generators/add-linting/add-linting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export async function addLintingGenerator(
skipFormat: true,
rootProject: rootProject,
addPlugin: 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;
}
5 changes: 3 additions & 2 deletions packages/angular/src/generators/application/lib/add-e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ export async function addE2e(tree: Tree, options: NormalizedSchema) {
devServerTarget: `${options.name}:serve:development`,
baseUrl: 'http://localhost:4200',
rootProject: options.rootProject,
addPlugin: options.addPlugin,
addPlugin: false,
addExplicitTargets: false, // since e2e is a separate project, use inferred targets
});
} else if (options.e2eTestRunner === 'playwright') {
const { configurationGenerator: playwrightConfigurationGenerator } =
Expand Down Expand Up @@ -62,7 +63,7 @@ export async function addE2e(tree: Tree, options: NormalizedSchema) {
}`,
webServerAddress: `http://localhost:${options.port ?? 4200}`,
rootProject: options.rootProject,
addPlugin: options.addPlugin,
addPlugin: false,
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ export async function addLinting(host: Tree, options: NormalizedSchema) {
skipPackageJson: options.skipPackageJson,
unitTestRunner: options.unitTestRunner,
skipFormat: true,
addPlugin: options.addPlugin,
addPlugin: false,
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export async function addUnitTestRunner(host: Tree, options: NormalizedSchema) {
projectRoot: options.appProjectRoot,
skipPackageJson: options.skipPackageJson,
strict: options.strict,
addPlugin: options.addPlugin,
addPlugin: false,
});
}
}
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 @@ -52,7 +52,8 @@ export async function cypressComponentConfigurationInternal(
const installTask = await baseCyCTConfig(tree, {
project: options.project,
skipFormat: true,
addPlugin: options.addPlugin,
addPlugin: false,
addExplicitTargets: true,
});

await configureCypressCT(tree, options);
Expand Down
2 changes: 1 addition & 1 deletion packages/angular/src/generators/library/library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ async function addUnitTestRunner(
projectRoot: options.projectRoot,
skipPackageJson: options.skipPackageJson,
strict: options.strict,
addPlugin: options.addPlugin,
addPlugin: false,
});
}
}
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
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ export async function updateRootEsLintConfig(
unitTestRunner?: string
): Promise<void> {
await lintInitGenerator(tree, {
addPlugin: process.env.NX_ADD_PLUGINS !== 'false',
addPlugin: process.env.NX_ADD_PLUGINS === 'true',
});

if (!existingEsLintConfig) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ export async function generateStorybookConfiguration(
tree: Tree,
options: StorybookConfigurationOptions
): Promise<GeneratorCallback> {
const { configurationGenerator } = ensurePackage('@nx/storybook', nxVersion);
const addPlugin = process.env.NX_ADD_PLUGINS === 'true';

const { configurationGenerator } = ensurePackage<
typeof import('@nx/storybook')
>('@nx/storybook', nxVersion);
return await configurationGenerator(tree, {
project: options.project,
uiFramework: '@storybook/angular',
Expand All @@ -17,5 +21,7 @@ export async function generateStorybookConfiguration(
interactionTests: options.interactionTests,
configureStaticServe: options.configureStaticServe,
skipFormat: true,
addPlugin: addPlugin,
addExplicitTargets: !addPlugin,
});
}
3 changes: 2 additions & 1 deletion packages/angular/src/generators/utils/add-jest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ export async function addJest(
skipSerializers: false,
skipPackageJson: options.skipPackageJson,
skipFormat: true,
addPlugin: options.addPlugin,
addPlugin: false,
addExplicitTargets: true,
});

const setupFile = joinPathFragments(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export async function componentConfigurationGeneratorInternal(
tasks.push(updateDeps(tree, opts));

addProjectFiles(tree, projectConfig, opts);
if (!hasPlugin) {
if (!hasPlugin || opts.addExplicitTargets) {
addTargetToProject(tree, projectConfig, opts);
}
updateNxJsonConfiguration(tree, hasPlugin);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,9 @@ export interface CypressComponentConfigurationSchema {
bundler?: 'webpack' | 'vite';
jsx?: boolean;
addPlugin?: boolean;

/**
* @internal
*/
addExplicitTargets?: boolean;
}
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
10 changes: 8 additions & 2 deletions packages/cypress/src/generators/configuration/configuration.ts
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,13 +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
4 changes: 4 additions & 0 deletions packages/cypress/src/utils/add-linter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ export interface CyLinterOptions {
* This is useful when adding linting to a brand new project vs an existing one
**/
overwriteExisting?: boolean;
addPlugin?: boolean;
addExplicitTargets?: boolean;
}

export async function addLinterToCyProject(
Expand All @@ -63,6 +65,8 @@ export async function addLinterToCyProject(
setParserOptionsProject: options.setParserOptionsProject,
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();
});
});
7 changes: 6 additions & 1 deletion packages/eslint/src/generators/lint-project/lint-project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ interface LintProjectOptions {
rootProject?: boolean;
keepExistingVersions?: boolean;
addPlugin?: boolean;

/**
* @internal
*/
addExplicitTargets?: boolean;
}

export function lintProjectGenerator(tree: Tree, options: LintProjectOptions) {
Expand Down Expand Up @@ -91,7 +96,7 @@ export async function lintProjectGeneratorInternal(
}

const hasPlugin = hasEslintPlugin(tree);
if (hasPlugin) {
if (hasPlugin && !options.addExplicitTargets) {
if (
lintFilePatterns &&
lintFilePatterns.length &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export async function configurationGeneratorInternal(
);
}
});
if (!hasPlugin) {
if (!hasPlugin || options.addExplicitTargets) {
updateWorkspace(tree, options);
}

Expand Down
5 changes: 5 additions & 0 deletions packages/jest/src/generators/configuration/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ export interface JestProjectSchema {
compiler?: 'tsc' | 'babel' | 'swc';
skipPackageJson?: boolean;
js?: boolean;

/**
* @internal
*/
addExplicitTargets?: boolean;
}

export type NormalizedJestProjectSchema = JestProjectSchema & {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ export async function configurationGeneratorInternal(

let devDeps = {};

if (!hasPlugin) {
if (!hasPlugin || schema.addExplicitTargets) {
if (schema.uiFramework === '@storybook/angular') {
addAngularStorybookTarget(tree, schema.project, schema.interactionTests);
} else {
Expand Down
5 changes: 5 additions & 0 deletions packages/storybook/src/generators/configuration/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,9 @@ export interface StorybookConfigureSchema {
*/
cypressDirectory?: string;
addPlugin?: boolean;

/**
* @internal
*/
addExplicitTargets?: boolean;
}

0 comments on commit 5b6d1b4

Please sign in to comment.