Skip to content

Commit e2f2abe

Browse files
authored
feat(examples-plugins): add package-json plugin 📦 (#354)
1 parent 81db87b commit e2f2abe

38 files changed

+2028
-46
lines changed

code-pushup.config.ts

+24-4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ import eslintPlugin, {
66
import {
77
fileSizePlugin,
88
fileSizeRecommendedRefs,
9+
packageJsonDocumentationGroupRef,
10+
packageJsonPerformanceGroupRef,
11+
packageJsonPlugin,
12+
packageJsonVersionControlGroupRef,
913
} from './examples/plugins/src';
1014
import type { CoreConfig } from './packages/models/src';
1115

@@ -34,18 +38,29 @@ const config: CoreConfig = {
3438

3539
plugins: [
3640
await eslintPlugin(await eslintConfigFromNxProjects()),
37-
await fileSizePlugin({
41+
fileSizePlugin({
3842
directory: './dist/packages',
3943
pattern: /\.js$/,
40-
budget: 42000,
44+
budget: 42_000,
45+
}),
46+
packageJsonPlugin({
47+
directory: './packages',
48+
license: 'MIT',
49+
type: 'module',
50+
dependencies: {
51+
zod: '^3.22.4',
52+
},
4153
}),
4254
],
4355

4456
categories: [
4557
{
4658
slug: 'bug-prevention',
4759
title: 'Bug prevention',
48-
refs: [{ type: 'group', plugin: 'eslint', slug: 'problems', weight: 1 }],
60+
refs: [
61+
{ type: 'group', plugin: 'eslint', slug: 'problems', weight: 1 },
62+
packageJsonVersionControlGroupRef,
63+
],
4964
},
5065
{
5166
slug: 'code-style',
@@ -57,7 +72,12 @@ const config: CoreConfig = {
5772
{
5873
slug: 'performance',
5974
title: 'Performance',
60-
refs: [...fileSizeRecommendedRefs],
75+
refs: [...fileSizeRecommendedRefs, packageJsonPerformanceGroupRef],
76+
},
77+
{
78+
slug: 'documentation',
79+
title: 'Documentation',
80+
refs: [packageJsonDocumentationGroupRef],
6181
},
6282
],
6383
};

e2e/cli-e2e/mocks/code-pushup.config.js

+14-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,20 @@ export default {
1111
apiKey: 'e2e-api-key',
1212
server: 'https://e2e.com/api',
1313
},
14-
categories: [],
14+
categories: [
15+
{
16+
slug: 'performance',
17+
title: 'Performance',
18+
refs: [
19+
{
20+
plugin: 'lighthouse',
21+
type: 'audit',
22+
slug: 'largest-contentful-paint',
23+
weight: 1
24+
}
25+
]
26+
}
27+
],
1528
plugins: [
1629
await eslintPlugin({ eslintrc: '.eslintrc.json', patterns: '**/*.ts' }),
1730
lighthousePlugin({ config: '.lighthouserc.json' }),

e2e/cli-e2e/mocks/code-pushup.config.mjs

+14-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,20 @@ export default {
1111
apiKey: 'e2e-api-key',
1212
server: 'https://e2e.com/api',
1313
},
14-
categories: [],
14+
categories: [
15+
{
16+
slug: 'performance',
17+
title: 'Performance',
18+
refs: [
19+
{
20+
plugin: 'lighthouse',
21+
type: 'audit',
22+
slug: 'largest-contentful-paint',
23+
weight: 1
24+
}
25+
]
26+
}
27+
],
1528
plugins: [
1629
await eslintPlugin({ eslintrc: '.eslintrc.json', patterns: '**/*.ts' }),
1730
lighthousePlugin({ config: '.lighthouserc.json' }),

e2e/cli-e2e/mocks/code-pushup.config.ts

+14-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,20 @@ export default {
1212
apiKey: 'e2e-api-key',
1313
server: 'https://e2e.com/api',
1414
},
15-
categories: [],
15+
categories: [
16+
{
17+
slug: 'performance',
18+
title: 'Performance',
19+
refs: [
20+
{
21+
plugin: 'lighthouse',
22+
type: 'audit',
23+
slug: 'largest-contentful-paint',
24+
weight: 1
25+
}
26+
]
27+
}
28+
],
1629
plugins: [
1730
await eslintPlugin({ eslintrc: '.eslintrc.json', patterns: '**/*.ts' }),
1831
lighthousePlugin({ config: '.lighthouserc.json' }),

e2e/cli-e2e/tests/__snapshots__/collect.e2e.test.ts.snap

-6
Original file line numberDiff line numberDiff line change
@@ -1113,12 +1113,6 @@ exports[`CLI collect > should run ESLint plugin and create report.json 1`] = `
11131113
"slug": "suggestions",
11141114
"title": "Suggestions",
11151115
},
1116-
{
1117-
"description": "Primarily about whitespace, semicolons, commas, and parentheses, all the parts of the program that determine how the code looks rather than how it executes.",
1118-
"refs": [],
1119-
"slug": "formatting",
1120-
"title": "Formatting",
1121-
},
11221116
{
11231117
"refs": [
11241118
{

examples/plugins/code-pushup.config.ts

+26-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
import {
2+
packageJsonDocumentationGroupRef,
3+
packageJsonPerformanceGroupRef,
4+
packageJsonPlugin,
5+
packageJsonVersionControlGroupRef,
6+
} from './src';
17
import fileSizePlugin, {
28
recommendedRefs as fileSizeRecommendedRefs,
39
} from './src/file-size/src/file-size.plugin';
@@ -19,17 +25,35 @@ const config = (() => ({
1925
},
2026
plugins: [
2127
fileSizePlugin({
22-
directory: './dist',
28+
directory: './dist/packages',
2329
pattern: /\.js$/,
2430
// eslint-disable-next-line no-magic-numbers
2531
budget: 42_000,
2632
}),
33+
packageJsonPlugin({
34+
directory: './packages',
35+
license: 'MIT',
36+
type: 'module',
37+
dependencies: {
38+
zod: '^3.22.4',
39+
},
40+
}),
2741
],
2842
categories: [
2943
{
3044
slug: 'performance',
3145
title: 'Performance',
32-
refs: [...fileSizeRecommendedRefs],
46+
refs: [...fileSizeRecommendedRefs, packageJsonPerformanceGroupRef],
47+
},
48+
{
49+
slug: 'bug-prevention',
50+
title: 'Bug prevention',
51+
refs: [packageJsonVersionControlGroupRef],
52+
},
53+
{
54+
slug: 'documentation',
55+
title: 'Documentation',
56+
refs: [packageJsonDocumentationGroupRef],
3357
},
3458
],
3559
}))();

examples/plugins/mocks/constants.ts

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import {
2+
PackageJson,
3+
SourceResult,
4+
} from '../src/package-json/src/integration/types';
5+
6+
export const packageJsonName = 'package.json';
7+
export const packageJson: PackageJson = {
8+
dependencies: {
9+
lib1: '0.0.0',
10+
},
11+
};
12+
13+
export function packageResult(json?: Partial<PackageJson>): SourceResult {
14+
const jsonData = {
15+
...packageJson,
16+
...json,
17+
};
18+
return {
19+
file: packageJsonName,
20+
json: jsonData,
21+
content: JSON.stringify(json),
22+
};
23+
}

examples/plugins/src/file-size/src/file-size.plugin.unit.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { vol } from 'memfs';
22
import { unlink } from 'node:fs/promises';
33
import { basename, join } from 'node:path';
44
import { beforeEach, describe, expect, it } from 'vitest';
5-
import { formatBytes } from '@code-pushup/utils';
5+
import { formatBytes } from '../../../../../dist/packages/utils';
66
import {
77
PluginOptions,
88
assertFileSize,

examples/plugins/src/index.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
export {
2-
audits as fileSizeAudits,
32
create as fileSizePlugin,
3+
audits as fileSizeAudits,
44
recommendedRefs as fileSizeRecommendedRefs,
55
PluginOptions as FileSizePluginOptions,
66
} from './file-size/src/file-size.plugin';
7+
export {
8+
recommendedRefs as packageJsonRecommendedRefs,
9+
versionControlGroupRef as packageJsonVersionControlGroupRef,
10+
documentationGroupRef as packageJsonDocumentationGroupRef,
11+
performanceGroupRef as packageJsonPerformanceGroupRef,
12+
} from './package-json/src/scoring';
13+
export { create as packageJsonPlugin } from './package-json/src/package-json.plugin';
+141
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
# package-json-plugin
2+
3+
🕵️ **Code PushUp plugin for evaluating `package.json` configurations** 📦
4+
5+
---
6+
7+
The plugin crawls the file base depending on your configuration and checks the content `package.json` files.
8+
9+
You can configure the plugin with the following options:
10+
11+
- `directory` - directory to crawl as string
12+
- `license` - file name pattern as string
13+
- `type` - size budget as number in bytes
14+
- `dependencies` - package dependencies as object
15+
- `devDependencies` - package dependencies as object
16+
- `optionalDependencies` - package dependencies as object
17+
18+
## Getting started
19+
20+
1. If you haven't already, install [@code-pushup/cli](../cli/README.md) and create a configuration file.
21+
22+
2. Copy the [plugin source](../package-json) as is into your project
23+
24+
1. Add this plugin to the `plugins` array in your Code PushUp CLI config file (e.g. `code-pushup.config.js`).
25+
26+
Pass in the path or the directory to crawl (relative to `process.cwd()`).
27+
28+
```js
29+
import packageJsonPlugin from './package-json.plugin';
30+
31+
export default {
32+
// ...
33+
plugins: [
34+
// ...
35+
packageJsonPlugin({
36+
directory: 'dist',
37+
license: 'MIT',
38+
type: 'module',
39+
dependencies: {
40+
package1: '0.0.1',
41+
},
42+
devDependencies: {
43+
package2: '0.0.1',
44+
},
45+
optionalDependencies: {
46+
package3: '0.0.1',
47+
},
48+
}),
49+
],
50+
};
51+
```
52+
53+
3. (Optional) Reference audits (or groups) which you wish to include in custom categories (use `npx code-pushup print-config` to list audits and groups).
54+
55+
Assign weights based on what influence each audit and group should have on the overall category score (assign weight 0 to only include it for extra info, without influencing the category score).
56+
57+
Use the recommendedRefs as quick starting point:
58+
59+
```js
60+
import packageJsonPlugin, { recommendedRefs } from './package-json.plugin';
61+
62+
export default {
63+
// ...
64+
categories: [
65+
// ...
66+
{
67+
slug: 'package-json',
68+
title: 'Package Json',
69+
refs: [...recommendedRefs],
70+
},
71+
],
72+
};
73+
```
74+
75+
Or set up more fine-grained categories over the exported group references:
76+
77+
```js
78+
import packageJsonPlugin, { packageJsonDocumentationGroupRef, packageJsonPerformanceGroupRef, packageJsonVersionControlGroupRef } from './package-json.plugin';
79+
80+
export default {
81+
// ...
82+
categories: [
83+
// ...
84+
{
85+
slug: 'bug-prevention',
86+
title: 'Bug prevention',
87+
refs: [packageJsonVersionControlGroupRef],
88+
},
89+
{
90+
slug: 'performance',
91+
title: 'Performance',
92+
refs: [packageJsonPerformanceGroupRef],
93+
},
94+
{
95+
slug: 'documentation',
96+
title: 'Documentation',
97+
refs: [packageJsonDocumentationGroupRef],
98+
},
99+
],
100+
};
101+
```
102+
103+
4. Run the CLI with `npx code-pushup collect` and view or upload report (refer to [CLI docs](../cli/README.md)).
104+
105+
## Audits
106+
107+
Detailed information about the audits can be found in the docs folder of the plugin.
108+
109+
The following audits are present:
110+
111+
**Documentation**:
112+
113+
- [package-json-license](./docs/license.audit.md)
114+
115+
**Performance**:
116+
117+
- [package-json-type](./docs/type.audit.md)
118+
119+
**Bug Prevention**:
120+
121+
- [package-json-dependencies](./docs/dependencies.audit.md)
122+
123+
## Helper
124+
125+
You can use the following exports:
126+
127+
### Constants:
128+
129+
- [`packageSlug`](./src/constants.ts#L5)
130+
- [`audits`](./src/constants.ts#L6)
131+
132+
### References:
133+
134+
Preconfigured audit and group references are available
135+
136+
The following groups can be referenced are present:
137+
138+
- [`reccomendedRefs`](./src/scoring.ts#L65)
139+
- [`packageJsonVersionControlGroupRef`](./src/scoring.ts#L20)
140+
- [`packageJsonPerformanceGroupRef`](./src/scoring.ts#L39)
141+
- [`packageJsonVersionControlGroupRef`](./src/scoring.ts#L58)

0 commit comments

Comments
 (0)