Skip to content

feat: add artifaction options to plugin-eslint #1036

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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: 1 addition & 0 deletions packages/models/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,4 @@ export {
artifactGenerationCommandSchema,
pluginArtifactOptionsSchema,
} from './lib/configuration.js';
export type { PluginArtifactOptions } from './lib/configuration.js';
2 changes: 2 additions & 0 deletions packages/plugin-eslint/src/lib/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { z } from 'zod';
import { pluginArtifactOptionsSchema } from '@code-pushup/models';
import { toArray } from '@code-pushup/utils';

const patternsSchema = z.union([z.string(), z.array(z.string()).min(1)], {
Expand Down Expand Up @@ -64,5 +65,6 @@ export type CustomGroup = z.infer<typeof customGroupSchema>;

export const eslintPluginOptionsSchema = z.object({
groups: z.array(customGroupSchema).optional(),
artifacts: pluginArtifactOptionsSchema.optional(),
});
export type ESLintPluginOptions = z.infer<typeof eslintPluginOptionsSchema>;
26 changes: 26 additions & 0 deletions packages/plugin-eslint/src/lib/eslint-plugin.int.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,30 @@ describe('eslintPlugin', () => {
eslintPlugin({ eslintrc: '.eslintrc.yml', patterns: '**/*.js' }),
).rejects.toThrow(/Failed to load url .*\.eslintrc.yml/);
});

it('should initialize with artifact options', async () => {
cwdSpy.mockReturnValue(path.join(fixturesDir, 'todos-app'));
const plugin = await eslintPlugin(
{
eslintrc: 'eslint.config.js',
patterns: ['src/**/*.js'],
},
{
artifacts: {
artifactsPaths: './artifacts/eslint-output.json',
generateArtifactsCommand: 'echo "Generating artifacts"',
},
},
);

expect(typeof plugin.runner).toBe('object');
const runnerConfig = plugin.runner as {
command: string;
args?: string[];
outputFile: string;
};
expect(runnerConfig.command).toBe('node');
expect(runnerConfig.args).toContain('echo "Generating artifacts"');
expect(runnerConfig.outputFile).toBe('./artifacts/eslint-output.json');
});
});
13 changes: 10 additions & 3 deletions packages/plugin-eslint/src/lib/eslint-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,14 @@ export async function eslintPlugin(
schemaType: 'ESLint plugin config',
});

const customGroups = options
const parsedOptions = options
? parseSchema(eslintPluginOptionsSchema, options, {
schemaType: 'ESLint plugin options',
}).groups
})
: undefined;

const customGroups = parsedOptions?.groups;

const { audits, groups } = await listAuditsAndGroups(targets, customGroups);

const runnerScriptPath = path.join(
Expand All @@ -71,6 +73,11 @@ export async function eslintPlugin(
audits,
groups,

runner: await createRunnerConfig(runnerScriptPath, audits, targets),
runner: await createRunnerConfig(
runnerScriptPath,
audits,
targets,
parsedOptions?.artifacts,
),
};
}
57 changes: 48 additions & 9 deletions packages/plugin-eslint/src/lib/runner/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import type {
Audit,
AuditOutput,
PluginArtifactOptions,
RunnerConfig,
RunnerFilesPaths,
} from '@code-pushup/models';
import { pluginArtifactOptionsSchema } from '@code-pushup/models';

Check warning on line 10 in packages/plugin-eslint/src/lib/runner/index.ts

View workflow job for this annotation

GitHub Actions / Code PushUp

<✓> ESLint | Disallow duplicate module imports

'@code-pushup/models' import is duplicated.
import {
asyncSequential,
createRunnerFiles,
Expand Down Expand Up @@ -51,23 +53,60 @@
scriptPath: string,
audits: Audit[],
targets: ESLintTarget[],
artifactOptions?: PluginArtifactOptions,
): Promise<RunnerConfig> {
const parsedOptions = artifactOptions
? pluginArtifactOptionsSchema.parse(artifactOptions)
: undefined;

const config: ESLintPluginRunnerConfig = {
targets,
slugs: audits.map(audit => audit.slug),
slugs: audits.map(a => a.slug),
};
const { runnerConfigPath, runnerOutputPath } = await createRunnerFiles(
'eslint',
JSON.stringify(config),
);

const { runnerConfigPath, runnerOutputPath } = parsedOptions
? await createCustomRunnerPaths(parsedOptions, config)
: await createRunnerFiles('eslint', JSON.stringify(config));

const args = [
filePathToCliArg(scriptPath),
...objectToCliArgs({ runnerConfigPath, runnerOutputPath }),
...resolveCommandArgs(parsedOptions?.generateArtifactsCommand),
];

return {
command: 'node',
args: [
filePathToCliArg(scriptPath),
...objectToCliArgs({ runnerConfigPath, runnerOutputPath }),
],
args,
configFile: runnerConfigPath,
outputFile: runnerOutputPath,
};
}

async function createCustomRunnerPaths(
options: PluginArtifactOptions,
config: ESLintPluginRunnerConfig,
): Promise<RunnerFilesPaths> {
const artifactPaths = Array.isArray(options.artifactsPaths)
? options.artifactsPaths
: [options.artifactsPaths];

const runnerOutputPath = artifactPaths[0] ?? '';
const runnerConfigPath = path.join(
path.dirname(runnerOutputPath),
'plugin-config.json',
);

await ensureDirectoryExists(path.dirname(runnerConfigPath));
await writeFile(runnerConfigPath, JSON.stringify(config));

return { runnerConfigPath, runnerOutputPath };
}

function resolveCommandArgs(
command?: string | { command: string; args?: string[] },
): string[] {
if (!command) return [];

Check warning on line 108 in packages/plugin-eslint/src/lib/runner/index.ts

View workflow job for this annotation

GitHub Actions / Code PushUp

<✓> ESLint | Enforce consistent brace style for all control statements

Expected { after 'if' condition.
return typeof command === 'string'
? [command]
: [command.command, ...(command.args ?? [])];
}
Loading