@@ -6,6 +6,8 @@ import { GeneratorSource } from './Source';
66import { Hook , HookHelper } from './Hook' ;
77import { DirInfo } from './Dir' ;
88import np from 'normalize-path' ;
9+ import { isDir } from './utils' ;
10+ import { getConfirm } from './qa' ;
911
1012import type { Ora } from 'ora' ;
1113import type { SourceInfo } from './Source' ;
@@ -189,13 +191,41 @@ export class Generator<N extends string = string> {
189191 }
190192 }
191193
192- public async clearTemp ( ) {
193- await fs . rm ( this . tempPathname , { recursive : true } ) ;
194+ private async before ( ) {
195+ if ( ! fs . existsSync ( this . target . pathname ) ) return ;
196+ let confirm = false ;
197+ if ( isDir ( this . target . pathname ) )
198+ if ( fs . readdirSync ( this . target . pathname ) . length )
199+ confirm = await getConfirm (
200+ `The content in folder "${ this . target . name } " will be deleted, whether to continue` ,
201+ ) ;
202+ else
203+ confirm = await getConfirm (
204+ `Existing file "${ this . target . name } " will be deleted, whether to continue` ,
205+ ) ;
206+ if ( ! confirm ) process . exit ( 0 ) ;
207+ fs . rmSync ( this . target . pathname , { recursive : true } ) ;
208+ fs . ensureDirSync ( this . target . pathname ) ;
194209 }
195210
196- private async output ( ) {
197- this . spinner . start ( 'Copy Files' ) ;
198- await fs . copy ( this . tempPathname , this . target . pathname , { overwrite : true } ) ;
211+ private async after ( ) {
212+ const pkg = this . targetDirInfo . get ( '/package.json' , { type : 'file' } ) ;
213+ if ( pkg ?. getJson ( ) ) {
214+ const pkgJson = pkg . getJson ( ) ;
215+ pkgJson . name = this . target . name ;
216+ pkg . setContent ( JSON . stringify ( pkgJson ) ) ;
217+ }
218+ }
219+
220+ private async output ( map = this . targetDirInfo . getMap ( ) ) {
221+ this . spinner . start ( 'Output Files' ) ;
222+ for ( const item of Object . values ( map ) ) {
223+ if ( item . isDir ) this . output ( item . getMap ( ) ) ;
224+ else {
225+ fs . ensureFileSync ( item . pathname ) ;
226+ fs . writeFileSync ( item . pathname , ( item as FileInfo ) . getContent ( ) ) ;
227+ }
228+ }
199229 }
200230
201231 private async installDeps ( ) {
@@ -208,11 +238,20 @@ export class Generator<N extends string = string> {
208238
209239 public async generate ( ) {
210240 try {
241+ await this . before ( ) ;
242+ await this . callHooks ( 'beforeGenerate' ) ;
211243
212244 await this . generateBase ( ) ;
213245 await this . generateInject ( ) ;
246+ $ . verbose = false ;
247+
248+ await this . callHooks ( 'afterGenerate' , { targetDir : this . targetDirInfo } ) ;
249+ await this . after ( ) ;
250+
214251 await this . output ( ) ;
215252
253+ await this . callHooks ( 'afterOutput' ) ;
254+
216255 await this . installDeps ( ) ;
217256
218257 this . spinner . succeed (
@@ -225,8 +264,6 @@ export class Generator<N extends string = string> {
225264 chalk . redBright ( this . spinner . text || 'Something error when generating' ) ,
226265 ) ;
227266 console . error ( chalk . redBright ( ( e as Error ) . stack ) ) ;
228- } finally {
229- await this . clearTemp ( ) ;
230267 }
231268 }
232269}
0 commit comments