11import fs from 'fs'
22
3- // check if any file exist
4- async function isFileExisted ( path , fileName , res ) {
5- const data = fs . readdirSync ( path )
6-
7- if ( data . includes ( fileName ) ) {
8- res ( [ true , path ] )
9- } else {
10- res ( [ false , path ] )
11- data . forEach ( ( path1 ) => {
12- if ( ! path1 . includes ( '.' ) ) {
13- let path2 = path + '/' + path1
14- const data1 = fs . readdirSync ( path2 )
15- if ( data1 . includes ( fileName ) ) {
16- res ( [ true , path2 ] )
17- } else {
18- res ( [ false , path2 ] )
19- data1 . forEach ( ( path2 ) => {
20- if ( ! path2 . includes ( '.' ) ) {
21- let path3 = path + '/' + path1 + '/' + path2
22- const data2 = fs . readdirSync ( path3 )
23- if ( data2 . includes ( fileName ) ) {
24- res ( [ true , path3 ] )
25- } else {
26- res ( [ false , path3 ] )
27- data2 . forEach ( ( path3 ) => {
28- if ( ! path3 . includes ( '.' ) ) {
29- let path4 = path + '/' + path1 + '/' + path2 + '/' + path3
30- const data3 = fs . readdirSync ( path4 )
31- if ( data3 . includes ( fileName ) ) {
32- res ( [ true , path4 ] )
33- } else {
34- res ( [ false , path4 ] )
35- data3 . forEach ( ( path4 ) => {
36- if ( ! path4 . includes ( '.' ) ) {
37- let path5 = path + '/' + path1 + '/' + path2 + '/' + path3 + '/' + path4
38- const data4 = fs . readdirSync ( path5 )
39- if ( data4 . includes ( fileName ) ) {
40- res ( [ true , path5 ] )
41- } else {
42- res ( [ false , path5 ] )
43- data4 . forEach ( ( path5 ) => {
44- if ( ! path5 . includes ( '.' ) ) {
45- let path6 = path + '/' + path1 + '/' + path2 + '/' + path3 + '/' + path4 + '/' + path5
46- const data5 = fs . readdirSync ( path6 )
47- if ( data5 . includes ( fileName ) ) {
48- res ( [ true , path6 ] )
49- } else {
50- res ( [ false , path6 ] )
51- data5 . forEach ( ( path6 ) => {
52- if ( ! path6 . includes ( '.' ) ) {
53- let path7 = path + '/' + path1 + '/' + path2 + '/' + path3 + '/' + path4 + '/' + path5 + '/' + path6
54- const data6 = fs . readdirSync ( path7 )
55- if ( data6 . includes ( fileName ) ) {
56- res ( [ true , path7 ] )
57- } else {
58- res ( [ false , path7 ] )
59- data6 . forEach ( ( path7 ) => {
60- if ( ! path7 . includes ( '.' ) ) {
61- let path8 = path + '/' + path1 + '/' + path2 + '/' + path3 + '/' + path4 + '/' + path5 + '/' + path6 + '/' + path7
62- const data7 = fs . readdirSync ( path8 )
63- if ( data7 . includes ( fileName ) ) {
64- res ( [ true , path8 ] )
65- } else {
66- res ( [ false , path8 ] )
67- data7 . forEach ( ( path8 ) => {
68- if ( ! path8 . includes ( '.' ) ) {
69- let path9 = path + '/' + path1 + '/' + path2 + '/' + path3 + '/' + path4 + '/' + path5 + '/' + path6 + '/' + path7 + '/' + path8
70- const data8 = fs . readdirSync ( path9 )
71- if ( data8 . includes ( fileName ) ) {
72- res ( [ true , path9 ] )
73- } else {
74- res ( [ false , path9 ] )
75- }
76- }
77- } )
78- }
79- }
80- } )
81- }
82- }
83- } )
84- }
85- }
86- } )
87- }
88- }
89- } )
90- }
91- }
92- } )
93- }
94- }
95- } )
96- }
97- }
98- } )
3+ async function isFileExisted ( path , fileName ) {
4+ const data = fs . readdirSync ( path ) ;
5+ for ( let file of data ) {
6+ const curPath = path + '/' + file ;
7+ if ( fs . statSync ( curPath ) . isDirectory ( ) ) {
8+ const res = await isFileExisted ( curPath , fileName ) ;
9+ if ( res [ 0 ] ) return [ true , res [ 1 ] ] ;
10+ } else if ( file === fileName ) {
11+ return [ true , curPath ] ;
12+ }
9913 }
14+ return [ false , null ] ;
10015}
10116
10217export default isFileExisted
0 commit comments