@@ -15,7 +15,7 @@ function cleanPath(path) {
1515 return homeExpanded ;
1616}
1717
18- function transformAndSave ( files , mode , maxHeaderLevel , minHeaderLevel , title , notitle , entryPrefix , processAll , stdOut , updateOnly ) {
18+ function transformAndSave ( files , mode , maxHeaderLevel , minHeaderLevel , title , notitle , entryPrefix , processAll , stdOut , updateOnly , dryRun ) {
1919 if ( processAll ) {
2020 console . log ( '--all flag is enabled. Including headers before the TOC location.' )
2121 }
@@ -44,17 +44,28 @@ function transformAndSave(files, mode, maxHeaderLevel, minHeaderLevel, title, no
4444 }
4545
4646 unchanged . forEach ( function ( x ) {
47- console . log ( '"%s" is up to date' , x . path ) ;
47+ if ( stdOut ) {
48+ console . log ( '==================\n\n"%s" is up to date' , x . path )
49+ }
50+ else {
51+ console . log ( '"%s" is up to date' , x . path ) ;
52+ }
4853 } ) ;
4954
5055 changed . forEach ( function ( x ) {
5156 if ( stdOut ) {
5257 console . log ( '==================\n\n"%s" should be updated' , x . path )
53- } else {
58+ } else if ( dryRun ) {
59+ console . log ( '"%s" should be updated but wasn\'t due to dry run.' , x . path ) ;
60+ }
61+ else {
5462 console . log ( '"%s" will be updated' , x . path ) ;
5563 fs . writeFileSync ( x . path , x . data , 'utf8' ) ;
5664 }
5765 } ) ;
66+ if ( dryRun && changed . length > 0 ) {
67+ process . exitCode = 1 ;
68+ }
5869}
5970
6071function printUsageAndExit ( isErr ) {
@@ -82,7 +93,7 @@ var modes = {
8293var mode = modes [ 'github' ] ;
8394
8495var argv = minimist ( process . argv . slice ( 2 )
85- , { boolean : [ 'h' , 'help' , 'T' , 'notitle' , 's' , 'stdout' , 'all' , 'u' , 'update-only' ] . concat ( Object . keys ( modes ) )
96+ , { boolean : [ 'h' , 'help' , 'T' , 'notitle' , 's' , 'stdout' , 'all' , 'u' , 'update-only' , 'd' , 'dryrun' ] . concat ( Object . keys ( modes ) )
8697 , string : [ 'title' , 't' , 'maxlevel' , 'm' , 'minlevel' , 'n' , 'entryprefix' ]
8798 , unknown : function ( a ) { return ( a [ 0 ] == '-' ? ( console . error ( 'Unknown option(s): ' + a ) , printUsageAndExit ( true ) ) : true ) ; }
8899 } ) ;
@@ -103,6 +114,7 @@ var entryPrefix = argv.entryprefix || '-';
103114var processAll = argv . all ;
104115var stdOut = argv . s || argv . stdout
105116var updateOnly = argv . u || argv [ 'update-only' ]
117+ var dryRun = argv . d || argv . dryrun || false ;
106118
107119var maxHeaderLevel = argv . m || argv . maxlevel ;
108120if ( maxHeaderLevel && isNaN ( maxHeaderLevel ) ) { console . error ( 'Max. heading level specified is not a number: ' + maxHeaderLevel ) , printUsageAndExit ( true ) ; }
@@ -126,9 +138,14 @@ for (var i = 0; i < argv._.length; i++) {
126138 files = [ { path : target } ] ;
127139 }
128140
129- transformAndSave ( files , mode , maxHeaderLevel , minHeaderLevel , title , notitle , entryPrefix , processAll , stdOut , updateOnly ) ;
141+ transformAndSave ( files , mode , maxHeaderLevel , minHeaderLevel , title , notitle , entryPrefix , processAll , stdOut , updateOnly , dryRun ) ;
130142
131- console . log ( '\nEverything is OK.' ) ;
143+ if ( dryRun && process . exitCode === 1 ) {
144+ console . log ( '\nDocumentation tables of contents are out of date.' ) ;
145+ }
146+ else {
147+ console . log ( '\nEverything is OK.' ) ;
148+ }
132149}
133150
134151module . exports . transform = transform ;
0 commit comments