From 783038db0115bbc7c35d7ac2772eee72a9cd1e56 Mon Sep 17 00:00:00 2001 From: mvedernikov Date: Wed, 8 Nov 2023 16:59:53 +0700 Subject: [PATCH] support multiple output file formats --- src/Config/@enums/outputFormat.ts | 4 --- src/Config/@types/configArgument.ts | 2 +- src/Config/Config.ts | 7 ++-- src/Config/constants/defaults.ts | 2 +- src/app.ts | 53 ++++++++++++++++++----------- 5 files changed, 39 insertions(+), 29 deletions(-) diff --git a/src/Config/@enums/outputFormat.ts b/src/Config/@enums/outputFormat.ts index 144be9a..e69de29 100644 --- a/src/Config/@enums/outputFormat.ts +++ b/src/Config/@enums/outputFormat.ts @@ -1,4 +0,0 @@ -export enum OutputFormat { - default = 'default', - gitlab = 'gitlab', -} diff --git a/src/Config/@types/configArgument.ts b/src/Config/@types/configArgument.ts index 2b3e450..8585d7d 100644 --- a/src/Config/@types/configArgument.ts +++ b/src/Config/@types/configArgument.ts @@ -11,7 +11,7 @@ export type ConfigArgument = { gitlabToken: string; buildLogFile: BuildLogFile[]; output: string; // =logFilePath - outputFormat: 'default' | 'gitlab'; + outputFormat: string[]; removeOldComment: boolean; failOnWarnings: boolean; suppressRules: string[]; diff --git a/src/Config/Config.ts b/src/Config/Config.ts index 6fffec3..f33b0fe 100644 --- a/src/Config/Config.ts +++ b/src/Config/Config.ts @@ -88,14 +88,15 @@ and is build root directory (optional (Will use current context as cwd)). }) .option('output', { alias: 'o', - describe: 'Output parsed log file', + describe: 'Default output file name', type: 'string', default: DEFAULT_OUTPUT_FILE, }) .option('outputFormat', { - describe: 'Output format', + type: 'array', + describe: 'Output formats', choices: ['default', 'gitlab'], - default: 'default', + default: ['default'], }) .option('removeOldComment', { alias: 'r', diff --git a/src/Config/constants/defaults.ts b/src/Config/constants/defaults.ts index 2198be8..f1ab3c0 100644 --- a/src/Config/constants/defaults.ts +++ b/src/Config/constants/defaults.ts @@ -1,4 +1,4 @@ -export const DEFAULT_OUTPUT_FILE = 'build.json'; +export const DEFAULT_OUTPUT_FILE = 'output.json'; export const USER_AGENT = 'CodeCoach'; export const TIME_ZONE = 'Asia/Bangkok'; diff --git a/src/app.ts b/src/app.ts index 9132fc2..25ea333 100644 --- a/src/app.ts +++ b/src/app.ts @@ -5,16 +5,16 @@ import { File } from './File'; import { Log } from './Logger'; import { AndroidLintStyleParser, + DartLintParser, DotnetBuildParser, ESLintParser, + JscpdParser, LintItem, MSBuildParser, Parser, ScalaStyleParser, - TSLintParser, - DartLintParser, SwiftLintParser, - JscpdParser, + TSLintParser, } from './Parser'; import { GitHubPRService, VCS } from './Provider'; import { GitLabMRService } from './Provider/GitLab/GitLabMRService'; @@ -23,15 +23,16 @@ import { VCSEngine } from './Provider/CommonVCS/VCSEngine'; import { GitLabAdapter } from './Provider/GitLab/GitLabAdapter'; import { VCSAdapter } from './Provider/@interfaces/VCSAdapter'; import { AnalyzerBot } from './AnalyzerBot/AnalyzerBot'; -import { - defaultFormatter, - gitLabFormatter, - OutputFormatter, -} from './OutputFormatter/OutputFormatter'; +import { defaultFormatter, gitLabFormatter } from './OutputFormatter/OutputFormatter'; + +type OutputFileMeta = { + formatter: (items: LintItem[]) => string; + filename: string; +}; class App { private vcs: VCS | null = null; - private outputFormatter: OutputFormatter; + private outputFileMeta: OutputFileMeta[]; async start(): Promise { if (!configs.dryRun) { @@ -44,14 +45,14 @@ class App { this.vcs = new VCSEngine(configs, analyzer, adapter); } - this.outputFormatter = App.getOutputFormatter(); + this.outputFileMeta = App.getOutputFileMetas(); const logs = await this.parseBuildData(configs.buildLogFile); Log.info('Build data parsing completed'); const reportToVcs = this.reportToVcs(logs); - const writeOutputFile = this.writeOutputFile(logs); - const [passed] = await Promise.all([reportToVcs, writeOutputFile]); + const writeOutputFiles = this.writeOutputFiles(logs); + const [passed] = await Promise.all([reportToVcs, writeOutputFiles]); if (!passed) { Log.error('There are some linting error and exit code reporting is enabled'); process.exit(1); @@ -94,13 +95,19 @@ class App { } } - private static getOutputFormatter(): OutputFormatter { - switch (configs.outputFormat) { - case 'default': - return defaultFormatter; - case 'gitlab': - return gitLabFormatter; + private static getOutputFileMetas(): OutputFileMeta[] { + const outputFileMetas: OutputFileMeta[] = []; + if (configs.outputFormat.includes('default')) { + outputFileMetas.push({ formatter: defaultFormatter, filename: configs.output }); + } + if (configs.outputFormat.includes('gitlab')) { + outputFileMetas.push({ + formatter: gitLabFormatter, + filename: 'gl-code-quality-report.json', + }); } + + return outputFileMetas; } private async parseBuildData(files: BuildLogFile[]): Promise { @@ -130,9 +137,15 @@ class App { } } - private async writeOutputFile(items: LintItem[]): Promise { + private async writeOutputFiles(items: LintItem[]): Promise { try { - await File.writeFileHelper(configs.output, this.outputFormatter(items)); + await Promise.all( + this.outputFileMeta.map((meta) => { + Log.info('Write output to ' + meta.filename, { meta }); + return File.writeFileHelper(meta.filename, meta.formatter(items)); + }), + ); + Log.info('Write output completed'); } catch (error) { Log.error('Write output failed', { error });