Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
0616166
wip
BioPhoton Nov 24, 2023
04d1536
docs(examples): add custom file-size plugin example
BioPhoton Nov 24, 2023
f40eb8a
refactor: remove target lint
BioPhoton Nov 24, 2023
5cf9a3b
revert changes
BioPhoton Nov 24, 2023
f691181
test(examples-plugins): add tests
BioPhoton Nov 24, 2023
17ad833
refactor(examples-plugins): format
BioPhoton Nov 24, 2023
12783e6
refactor(examples-plugins): format
BioPhoton Nov 24, 2023
ad22e2d
test(examples-plugins): fix target
BioPhoton Nov 24, 2023
cee2772
test(examples-plugins): fix target
BioPhoton Nov 24, 2023
81fadd3
test(examples-plugins): wip
BioPhoton Nov 24, 2023
65760a9
test(examples-plugins): test file-size plugin
BioPhoton Nov 25, 2023
76ca096
test(examples-plugins): adopt path for windows
BioPhoton Nov 25, 2023
9112b10
test(examples-plugins): adopt path for windows
BioPhoton Nov 25, 2023
2b89a33
ci(examples-plugins): setup lint target and fix issues
BioPhoton Nov 25, 2023
f546538
ci(test-utils): add memfs
BioPhoton Nov 25, 2023
415695d
test(examples-plugins): add dependency
BioPhoton Nov 25, 2023
d2651d9
test(examples-plugins): fix imports
BioPhoton Nov 25, 2023
5a22841
test(examples-plugins): fix lint config
BioPhoton Nov 25, 2023
1750b88
test(examples-plugins): fix format
BioPhoton Nov 25, 2023
63f3b9d
test(examples-plugins): fix format
BioPhoton Nov 25, 2023
5db05a2
refactor(examples-plugins): reduce code
BioPhoton Nov 25, 2023
93f7584
refactor(examples-plugins): reduce code
BioPhoton Nov 25, 2023
ede253e
refactor(examples-plugins): refactor code
BioPhoton Nov 25, 2023
5eeaae6
refactor(examples-plugins): refactor code
BioPhoton Nov 25, 2023
de95230
refactor(examples-plugins): add comments
BioPhoton Nov 25, 2023
9962271
Update examples/plugins/src/file-size.plugin.ts
BioPhoton Nov 27, 2023
8641070
Update examples/plugins/src/file-size.plugin.ts
BioPhoton Nov 27, 2023
2f59ec0
Update examples/plugins/src/file-size.plugin.unit.test.ts
BioPhoton Nov 27, 2023
1f391a8
refactor(examples-plugins): adopt logic and tests
BioPhoton Nov 27, 2023
4528bca
refactor(examples-plugins): add file path formatting
BioPhoton Nov 28, 2023
745557b
Merge branch 'main' into example-plugin-file-size
BioPhoton Nov 29, 2023
43279db
refactor: merge
BioPhoton Nov 29, 2023
15a37e2
refactor(examples-plugins): fix lint
BioPhoton Nov 29, 2023
e0afab7
refactor(examples-plugins): fix lint
BioPhoton Nov 29, 2023
b5f0003
merge main
BioPhoton Nov 30, 2023
dc3f959
Update examples/plugins/src/file-size.plugin.unit.test.ts
BioPhoton Nov 30, 2023
860dd10
merge main
BioPhoton Nov 30, 2023
3bcd9f2
merge main
BioPhoton Nov 30, 2023
62a1626
wip
BioPhoton Dec 1, 2023
f58c427
wip
BioPhoton Dec 1, 2023
10d2f7e
test(examples-plugins): adopt tests
BioPhoton Dec 1, 2023
df683b3
test(examples-plugins): format
BioPhoton Dec 1, 2023
0fecf08
test(examples-plugins): change imports
BioPhoton Dec 1, 2023
641c19c
test(examples-plugins): change imports 2
BioPhoton Dec 1, 2023
dff457d
test(examples-plugins): change imports 3
BioPhoton Dec 1, 2023
016ca2f
test(examples-plugins): change file type
BioPhoton Dec 1, 2023
0d38661
test(examples-plugins): fix linting
BioPhoton Dec 1, 2023
38b2940
Update testing-utils/src/lib/setup/fs.mock.ts
BioPhoton Dec 1, 2023
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
19 changes: 18 additions & 1 deletion code-pushup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@ import nx from '@nx/devkit';
import 'dotenv/config';
import type { ESLint } from 'eslint';
import { stat } from 'node:fs/promises';
import { join } from 'path';
import { z } from 'zod';
import eslintPlugin from './dist/packages/plugin-eslint';
import {
fileSizePlugin,
fileSizeRecommendedRefs,
} from './examples/plugins/src';
import type { CoreConfig } from './packages/models/src';

const exists = (path: string) =>
Expand Down Expand Up @@ -58,7 +63,14 @@ const config: CoreConfig = {
project: env.CP_PROJECT,
},

plugins: [await eslintPlugin({ eslintrc: eslintConfig, patterns })],
plugins: [
await eslintPlugin({ eslintrc: eslintConfig, patterns }),
await fileSizePlugin({
directory: './dist/packages',
pattern: /\.js$/,
budget: 42000,
}),
],

categories: [
{
Expand All @@ -73,6 +85,11 @@ const config: CoreConfig = {
{ type: 'group', plugin: 'eslint', slug: 'suggestions', weight: 1 },
],
},
{
slug: 'performance',
title: 'Performance',
refs: [...fileSizeRecommendedRefs],
},
],
};

Expand Down
39 changes: 39 additions & 0 deletions examples/plugins/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"extends": ["../../.eslintrc.json"],
"ignorePatterns": ["!**/*"],
"overrides": [
{
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
"rules": {
"@nx/enforce-module-boundaries": ["off"]
}
},
{
"files": ["*.ts", "*.tsx"],
"parserOptions": {
"project": ["examples/plugins/tsconfig.*?.json"]
},
"rules": {}
},
{
"files": ["*.test.ts", "*.test.tsx"],
"parserOptions": {
"project": ["examples/plugins/tsconfig.*?.json"]
},
"rules": {
"max-lines": "off"
}
},
{
"files": ["*.js", "*.jsx"],
"rules": {}
},
{
"files": ["*.json"],
"parser": "jsonc-eslint-parser",
"rules": {
"@nx/dependency-checks": ["error"]
}
}
]
}
38 changes: 38 additions & 0 deletions examples/plugins/code-pushup.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import {
create as fileSizePlugin,
recommendedRefs as fileSizeRecommendedRefs,
} from './src/file-size/file-size.plugin';

/**
* Run it with:
* `nx run-collect examples-plugins`
*
* - For all formats use `--persist.format=md,json`
* - For better debugging use `--verbose --no-progress`
*
*/

const outputDir = '.code-pushup';
// eslint-disable-next-line unicorn/no-unreadable-iife
const config = (() => ({
persist: {
outputDir,
},
plugins: [
fileSizePlugin({
directory: './dist',
pattern: /\.js$/,
// eslint-disable-next-line no-magic-numbers
budget: 42_000,
}),
],
categories: [
{
slug: 'performance',
title: 'Performance',
refs: [...fileSizeRecommendedRefs],
},
],
}))();

export default config;
40 changes: 40 additions & 0 deletions examples/plugins/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"name": "examples-plugins",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "examples/plugins/src",
"projectType": "library",
"implicitDependencies": ["cli"],
"targets": {
"lint": {
"executor": "@nx/linter:eslint",
"outputs": ["{options.outputFile}"],
"options": {
"lintFilePatterns": ["examples/plugins/**/*.ts"]
},
"dependsOn": ["^build"]
},
"unit-test": {
"executor": "@nx/vite:test",
"outputs": ["{options.reportsDirectory}"],
"options": {
"config": "examples/plugins/vite.config.unit.ts",
"reportsDirectory": "../../coverage/examples-plugins/unit-tests"
},
"dependsOn": ["^build"]
},
"integration-test": {
"executor": "@nx/vite:test",
"outputs": ["{options.reportsDirectory}"],
"options": {
"config": "examples/plugins/vite.config.integration.ts",
"reportsDirectory": "../../coverage/examples-plugins/integration-tests"
},
"dependsOn": ["^build"]
},
"run-collect": {
"command": "npx dist/packages/cli collect --config=examples/plugins/code-pushup.config.ts --persist.format=md",
"dependsOn": ["^build"]
}
},
"tags": []
}
115 changes: 115 additions & 0 deletions examples/plugins/src/file-size/file-size.plugin.integration.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import { vol } from 'memfs';
import { beforeEach, describe, expect, it } from 'vitest';
import { executePlugin } from '@code-pushup/core';
import {
auditSchema,
categoryRefSchema,
pluginConfigSchema,
} from '@code-pushup/models';
import { MEMFS_VOLUME } from '@code-pushup/testing-utils';
import {
PluginOptions,
audits,
create,
recommendedRefs,
pluginSlug as slug,
} from './file-size.plugin';

const projectJson = JSON.stringify(
{
test: 42,
arr: [1, 2, 3],
obj: {
test: 42,
},
},
null,
2,
);
const testJs = `
const str = 'Hello World'
const num = 42;
const obj = ${projectJson};
`;

describe('create', () => {
const baseOptions: PluginOptions = {
directory: '/',
};

beforeEach(() => {
vol.fromJSON(
{
'project.json': projectJson,
'src/test.js': testJs,
},
MEMFS_VOLUME,
);
});

it('should return valid PluginConfig', () => {
const pluginConfig = create(baseOptions);
expect(() => pluginConfigSchema.parse(pluginConfig)).not.toThrow();
expect(pluginConfig).toEqual({
audits,
description:
'A plugin to measure and assert filesize of files in a directory.',
icon: 'javascript',
runner: expect.any(Function),
slug,
title: 'File Size',
});
});

it('should return PluginConfig that executes correctly', async () => {
const pluginConfig = create(baseOptions);
await expect(executePlugin(pluginConfig)).resolves.toMatchObject({
description:
'A plugin to measure and assert filesize of files in a directory.',
slug,
title: 'File Size',
duration: expect.any(Number),
date: expect.any(String),
audits: expect.any(Array),
});
});

it('should use pattern', async () => {
const pluginConfig = create({
...baseOptions,
pattern: /\.js$/,
});
const { audits: auditOutputs } = await executePlugin(pluginConfig);

expect(auditOutputs).toHaveLength(1);
expect(auditOutputs[0]?.score).toBe(1);
expect(auditOutputs[0]?.details?.issues).toHaveLength(1);
});

it('should use budget', async () => {
const pluginConfig = create({
...baseOptions,
budget: 0,
});
const { audits: auditOutputs } = await executePlugin(pluginConfig);

expect(auditOutputs).toHaveLength(1);
expect(auditOutputs[0]?.score).toBe(0);
expect(auditOutputs[0]?.details?.issues).toHaveLength(2);
});
});

describe('audits', () => {
it.each(audits)('should be a valid audit meta info', audit => {
expect(() => auditSchema.parse(audit)).not.toThrow();
});
});

describe('recommendedRefs', () => {
it.each(recommendedRefs)(
'should be a valid category reference',
categoryRef => {
expect(() => categoryRefSchema.parse(categoryRef)).not.toThrow();
},
);
});
Loading