11import chalk from 'chalk' ;
2- import { ArgumentsCamelCase , CommandModule } from 'yargs' ;
3- import { HistoryOptions , getHashes , history } from '@code-pushup/core' ;
4- import { getCurrentBranchOrTag , safeCheckout , ui } from '@code-pushup/utils' ;
2+ import { CommandModule } from 'yargs' ;
3+ import { HistoryOptions , history } from '@code-pushup/core' ;
4+ import {
5+ LogResult ,
6+ getCurrentBranchOrTag ,
7+ getHashes ,
8+ getSemverTags ,
9+ safeCheckout ,
10+ ui ,
11+ } from '@code-pushup/utils' ;
512import { CLI_NAME } from '../constants' ;
613import { yargsOnlyPluginsOptionsDefinition } from '../implementation/only-plugins.options' ;
714import { HistoryCliOptions } from './history.model' ;
815import { yargsHistoryOptionsDefinition } from './history.options' ;
16+ import { normalizeHashOptions } from './utils' ;
17+
18+ const command = 'history' ;
19+ async function handler ( args : unknown ) {
20+ ui ( ) . logger . info ( chalk . bold ( CLI_NAME ) ) ;
21+ ui ( ) . logger . info ( chalk . gray ( `Run ${ command } ` ) ) ;
22+
23+ const currentBranch = await getCurrentBranchOrTag ( ) ;
24+ const { targetBranch : rawTargetBranch , ...opt } = args as HistoryCliOptions &
25+ HistoryOptions ;
26+ const {
27+ targetBranch,
28+ from,
29+ to,
30+ maxCount,
31+ onlySemverTags,
32+ ...historyOptions
33+ } = await normalizeHashOptions ( {
34+ ...opt ,
35+ targetBranch : rawTargetBranch ?? currentBranch ,
36+ } ) ;
37+
38+ const filterOptions = { targetBranch, from, to, maxCount } ;
39+ const results : LogResult [ ] = onlySemverTags
40+ ? await getSemverTags ( filterOptions )
41+ : await getHashes ( filterOptions ) ;
42+
43+ try {
44+ // run history logic
45+ const reports = await history (
46+ {
47+ targetBranch,
48+ ...historyOptions ,
49+ } ,
50+ results . map ( ( { hash } ) => hash ) ,
51+ ) ;
52+
53+ ui ( ) . logger . log ( `Reports: ${ reports . length } ` ) ;
54+ } finally {
55+ // go back to initial branch
56+ await safeCheckout ( currentBranch ) ;
57+ }
58+ }
959
1060export function yargsHistoryCommandObject ( ) {
11- const command = 'history' ;
1261 return {
1362 command,
1463 describe : 'Collect reports for commit history' ,
@@ -23,38 +72,6 @@ export function yargsHistoryCommandObject() {
2372 ) ;
2473 return yargs ;
2574 } ,
26- handler : async < T > ( args : ArgumentsCamelCase < T > ) => {
27- ui ( ) . logger . info ( chalk . bold ( CLI_NAME ) ) ;
28- ui ( ) . logger . info ( chalk . gray ( `Run ${ command } ` ) ) ;
29-
30- const currentBranch = await getCurrentBranchOrTag ( ) ;
31- const {
32- targetBranch = currentBranch ,
33- forceCleanStatus,
34- maxCount,
35- from,
36- to,
37- ...restOptions
38- } = args as unknown as HistoryCliOptions & HistoryOptions ;
39-
40- // determine history to walk
41- const commits : string [ ] = await getHashes ( { maxCount, from, to } ) ;
42- try {
43- // run history logic
44- const reports = await history (
45- {
46- ...restOptions ,
47- targetBranch,
48- forceCleanStatus,
49- } ,
50- commits ,
51- ) ;
52-
53- ui ( ) . logger . log ( `Reports: ${ reports . length } ` ) ;
54- } finally {
55- // go back to initial branch
56- await safeCheckout ( currentBranch ) ;
57- }
58- } ,
75+ handler,
5976 } satisfies CommandModule ;
6077}
0 commit comments