Skip to content

Commit 75cdc16

Browse files
committed
Standardize findExistingFile
We can't extend the `fs` module though.
1 parent c39701f commit 75cdc16

File tree

2 files changed

+42
-70
lines changed

2 files changed

+42
-70
lines changed

Sources/express/Render.swift

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -171,48 +171,6 @@ public extension Express {
171171
self.render(path: path, options: viewOptions, to: res)
172172
}
173173
}
174-
175-
func lookupTemplatePath(_ template: String, in dir: String,
176-
preferredEngine: String? = nil,
177-
yield: @escaping ( String? ) -> Void)
178-
{
179-
// Hm, Swift only has pathComponents on URL?
180-
// FIXME
181-
182-
let pathesToCheck : [ String ] = { () -> [ String ] in
183-
if let ext = preferredEngine { return [ ext ] + engines.keys }
184-
else { return Array(engines.keys) }
185-
}()
186-
.map {
187-
assert($0.hasPrefix("."))
188-
return "\(dir)/\(template)\($0)"
189-
}
190-
191-
guard !pathesToCheck.isEmpty else { return yield(nil) }
192-
193-
final class State {
194-
var pending : ArraySlice<String>
195-
let yield : ( String? ) -> Void
196-
197-
init(_ pathesToCheck: [ String ], yield: @escaping ( String? ) -> Void) {
198-
pending = pathesToCheck[...]
199-
self.yield = yield
200-
}
201-
202-
func step() {
203-
guard let pathToCheck = pending.popFirst() else { return yield(nil) }
204-
fs.stat(pathToCheck) { error, stat in
205-
guard let stat = stat, stat.isFile() else {
206-
return self.step()
207-
}
208-
self.yield(pathToCheck)
209-
}
210-
}
211-
}
212-
213-
let state = State(pathesToCheck, yield: yield)
214-
state.step()
215-
}
216174
}
217175

218176

Sources/express/View.swift

Lines changed: 42 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)