Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ exports[`nx-plugin > should add configuration target dynamically 1`] = `
"configurations": {},
"executor": "nx:run-commands",
"options": {
"command": "nx g @code-pushup/nx-plugin:configuration --skipTarget --targetName="code-pushup" --project="my-lib"",
"command": "nx g @code-pushup/nx-plugin:configuration --project="my-lib"",
},
"parallelism": true,
},
Expand Down
94 changes: 1 addition & 93 deletions e2e/nx-plugin-e2e/tests/generator-configuration.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ describe('nx-plugin g configuration', () => {
await teardownTestFolder(testFileDir);
});

it('should generate code-pushup.config.ts file and add target to project.json', async () => {
it('should generate code-pushup.config.ts file', async () => {
const cwd = path.join(testFileDir, 'configure');
await materializeTree(tree, cwd);

Expand Down Expand Up @@ -64,22 +64,7 @@ describe('nx-plugin g configuration', () => {
'NX Generating @code-pushup/nx-plugin:configuration',
);
expect(cleanedStdout).toMatch(/^CREATE.*code-pushup.config.ts/m);
expect(cleanedStdout).toMatch(/^UPDATE.*project.json/m);

const projectJson = await readFile(
path.join(cwd, 'libs', project, 'project.json'),
'utf8',
);

expect(JSON.parse(projectJson)).toStrictEqual(
expect.objectContaining({
targets: expect.objectContaining({
'code-pushup': {
executor: '@code-pushup/nx-plugin:cli',
},
}),
}),
);
await expect(
readFile(
path.join(cwd, 'libs', project, 'code-pushup.config.ts'),
Expand Down Expand Up @@ -111,21 +96,6 @@ describe('nx-plugin g configuration', () => {
'NX Generating @code-pushup/nx-plugin:configuration',
);
expect(cleanedStdout).not.toMatch(/^CREATE.*code-pushup.config.ts/m);
expect(cleanedStdout).toMatch(/^UPDATE.*project.json/m);

const projectJson = await readFile(
path.join(cwd, 'libs', project, 'project.json'),
'utf8',
);
expect(JSON.parse(projectJson)).toStrictEqual(
expect.objectContaining({
targets: expect.objectContaining({
'code-pushup': {
executor: '@code-pushup/nx-plugin:cli',
},
}),
}),
);
});

it('should NOT create a code-pushup.config.ts file if skipConfig is given', async () => {
Expand All @@ -152,21 +122,6 @@ describe('nx-plugin g configuration', () => {
'NX Generating @code-pushup/nx-plugin:configuration',
);
expect(cleanedStdout).not.toMatch(/^CREATE.*code-pushup.config.ts/m);
expect(cleanedStdout).toMatch(/^UPDATE.*project.json/m);

const projectJson = await readFile(
path.join(cwd, 'libs', project, 'project.json'),
'utf8',
);
expect(JSON.parse(projectJson)).toStrictEqual(
expect.objectContaining({
targets: expect.objectContaining({
'code-pushup': {
executor: '@code-pushup/nx-plugin:cli',
},
}),
}),
);

await expect(
readFile(
Expand All @@ -176,53 +131,6 @@ describe('nx-plugin g configuration', () => {
).rejects.toThrow('no such file or directory');
});

it('should NOT add target to project.json if skipTarget is given', async () => {
const cwd = path.join(testFileDir, 'configure-skip-target');
await materializeTree(tree, cwd);

const { code, stdout } = await executeProcess({
command: 'npx',
args: [
'nx',
'g',
'@code-pushup/nx-plugin:configuration',
project,
'--skipTarget',
],
cwd,
});
expect(code).toBe(0);

const cleanedStdout = removeColorCodes(stdout);

expect(cleanedStdout).toContain(
'NX Generating @code-pushup/nx-plugin:configuration',
);
expect(cleanedStdout).toMatch(/^CREATE.*code-pushup.config.ts/m);
expect(cleanedStdout).not.toMatch(/^UPDATE.*project.json/m);

const projectJson = await readFile(
path.join(cwd, 'libs', project, 'project.json'),
'utf8',
);
expect(JSON.parse(projectJson)).toStrictEqual(
expect.objectContaining({
targets: expect.not.objectContaining({
'code-pushup': {
executor: '@code-pushup/nx-plugin:cli',
},
}),
}),
);

await expect(
readFile(
path.join(cwd, 'libs', project, 'code-pushup.config.ts'),
'utf8',
),
).resolves.toStrictEqual(expect.any(String));
});

it('should inform about dry run', async () => {
const cwd = path.join(testFileDir, 'configure');
await materializeTree(tree, cwd);
Expand Down
4 changes: 2 additions & 2 deletions e2e/nx-plugin-e2e/tests/plugin-create-nodes.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ describe('nx-plugin', () => {
configurations: {},
executor: 'nx:run-commands',
options: {
command: `nx g @code-pushup/nx-plugin:configuration --skipTarget --targetName="code-pushup" --project="${project}"`,
command: `nx g @code-pushup/nx-plugin:configuration --project="${project}"`,
},
parallelism: true,
},
Expand Down Expand Up @@ -120,7 +120,7 @@ describe('nx-plugin', () => {
expect(projectJson.targets).toStrictEqual({
'code-pushup--configuration': expect.objectContaining({
options: {
command: `nx g XYZ:configuration --skipTarget --targetName="code-pushup" --project="${project}"`,
command: `nx g XYZ:configuration --project="${project}"`,
},
}),
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,73 +8,7 @@ import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing';
import * as path from 'node:path';
import { afterEach, describe, expect, it, vi } from 'vitest';
import { DEFAULT_TARGET_NAME, PACKAGE_NAME } from '../../internal/constants.js';
import { addTargetToProject, configurationGenerator } from './generator.js';

describe('addTargetToProject', () => {
let tree: Tree;
const testProjectName = 'test-app';

beforeEach(() => {
tree = createTreeWithEmptyWorkspace();
addProjectConfiguration(tree, 'test-app', {
root: 'test-app',
});
});

afterEach(() => {
//reset tree
tree.delete(testProjectName);
});

it('should generate a project target', () => {
addTargetToProject(
tree,
{
root: testProjectName,
projectType: 'library',
sourceRoot: `${testProjectName}/src`,
targets: {},
},
{
project: testProjectName,
},
);

const projectConfiguration = readProjectConfiguration(
tree,
testProjectName,
);

expect(projectConfiguration.targets?.[DEFAULT_TARGET_NAME]).toEqual({
executor: `${PACKAGE_NAME}:cli`,
});
});

it('should use targetName to generate a project target', () => {
addTargetToProject(
tree,
{
root: testProjectName,
projectType: 'library',
sourceRoot: `${testProjectName}/src`,
targets: {},
},
{
project: testProjectName,
targetName: 'cp',
},
);

const projectConfiguration = readProjectConfiguration(
tree,
testProjectName,
);

expect(projectConfiguration.targets?.['cp']).toEqual({
executor: `${PACKAGE_NAME}:cli`,
});
});
});
import { configurationGenerator } from './generator.js';

describe('configurationGenerator', () => {
let tree: Tree;
Expand All @@ -92,21 +26,6 @@ describe('configurationGenerator', () => {
tree.delete(testProjectName);
});

it('should generate a project target and config file', async () => {
await configurationGenerator(tree, {
project: testProjectName,
});

const projectConfiguration = readProjectConfiguration(
tree,
testProjectName,
);

expect(projectConfiguration.targets?.[DEFAULT_TARGET_NAME]).toEqual({
executor: `${PACKAGE_NAME}:cli`,
});
});

it('should skip config creation if skipConfig is used', async () => {
await configurationGenerator(tree, {
project: testProjectName,
Expand All @@ -121,20 +40,6 @@ describe('configurationGenerator', () => {
expect(loggerInfoSpy).toHaveBeenCalledWith('Skip config file creation');
});

it('should skip target creation if skipTarget is used', async () => {
await configurationGenerator(tree, {
project: testProjectName,
skipTarget: true,
});

const projectConfiguration = readProjectConfiguration(
tree,
testProjectName,
);
expect(projectConfiguration.targets).toBeUndefined();
expect(loggerInfoSpy).toHaveBeenCalledWith('Skip adding target to project');
});

it('should skip formatting', async () => {
await configurationGenerator(tree, {
project: testProjectName,
Expand Down
32 changes: 1 addition & 31 deletions packages/nx-plugin/src/generators/configuration/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ import {
formatFiles,
logger,
readProjectConfiguration,
updateProjectConfiguration,
} from '@nx/devkit';
import type { ProjectConfiguration } from 'nx/src/config/workspace-json-project-json';
import { DEFAULT_TARGET_NAME, PACKAGE_NAME } from '../../internal/constants.js';
import { generateCodePushupConfig } from './code-pushup-config.js';
import type { ConfigurationGeneratorOptions } from './schema.js';

Expand All @@ -16,46 +13,19 @@ export async function configurationGenerator(
) {
const projectConfiguration = readProjectConfiguration(tree, options.project);

const { skipConfig, skipTarget, skipFormat } = options;
const { skipConfig, skipFormat } = options;

if (skipConfig === true) {
logger.info('Skip config file creation');
} else {
generateCodePushupConfig(tree, projectConfiguration.root);
}

if (skipTarget === true) {
logger.info('Skip adding target to project');
} else {
addTargetToProject(tree, projectConfiguration, options);
}

if (skipFormat === true) {
logger.info('Skip formatting files');
} else {
await formatFiles(tree);
}
}

export function addTargetToProject(
tree: Tree,
projectConfiguration: ProjectConfiguration,
options: ConfigurationGeneratorOptions,
) {
const { targets } = projectConfiguration;
const { targetName, project } = options;

const codePushupTargetConfig = {
executor: `${PACKAGE_NAME}:cli`,
};

updateProjectConfiguration(tree, project, {
...projectConfiguration,
targets: {
...targets,
[targetName ?? DEFAULT_TARGET_NAME]: codePushupTargetConfig,
},
});
}

export default configurationGenerator;
11 changes: 0 additions & 11 deletions packages/nx-plugin/src/generators/configuration/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,10 @@
"index": 0
}
},
"targetName": {
"type": "string",
"description": "The name of the target.",
"x-prompt": "Which name should the target get? default is code-pushup.",
"default": "code-pushup"
},
"bin": {
"type": "string",
"description": "Path to Code PushUp CLI"
},
"skipTarget": {
"type": "boolean",
"description": "Skip adding the target to project.json.",
"$default": "false"
},
"skipConfig": {
"type": "boolean",
"description": "Skip adding the code-pushup.config.ts to the project root.",
Expand Down
4 changes: 2 additions & 2 deletions packages/nx-plugin/src/plugin/plugin.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe('@code-pushup/nx-plugin/plugin', () => {
[projectRoot]: {
targets: {
[`${CP_TARGET_NAME}--configuration`]: {
command: `nx g ${PACKAGE_NAME}:configuration --skipTarget --targetName="code-pushup" --project="@org/empty-root"`,
command: `nx g ${PACKAGE_NAME}:configuration --project="@org/empty-root"`,
},
},
},
Expand All @@ -66,7 +66,7 @@ describe('@code-pushup/nx-plugin/plugin', () => {
[projectRoot]: {
targets: {
[`${CP_TARGET_NAME}--configuration`]: {
command: `nx g ${PACKAGE_NAME}:configuration --skipTarget --targetName="code-pushup" --project="@org/empty-root"`,
command: `nx g ${PACKAGE_NAME}:configuration --project="@org/empty-root"`,
},
},
},
Expand Down
Loading
Loading