Skip to content

Commit e960ed3

Browse files
authored
fix(plugin-knip): use runner function
2 parents a38e64f + cd17a09 commit e960ed3

File tree

3 files changed

+34
-28
lines changed

3 files changed

+34
-28
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import path from 'node:path';
22
import type { PluginConfig } from '@code-pushup/models';
33
import { KNIP_AUDITS, KNIP_GROUPS, KNIP_PLUGIN_SLUG } from './constants.js';
4-
import { RunnerOptions, createRunnerConfig } from './runner/index.js';
4+
import { RunnerOptions, createRunnerFunction } from './runner/index.js';
55

66
export type PluginOptions = RunnerOptions;
77

@@ -19,7 +19,7 @@ export function knipPlugin(options: PluginOptions = {}): PluginConfig {
1919
title: 'Knip',
2020
icon: 'folder-javascript',
2121
description: 'A plugin to track dependencies and duplicates',
22-
runner: createRunnerConfig({
22+
runner: createRunnerFunction({
2323
...runnerOptions,
2424
outputFile,
2525
}),

packages/plugin-knip/src/lib/runner/index.ts

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import path from 'node:path';
2-
import type { RunnerConfig } from '@code-pushup/models';
2+
import type { AuditOutputs, RunnerFunction } from '@code-pushup/models';
3+
import { executeProcess, readJsonFile } from '@code-pushup/utils';
34
import {
45
KNIP_PLUGIN_SLUG,
56
KNIP_REPORT_NAME,
@@ -45,7 +46,9 @@ export type KnipCliOptions = Partial<{
4546
}>;
4647
export type RunnerOptions = KnipCliOptions & CustomReporterOptions;
4748

48-
export function createRunnerConfig(options: RunnerOptions = {}): RunnerConfig {
49+
export function createRunnerFunction(
50+
options: RunnerOptions = {},
51+
): RunnerFunction {
4952
const {
5053
outputFile = path.join(KNIP_PLUGIN_SLUG, KNIP_REPORT_NAME),
5154
rawOutputFile,
@@ -54,24 +57,27 @@ export function createRunnerConfig(options: RunnerOptions = {}): RunnerConfig {
5457
// Resolve the reporter path from the installed package
5558
const reporterPath = '@code-pushup/knip-plugin/src/lib/reporter.js';
5659

57-
return {
58-
command: 'npx',
59-
args: [
60-
'knip',
61-
// off as we want to CI to pass
62-
'--no-exit-code',
63-
// off by default to guarantee execution without interference
64-
'--no-progress',
65-
// code-pushup reporter is used from the installed package
66-
`--reporter=${reporterPath}`,
67-
// code-pushup reporter options are passed as string. Double JSON.stringify ensures proper escaping on all platforms
68-
`--reporter-options=${JSON.stringify(
69-
JSON.stringify({
70-
outputFile,
71-
rawOutputFile,
72-
} satisfies CustomReporterOptions),
73-
)}`,
74-
],
75-
outputFile,
60+
return async () => {
61+
await executeProcess({
62+
command: 'npx',
63+
args: [
64+
'knip',
65+
// off as we want to CI to pass
66+
'--no-exit-code',
67+
// off by default to guarantee execution without interference
68+
'--no-progress',
69+
// code-pushup reporter is used from the installed package
70+
`--reporter=${reporterPath}`,
71+
// code-pushup reporter options are passed as string. Double JSON.stringify ensures proper escaping on all platforms
72+
`--reporter-options=${JSON.stringify(
73+
JSON.stringify({
74+
outputFile,
75+
rawOutputFile,
76+
} satisfies CustomReporterOptions),
77+
)}`,
78+
],
79+
});
80+
81+
return readJsonFile<AuditOutputs>(outputFile);
7682
};
7783
}
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { describe, expect, it } from 'vitest';
2-
import { runnerConfigSchema } from '@code-pushup/models';
3-
import { createRunnerConfig } from './index.js';
2+
import { runnerFunctionSchema } from '@code-pushup/models';
3+
import { createRunnerFunction } from './index.js';
44

5-
describe('runnerConfig', () => {
6-
it('should return correct runner config object', () => {
5+
describe('createRunnerFunction', () => {
6+
it('should return correct runner function', () => {
77
expect(() =>
8-
runnerConfigSchema.parse(createRunnerConfig()),
8+
runnerFunctionSchema.parse(createRunnerFunction()),
99
).not.toThrowError();
1010
});
1111
});

0 commit comments

Comments
 (0)