@@ -6,6 +6,7 @@ import { PathString } from '@battis/descriptive-types';
66import { Colors } from '@qui-cli/colors' ;
77import { Log } from '@qui-cli/log' ;
88import { IPackageJson } from 'package-json-type' ;
9+ import * as Confirm from '@qui-cli/init/dist/Init/Confirm/index.js' ;
910
1011type Configuration = Plugin . Configuration & {
1112 scanPath ?: PathString ;
@@ -81,7 +82,7 @@ function init({ values }: Plugin.ExpectedArguments<typeof options>) {
8182 configure ( { scanPath : Positionals . get ( 'scanPath' ) , ...values } ) ;
8283}
8384
84- function run ( ) {
85+ async function run ( ) {
8586 if ( ! config . scanPath ) {
8687 Log . error ( `${ Colors . positionalArg ( 'scanPath' ) } must be defined` ) ;
8788 process . exit ( 1 ) ;
@@ -93,6 +94,7 @@ function run() {
9394 } else {
9495 config . outputPath = path . resolve ( process . cwd ( ) , config . outputPath ) ;
9596 }
97+ const outputPath = config . outputPath ;
9698
9799 const entries = scan ( config . scanPath ) ;
98100 if ( entries ) {
@@ -105,20 +107,15 @@ function run() {
105107 . replace ( '{{TOC}}' , toc ) ;
106108 }
107109 }
108- if ( fs . existsSync ( config . outputPath ) ) {
109- if ( ! config . overwrite ) {
110- Log . error (
111- `Cannot overwrite existing ${ Colors . path ( config . outputPath ) } (set ${ Colors . flagArg ( '--overwrite' ) } )`
112- ) ;
113- Log . info ( toc ) ;
114- process . exit ( 2 ) ;
115- }
116- Log . warning (
117- `Overwriting existing ${ Colors . path ( path . basename ( config . outputPath ) ) } `
118- ) ;
119- }
120- fs . writeFileSync ( config . outputPath , toc ) ;
121- Log . info ( `TOC written to ${ Colors . path ( config . outputPath ) } ` ) ;
110+ await Confirm . withDiff ( {
111+ src : toc ,
112+ dest : fs . existsSync ( config . outputPath )
113+ ? fs . readFileSync ( outputPath , 'utf8' )
114+ : undefined ,
115+ identifier : Colors . path ( outputPath , Colors . keyword ) ,
116+ action : ( ) => fs . writeFileSync ( outputPath , toc ) ,
117+ force : config . overwrite
118+ } ) ;
122119 } else {
123120 Log . error (
124121 `No indexable packages and README files found in ${ Colors . path ( config . scanPath ) } `
0 commit comments