1
- import fs , { constants , promises as fsp } from 'node:fs'
1
+ import fs from 'node:fs'
2
2
import { dirname , parse , resolve } from 'node:path'
3
3
import process from 'node:process'
4
+ import { quansync } from 'quansync/macro'
4
5
5
6
export interface FindUpOptions {
6
7
/**
@@ -21,44 +22,73 @@ export interface FindUpOptions {
21
22
allowSymlinks ?: boolean
22
23
}
23
24
24
- function existsSync ( fp : string ) {
25
- try {
26
- fs . accessSync ( fp , constants . R_OK )
27
- return true
28
- }
29
- catch {
30
- return false
31
- }
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
25
+ const isFile = quansync ( {
26
+ sync : ( path : string , allowSymlinks : boolean ) => {
27
+ try {
28
+ return fs [ allowSymlinks ? 'lstatSync' : 'statSync' ] ( path ) . isFile ( )
29
+ }
30
+ catch {
31
+ return false
32
+ }
33
+ } ,
34
+ async : async ( path : string , allowSymlinks : boolean ) => {
35
+ try {
36
+ return ( await fs . promises [ allowSymlinks ? 'lstat' : 'stat' ] ( path ) ) . isFile ( )
37
+ }
38
+ catch {
39
+ return false
40
+ }
41
+ } ,
42
+ } )
41
43
42
- let current = cwd
44
+ export const findUp = quansync (
45
+ async ( paths : string [ ] , options : FindUpOptions = { } ) : Promise < string [ ] > => {
46
+ const {
47
+ cwd = process . cwd ( ) ,
48
+ stopAt = parse ( cwd ) . root ,
49
+ multiple = false ,
50
+ allowSymlinks = true ,
51
+ } = options
43
52
44
- const files : string [ ] = [ ]
53
+ let current = cwd
45
54
46
- const stat = allowSymlinks ? fsp . stat : fsp . lstat
55
+ const files : string [ ] = [ ]
47
56
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
57
+ while ( current && current !== stopAt ) {
58
+ for ( const path of paths ) {
59
+ const filepath = resolve ( current , path )
60
+ if ( await isFile ( filepath , allowSymlinks ) ) {
61
+ files . push ( filepath )
62
+ if ( ! multiple )
63
+ return files
64
+ }
55
65
}
66
+ const parent = dirname ( current )
67
+ if ( parent === current )
68
+ break
69
+ current = parent
56
70
}
57
- const parent = dirname ( current )
58
- if ( parent === current )
59
- break
60
- current = parent
61
- }
62
71
63
- return files
64
- }
72
+ return files
73
+ } ,
74
+ )
75
+
76
+ export const readFile = quansync ( {
77
+ sync : ( path : string ) => fs . readFileSync ( path , 'utf8' ) ,
78
+ async : path => fs . promises . readFile ( path , 'utf8' ) ,
79
+ } )
80
+
81
+ export const writeFile = quansync ( {
82
+ sync : ( path : string , data : string ) => fs . writeFileSync ( path , data ) ,
83
+ async : ( path , data ) => fs . promises . writeFile ( path , data ) ,
84
+ } )
85
+
86
+ export const unlink = quansync ( {
87
+ sync : ( path : string ) => {
88
+ try {
89
+ fs . unlinkSync ( path )
90
+ }
91
+ catch { }
92
+ } ,
93
+ async : path => fs . promises . unlink ( path ) . catch ( ( ) => { } ) ,
94
+ } )
0 commit comments