1- import fs , { constants , promises as fsp } from 'node:fs'
21import { dirname , parse , resolve } from 'node:path'
32import process from 'node:process'
3+ import { lstat , stat } from '@quansync/fs'
4+ import { quansync } from 'quansync/macro'
45
56export interface FindUpOptions {
67 /**
@@ -21,44 +22,43 @@ export interface FindUpOptions {
2122 allowSymlinks ?: boolean
2223}
2324
24- function existsSync ( fp : string ) {
25+ const isFile = quansync ( async ( path : string , allowSymlinks : boolean ) => {
2526 try {
26- fs . accessSync ( fp , constants . R_OK )
27- return true
27+ return ( await ( allowSymlinks ? lstat : stat ) ( path ) ) . isFile ( )
2828 }
2929 catch {
3030 return false
3131 }
32- }
33-
34- export async function findUp ( paths : string [ ] , options : FindUpOptions = { } ) : Promise < string [ ] > {
35- const {
36- cwd = process . cwd ( ) ,
37- stopAt = parse ( cwd ) . root ,
38- multiple = false ,
39- allowSymlinks = true ,
40- } = options
32+ } )
4133
42- let current = cwd
34+ export const findUp = quansync (
35+ async ( paths : string [ ] , options : FindUpOptions = { } ) : Promise < string [ ] > => {
36+ const {
37+ cwd = process . cwd ( ) ,
38+ stopAt = parse ( cwd ) . root ,
39+ multiple = false ,
40+ allowSymlinks = true ,
41+ } = options
4342
44- const files : string [ ] = [ ]
43+ let current = cwd
4544
46- const stat = allowSymlinks ? fsp . stat : fsp . lstat
45+ const files : string [ ] = [ ]
4746
48- while ( current && current !== stopAt ) {
49- for ( const path of paths ) {
50- const filepath = resolve ( current , path )
51- if ( existsSync ( filepath ) && ( await stat ( filepath ) ) . isFile ( ) ) {
52- files . push ( filepath )
53- if ( ! multiple )
54- return files
47+ while ( current && current !== stopAt ) {
48+ for ( const path of paths ) {
49+ const filepath = resolve ( current , path )
50+ if ( await isFile ( filepath , allowSymlinks ) ) {
51+ files . push ( filepath )
52+ if ( ! multiple )
53+ return files
54+ }
5555 }
56+ const parent = dirname ( current )
57+ if ( parent === current )
58+ break
59+ current = parent
5660 }
57- const parent = dirname ( current )
58- if ( parent === current )
59- break
60- current = parent
61- }
6261
63- return files
64- }
62+ return files
63+ } ,
64+ )
0 commit comments