-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): implement compare command
- Loading branch information
1 parent
10df94c
commit 314e7ba
Showing
6 changed files
with
116 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import chalk from 'chalk'; | ||
import { join } from 'node:path'; | ||
import { CommandModule } from 'yargs'; | ||
import { compareReportFiles } from '@code-pushup/core'; | ||
import { PersistConfig } from '@code-pushup/models'; | ||
import { CLI_NAME } from '../constants'; | ||
import type { CompareOptions } from '../implementation/compare.model'; | ||
import { yargsCompareOptionsDefinition } from '../implementation/compare.options'; | ||
import { ui } from '../implementation/logging'; | ||
|
||
export function yargsCompareCommandObject() { | ||
const command = 'compare'; | ||
return { | ||
command, | ||
describe: 'Compare 2 report files and create a diff file', | ||
builder: yargsCompareOptionsDefinition(), | ||
handler: async (args: unknown) => { | ||
ui().logger.log(chalk.bold(CLI_NAME)); | ||
ui().logger.info(chalk.gray(`Run ${command}...`)); | ||
|
||
const options = args as CompareOptions & { | ||
persist: Required<PersistConfig>; | ||
}; | ||
|
||
const { before, after, persist } = options; | ||
const outputPath = join( | ||
persist.outputDir, | ||
`${persist.filename}-diff.json`, | ||
); | ||
|
||
await compareReportFiles({ before, after }, outputPath); | ||
|
||
ui().logger.info(`Reports diff written to ${chalk.bold(outputPath)}`); | ||
}, | ||
} satisfies CommandModule; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { join } from 'node:path'; | ||
import { compareReportFiles } from '@code-pushup/core'; | ||
import { PERSIST_FILENAME, PERSIST_OUTPUT_DIR } from '@code-pushup/models'; | ||
import { DEFAULT_CLI_CONFIGURATION } from '../../../mocks/constants'; | ||
import { yargsCli } from '../yargs-cli'; | ||
import { yargsCompareCommandObject } from './compare-command'; | ||
|
||
vi.mock('@code-pushup/core', async () => { | ||
const core: object = await vi.importActual('@code-pushup/core'); | ||
const { CORE_CONFIG_MOCK }: typeof import('@code-pushup/test-utils') = | ||
await vi.importActual('@code-pushup/test-utils'); | ||
return { | ||
...core, | ||
autoloadRc: vi.fn().mockResolvedValue(CORE_CONFIG_MOCK), | ||
compareReportFiles: vi.fn(), | ||
}; | ||
}); | ||
|
||
describe('compare-command', () => { | ||
it('should parse input paths from command line and output path from persist config', async () => { | ||
await yargsCli( | ||
['compare', '--before=source-report.json', '--after=target-report.json'], | ||
{ ...DEFAULT_CLI_CONFIGURATION, commands: [yargsCompareCommandObject()] }, | ||
).parseAsync(); | ||
|
||
expect(compareReportFiles).toHaveBeenCalledWith< | ||
Parameters<typeof compareReportFiles> | ||
>( | ||
{ before: 'source-report.json', after: 'target-report.json' }, | ||
join(PERSIST_OUTPUT_DIR, `${PERSIST_FILENAME}-diff.json`), | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { Diff } from '@code-pushup/utils'; | ||
|
||
export type CompareOptions = Diff<string>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { Options } from 'yargs'; | ||
import type { CompareOptions } from './compare.model'; | ||
|
||
export function yargsCompareOptionsDefinition(): Record< | ||
keyof CompareOptions, | ||
Options | ||
> { | ||
return { | ||
before: { | ||
describe: 'Path to source report.json', | ||
type: 'string', | ||
demandOption: true, | ||
}, | ||
after: { | ||
describe: 'Path to target report.json', | ||
type: 'string', | ||
demandOption: true, | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters