diff --git a/packages/plugin-knip/src/lib/knip.plugin.ts b/packages/plugin-knip/src/lib/knip.plugin.ts index d8edf10..c60e4fd 100644 --- a/packages/plugin-knip/src/lib/knip.plugin.ts +++ b/packages/plugin-knip/src/lib/knip.plugin.ts @@ -1,7 +1,7 @@ import path from 'node:path'; import type { PluginConfig } from '@code-pushup/models'; import { KNIP_AUDITS, KNIP_GROUPS, KNIP_PLUGIN_SLUG } from './constants.js'; -import { RunnerOptions, createRunnerConfig } from './runner/index.js'; +import { RunnerOptions, createRunnerFunction } from './runner/index.js'; export type PluginOptions = RunnerOptions; @@ -19,7 +19,7 @@ export function knipPlugin(options: PluginOptions = {}): PluginConfig { title: 'Knip', icon: 'folder-javascript', description: 'A plugin to track dependencies and duplicates', - runner: createRunnerConfig({ + runner: createRunnerFunction({ ...runnerOptions, outputFile, }), diff --git a/packages/plugin-knip/src/lib/runner/index.ts b/packages/plugin-knip/src/lib/runner/index.ts index 490d2bd..c0e9e79 100644 --- a/packages/plugin-knip/src/lib/runner/index.ts +++ b/packages/plugin-knip/src/lib/runner/index.ts @@ -1,5 +1,6 @@ import path from 'node:path'; -import type { RunnerConfig } from '@code-pushup/models'; +import type { AuditOutputs, RunnerFunction } from '@code-pushup/models'; +import { executeProcess, readJsonFile } from '@code-pushup/utils'; import { KNIP_PLUGIN_SLUG, KNIP_REPORT_NAME, @@ -45,7 +46,9 @@ export type KnipCliOptions = Partial<{ }>; export type RunnerOptions = KnipCliOptions & CustomReporterOptions; -export function createRunnerConfig(options: RunnerOptions = {}): RunnerConfig { +export function createRunnerFunction( + options: RunnerOptions = {}, +): RunnerFunction { const { outputFile = path.join(KNIP_PLUGIN_SLUG, KNIP_REPORT_NAME), rawOutputFile, @@ -54,24 +57,27 @@ export function createRunnerConfig(options: RunnerOptions = {}): RunnerConfig { // Resolve the reporter path from the installed package const reporterPath = '@code-pushup/knip-plugin/src/lib/reporter.js'; - return { - command: 'npx', - args: [ - 'knip', - // off as we want to CI to pass - '--no-exit-code', - // off by default to guarantee execution without interference - '--no-progress', - // code-pushup reporter is used from the installed package - `--reporter=${reporterPath}`, - // code-pushup reporter options are passed as string. Double JSON.stringify ensures proper escaping on all platforms - `--reporter-options=${JSON.stringify( - JSON.stringify({ - outputFile, - rawOutputFile, - } satisfies CustomReporterOptions), - )}`, - ], - outputFile, + return async () => { + await executeProcess({ + command: 'npx', + args: [ + 'knip', + // off as we want to CI to pass + '--no-exit-code', + // off by default to guarantee execution without interference + '--no-progress', + // code-pushup reporter is used from the installed package + `--reporter=${reporterPath}`, + // code-pushup reporter options are passed as string. Double JSON.stringify ensures proper escaping on all platforms + `--reporter-options=${JSON.stringify( + JSON.stringify({ + outputFile, + rawOutputFile, + } satisfies CustomReporterOptions), + )}`, + ], + }); + + return readJsonFile(outputFile); }; } diff --git a/packages/plugin-knip/src/lib/runner/index.unit.test.ts b/packages/plugin-knip/src/lib/runner/index.unit.test.ts index bd643a2..f250fc3 100644 --- a/packages/plugin-knip/src/lib/runner/index.unit.test.ts +++ b/packages/plugin-knip/src/lib/runner/index.unit.test.ts @@ -1,11 +1,11 @@ import { describe, expect, it } from 'vitest'; -import { runnerConfigSchema } from '@code-pushup/models'; -import { createRunnerConfig } from './index.js'; +import { runnerFunctionSchema } from '@code-pushup/models'; +import { createRunnerFunction } from './index.js'; -describe('runnerConfig', () => { - it('should return correct runner config object', () => { +describe('createRunnerFunction', () => { + it('should return correct runner function', () => { expect(() => - runnerConfigSchema.parse(createRunnerConfig()), + runnerFunctionSchema.parse(createRunnerFunction()), ).not.toThrowError(); }); });