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

[POC/Proposal] Custom output defined by user #68

Merged
merged 8 commits into from
Dec 18, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
46 changes: 46 additions & 0 deletions docs/output.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
]
```

---

## `github`

Create check run, post commit status and a detailed comment on your PR.
Expand Down Expand Up @@ -68,6 +70,8 @@ Post comment on PR

<img src="../assets/pr-comment.png" alt="pr comment" height="300px" />

---

## `json`

Save raw results in json file.
Expand Down Expand Up @@ -100,3 +104,45 @@ Override default options
type: `string` default: `bundlemon-results.json`

Use custom file name for results.

---

## `custom`

Use our own implementation to output or process results.

### Example

`path` option is required.

```json
"reportOutput": [
[
"custom",
{
"path": "custom-output.js"
}
]
]
```

In the root of your project create `custom-output.js`:

```js
// Function that accepts generated report as parameter
const output = (report) => {
console.log(report);
};

module.exports = output;
```

TODO: Document report object structure.

### Options

#### `path`

type: `string`

Relative path to the js file exporting a function.
50 changes: 50 additions & 0 deletions packages/bundlemon/src/main/outputs/outputs/custom.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import * as yup from 'yup';
import path from 'path';
import { Report } from 'bundlemon-utils';
import { createLogger } from '../../../common/logger';
import { validateYup } from '../../utils/validationUtils';
import type { Output, OutputInstance } from '../types';

const NAME = 'custom';

const logger = createLogger(`${NAME} output`);

interface CustomOutputOptions {
path?: string;
}
interface NormalizedCustomOutputOptions {
path: string;
}

export type CustomOutputFunction = (results: Report) => any;

function validateOptions(options: unknown): NormalizedCustomOutputOptions {
const schema: yup.SchemaOf<CustomOutputOptions, CustomOutputOptions> = yup.object().required().shape({
path: yup.string().required(),
});

const normalizedOptions = validateYup(schema, options, `${NAME} output`);
if (normalizedOptions === undefined) {
throw new Error(`validation error in output "${NAME}" options`);
}
return normalizedOptions as NormalizedCustomOutputOptions;
}

const output: Output = {
name: NAME,
create: ({ options }): OutputInstance | Promise<OutputInstance | undefined> | undefined => {
const normalizedOptions = validateOptions(options);

return {
generate: async (report: Report): Promise<void> => {
const resolvedPath = path.join(process.cwd(), normalizedOptions.path);
logger.debug(`Requiring ${resolvedPath}`);
// eslint-disable-next-line @typescript-eslint/no-var-requires
const customOutput = require(resolvedPath);
customOutput(report);
},
};
},
};

export default output;
3 changes: 2 additions & 1 deletion packages/bundlemon/src/main/outputs/outputs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
import consoleOutput from './console';
import githubOutput from './github';
import jsonOutput from './json';
import customOutput from './custom';

import type { Output } from '../types';

const outputs: Output[] = [consoleOutput, githubOutput, jsonOutput];
const outputs: Output[] = [consoleOutput, githubOutput, jsonOutput, customOutput];

export const getAllOutputs = (): Output[] => outputs;