Re-read custom `index.html` on updates (#342)

We were reading custom `index.html` only once on launch, which meant people had to restart `carton dev` every time they've updated `index.html` during the build process to see changes they've made.
This commit is contained in:
Max Desiatov 2022-05-20 13:42:23 +01:00 committed by GitHub
parent 1ba7dd5d82
commit c834751074
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 23 additions and 16 deletions

View File

@ -18,7 +18,7 @@ jobs:
swift_version: 5.5
xcode: /Applications/Xcode_13.2.1.app/Contents/Developer
- os: ubuntu-20.04
swift_version: 5.5
swift_version: 5.6
name: Build on ${{ matrix.os }} with Swift ${{ matrix.swift_version }}
timeout-minutes: 40
runs-on: ${{ matrix.os }}

View File

@ -127,7 +127,7 @@ struct Dev: AsyncParsableCommand {
shouldSkipAutoOpen: skipAutoOpen,
port: port,
host: host,
customIndexContent: HTML.readCustomIndexPage(at: customIndexPage, on: localFileSystem),
customIndexPath: customIndexPage.map { AbsolutePath($0, relativeTo: localFileSystem.currentWorkingDirectory!) },
// swiftlint:disable:next force_try
manifest: try! toolchain.manifest.get(),
product: build.product,

View File

@ -39,7 +39,7 @@ struct BrowserTestRunner: TestRunner {
shouldSkipAutoOpen: false,
port: port,
host: host,
customIndexContent: nil,
customIndexPath: nil,
manifest: manifest,
product: nil,
entrypoint: Constants.entrypoint,

View File

@ -24,7 +24,7 @@ extension Application {
let port: Int
let host: String
let mainWasmPath: AbsolutePath
let customIndexContent: String?
let customIndexPath: AbsolutePath?
let manifest: Manifest
let product: ProductDescription?
let entrypoint: Entrypoint
@ -43,11 +43,20 @@ extension Application {
middleware.use(FileMiddleware(publicDirectory: directory))
// register routes
get { _ in
HTML(value: HTML.indexPage(
customContent: configuration.customIndexContent,
entrypointName: configuration.entrypoint.fileName
))
get { (request: Request) -> EventLoopFuture<HTML> in
let customIndexContent: EventLoopFuture<String?>
if let path = configuration.customIndexPath?.pathString {
customIndexContent = request.fileio.collectFile(at: path).map { String(buffer: $0) }
} else {
customIndexContent = request.eventLoop.makeSucceededFuture(nil)
}
return customIndexContent.map {
HTML(value: HTML.indexPage(
customContent: $0,
entrypointName: configuration.entrypoint.fileName
))
}
}
webSocket("watcher") { request, ws in

View File

@ -34,9 +34,7 @@ extension HTML: ResponseEncodable {
))
}
public static func readCustomIndexPage(at path: String?,
on fileSystem: FileSystem) throws -> String?
{
public static func readCustomIndexPage(at path: String?, on fileSystem: FileSystem) throws -> String? {
if let customIndexPage = path {
let content = try localFileSystem.readFileContents(customIndexPage.isAbsolutePath ?
AbsolutePath(customIndexPage) :

View File

@ -95,7 +95,7 @@ public actor Server {
let shouldSkipAutoOpen: Bool
let port: Int
let host: String
let customIndexContent: String?
let customIndexPath: AbsolutePath?
let manifest: Manifest
let product: ProductDescription?
let entrypoint: Entrypoint
@ -108,7 +108,7 @@ public actor Server {
shouldSkipAutoOpen: Bool,
port: Int,
host: String,
customIndexContent: String?,
customIndexPath: AbsolutePath?,
manifest: Manifest,
product: ProductDescription?,
entrypoint: Entrypoint,
@ -120,7 +120,7 @@ public actor Server {
self.shouldSkipAutoOpen = shouldSkipAutoOpen
self.port = port
self.host = host
self.customIndexContent = customIndexContent
self.customIndexPath = customIndexPath
self.manifest = manifest
self.product = product
self.entrypoint = entrypoint
@ -147,7 +147,7 @@ public actor Server {
port: configuration.port,
host: configuration.host,
mainWasmPath: configuration.mainWasmPath,
customIndexContent: configuration.customIndexContent,
customIndexPath: configuration.customIndexPath,
manifest: configuration.manifest,
product: configuration.product,
entrypoint: configuration.entrypoint,