From 5d9024479987292de4d5765272833fb3162efb0d Mon Sep 17 00:00:00 2001 From: Michael Hladky Date: Tue, 9 Dec 2025 16:37:01 +0100 Subject: [PATCH 1/3] refactor: wip --- packages/plugin-knip/src/lib/knip.plugin.ts | 49 ++++++++++--------- packages/plugin-knip/src/lib/runner/index.ts | 43 +++++++++++++++- .../src/lib/runner/index.unit.test.ts | 6 +-- 3 files changed, 71 insertions(+), 27 deletions(-) diff --git a/packages/plugin-knip/src/lib/knip.plugin.ts b/packages/plugin-knip/src/lib/knip.plugin.ts index d8edf10..0ec890c 100644 --- a/packages/plugin-knip/src/lib/knip.plugin.ts +++ b/packages/plugin-knip/src/lib/knip.plugin.ts @@ -1,29 +1,32 @@ 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 type {PluginConfig} from '@code-pushup/models'; +import {KNIP_AUDITS, KNIP_GROUPS, KNIP_PLUGIN_SLUG} from './constants.js'; +import { + RunnerOptions, + createRunnerFunction, +} from './runner/index.js'; export type PluginOptions = RunnerOptions; export function knipPlugin(options: PluginOptions = {}): PluginConfig { - const { - outputFile = path.join( - '.code-pushup', - KNIP_PLUGIN_SLUG, - `knip-report-${Date.now()}.json`, - ), - ...runnerOptions - } = options; - return { - slug: KNIP_PLUGIN_SLUG, - title: 'Knip', - icon: 'folder-javascript', - description: 'A plugin to track dependencies and duplicates', - runner: createRunnerConfig({ - ...runnerOptions, - outputFile, - }), - audits: KNIP_AUDITS, - groups: KNIP_GROUPS, - }; + const { + outputFile = path.join( + '.code-pushup', + KNIP_PLUGIN_SLUG, + `knip-report-${Date.now()}.json`, + ), + ...runnerOptions + } = options; + return { + slug: KNIP_PLUGIN_SLUG, + title: 'Knip', + icon: 'folder-javascript', + description: 'A plugin to track dependencies and duplicates', + runner: createRunnerFunction({ + ...runnerOptions, + outputFile, + }), + audits: KNIP_AUDITS, + groups: KNIP_GROUPS, + }; } diff --git a/packages/plugin-knip/src/lib/runner/index.ts b/packages/plugin-knip/src/lib/runner/index.ts index 490d2bd..2c31457 100644 --- a/packages/plugin-knip/src/lib/runner/index.ts +++ b/packages/plugin-knip/src/lib/runner/index.ts @@ -1,5 +1,10 @@ import path from 'node:path'; -import type { RunnerConfig } from '@code-pushup/models'; +import type { + AuditOutputs, + RunnerConfig, + RunnerFunction, +} from '@code-pushup/models'; +import { executeProcess, readJsonFile } from '@code-pushup/utils'; import { KNIP_PLUGIN_SLUG, KNIP_REPORT_NAME, @@ -75,3 +80,39 @@ export function createRunnerConfig(options: RunnerOptions = {}): RunnerConfig { outputFile, }; } + +export function createRunnerFunction( + options: RunnerOptions = {}, +): RunnerFunction { + const { + outputFile = path.join(KNIP_PLUGIN_SLUG, KNIP_REPORT_NAME), + rawOutputFile, + } = options; + + // Resolve the reporter path from the installed package + const reporterPath = '@code-pushup/knip-plugin/src/lib/reporter.js'; + + 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..b612364 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 {createRunnerConfig, createRunnerFunction} from './index.js'; -describe('runnerConfig', () => { +describe('createRunnerFunction', () => { it('should return correct runner config object', () => { expect(() => - runnerConfigSchema.parse(createRunnerConfig()), + runnerConfigSchema.parse(createRunnerFunction()), ).not.toThrowError(); }); }); From ee4b7bf152261327caef9d1758ed0c670a526de5 Mon Sep 17 00:00:00 2001 From: Michael Hladky Date: Tue, 9 Dec 2025 16:43:10 +0100 Subject: [PATCH 2/3] refactor: wip --- packages/plugin-knip/src/lib/knip.plugin.ts | 49 +++++++++---------- packages/plugin-knip/src/lib/runner/index.ts | 31 ------------ .../src/lib/runner/index.unit.test.ts | 8 +-- 3 files changed, 27 insertions(+), 61 deletions(-) diff --git a/packages/plugin-knip/src/lib/knip.plugin.ts b/packages/plugin-knip/src/lib/knip.plugin.ts index 0ec890c..c60e4fd 100644 --- a/packages/plugin-knip/src/lib/knip.plugin.ts +++ b/packages/plugin-knip/src/lib/knip.plugin.ts @@ -1,32 +1,29 @@ 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, - createRunnerFunction, -} from './runner/index.js'; +import type { PluginConfig } from '@code-pushup/models'; +import { KNIP_AUDITS, KNIP_GROUPS, KNIP_PLUGIN_SLUG } from './constants.js'; +import { RunnerOptions, createRunnerFunction } from './runner/index.js'; export type PluginOptions = RunnerOptions; export function knipPlugin(options: PluginOptions = {}): PluginConfig { - const { - outputFile = path.join( - '.code-pushup', - KNIP_PLUGIN_SLUG, - `knip-report-${Date.now()}.json`, - ), - ...runnerOptions - } = options; - return { - slug: KNIP_PLUGIN_SLUG, - title: 'Knip', - icon: 'folder-javascript', - description: 'A plugin to track dependencies and duplicates', - runner: createRunnerFunction({ - ...runnerOptions, - outputFile, - }), - audits: KNIP_AUDITS, - groups: KNIP_GROUPS, - }; + const { + outputFile = path.join( + '.code-pushup', + KNIP_PLUGIN_SLUG, + `knip-report-${Date.now()}.json`, + ), + ...runnerOptions + } = options; + return { + slug: KNIP_PLUGIN_SLUG, + title: 'Knip', + icon: 'folder-javascript', + description: 'A plugin to track dependencies and duplicates', + runner: createRunnerFunction({ + ...runnerOptions, + outputFile, + }), + audits: KNIP_AUDITS, + groups: KNIP_GROUPS, + }; } diff --git a/packages/plugin-knip/src/lib/runner/index.ts b/packages/plugin-knip/src/lib/runner/index.ts index 2c31457..10cd1c1 100644 --- a/packages/plugin-knip/src/lib/runner/index.ts +++ b/packages/plugin-knip/src/lib/runner/index.ts @@ -50,37 +50,6 @@ export type KnipCliOptions = Partial<{ }>; export type RunnerOptions = KnipCliOptions & CustomReporterOptions; -export function createRunnerConfig(options: RunnerOptions = {}): RunnerConfig { - const { - outputFile = path.join(KNIP_PLUGIN_SLUG, KNIP_REPORT_NAME), - rawOutputFile, - } = options; - - // 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, - }; -} - export function createRunnerFunction( options: RunnerOptions = {}, ): RunnerFunction { 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 b612364..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, createRunnerFunction} from './index.js'; +import { runnerFunctionSchema } from '@code-pushup/models'; +import { createRunnerFunction } from './index.js'; describe('createRunnerFunction', () => { - it('should return correct runner config object', () => { + it('should return correct runner function', () => { expect(() => - runnerConfigSchema.parse(createRunnerFunction()), + runnerFunctionSchema.parse(createRunnerFunction()), ).not.toThrowError(); }); }); From cd17a092267014be150d1659d492c23ae7a92d47 Mon Sep 17 00:00:00 2001 From: Michael Hladky Date: Tue, 9 Dec 2025 16:46:07 +0100 Subject: [PATCH 3/3] refactor: wip --- packages/plugin-knip/src/lib/runner/index.ts | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/packages/plugin-knip/src/lib/runner/index.ts b/packages/plugin-knip/src/lib/runner/index.ts index 10cd1c1..c0e9e79 100644 --- a/packages/plugin-knip/src/lib/runner/index.ts +++ b/packages/plugin-knip/src/lib/runner/index.ts @@ -1,9 +1,5 @@ import path from 'node:path'; -import type { - AuditOutputs, - RunnerConfig, - RunnerFunction, -} from '@code-pushup/models'; +import type { AuditOutputs, RunnerFunction } from '@code-pushup/models'; import { executeProcess, readJsonFile } from '@code-pushup/utils'; import { KNIP_PLUGIN_SLUG,