@@ -115,40 +115,54 @@ extension Express {
115115 }
116116 }
117117
118- lookupFilePath ( pathesToCheck, yield: yield)
118+ FileSystemModule . findExistingFile ( pathesToCheck, where: { $0. isFile ( ) } ,
119+ yield: yield)
119120 }
120-
121- /**
122- * This asynchronously finds the first path in the given set of pathes that
123- * exists.
124- */
125- public func lookupFilePath( _ pathesToCheck: [ String ] ,
121+ }
122+ }
123+
124+ import xsys
125+
126+ public extension FileSystemModule {
127+
128+ /**
129+ * This asynchronously finds the first path in the given set of pathes that
130+ * exists.
131+ */
132+ static func findExistingFile( _ pathesToCheck: [ String ] ,
133+ where statCondition: @escaping
134+ ( stat_struct ) -> Bool ,
126135 yield: @escaping ( String ? ) -> Void )
127- {
128- guard !pathesToCheck. isEmpty else { return yield ( nil ) }
136+ {
137+ guard !pathesToCheck. isEmpty else { return yield ( nil ) }
138+
139+ final class State {
140+ var pending : ArraySlice < String >
141+ let statCondition : ( stat_struct ) -> Bool
142+ let yield : ( String ? ) -> Void
129143
130- final class State {
131- var pending : ArraySlice < String >
132- let yield : ( String ? ) -> Void
133-
134- init ( _ pathesToCheck: [ String ] , yield: @escaping ( String ? ) -> Void ) {
135- pending = pathesToCheck [ ... ]
136- self . yield = yield
137- }
138-
139- func step( ) {
140- guard let pathToCheck = pending. popFirst ( ) else { return yield ( nil ) }
141- fs. stat ( pathToCheck) { error, stat in
142- guard let stat = stat, stat. isFile ( ) else {
143- return self . step ( )
144- }
145- self . yield ( pathToCheck)
144+ init ( _ pathesToCheck: [ String ] ,
145+ statCondition: @escaping ( stat_struct ) -> Bool ,
146+ yield: @escaping ( String ? ) -> Void )
147+ {
148+ pending = pathesToCheck [ ... ]
149+ self . statCondition = statCondition
150+ self . yield = yield
151+ }
152+
153+ func step( ) {
154+ let statCondition = self . statCondition
155+ guard let pathToCheck = pending. popFirst ( ) else { return yield ( nil ) }
156+ fs. stat ( pathToCheck) { error, stat in
157+ guard let stat = stat, statCondition ( stat) else {
158+ return self . step ( )
146159 }
160+ self . yield ( pathToCheck)
147161 }
148162 }
149-
150- let state = State ( pathesToCheck, yield: yield)
151- state. step ( )
152163 }
164+
165+ let state = State ( pathesToCheck, statCondition: statCondition, yield: yield)
166+ state. step ( )
153167 }
154168}
0 commit comments