Skip to content

Commit 5d90244

Browse files
committed
refactor: wip
1 parent a38e64f commit 5d90244

3 files changed

Lines changed: 71 additions & 27 deletions

File tree

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

69
export type PluginOptions = RunnerOptions;
710

811
export function knipPlugin(options: PluginOptions = {}): PluginConfig {
9-
const {
10-
outputFile = path.join(
11-
'.code-pushup',
12-
KNIP_PLUGIN_SLUG,
13-
`knip-report-${Date.now()}.json`,
14-
),
15-
...runnerOptions
16-
} = options;
17-
return {
18-
slug: KNIP_PLUGIN_SLUG,
19-
title: 'Knip',
20-
icon: 'folder-javascript',
21-
description: 'A plugin to track dependencies and duplicates',
22-
runner: createRunnerConfig({
23-
...runnerOptions,
24-
outputFile,
25-
}),
26-
audits: KNIP_AUDITS,
27-
groups: KNIP_GROUPS,
28-
};
12+
const {
13+
outputFile = path.join(
14+
'.code-pushup',
15+
KNIP_PLUGIN_SLUG,
16+
`knip-report-${Date.now()}.json`,
17+
),
18+
...runnerOptions
19+
} = options;
20+
return {
21+
slug: KNIP_PLUGIN_SLUG,
22+
title: 'Knip',
23+
icon: 'folder-javascript',
24+
description: 'A plugin to track dependencies and duplicates',
25+
runner: createRunnerFunction({
26+
...runnerOptions,
27+
outputFile,
28+
}),
29+
audits: KNIP_AUDITS,
30+
groups: KNIP_GROUPS,
31+
};
2932
}

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

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import path from 'node:path';
2-
import type { RunnerConfig } from '@code-pushup/models';
2+
import type {
3+
AuditOutputs,
4+
RunnerConfig,
5+
RunnerFunction,
6+
} from '@code-pushup/models';
7+
import { executeProcess, readJsonFile } from '@code-pushup/utils';
38
import {
49
KNIP_PLUGIN_SLUG,
510
KNIP_REPORT_NAME,
@@ -75,3 +80,39 @@ export function createRunnerConfig(options: RunnerOptions = {}): RunnerConfig {
7580
outputFile,
7681
};
7782
}
83+
84+
export function createRunnerFunction(
85+
options: RunnerOptions = {},
86+
): RunnerFunction {
87+
const {
88+
outputFile = path.join(KNIP_PLUGIN_SLUG, KNIP_REPORT_NAME),
89+
rawOutputFile,
90+
} = options;
91+
92+
// Resolve the reporter path from the installed package
93+
const reporterPath = '@code-pushup/knip-plugin/src/lib/reporter.js';
94+
95+
return async () => {
96+
await executeProcess({
97+
command: 'npx',
98+
args: [
99+
'knip',
100+
// off as we want to CI to pass
101+
'--no-exit-code',
102+
// off by default to guarantee execution without interference
103+
'--no-progress',
104+
// code-pushup reporter is used from the installed package
105+
`--reporter=${reporterPath}`,
106+
// code-pushup reporter options are passed as string. Double JSON.stringify ensures proper escaping on all platforms
107+
`--reporter-options=${JSON.stringify(
108+
JSON.stringify({
109+
outputFile,
110+
rawOutputFile,
111+
} satisfies CustomReporterOptions),
112+
)}`,
113+
],
114+
});
115+
116+
return readJsonFile<AuditOutputs>(outputFile);
117+
};
118+
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { describe, expect, it } from 'vitest';
22
import { runnerConfigSchema } from '@code-pushup/models';
3-
import { createRunnerConfig } from './index.js';
3+
import {createRunnerConfig, createRunnerFunction} from './index.js';
44

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

0 commit comments

Comments
 (0)