feat: Adds browser-sync / hot reload
This commit is contained in:
17
Sources/App/BrowserSync.swift
Normal file
17
Sources/App/BrowserSync.swift
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
import Foundation
|
||||||
|
import Vapor
|
||||||
|
|
||||||
|
#if DEBUG
|
||||||
|
struct BrowserSyncHandler: LifecycleHandler {
|
||||||
|
func didBoot(_ application: Application) throws {
|
||||||
|
let process = Process()
|
||||||
|
process.executableURL = URL(filePath: "/bin/sh")
|
||||||
|
process.arguments = ["-c", "browser-sync reload"]
|
||||||
|
do {
|
||||||
|
try process.run()
|
||||||
|
} catch {
|
||||||
|
print("Could not auto-reload: \(error)")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
@@ -11,6 +11,10 @@ public func configure(_ app: Application) async throws {
|
|||||||
app.middleware.use(app.sessions.middleware)
|
app.middleware.use(app.sessions.middleware)
|
||||||
app.middleware.use(User.sessionAuthenticator())
|
app.middleware.use(User.sessionAuthenticator())
|
||||||
|
|
||||||
|
#if DEBUG
|
||||||
|
app.lifecycle.use(BrowserSyncHandler())
|
||||||
|
#endif
|
||||||
|
|
||||||
switch app.environment {
|
switch app.environment {
|
||||||
case .production, .development:
|
case .production, .development:
|
||||||
app.databases.use(DatabaseConfigurationFactory.sqlite(.file("db.sqlite")), as: .sqlite)
|
app.databases.use(DatabaseConfigurationFactory.sqlite(.file("db.sqlite")), as: .sqlite)
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import Vapor
|
|
||||||
import Logging
|
import Logging
|
||||||
import NIOCore
|
import NIOCore
|
||||||
import NIOPosix
|
import NIOPosix
|
||||||
|
import Vapor
|
||||||
|
|
||||||
@main
|
@main
|
||||||
enum Entrypoint {
|
enum Entrypoint {
|
||||||
|
|||||||
6
swift-dev
Executable file
6
swift-dev
Executable file
@@ -0,0 +1,6 @@
|
|||||||
|
#!/usr/bin/env zsh
|
||||||
|
touch .build/browser-dev-sync
|
||||||
|
browser-sync start -p localhost:8080 --ws &
|
||||||
|
|
||||||
|
watchexec -w Sources -e .swift -r 'swift build --product App && touch .build/browser-dev-sync' &
|
||||||
|
watchexec -w .build/browser-dev-sync --ignore-nothing -r '.build/debug/App'
|
||||||
Reference in New Issue
Block a user