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
21 changes: 8 additions & 13 deletions packages/core/src/lib/implementation/collect.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import { createRequire } from 'node:module';
import {
type CacheConfigObject,
type CoreConfig,
DEFAULT_PERSIST_OUTPUT_DIR,
type PersistConfig,
type Report,
import type {
CacheConfigObject,
CoreConfig,
PersistConfig,
Report,
} from '@code-pushup/models';
import { calcDuration, getLatestCommit } from '@code-pushup/utils';
import type { GlobalOptions } from '../types.js';
import { executePlugins } from './execute-plugin.js';

export type CollectOptions = Pick<CoreConfig, 'plugins' | 'categories'> & {
persist?: Required<Pick<PersistConfig, 'outputDir'>>;
persist?: PersistConfig;
cache: CacheConfigObject;
} & Partial<GlobalOptions>;

Expand All @@ -20,16 +19,12 @@ export type CollectOptions = Pick<CoreConfig, 'plugins' | 'categories'> & {
* @param options
*/
export async function collect(options: CollectOptions): Promise<Report> {
const { plugins, categories, persist, cache, ...otherOptions } = options;
const { plugins, categories, persist = {}, cache, ...otherOptions } = options;
const date = new Date().toISOString();
const start = performance.now();
const commit = await getLatestCommit();
const pluginOutputs = await executePlugins(
{
plugins,
persist: { outputDir: DEFAULT_PERSIST_OUTPUT_DIR, ...persist },
cache,
},
{ plugins, persist, cache },
otherOptions,
);
const packageJson = createRequire(import.meta.url)(
Expand Down
41 changes: 22 additions & 19 deletions packages/core/src/lib/implementation/execute-plugin.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { bold } from 'ansis';
import type {
Audit,
AuditOutput,
AuditReport,
CacheConfigObject,
PersistConfig,
PluginConfig,
PluginReport,
import {
type AuditOutput,
type AuditReport,
type CacheConfigObject,
DEFAULT_PERSIST_CONFIG,
type PersistConfig,
type PluginConfig,
type PluginReport,
type RunnerArgs,
} from '@code-pushup/models';
import {
type ProgressBar,
Expand Down Expand Up @@ -48,10 +49,9 @@ export async function executePlugin(
pluginConfig: PluginConfig,
opt: {
cache: CacheConfigObject;
persist: Required<Pick<PersistConfig, 'outputDir'>>;
persist: PersistConfig;
},
): Promise<PluginReport> {
const { cache, persist } = opt;
const {
runner,
audits: pluginConfigAudits,
Expand All @@ -61,15 +61,19 @@ export async function executePlugin(
scoreTargets,
...pluginMeta
} = pluginConfig;
const { write: cacheWrite = false, read: cacheRead = false } = cache;
const { outputDir } = persist;
const { write: cacheWrite = false, read: cacheRead = false } = opt.cache;

const args: RunnerArgs = {
persist: { ...DEFAULT_PERSIST_CONFIG, ...opt.persist },
};
const { outputDir } = args.persist;

const { audits, ...executionMeta } = cacheRead
? // IF not null, take the result from cache
((await readRunnerResults(pluginMeta.slug, outputDir)) ??
// ELSE execute the plugin runner
(await executePluginRunner(pluginConfig, persist)))
: await executePluginRunner(pluginConfig, persist);
(await executePluginRunner(pluginConfig, args)))
: await executePluginRunner(pluginConfig, args);

if (cacheWrite) {
await writeRunnerResults(pluginMeta.slug, outputDir, {
Expand All @@ -87,9 +91,8 @@ export async function executePlugin(
const auditReports: AuditReport[] = scoredAuditsWithTarget.map(
(auditOutput: AuditOutput) => ({
...auditOutput,
...(pluginConfigAudits.find(
audit => audit.slug === auditOutput.slug,
) as Audit),
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
...pluginConfigAudits.find(audit => audit.slug === auditOutput.slug)!,
}),
);

Expand All @@ -107,7 +110,7 @@ export async function executePlugin(
const wrapProgress = async (
cfg: {
plugin: PluginConfig;
persist: Required<Pick<PersistConfig, 'outputDir'>>;
persist: PersistConfig;
cache: CacheConfigObject;
},
steps: number,
Expand Down Expand Up @@ -155,7 +158,7 @@ const wrapProgress = async (
export async function executePlugins(
cfg: {
plugins: PluginConfig[];
persist: Required<Pick<PersistConfig, 'outputDir'>>;
persist: PersistConfig;
cache: CacheConfigObject;
},
options?: { progress?: boolean },
Expand Down
12 changes: 8 additions & 4 deletions packages/core/src/lib/implementation/execute-plugin.unit.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { bold } from 'ansis';
import { vol } from 'memfs';
import { describe, expect, it, vi } from 'vitest';
import type { AuditOutputs, PluginConfig } from '@code-pushup/models';
import {
type AuditOutputs,
DEFAULT_PERSIST_CONFIG,
type PluginConfig,
} from '@code-pushup/models';
import {
MEMFS_VOLUME,
MINIMAL_PLUGIN_CONFIG_MOCK,
Expand All @@ -26,7 +30,7 @@ describe('executePlugin', () => {

await expect(
executePlugin(MINIMAL_PLUGIN_CONFIG_MOCK, {
persist: { outputDir: '' },
persist: {},
cache: { read: false, write: false },
}),
).resolves.toStrictEqual({
Expand All @@ -47,7 +51,7 @@ describe('executePlugin', () => {

expect(executePluginRunnerSpy).toHaveBeenCalledWith(
MINIMAL_PLUGIN_CONFIG_MOCK,
{ outputDir: '' },
{ persist: DEFAULT_PERSIST_CONFIG },
);
});

Expand Down Expand Up @@ -132,7 +136,7 @@ describe('executePlugin', () => {

expect(executePluginRunnerSpy).toHaveBeenCalledWith(
MINIMAL_PLUGIN_CONFIG_MOCK,
{ outputDir: MEMFS_VOLUME },
{ persist: { ...DEFAULT_PERSIST_CONFIG, outputDir: MEMFS_VOLUME } },
);
});

Expand Down
25 changes: 13 additions & 12 deletions packages/core/src/lib/implementation/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { writeFile } from 'node:fs/promises';
import path from 'node:path';
import {
type AuditOutputs,
type PersistConfig,
type PluginConfig,
type RunnerArgs,
type RunnerConfig,
type RunnerFunction,
auditOutputsSchema,
Expand All @@ -15,9 +15,9 @@ import {
executeProcess,
fileExists,
isVerbose,
objectToCliArgs,
readJsonFile,
removeDirectoryIfExists,
runnerArgsToEnv,
ui,
} from '@code-pushup/utils';
import { normalizeAuditOutputs } from '../normalize.js';
Expand All @@ -33,14 +33,14 @@ export type ValidatedRunnerResult = Omit<RunnerResult, 'audits'> & {
};

export async function executeRunnerConfig(
cfg: RunnerConfig,
config: Required<Pick<PersistConfig, 'outputDir'>>,
config: RunnerConfig,
args: RunnerArgs,
): Promise<RunnerResult> {
const { args, command, outputFile, outputTransform } = cfg;
const { outputFile, outputTransform } = config;

const { duration, date } = await executeProcess({
command,
args: [...(args ?? []), ...objectToCliArgs(config)],
command: config.command,
args: config.args,
observer: {
onStdout: stdout => {
if (isVerbose()) {
Expand All @@ -49,6 +49,7 @@ export async function executeRunnerConfig(
},
onStderr: stderr => ui().logger.error(stderr),
},
env: { ...process.env, ...runnerArgsToEnv(args) },
});

// read process output from the file system and parse it
Expand All @@ -69,13 +70,13 @@ export async function executeRunnerConfig(

export async function executeRunnerFunction(
runner: RunnerFunction,
config: PersistConfig,
args: RunnerArgs,
): Promise<RunnerResult> {
const date = new Date().toISOString();
const start = performance.now();

// execute plugin runner
const audits = await runner(config);
const audits = await runner(args);

// create runner result
return {
Expand All @@ -100,13 +101,13 @@ export class AuditOutputsMissingAuditError extends Error {

export async function executePluginRunner(
pluginConfig: Pick<PluginConfig, 'audits' | 'runner'>,
persist: Required<Pick<PersistConfig, 'outputDir'>>,
args: RunnerArgs,
): Promise<Omit<RunnerResult, 'audits'> & { audits: AuditOutputs }> {
const { audits: pluginConfigAudits, runner } = pluginConfig;
const runnerResult: RunnerResult =
typeof runner === 'object'
? await executeRunnerConfig(runner, persist)
: await executeRunnerFunction(runner, persist);
? await executeRunnerConfig(runner, args)
: await executeRunnerFunction(runner, args);
const { audits: unvalidatedAuditOutputs, ...executionMeta } = runnerResult;

const result = auditOutputsSchema.safeParse(unvalidatedAuditOutputs);
Expand Down
Loading