@@ -29,62 +29,63 @@ const main = defineCommand({
29
29
} ,
30
30
} ,
31
31
async setup ( { args } ) {
32
- const fileUpdates = await automd ( {
32
+ const results = await automd ( {
33
33
dir : args . dir ,
34
34
input : args . input ,
35
35
output : args . output ,
36
36
} ) ;
37
37
38
- if ( fileUpdates . length === 0 ) {
38
+ if ( results . length === 0 ) {
39
39
consola . warn ( `No files processed!` ) ;
40
40
process . exit ( 1 ) ;
41
41
}
42
-
43
42
consola . success (
44
- `Automd updated in \`${ relative ( process . cwd ( ) , fileUpdates [ 0 ] . _config . dir ) } \`` ,
43
+ `Automd updated in \`${ relative ( process . cwd ( ) , results [ 0 ] . _config . dir ) } \`` ,
45
44
) ;
45
+ _printResults ( results ) ;
46
+ } ,
47
+ } ) ;
46
48
47
- const changeTypes = {
48
- updated : { label : "updated" , color : getColor ( "blue" ) } ,
49
- noChanges : { label : "no changes" , color : getColor ( "green" ) } ,
50
- alreadyUpdate : { label : "already up-to-date" , color : getColor ( "gray" ) } ,
51
- issues : { label : "with issues" , color : getColor ( "yellow" ) } ,
52
- } ;
53
- const getChangeType = ( res : AutomdResult ) => {
54
- if ( res . updates . length === 0 ) {
55
- return changeTypes . alreadyUpdate ;
56
- }
57
- if ( res . hasIssues ) {
58
- return changeTypes . issues ;
59
- }
60
- return res . hasChanged ? changeTypes . updated : changeTypes . noChanges ;
61
- } ;
49
+ runMain ( main ) ;
62
50
63
- for ( const res of fileUpdates ) {
64
- const [ input , output ] = [ res . input , res . output ] . map ( ( i ) =>
65
- relative ( res . _config . dir , i ) ,
66
- ) ;
67
- const t = getChangeType ( res ) ;
51
+ // --- internal utils ---
68
52
69
- const f = `${ input === output ? ` ${ input } ` : ` ${ input } ~> ${ output } ` } ` ;
53
+ const _types = {
54
+ updated : { label : "updated" , color : getColor ( "blue" ) } ,
55
+ noChanges : { label : "no changes" , color : getColor ( "green" ) } ,
56
+ alreadyUpdate : { label : "already up-to-date" , color : getColor ( "gray" ) } ,
57
+ issues : { label : "with issues" , color : getColor ( "yellow" ) } ,
58
+ } ;
70
59
71
- consola . log ( t . color ( ` ─ ${ f } ${ t . label } ` ) ) ;
60
+ function _printResults ( results : AutomdResult [ ] ) {
61
+ for ( const res of results ) {
62
+ const type = _getChangeType ( res ) ;
63
+ const input = relative ( res . _config . dir , res . input ) ;
64
+ const output = relative ( res . _config . dir , res . output ) ;
65
+ const name = `${ input === output ? ` ${ input } ` : ` ${ input } ~> ${ output } ` } ` ;
66
+ consola . log ( type . color ( ` ─ ${ name } ${ type . label } ` ) ) ;
67
+ }
68
+ const issues = results
69
+ . filter ( ( res ) => res . hasIssues )
70
+ . map ( ( res ) => _formatIssues ( res ) ) ;
71
+ if ( issues . length > 0 ) {
72
+ consola . warn ( `Some issues happened during update:` ) ;
73
+ for ( const issue of issues ) {
74
+ consola . error ( issue ) ;
72
75
}
76
+ }
77
+ }
73
78
74
- const issues = fileUpdates
75
- . filter ( ( f ) => f . hasIssues )
76
- . map (
77
- ( f ) =>
78
- `${ changeTypes . issues . color ( relative ( f . _config . dir , f . input ) ) } \n\n ${ f . updates . flatMap ( ( u ) => u . result . issues ) . join ( "\n" ) } ` ,
79
- ) ;
79
+ function _getChangeType ( res : AutomdResult ) {
80
+ if ( res . updates . length === 0 ) {
81
+ return _types . alreadyUpdate ;
82
+ }
83
+ if ( res . hasIssues ) {
84
+ return _types . issues ;
85
+ }
86
+ return res . hasChanged ? _types . updated : _types . noChanges ;
87
+ }
80
88
81
- if ( issues . length > 0 ) {
82
- consola . warn ( `Some issues happened during update!` ) ;
83
- for ( const issue of issues ) {
84
- consola . error ( issue ) ;
85
- }
86
- }
87
- } ,
88
- } ) ;
89
-
90
- runMain ( main ) ;
89
+ function _formatIssues ( res : AutomdResult ) {
90
+ return `${ _types . issues . color ( relative ( res . _config . dir , res . input ) ) } \n\n ${ res . updates . flatMap ( ( u ) => u . result . issues ) . join ( "\n" ) } ` ;
91
+ }
0 commit comments