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
1 change: 0 additions & 1 deletion .github/actions/code-pushup/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,3 @@ runs:
env:
TSX_TSCONFIG_PATH: .github/actions/code-pushup/tsconfig.json
GH_TOKEN: ${{ inputs.token }}
CP_VERBOSE: true
10 changes: 9 additions & 1 deletion .github/actions/code-pushup/src/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ import {
type SourceFileIssue,
runInCI,
} from '@code-pushup/ci';
import { CODE_PUSHUP_UNICODE_LOGO, stringifyError } from '@code-pushup/utils';
import {
CODE_PUSHUP_UNICODE_LOGO,
logger,
stringifyError,
} from '@code-pushup/utils';

type GitHubRefs = {
head: GitBranch;
Expand Down Expand Up @@ -126,6 +130,10 @@ function createGitHubApiClient(): ProviderAPIClient {

async function run(): Promise<void> {
try {
if (core.isDebug()) {
logger.setVerbose(true);
}

const options: Options = {
bin: 'npx nx code-pushup --nx-bail --',
};
Expand Down
1 change: 1 addition & 0 deletions packages/ci/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ Optionally, you can override default options for further customization:
| `nxProjectsFilter` | `string \| string[]` | `'--with-target={task}'` | Arguments passed to [`nx show projects`](https://nx.dev/nx-api/nx/documents/show#projects), only relevant for Nx in [monorepo mode](#monorepo-mode) [^2] |
| `directory` | `string` | `process.cwd()` | Directory in which Code PushUp CLI should run |
| `config` | `string \| null` | `null` [^1] | Path to config file (`--config` option) |
| `silent` | `boolean` | `false` | Hides logs from CLI commands (errors will be printed) |
| `bin` | `string` | `'npx --no-install code-pushup'` | Command for executing Code PushUp CLI |
| `detectNewIssues` | `boolean` | `true` | Toggles if new issues should be detected and returned in `newIssues` property |
| `skipComment` | `boolean` | `false` | Toggles if comparison comment is posted to PR |
Expand Down
18 changes: 4 additions & 14 deletions packages/ci/src/lib/cli/commands/collect.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,9 @@
import { DEFAULT_PERSIST_FORMAT } from '@code-pushup/models';
import { executeProcess } from '@code-pushup/utils';
import type { CommandContext } from '../context.js';
import { executeCliCommand } from '../exec.js';

export async function runCollect(
{ bin, config, directory }: CommandContext,
{ hasFormats }: { hasFormats: boolean },
context: CommandContext,
options: { hasFormats: boolean },
): Promise<void> {
await executeProcess({
command: bin,
args: [
...(config ? [`--config=${config}`] : []),
...(hasFormats
? []
: DEFAULT_PERSIST_FORMAT.map(format => `--persist.format=${format}`)),
],
cwd: directory,
});
await executeCliCommand([], context, options);
}
19 changes: 4 additions & 15 deletions packages/ci/src/lib/cli/commands/compare.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,9 @@
import { DEFAULT_PERSIST_FORMAT } from '@code-pushup/models';
import { executeProcess } from '@code-pushup/utils';
import type { CommandContext } from '../context.js';
import { executeCliCommand } from '../exec.js';

export async function runCompare(
{ bin, config, directory }: CommandContext,
{ hasFormats }: { hasFormats: boolean },
context: CommandContext,
options: { hasFormats: boolean },
): Promise<void> {
await executeProcess({
command: bin,
args: [
'compare',
...(config ? [`--config=${config}`] : []),
...(hasFormats
? []
: DEFAULT_PERSIST_FORMAT.map(format => `--persist.format=${format}`)),
],
cwd: directory,
});
await executeCliCommand(['compare'], context, options);
}
16 changes: 7 additions & 9 deletions packages/ci/src/lib/cli/commands/merge-diffs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,25 @@ import {
DEFAULT_PERSIST_FILENAME,
DEFAULT_PERSIST_OUTPUT_DIR,
} from '@code-pushup/models';
import { executeProcess } from '@code-pushup/utils';
import type { CommandContext } from '../context.js';
import { executeCliCommand } from '../exec.js';

export async function runMergeDiffs(
files: string[],
{ bin, config, directory }: CommandContext,
context: CommandContext,
): Promise<string> {
const outputDir = path.join(directory, DEFAULT_PERSIST_OUTPUT_DIR);
const outputDir = path.join(context.directory, DEFAULT_PERSIST_OUTPUT_DIR);
const filename = `merged-${DEFAULT_PERSIST_FILENAME}`;

await executeProcess({
command: bin,
args: [
await executeCliCommand(
[
'merge-diffs',
...files.map(file => `--files=${file}`),
...(config ? [`--config=${config}`] : []),
`--persist.outputDir=${outputDir}`,
`--persist.filename=${filename}`,
],
cwd: directory,
});
context,
);

return path.join(outputDir, `${filename}-diff.md`);
}
34 changes: 10 additions & 24 deletions packages/ci/src/lib/cli/commands/print-config.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,24 @@
import { rm } from 'node:fs/promises';
import path from 'node:path';
import {
executeProcess,
readJsonFile,
stringifyError,
} from '@code-pushup/utils';
import { readJsonFile, stringifyError } from '@code-pushup/utils';
import type { CommandContext } from '../context.js';
import { executeCliCommand } from '../exec.js';

export async function runPrintConfig({
bin,
config,
directory,
project,
}: CommandContext): Promise<unknown> {
export async function runPrintConfig(
context: CommandContext,
): Promise<unknown> {
// unique file name per project so command can be run in parallel
const outputFile = ['code-pushup', 'config', project, 'json']
const outputFile = ['code-pushup', 'config', context.project, 'json']
.filter(Boolean)
.join('.');
const outputPath =
project && directory === process.cwd()
context.project && context.directory === process.cwd()
? // cache-friendly path for Nx projects (assuming {workspaceRoot}/.code-pushup/{projectName})
path.join(process.cwd(), '.code-pushup', project, outputFile)
path.join(process.cwd(), '.code-pushup', context.project, outputFile)
: // absolute path
path.resolve(directory, '.code-pushup', outputFile);
path.resolve(context.directory, '.code-pushup', outputFile);

await executeProcess({
command: bin,
args: [
...(config ? [`--config=${config}`] : []),
'print-config',
`--output=${outputPath}`,
],
cwd: directory,
});
await executeCliCommand(['print-config', `--output=${outputPath}`], context);

try {
const content = await readJsonFile(outputPath);
Expand Down
8 changes: 6 additions & 2 deletions packages/ci/src/lib/cli/context.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import type { Settings } from '../models.js';
import type { ProjectConfig } from '../monorepo/index.js';

export type CommandContext = Pick<Settings, 'bin' | 'config' | 'directory'> & {
export type CommandContext = Pick<
Settings,
'bin' | 'config' | 'directory' | 'silent'
> & {
project?: string;
};

export function createCommandContext(
{ config, bin, directory }: Settings,
{ config, bin, directory, silent }: Settings,
project: ProjectConfig | null | undefined,
): CommandContext {
return {
bin: project?.bin ?? bin,
directory: project?.directory ?? directory,
silent,
config,
...(project?.name && { project: project.name }),
};
Expand Down
4 changes: 4 additions & 0 deletions packages/ci/src/lib/cli/context.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ describe('createCommandContext', () => {
config: null,
detectNewIssues: true,
directory: '/test',
silent: false,
monorepo: false,
parallel: false,
nxProjectsFilter: '--with-target={task}',
Expand All @@ -24,6 +25,7 @@ describe('createCommandContext', () => {
).toStrictEqual<CommandContext>({
bin: 'npx --no-install code-pushup',
directory: '/test',
silent: false,
config: null,
});
});
Expand All @@ -36,6 +38,7 @@ describe('createCommandContext', () => {
config: null,
detectNewIssues: true,
directory: '/test',
silent: false,
monorepo: false,
parallel: false,
nxProjectsFilter: '--with-target={task}',
Expand All @@ -54,6 +57,7 @@ describe('createCommandContext', () => {
).toStrictEqual<CommandContext>({
bin: 'yarn code-pushup',
directory: '/test/ui',
silent: false,
config: null,
project: 'ui',
});
Expand Down
80 changes: 80 additions & 0 deletions packages/ci/src/lib/cli/exec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { DEFAULT_PERSIST_FORMAT } from '@code-pushup/models';
import {
type ProcessConfig,
type ProcessObserver,
executeProcess,
logger,
serializeCommandWithArgs,
} from '@code-pushup/utils';
import type { CommandContext } from './context.js';

export async function executeCliCommand(
args: string[],
context: CommandContext,
options?: { hasFormats: boolean },
): Promise<void> {
// eslint-disable-next-line functional/no-let
let output = '';

const logRaw = (message: string) => {
if (!context.silent) {
if (!output) {
logger.newline();
}
logger.info(message, { noIndent: true, noLineBreak: true });
}
output += message;
};

const logEnd = () => {
if (!context.silent && output) {
logger.newline();
}
};

const observer: ProcessObserver = {
onStdout: logRaw,
onStderr: logRaw,
onComplete: logEnd,
onError: logEnd,
};

const config: ProcessConfig = {
command: context.bin,
args: combineArgs(args, context, options),
cwd: context.directory,
observer,
silent: true,
};
const bin = serializeCommandWithArgs(config);

await logger.command(bin, async () => {
try {
await executeProcess(config);
} catch (error) {
// ensure output of failed process is always logged for debugging
if (context.silent) {
logger.newline();
logger.info(output, { noIndent: true });
if (!output.endsWith('\n')) {
logger.newline();
}
}
throw error;
}
});
}

function combineArgs(
args: string[],
context: CommandContext,
options: { hasFormats?: boolean } | undefined,
): string[] {
return [
...(context.config ? [`--config=${context.config}`] : []),
...args,
...(options?.hasFormats === false
? DEFAULT_PERSIST_FORMAT.map(format => `--persist.format=${format}`)
: []),
];
}
Loading
Loading