1
1
import 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' ;
5
12
import { CLI_NAME } from '../constants' ;
6
13
import { yargsOnlyPluginsOptionsDefinition } from '../implementation/only-plugins.options' ;
7
14
import { HistoryCliOptions } from './history.model' ;
8
15
import { 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
+ }
9
59
10
60
export function yargsHistoryCommandObject ( ) {
11
- const command = 'history' ;
12
61
return {
13
62
command,
14
63
describe : 'Collect reports for commit history' ,
@@ -23,38 +72,6 @@ export function yargsHistoryCommandObject() {
23
72
) ;
24
73
return yargs ;
25
74
} ,
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,
59
76
} satisfies CommandModule ;
60
77
}
0 commit comments