Skip to content

Commit

Permalink
feat(examples-plugins): add package-json plugin 📦 (#354)
Browse files Browse the repository at this point in the history
  • Loading branch information
BioPhoton committed Dec 17, 2023
1 parent 81db87b commit e2f2abe
Show file tree
Hide file tree
Showing 38 changed files with 2,028 additions and 46 deletions.
28 changes: 24 additions & 4 deletions code-pushup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import eslintPlugin, {
import {
fileSizePlugin,
fileSizeRecommendedRefs,
packageJsonDocumentationGroupRef,
packageJsonPerformanceGroupRef,
packageJsonPlugin,
packageJsonVersionControlGroupRef,
} from './examples/plugins/src';
import type { CoreConfig } from './packages/models/src';

Expand Down Expand Up @@ -34,18 +38,29 @@ const config: CoreConfig = {

plugins: [
await eslintPlugin(await eslintConfigFromNxProjects()),
await fileSizePlugin({
fileSizePlugin({
directory: './dist/packages',
pattern: /\.js$/,
budget: 42000,
budget: 42_000,
}),
packageJsonPlugin({
directory: './packages',
license: 'MIT',
type: 'module',
dependencies: {
zod: '^3.22.4',
},
}),
],

categories: [
{
slug: 'bug-prevention',
title: 'Bug prevention',
refs: [{ type: 'group', plugin: 'eslint', slug: 'problems', weight: 1 }],
refs: [
{ type: 'group', plugin: 'eslint', slug: 'problems', weight: 1 },
packageJsonVersionControlGroupRef,
],
},
{
slug: 'code-style',
Expand All @@ -57,7 +72,12 @@ const config: CoreConfig = {
{
slug: 'performance',
title: 'Performance',
refs: [...fileSizeRecommendedRefs],
refs: [...fileSizeRecommendedRefs, packageJsonPerformanceGroupRef],
},
{
slug: 'documentation',
title: 'Documentation',
refs: [packageJsonDocumentationGroupRef],
},
],
};
Expand Down
15 changes: 14 additions & 1 deletion e2e/cli-e2e/mocks/code-pushup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,20 @@ export default {
apiKey: 'e2e-api-key',
server: 'https://e2e.com/api',
},
categories: [],
categories: [
{
slug: 'performance',
title: 'Performance',
refs: [
{
plugin: 'lighthouse',
type: 'audit',
slug: 'largest-contentful-paint',
weight: 1
}
]
}
],
plugins: [
await eslintPlugin({ eslintrc: '.eslintrc.json', patterns: '**/*.ts' }),
lighthousePlugin({ config: '.lighthouserc.json' }),
Expand Down
15 changes: 14 additions & 1 deletion e2e/cli-e2e/mocks/code-pushup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,20 @@ export default {
apiKey: 'e2e-api-key',
server: 'https://e2e.com/api',
},
categories: [],
categories: [
{
slug: 'performance',
title: 'Performance',
refs: [
{
plugin: 'lighthouse',
type: 'audit',
slug: 'largest-contentful-paint',
weight: 1
}
]
}
],
plugins: [
await eslintPlugin({ eslintrc: '.eslintrc.json', patterns: '**/*.ts' }),
lighthousePlugin({ config: '.lighthouserc.json' }),
Expand Down
15 changes: 14 additions & 1 deletion e2e/cli-e2e/mocks/code-pushup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,20 @@ export default {
apiKey: 'e2e-api-key',
server: 'https://e2e.com/api',
},
categories: [],
categories: [
{
slug: 'performance',
title: 'Performance',
refs: [
{
plugin: 'lighthouse',
type: 'audit',
slug: 'largest-contentful-paint',
weight: 1
}
]
}
],
plugins: [
await eslintPlugin({ eslintrc: '.eslintrc.json', patterns: '**/*.ts' }),
lighthousePlugin({ config: '.lighthouserc.json' }),
Expand Down
6 changes: 0 additions & 6 deletions e2e/cli-e2e/tests/__snapshots__/collect.e2e.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1113,12 +1113,6 @@ exports[`CLI collect > should run ESLint plugin and create report.json 1`] = `
"slug": "suggestions",
"title": "Suggestions",
},
{
"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.",
"refs": [],
"slug": "formatting",
"title": "Formatting",
},
{
"refs": [
{
Expand Down
28 changes: 26 additions & 2 deletions examples/plugins/code-pushup.config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
import {
packageJsonDocumentationGroupRef,
packageJsonPerformanceGroupRef,
packageJsonPlugin,
packageJsonVersionControlGroupRef,
} from './src';
import fileSizePlugin, {
recommendedRefs as fileSizeRecommendedRefs,
} from './src/file-size/src/file-size.plugin';
Expand All @@ -19,17 +25,35 @@ const config = (() => ({
},
plugins: [
fileSizePlugin({
directory: './dist',
directory: './dist/packages',
pattern: /\.js$/,
// eslint-disable-next-line no-magic-numbers
budget: 42_000,
}),
packageJsonPlugin({
directory: './packages',
license: 'MIT',
type: 'module',
dependencies: {
zod: '^3.22.4',
},
}),
],
categories: [
{
slug: 'performance',
title: 'Performance',
refs: [...fileSizeRecommendedRefs],
refs: [...fileSizeRecommendedRefs, packageJsonPerformanceGroupRef],
},
{
slug: 'bug-prevention',
title: 'Bug prevention',
refs: [packageJsonVersionControlGroupRef],
},
{
slug: 'documentation',
title: 'Documentation',
refs: [packageJsonDocumentationGroupRef],
},
],
}))();
Expand Down
23 changes: 23 additions & 0 deletions examples/plugins/mocks/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import {
PackageJson,
SourceResult,
} from '../src/package-json/src/integration/types';

export const packageJsonName = 'package.json';
export const packageJson: PackageJson = {
dependencies: {
lib1: '0.0.0',
},
};

export function packageResult(json?: Partial<PackageJson>): SourceResult {
const jsonData = {
...packageJson,
...json,
};
return {
file: packageJsonName,
json: jsonData,
content: JSON.stringify(json),
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { vol } from 'memfs';
import { unlink } from 'node:fs/promises';
import { basename, join } from 'node:path';
import { beforeEach, describe, expect, it } from 'vitest';
import { formatBytes } from '@code-pushup/utils';
import { formatBytes } from '../../../../../dist/packages/utils';
import {
PluginOptions,
assertFileSize,
Expand Down
9 changes: 8 additions & 1 deletion examples/plugins/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
export {
audits as fileSizeAudits,
create as fileSizePlugin,
audits as fileSizeAudits,
recommendedRefs as fileSizeRecommendedRefs,
PluginOptions as FileSizePluginOptions,
} from './file-size/src/file-size.plugin';
export {
recommendedRefs as packageJsonRecommendedRefs,
versionControlGroupRef as packageJsonVersionControlGroupRef,
documentationGroupRef as packageJsonDocumentationGroupRef,
performanceGroupRef as packageJsonPerformanceGroupRef,
} from './package-json/src/scoring';
export { create as packageJsonPlugin } from './package-json/src/package-json.plugin';
141 changes: 141 additions & 0 deletions examples/plugins/src/package-json/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
# package-json-plugin

🕵️ **Code PushUp plugin for evaluating `package.json` configurations** 📦

---

The plugin crawls the file base depending on your configuration and checks the content `package.json` files.

You can configure the plugin with the following options:

- `directory` - directory to crawl as string
- `license` - file name pattern as string
- `type` - size budget as number in bytes
- `dependencies` - package dependencies as object
- `devDependencies` - package dependencies as object
- `optionalDependencies` - package dependencies as object

## Getting started

1. If you haven't already, install [@code-pushup/cli](../cli/README.md) and create a configuration file.

2. Copy the [plugin source](../package-json) as is into your project

1. Add this plugin to the `plugins` array in your Code PushUp CLI config file (e.g. `code-pushup.config.js`).

Pass in the path or the directory to crawl (relative to `process.cwd()`).

```js
import packageJsonPlugin from './package-json.plugin';

export default {
// ...
plugins: [
// ...
packageJsonPlugin({
directory: 'dist',
license: 'MIT',
type: 'module',
dependencies: {
package1: '0.0.1',
},
devDependencies: {
package2: '0.0.1',
},
optionalDependencies: {
package3: '0.0.1',
},
}),
],
};
```

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).

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).

Use the recommendedRefs as quick starting point:

```js
import packageJsonPlugin, { recommendedRefs } from './package-json.plugin';

export default {
// ...
categories: [
// ...
{
slug: 'package-json',
title: 'Package Json',
refs: [...recommendedRefs],
},
],
};
```

Or set up more fine-grained categories over the exported group references:

```js
import packageJsonPlugin, { packageJsonDocumentationGroupRef, packageJsonPerformanceGroupRef, packageJsonVersionControlGroupRef } from './package-json.plugin';

export default {
// ...
categories: [
// ...
{
slug: 'bug-prevention',
title: 'Bug prevention',
refs: [packageJsonVersionControlGroupRef],
},
{
slug: 'performance',
title: 'Performance',
refs: [packageJsonPerformanceGroupRef],
},
{
slug: 'documentation',
title: 'Documentation',
refs: [packageJsonDocumentationGroupRef],
},
],
};
```

4. Run the CLI with `npx code-pushup collect` and view or upload report (refer to [CLI docs](../cli/README.md)).

## Audits

Detailed information about the audits can be found in the docs folder of the plugin.

The following audits are present:

**Documentation**:

- [package-json-license](./docs/license.audit.md)

**Performance**:

- [package-json-type](./docs/type.audit.md)

**Bug Prevention**:

- [package-json-dependencies](./docs/dependencies.audit.md)

## Helper

You can use the following exports:

### Constants:

- [`packageSlug`](./src/constants.ts#L5)
- [`audits`](./src/constants.ts#L6)

### References:

Preconfigured audit and group references are available

The following groups can be referenced are present:

- [`reccomendedRefs`](./src/scoring.ts#L65)
- [`packageJsonVersionControlGroupRef`](./src/scoring.ts#L20)
- [`packageJsonPerformanceGroupRef`](./src/scoring.ts#L39)
- [`packageJsonVersionControlGroupRef`](./src/scoring.ts#L58)
Loading

0 comments on commit e2f2abe

Please sign in to comment.