Skip to content

Commit

Permalink
refactor: add various logs WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
vmasek committed Feb 4, 2025
1 parent 7ce5067 commit fb015f6
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 5 deletions.
3 changes: 3 additions & 0 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#! /usr/bin/env node
import { hideBin } from 'yargs/helpers';
import { ui } from '@code-pushup/utils';
import { cli } from './lib/cli.js';

ui().logger.log(`~~ cli: ${hideBin(process.argv)}`);

// bootstrap Yargs, parse arguments and execute command
await cli(hideBin(process.argv)).argv;
4 changes: 2 additions & 2 deletions packages/cli/src/lib/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ export const cli = (args: string[]) =>
],
[
'code-pushup collect --tsconfig=tsconfig.base.json',
'Run collect using custom tsconfig to parse code-pushup.config.ts file.',
'Run collect using a custom tsconfig to parse code-pushup.config.ts file.',
],
[
'code-pushup collect --onlyPlugins=coverage',
'Run collect with only coverage plugin, other plugins from config file will be skipped.',
],
[
'code-pushup collect --skipPlugins=coverage',
'Run collect skiping the coverage plugin, other plugins from config file will be included.',
'Run collect & skip running the coverage plugin, other plugins from config file will be executed.',
],
[
'code-pushup upload --persist.outputDir=dist --upload.apiKey=$CP_API_KEY',
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/lib/collect-and-persist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export async function collectAndPersistReports(

const report = await collect(options);
const sortedScoredReport = sortReport(scoreReport(report));

const persistResults = await persistReport(
report,
sortedScoredReport,
Expand Down
7 changes: 6 additions & 1 deletion packages/core/src/lib/implementation/collect.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createRequire } from 'node:module';
import type { CoreConfig, Report } from '@code-pushup/models';
import { calcDuration, getLatestCommit } from '@code-pushup/utils';
import { calcDuration, getLatestCommit, ui } from '@code-pushup/utils';
import type { GlobalOptions } from '../types.js';
import { executePlugins } from './execute-plugin.js';

Expand All @@ -12,7 +12,12 @@ export type CollectOptions = Pick<CoreConfig, 'plugins' | 'categories'> &
* @param options
*/
export async function collect(options: CollectOptions): Promise<Report> {
ui().logger.log('Running collect');
const { plugins, categories } = options;
console.log(
'~~ plugin runners',
plugins.map(({ runner }) => runner),
);
const date = new Date().toISOString();
const start = performance.now();
const commit = await getLatestCommit();
Expand Down
12 changes: 10 additions & 2 deletions packages/core/src/lib/implementation/runner.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import { gray } from 'ansis';

Check failure on line 1 in packages/core/src/lib/implementation/runner.ts

View workflow job for this annotation

GitHub Actions / Code PushUp

<✓> ESLint | Disallow unused variables

'gray' is defined but never used.
import path from 'node:path';
import type {
OnProgress,
RunnerConfig,
RunnerFunction,
} from '@code-pushup/models';
import { calcDuration, executeProcess, readJsonFile } from '@code-pushup/utils';
import {
calcDuration,
executeProcess,
readJsonFile,
removeDirectoryIfExists,
ui,

Check failure on line 13 in packages/core/src/lib/implementation/runner.ts

View workflow job for this annotation

GitHub Actions / Code PushUp

<✓> ESLint | Disallow unused variables

'ui' is defined but never used.
} from '@code-pushup/utils';

export type RunnerResult = {
date: string;
Expand All @@ -26,7 +33,8 @@ export async function executeRunnerConfig(
});

// read process output from file system and parse it
const outputs = await readJsonFile(path.join(process.cwd(), outputFile));
const outputs = await readJsonFile(outputFile);
await removeDirectoryIfExists(path.dirname(outputFile));

// transform unknownAuditOutputs to auditOutputs
const audits = outputTransform ? await outputTransform(outputs) : outputs;
Expand Down
5 changes: 5 additions & 0 deletions packages/plugin-eslint/src/bin.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import process from 'node:process';

Check failure on line 1 in packages/plugin-eslint/src/bin.ts

View workflow job for this annotation

GitHub Actions / Code PushUp

<✓> Code coverage | Branch coverage

1st branch is not taken in any test case.
import { Parser } from 'yargs/helpers';
import { ui } from '@code-pushup/utils';
import { executeRunner } from './lib/runner/index.js';

const { runnerConfigPath, runnerOutputPath } = Parser(process.argv);

Check warning on line 6 in packages/plugin-eslint/src/bin.ts

View workflow job for this annotation

GitHub Actions / Code PushUp

<✓> JSDoc coverage | Variables coverage

Missing variables documentation for { runnerConfigPath, runnerOutputPath }

ui().logger.log(
`~~ Runner files: ${JSON.stringify({ runnerConfigPath, runnerOutputPath }, null, 2)}`,
);

await executeRunner({ runnerConfigPath, runnerOutputPath });

Check warning on line 12 in packages/plugin-eslint/src/bin.ts

View workflow job for this annotation

GitHub Actions / Code PushUp

<✓> Code coverage | Line coverage

Lines 1-12 are not covered in any test case.
3 changes: 3 additions & 0 deletions packages/plugin-eslint/src/lib/runner/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
filePathToCliArg,
objectToCliArgs,
readJsonFile,
ui,
} from '@code-pushup/utils';
import type { ESLintPluginRunnerConfig, ESLintTarget } from '../config.js';
import { lint } from './lint.js';
Expand All @@ -25,6 +26,8 @@ export async function executeRunner({
const { slugs, targets } =
await readJsonFile<ESLintPluginRunnerConfig>(runnerConfigPath);

ui().logger.log(`ESLint plugin executing ${targets.length} lint targets`);

const linterOutputs = await targets.reduce(
async (acc, target) => [...(await acc), await lint(target)],
Promise.resolve<LinterOutput[]>([]),
Expand Down
7 changes: 7 additions & 0 deletions packages/utils/src/lib/execute-process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
spawn,
} from 'node:child_process';
import type { Readable, Writable } from 'node:stream';
import { ui } from './logging.js';
import { calcDuration } from './reports/utils.js';

/**
Expand Down Expand Up @@ -148,6 +149,10 @@ export function executeProcess(cfg: ProcessConfig): Promise<ProcessResult> {
const date = new Date().toISOString();
const start = performance.now();

ui().logger.log(
`Executing command:\n${[command, ...(args || [])].join(' ')}`,

Check failure on line 153 in packages/utils/src/lib/execute-process.ts

View workflow job for this annotation

GitHub Actions / Code PushUp

<✓> Code coverage | Branch coverage

1st branch is not taken in any test case.
);

return new Promise((resolve, reject) => {
// shell:true tells Windows to use shell command for spawning a child process
const spawnedProcess = spawn(command, args ?? [], {
Expand Down Expand Up @@ -179,6 +184,8 @@ export function executeProcess(cfg: ProcessConfig): Promise<ProcessResult> {
const timings = { date, duration: calcDuration(start) };
if (code === 0 || ignoreExitCode) {
onComplete?.();
// todo: maybe if I update spinner instead of logging it will appear with a command it was supposed to be grouped with
ui().logger.log(`Command execution took ${calcDuration(start)}ms`);
resolve({ code, stdout, stderr, ...timings });
} else {
const errorMsg = new ProcessError({ code, stdout, stderr, ...timings });
Expand Down

0 comments on commit fb015f6

Please sign in to comment.