Skip to content

Commit 84caa0d

Browse files
committed
refactor(utils,core): move timings out of executeProcess function
1 parent 52dc118 commit 84caa0d

File tree

4 files changed

+35
-56
lines changed

4 files changed

+35
-56
lines changed

packages/core/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export {
99
} from './lib/compare.js';
1010
export {
1111
getRunnerOutputsPath,
12-
type ValidatedRunnerResult,
12+
type RunnerResult,
1313
} from './lib/implementation/runner.js';
1414

1515
export {
@@ -23,7 +23,6 @@ export {
2323
executePlugin,
2424
executePlugins,
2525
} from './lib/implementation/execute-plugin.js';
26-
export { AuditOutputsMissingAuditError } from './lib/implementation/runner.js';
2726
export {
2827
PersistDirError,
2928
PersistError,
@@ -34,6 +33,7 @@ export {
3433
ConfigPathError,
3534
readRcByPath,
3635
} from './lib/implementation/read-rc-file.js';
36+
export { AuditOutputsMissingAuditError } from './lib/implementation/runner.js';
3737
export { mergeDiffs } from './lib/merge-diffs.js';
3838
export type { GlobalOptions } from './lib/types.js';
3939
export { upload, type UploadOptions } from './lib/upload.js';

packages/core/src/lib/implementation/runner.ts

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,16 @@ import { normalizeAuditOutputs } from '../normalize.js';
2525
export type RunnerResult = {
2626
date: string;
2727
duration: number;
28-
audits: unknown;
29-
};
30-
31-
export type ValidatedRunnerResult = Omit<RunnerResult, 'audits'> & {
3228
audits: AuditOutputs;
3329
};
3430

3531
export async function executeRunnerConfig(
3632
config: RunnerConfig,
3733
args: RunnerArgs,
38-
): Promise<RunnerResult> {
34+
): Promise<unknown> {
3935
const { outputFile, outputTransform } = config;
4036

41-
const { duration, date } = await executeProcess({
37+
await executeProcess({
4238
command: config.command,
4339
args: config.args,
4440
observer: {
@@ -60,30 +56,16 @@ export async function executeRunnerConfig(
6056
// transform unknownAuditOutputs to auditOutputs
6157
const audits = outputTransform ? await outputTransform(outputs) : outputs;
6258

63-
// create runner result
64-
return {
65-
duration,
66-
date,
67-
audits,
68-
};
59+
return audits;
6960
}
7061

7162
export async function executeRunnerFunction(
7263
runner: RunnerFunction,
7364
args: RunnerArgs,
74-
): Promise<RunnerResult> {
75-
const date = new Date().toISOString();
76-
const start = performance.now();
77-
65+
): Promise<unknown> {
7866
// execute plugin runner
7967
const audits = await runner(args);
80-
81-
// create runner result
82-
return {
83-
date,
84-
duration: calcDuration(start),
85-
audits,
86-
};
68+
return audits;
8769
}
8870

8971
/**
@@ -102,13 +84,18 @@ export class AuditOutputsMissingAuditError extends Error {
10284
export async function executePluginRunner(
10385
pluginConfig: Pick<PluginConfig, 'audits' | 'runner'>,
10486
args: RunnerArgs,
105-
): Promise<Omit<RunnerResult, 'audits'> & { audits: AuditOutputs }> {
87+
): Promise<RunnerResult> {
10688
const { audits: pluginConfigAudits, runner } = pluginConfig;
107-
const runnerResult: RunnerResult =
89+
90+
const date = new Date().toISOString();
91+
const start = performance.now();
92+
93+
const unvalidatedAuditOutputs =
10894
typeof runner === 'object'
10995
? await executeRunnerConfig(runner, args)
11096
: await executeRunnerFunction(runner, args);
111-
const { audits: unvalidatedAuditOutputs, ...executionMeta } = runnerResult;
97+
98+
const duration = calcDuration(start);
11299

113100
const result = auditOutputsSchema.safeParse(unvalidatedAuditOutputs);
114101
if (!result.success) {
@@ -118,7 +105,8 @@ export async function executePluginRunner(
118105
auditOutputsCorrelateWithPluginOutput(auditOutputs, pluginConfigAudits);
119106

120107
return {
121-
...executionMeta,
108+
date,
109+
duration,
122110
audits: await normalizeAuditOutputs(auditOutputs),
123111
};
124112
}
@@ -150,7 +138,7 @@ export function getRunnerOutputsPath(pluginSlug: string, outputDir: string) {
150138
export async function writeRunnerResults(
151139
pluginSlug: string,
152140
outputDir: string,
153-
runnerResult: ValidatedRunnerResult,
141+
runnerResult: RunnerResult,
154142
): Promise<void> {
155143
const cacheFilePath = getRunnerOutputsPath(pluginSlug, outputDir);
156144
await ensureDirectoryExists(path.dirname(cacheFilePath));
@@ -160,7 +148,7 @@ export async function writeRunnerResults(
160148
export async function readRunnerResults(
161149
pluginSlug: string,
162150
outputDir: string,
163-
): Promise<ValidatedRunnerResult | null> {
151+
): Promise<RunnerResult | null> {
164152
const auditOutputsPath = getRunnerOutputsPath(pluginSlug, outputDir);
165153
if (await fileExists(auditOutputsPath)) {
166154
const cachedResult = await readJsonFile<AuditOutputs>(auditOutputsPath);

packages/core/src/lib/implementation/runner.unit.test.ts

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import {
99
auditOutputsSchema,
1010
} from '@code-pushup/models';
1111
import {
12-
ISO_STRING_REGEXP,
1312
MEMFS_VOLUME,
1413
MINIMAL_PLUGIN_CONFIG_MOCK,
1514
MINIMAL_RUNNER_CONFIG_MOCK,
@@ -18,7 +17,6 @@ import {
1817
} from '@code-pushup/test-utils';
1918
import * as utils from '@code-pushup/utils';
2019
import {
21-
type RunnerResult,
2220
executePluginRunner,
2321
executeRunnerConfig,
2422
executeRunnerFunction,
@@ -52,17 +50,18 @@ describe('executeRunnerConfig', () => {
5250
});
5351

5452
it('should execute valid runner config', async () => {
55-
const runnerResult = await executeRunnerConfig(MINIMAL_RUNNER_CONFIG_MOCK, {
56-
persist: DEFAULT_PERSIST_CONFIG,
57-
});
53+
const auditOutputs = (await executeRunnerConfig(
54+
MINIMAL_RUNNER_CONFIG_MOCK,
55+
{
56+
persist: DEFAULT_PERSIST_CONFIG,
57+
},
58+
)) as AuditOutputs;
5859

5960
// data sanity
60-
expect((runnerResult.audits as AuditOutputs)[0]?.slug).toBe('node-version');
61-
expect(runnerResult.date).toMatch(ISO_STRING_REGEXP);
62-
expect(runnerResult.duration).toBeGreaterThanOrEqual(0);
61+
expect(auditOutputs[0]?.slug).toBe('node-version');
6362

6463
// schema validation
65-
expect(() => auditOutputsSchema.parse(runnerResult.audits)).not.toThrow();
64+
expect(() => auditOutputsSchema.parse(auditOutputs)).not.toThrow();
6665

6766
// executed process configuration
6867
expect(utils.executeProcess).toHaveBeenCalledWith<[utils.ProcessConfig]>({
@@ -82,7 +81,7 @@ describe('executeRunnerConfig', () => {
8281
});
8382

8483
it('should use outputTransform when provided', async () => {
85-
const runnerResult = await executeRunnerConfig(
84+
const auditOutputs = (await executeRunnerConfig(
8685
{
8786
command: 'node',
8887
args: ['-v'],
@@ -98,8 +97,7 @@ describe('executeRunnerConfig', () => {
9897
]),
9998
},
10099
{ persist: DEFAULT_PERSIST_CONFIG },
101-
);
102-
const auditOutputs = runnerResult.audits as AuditOutputs;
100+
)) as AuditOutputs;
103101

104102
expect(auditOutputs[0]?.slug).toBe('node-version');
105103
expect(auditOutputs[0]?.displayValue).toBe('16.0.0');
@@ -123,11 +121,10 @@ describe('executeRunnerConfig', () => {
123121

124122
describe('executeRunnerFunction', () => {
125123
it('should execute a valid runner function', async () => {
126-
const runnerResult: RunnerResult = await executeRunnerFunction(
124+
const auditOutputs = (await executeRunnerFunction(
127125
MINIMAL_RUNNER_FUNCTION_MOCK,
128126
{ persist: DEFAULT_PERSIST_CONFIG },
129-
);
130-
const auditOutputs = runnerResult.audits as AuditOutputs;
127+
)) as AuditOutputs;
131128

132129
expect(auditOutputs[0]?.slug).toBe('node-version');
133130
expect(auditOutputs[0]?.details?.issues).toEqual([

packages/utils/src/lib/execute-process.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,19 @@ import type { Readable, Writable } from 'node:stream';
99
import { isVerbose } from './env.js';
1010
import { formatCommandLog } from './format-command-log.js';
1111
import { ui } from './logging.js';
12-
import { calcDuration } from './reports/utils.js';
1312

1413
/**
1514
* Represents the process result.
1615
* @category Types
1716
* @public
17+
* @property {number | null} code - The exit code of the process.
1818
* @property {string} stdout - The stdout of the process.
1919
* @property {string} stderr - The stderr of the process.
20-
* @property {number | null} code - The exit code of the process.
2120
*/
2221
export type ProcessResult = {
22+
code: number | null;
2323
stdout: string;
2424
stderr: string;
25-
code: number | null;
26-
date: string;
27-
duration: number;
2825
};
2926

3027
/**
@@ -148,8 +145,6 @@ export type ProcessObserver = {
148145
export function executeProcess(cfg: ProcessConfig): Promise<ProcessResult> {
149146
const { command, args, observer, ignoreExitCode = false, ...options } = cfg;
150147
const { onStdout, onStderr, onError, onComplete } = observer ?? {};
151-
const date = new Date().toISOString();
152-
const start = performance.now();
153148

154149
if (isVerbose()) {
155150
ui().logger.log(
@@ -185,12 +180,11 @@ export function executeProcess(cfg: ProcessConfig): Promise<ProcessResult> {
185180
});
186181

187182
spawnedProcess.on('close', code => {
188-
const timings = { date, duration: calcDuration(start) };
189183
if (code === 0 || ignoreExitCode) {
190184
onComplete?.();
191-
resolve({ code, stdout, stderr, ...timings });
185+
resolve({ code, stdout, stderr });
192186
} else {
193-
const errorMsg = new ProcessError({ code, stdout, stderr, ...timings });
187+
const errorMsg = new ProcessError({ code, stdout, stderr });
194188
onError?.(errorMsg);
195189
reject(errorMsg);
196190
}

0 commit comments

Comments
 (0)