11import { APTPackages } from "@discloudapp/api-types/v2" ;
22import { checkbox , confirm , number , search , select } from "@inquirer/prompts" ;
3- import { existsSync } from "fs" ;
4- import { readdir , stat } from "fs/promises" ;
3+ import { open , readdir } from "fs/promises" ;
54import { dirname , join , sep } from "path/posix" ;
65import { promptTrier } from "../utils" ;
76
@@ -19,47 +18,54 @@ export function promptAppAutoRestart(): Promise<boolean> {
1918 } ) ) ;
2019}
2120
21+ const localeOptions : Intl . CollatorOptions = { sensitivity : "accent" } ;
22+ const dot = "." ;
23+
2224export function promptAppMain ( ) : Promise < string > {
2325 return promptTrier ( ( ) => search ( {
2426 message : "Input the main file path of your app" ,
2527 async source ( term , _opt ) {
26- const result : string [ ] = [ ] ;
27-
2828 if ( term ) {
2929 let dir ;
30- if ( existsSync ( term ) ) {
31- const fileStat = await stat ( term ) ;
30+ try {
31+ const fileHandle = await open ( term ) ;
32+ const fileStat = await fileHandle . stat ( ) ;
3233 if ( fileStat . isFile ( ) ) return [ term ] ;
3334 dir = term ;
34- } else {
35+ } catch ( _ ) {
3536 dir = dirname ( term ) ;
3637 }
3738
3839 term = term . toLowerCase ( ) ;
3940
4041 const files = await readdir ( dir , { withFileTypes : true } ) ;
4142
42- files . sort ( ( a , b ) => a . name . localeCompare ( b . name , void 0 , { sensitivity : "accent" } ) +
43+ files . sort ( ( a , b ) => a . name . localeCompare ( b . name , undefined , localeOptions ) +
4344 ( a . isDirectory ( ) ? - 2 : 2 ) + ( b . isDirectory ( ) ? 2 : - 2 ) ) ;
4445
46+ const result : string [ ] = [ ] ;
47+
4548 for ( let i = 0 ; i < files . length ; i ++ ) {
4649 const file = files [ i ] ;
4750
4851 const filename = join ( dir , file . name ) ;
4952
50- if ( filename . toLowerCase ( ) . includes ( term ) ) result . push ( filename + ( file . isDirectory ( ) ? sep : "" ) ) ;
53+ if ( filename . toLowerCase ( ) . includes ( term ) )
54+ result . push ( filename + ( file . isDirectory ( ) ? sep : "" ) ) ;
5155 }
5256
5357 result . sort ( ( a , b ) => a . toLowerCase ( ) . indexOf ( term ! ) - b . toLowerCase ( ) . indexOf ( term ! ) ) ;
5458
5559 return result ;
5660 }
5761
58- const files = await readdir ( "." , { withFileTypes : true } ) ;
62+ const files = await readdir ( dot , { withFileTypes : true } ) ;
5963
60- files . sort ( ( a , b ) => a . name . localeCompare ( b . name , void 0 , { sensitivity : "accent" } ) +
64+ files . sort ( ( a , b ) => a . name . localeCompare ( b . name , undefined , localeOptions ) +
6165 ( a . isDirectory ( ) ? - 2 : 2 ) + ( b . isDirectory ( ) ? 2 : - 2 ) ) ;
6266
67+ const result : string [ ] = [ ] ;
68+
6369 for ( let i = 0 ; i < files . length ; i ++ ) {
6470 const file = files [ i ] ;
6571
@@ -69,10 +75,11 @@ export function promptAppMain(): Promise<string> {
6975 return result ;
7076 } ,
7177 async validate ( value ) {
72- if ( typeof value === "string" && existsSync ( value ) ) {
73- const fileStat = await stat ( value ) ;
78+ try {
79+ const fileHandle = await open ( value ) ;
80+ const fileStat = await fileHandle . stat ( ) ;
7481 if ( fileStat . isFile ( ) ) return true ;
75- }
82+ } catch ( _ ) { }
7683 return false ;
7784 } ,
7885 } ) ) ;
0 commit comments