Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions packages/plugin-knip/src/lib/knip.plugin.ts
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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,
}),
Expand Down
48 changes: 27 additions & 21 deletions packages/plugin-knip/src/lib/runner/index.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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<AuditOutputs>(outputFile);
};
}
10 changes: 5 additions & 5 deletions packages/plugin-knip/src/lib/runner/index.unit.test.ts
Original file line number Diff line number Diff line change
@@ -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();
});
});