@@ -45,15 +45,16 @@ class FileTreeSelectionPrompt extends Base {
4545 onlyShowDir : false ,
4646 multiple : false ,
4747 states : false ,
48- selectedList : false ,
4948 } ,
50- ...this . opt
49+ ...this . opt ,
50+ selectedList : this . opt . selectedList || this . opt . default ,
5151 }
5252
5353 // Make sure no default is set (so it won't be printed)
54- this . opt . default = null ;
55- if ( this . opt . selectedList ) {
56- this . selectedList = this . opt . selectedList
54+ // this.opt.default = null;
55+ this . selectedList = this . opt . selectedList ;
56+ if ( this . selectedList ) {
57+ ! Array . isArray ( this . selectedList ) && ( this . selectedList = [ this . selectedList ] )
5758 } else {
5859 if ( this . opt . states ) {
5960 this . selectedList = { } ;
@@ -117,11 +118,12 @@ class FileTreeSelectionPrompt extends Base {
117118 rootNode . open = true ;
118119 if ( this . opt . hideRoot ) {
119120 this . fileTree . children = rootNode . children ;
120- this . active = this . fileTree . children [ 0 ] ;
121+ this . active = this . active || this . fileTree . children [ 0 ] ;
121122 } else {
122- this . active = rootNode . children [ 0 ] ;
123+ this . active = this . active || rootNode . children [ 0 ] ;
123124 }
124125 this . render ( ) ;
126+ this . prepareChildren ( this . active ) ;
125127 }
126128
127129 return this ;
@@ -251,7 +253,41 @@ class FileTreeSelectionPrompt extends Base {
251253 await addValidity ( node ) ;
252254 }
253255
254- ! node . _rootNode && this . render ( ) ;
256+ // When it's single selection and has default value, we should expand to the default file.
257+ if ( this . firstRender && this . opt . default && ! this . opt . multiple ) {
258+ const defaultPath = this . opt . default ;
259+ const exists = fs . existsSync ( defaultPath ) ;
260+
261+ if ( exists ) {
262+ const founded = node . children . find ( item => {
263+ if ( item . path === defaultPath ) {
264+ return true ;
265+ }
266+
267+ if ( defaultPath . includes ( `${ item . path } ${ path . sep } ` ) ) {
268+ return true ;
269+ }
270+ } ) ;
271+
272+ if ( founded ) {
273+ if ( founded . path === defaultPath ) {
274+ this . active = founded ;
275+
276+ let parent = founded . parent ;
277+
278+ while ( parent && ! parent . _rootNode ) {
279+ parent . open = true ;
280+ parent = parent . parent ;
281+ }
282+ }
283+ else {
284+ return await this . prepareChildren ( founded ) ;
285+ }
286+ }
287+ }
288+ }
289+
290+ ! this . firstRender && this . render ( ) ;
255291 }
256292
257293 /**
0 commit comments