Skip to content

Commit 163f67f

Browse files
authored
feat(core): pass config to plugin runner (#1087)
This PR includes: - passing perts of the persist options to runner logic - adding a plugin slug as const for `plugin-eslint`
1 parent 7aab96e commit 163f67f

File tree

8 files changed

+34
-13
lines changed

8 files changed

+34
-13
lines changed

packages/core/src/lib/implementation/execute-plugin.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ export async function executePlugin(
6868
? // IF not null, take the result from cache
6969
((await readRunnerResults(pluginMeta.slug, outputDir)) ??
7070
// ELSE execute the plugin runner
71-
(await executePluginRunner(pluginConfig)))
72-
: await executePluginRunner(pluginConfig);
71+
(await executePluginRunner(pluginConfig, persist)))
72+
: await executePluginRunner(pluginConfig, persist);
7373

7474
if (cacheWrite) {
7575
await writeRunnerResults(pluginMeta.slug, outputDir, {

packages/core/src/lib/implementation/execute-plugin.unit.test.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,12 @@ describe('executePlugin', () => {
1818
vi.restoreAllMocks();
1919
});
2020

21-
it('should execute a valid plugin config', async () => {
21+
it('should execute a valid plugin config and pass runner params', async () => {
22+
const executePluginRunnerSpy = vi.spyOn(
23+
runnerModule,
24+
'executePluginRunner',
25+
);
26+
2227
await expect(
2328
executePlugin(MINIMAL_PLUGIN_CONFIG_MOCK, {
2429
persist: { outputDir: '' },
@@ -39,6 +44,11 @@ describe('executePlugin', () => {
3944
}),
4045
]),
4146
});
47+
48+
expect(executePluginRunnerSpy).toHaveBeenCalledWith(
49+
MINIMAL_PLUGIN_CONFIG_MOCK,
50+
{ outputDir: '' },
51+
);
4252
});
4353

4454
it('should try to read cache if cache.read is true', async () => {
@@ -102,7 +112,7 @@ describe('executePlugin', () => {
102112

103113
await expect(
104114
executePlugin(MINIMAL_PLUGIN_CONFIG_MOCK, {
105-
persist: { outputDir: 'dummy-path-result-is-mocked' },
115+
persist: { outputDir: MEMFS_VOLUME },
106116
cache: { read: true, write: false },
107117
}),
108118
).resolves.toStrictEqual({
@@ -122,6 +132,7 @@ describe('executePlugin', () => {
122132

123133
expect(executePluginRunnerSpy).toHaveBeenCalledWith(
124134
MINIMAL_PLUGIN_CONFIG_MOCK,
135+
{ outputDir: MEMFS_VOLUME },
125136
);
126137
});
127138

@@ -383,8 +394,8 @@ describe('executePlugins', () => {
383394
{
384395
...MINIMAL_PLUGIN_CONFIG_MOCK,
385396
runner: {
386-
command: 'node',
387-
args: ['-v'],
397+
command: 'echo',
398+
args: ['16'],
388399
outputFile: 'output.json',
389400
outputTransform: (outputs: unknown): Promise<AuditOutputs> =>
390401
Promise.resolve([
@@ -398,7 +409,7 @@ describe('executePlugins', () => {
398409
},
399410
},
400411
],
401-
persist: { outputDir: '.code-pushup' },
412+
persist: { outputDir: MEMFS_VOLUME },
402413
cache: { read: false, write: false },
403414
},
404415
{ progress: false },

packages/core/src/lib/implementation/runner.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { writeFile } from 'node:fs/promises';
33
import path from 'node:path';
44
import {
55
type AuditOutputs,
6+
type PersistConfig,
67
type PluginConfig,
78
type RunnerConfig,
89
type RunnerFunction,
@@ -14,6 +15,7 @@ import {
1415
executeProcess,
1516
fileExists,
1617
isVerbose,
18+
objectToCliArgs,
1719
readJsonFile,
1820
removeDirectoryIfExists,
1921
ui,
@@ -32,12 +34,13 @@ export type ValidatedRunnerResult = Omit<RunnerResult, 'audits'> & {
3234

3335
export async function executeRunnerConfig(
3436
cfg: RunnerConfig,
37+
config: Required<Pick<PersistConfig, 'outputDir'>>,
3538
): Promise<RunnerResult> {
3639
const { args, command, outputFile, outputTransform } = cfg;
3740

3841
const { duration, date } = await executeProcess({
3942
command,
40-
args,
43+
args: [...(args ?? []), ...objectToCliArgs(config)],
4144
observer: {
4245
onStdout: stdout => {
4346
if (isVerbose()) {
@@ -66,12 +69,13 @@ export async function executeRunnerConfig(
6669

6770
export async function executeRunnerFunction(
6871
runner: RunnerFunction,
72+
config: PersistConfig,
6973
): Promise<RunnerResult> {
7074
const date = new Date().toISOString();
7175
const start = performance.now();
7276

7377
// execute plugin runner
74-
const audits = await runner();
78+
const audits = await runner(config);
7579

7680
// create runner result
7781
return {
@@ -96,12 +100,13 @@ export class AuditOutputsMissingAuditError extends Error {
96100

97101
export async function executePluginRunner(
98102
pluginConfig: Pick<PluginConfig, 'audits' | 'runner'>,
103+
persist: Required<Pick<PersistConfig, 'outputDir'>>,
99104
): Promise<Omit<RunnerResult, 'audits'> & { audits: AuditOutputs }> {
100105
const { audits: pluginConfigAudits, runner } = pluginConfig;
101106
const runnerResult: RunnerResult =
102107
typeof runner === 'object'
103-
? await executeRunnerConfig(runner)
104-
: await executeRunnerFunction(runner);
108+
? await executeRunnerConfig(runner, persist)
109+
: await executeRunnerFunction(runner, persist);
105110
const { audits: unvalidatedAuditOutputs, ...executionMeta } = runnerResult;
106111

107112
const result = auditOutputsSchema.safeParse(unvalidatedAuditOutputs);

packages/models/docs/models-reference.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1421,7 +1421,7 @@ _Function._
14211421

14221422
_Parameters:_
14231423

1424-
- _none_
1424+
1. [PersistConfig](#persistconfig)
14251425

14261426
_Returns:_
14271427

packages/models/src/lib/runner-config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { z } from 'zod/v4';
22
import { auditOutputsSchema } from './audit-output.js';
33
import { convertAsyncZodFunctionToSchema } from './implementation/function.js';
44
import { filePathSchema } from './implementation/schemas.js';
5+
import { persistConfigSchema } from './persist-config.js';
56

67
export const outputTransformSchema = convertAsyncZodFunctionToSchema(
78
z.function({
@@ -25,6 +26,7 @@ export type RunnerConfig = z.infer<typeof runnerConfigSchema>;
2526

2627
export const runnerFunctionSchema = convertAsyncZodFunctionToSchema(
2728
z.function({
29+
input: [persistConfigSchema],
2830
output: z.union([auditOutputsSchema, z.promise(auditOutputsSchema)]),
2931
}),
3032
);

packages/plugin-eslint/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { eslintPlugin } from './lib/eslint-plugin.js';
33
export default eslintPlugin;
44

55
export type { ESLintPluginConfig } from './lib/config.js';
6+
export { ESLINT_PLUGIN_SLUG } from './lib/constants.js';
67

78
export {
89
eslintConfigFromAllNxProjects,
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const ESLINT_PLUGIN_SLUG = 'eslint';

packages/plugin-eslint/src/lib/eslint-plugin.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
eslintPluginConfigSchema,
1010
eslintPluginOptionsSchema,
1111
} from './config.js';
12+
import { ESLINT_PLUGIN_SLUG } from './constants.js';
1213
import { listAuditsAndGroups } from './meta/index.js';
1314
import { createRunnerConfig } from './runner/index.js';
1415

@@ -60,7 +61,7 @@ export async function eslintPlugin(
6061
) as typeof import('../../package.json');
6162

6263
return {
63-
slug: 'eslint',
64+
slug: ESLINT_PLUGIN_SLUG,
6465
title: 'ESLint',
6566
icon: 'eslint',
6667
description: 'Official Code PushUp ESLint plugin',

0 commit comments

Comments
 (0)