Skip to content

Commit

Permalink
Merge 7d6a05e into 7db09b9
Browse files Browse the repository at this point in the history
  • Loading branch information
BioPhoton authored Aug 6, 2024
2 parents 7db09b9 + 7d6a05e commit 35b9f38
Show file tree
Hide file tree
Showing 20 changed files with 653 additions and 154 deletions.
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 @@ -8,7 +8,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.

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import type { CoreConfig } from '../dist/packages/models';
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;
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

0 comments on commit 35b9f38

Please sign in to comment.