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-plugin): extend config generator #778

Merged
merged 13 commits into from
Aug 6, 2024
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ node_modules
**/*.mock.*
**/code-pushup.config.ts
**/mocks/fixtures/**
**/__snapshots__/**
3 changes: 2 additions & 1 deletion packages/nx-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"tslib": "2.6.2",
"nx": "^17.1.3",
"@code-pushup/models": "*",
"zod": "^3.22.4"
"zod": "^3.22.4",
"@code-pushup/utils": "*"
},
"type": "commonjs",
"main": "./src/index.js",
Expand Down

This file was deleted.

BioPhoton marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import type { CoreConfig } from '../dist/packages/models';

Check failure on line 1 in packages/nx-plugin/src/generators/configuration/__snapshots__/root-code-pushup.config.ts

View workflow job for this annotation

GitHub Actions / Code PushUp

<↗> Code coverage | Branch coverage

1st branch is not taken in any test case.
import * as myPlugin from 'my-plugin';
import { myPluginCategory } from 'my-plugin';

// see: https://github.com/code-pushup/cli/blob/main/packages/models/docs/models-reference.md#coreconfig
export default {
persist: {
filename: 'report-123',
},
update: {
apiKey: '123',
},
plugins: [myPlugin({ timeout: 42 })],
categories: [myPluginCategory()],
} satisfies CoreConfig;

Check warning on line 15 in packages/nx-plugin/src/generators/configuration/__snapshots__/root-code-pushup.config.ts

View workflow job for this annotation

GitHub Actions / Code PushUp

<↗> Code coverage | Line coverage

Lines 1-15 are not covered in any test case.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import * as devKit from '@nx/devkit';
import { formatFiles } from '@nx/devkit';
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing';
import { join } from 'node:path';
import { describe, expect, it } from 'vitest';
import { generateCodePushupConfig } from './code-pushup-config';

describe('generateCodePushupConfig options', () => {
let tree: devKit.Tree;
const project = 'test-app';
const projectRoot = join('libs', project);

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

it('should create code-pushup.config.ts with options in tree', async () => {
generateCodePushupConfig(tree, projectRoot, {
fileImports: 'import type { CoreConfig } from "../dist/packages/models";',
persist: { filename: 'report-123' },
upload: { apiKey: '123' },
plugins: [
{
fileImports: 'import * as myPlugin from "my-plugin";',
codeStrings: 'myPlugin({ timeout: 42})',
},
],
categories: [
{
fileImports: 'import {myPluginCategory} from "my-plugin";',
codeStrings: 'myPluginCategory()',
},
],
});

await formatFiles(tree);
// eslint-disable-next-line @typescript-eslint/no-floating-promises
expect(
tree.read(join(projectRoot, 'code-pushup.config.ts'))?.toString(),
).toMatchFileSnapshot('__snapshots__/root-code-pushup.config.ts');
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { Tree, generateFiles, logger } from '@nx/devkit';
import { join } from 'node:path';
import type { PersistConfig, UploadConfig } from '@code-pushup/models';
import type { ItemOrArray } from '@code-pushup/utils';
import { ExecutableCode } from './types';
import {
formatArrayToJSArray,
formatArrayToLinesOfJsString,
formatObjectToFormattedJsString,
normalizeExecutableCode,
normalizeItemOrArray,
} from './utils';

export const DEFAULT_IMPORTS = [
"import type { CoreConfig } from '@code-pushup/models';",
];

export type GenerateCodePushupConfigOptions = {
fileImports?: ItemOrArray<string>;
persist?: Partial<PersistConfig>;
upload?: Partial<UploadConfig>;
plugins?: ExecutableCode[];
categories?: ExecutableCode[];
};

export function generateCodePushupConfig(
tree: Tree,
root: string,
options?: GenerateCodePushupConfigOptions,
) {
const supportedFormats = ['ts', 'mjs', 'js'];
const firstExistingFormat = supportedFormats.find(ext =>
tree.exists(join(root, `code-pushup.config.${ext}`)),
);
if (firstExistingFormat) {
logger.warn(
`NOTE: No config file created as code-pushup.config.${firstExistingFormat} file already exists.`,
);
} else {
const {
fileImports: rawImports,
persist,
upload,
plugins: rawPlugins = [], // plugins are required
categories: rawCategories,
} = options ?? {};

const plugins = rawPlugins.map(normalizeExecutableCode);
const categories = rawCategories?.map(normalizeExecutableCode);
const configFileImports = [
...(rawImports ? normalizeItemOrArray(rawImports) : DEFAULT_IMPORTS),
...plugins.flatMap(({ fileImports }) => fileImports),
...(categories ?? []).flatMap(({ fileImports }) => fileImports),
];

generateFiles(tree, join(__dirname, 'files'), root, {
...options,
fileImports: formatArrayToLinesOfJsString(configFileImports),
persist: formatObjectToFormattedJsString(persist),
upload: formatObjectToFormattedJsString(upload),
plugins: formatArrayToJSArray(
plugins.flatMap(({ codeStrings }) => codeStrings),
),
categories:
categories &&
formatArrayToJSArray(
categories.flatMap(({ codeStrings }) => codeStrings),
),
});
}
}
Loading
Loading