importFilefrom"../file/classes/file";importtype{PageSettings}from"../types/site";import{Path}from"../utils/path";/** * Format a URL with the URL, port, and path. */constformatUrl=({
url,
port,
path,}: {url: string;port: number;path?: string;})=>`${url}${port ? ":"+port : ""}${path??""}`;/** * Remove the full path from the URL. */constwithoutUrl=(fullPath: string,url: string)=>fullPath.replace(url,"");/** * inject a hot reload script into the body iof an html string. */constinjectHotReload=({
htmlString,
websocketPath,}: {htmlString: string;websocketPath: string;})=>{constscript=` <script> console.log('hot reload script loaded'); const socket = new WebSocket('${websocketPath}'); socket.addEventListener('message', function (event) { console.log('Received message from server ', event.data); window.location.reload(); }); </script> `;returnhtmlString.replace("</body>",`${script}</body>`);};/** * make a response to a request for a file with the file */constmakeFileResponse=(file: File,{
siteName,
sourceDir,
rootUrl,
websocketPath,
resourcesDir,
faviconsDir,
targetDir,}: PageSettings&{websocketPath: string})=>{const{ contents, mimeType }=file.serve({
siteName,
rootUrl,
sourceDir,
targetDir,
resourcesDir,
faviconsDir,});letresponseText=mimeType==="text/html"
? injectHotReload({htmlString: contents, websocketPath })
: contents;returnnewResponse(responseText,{headers: {// NEVER cache. this is always a dev server."Cache-Control": "no-store, no-cache, must-revalidate, proxy-revalidate",Pragma: "no-cache",Expires: "0",// content-type (required)"content-type": mimeType,},});};/** * Format page settings according to the provided arguments. */constgetPageSettings=({
url,
port,
siteName,
absolutePathToDirectory,
fallbackDirPath,}: {url: string;port: number;siteName: string;absolutePathToDirectory: Path;fallbackDirPath: string;}): PageSettings=>{constsourceDir=absolutePathToDirectory.toString();constrootUrl=formatUrl({ url, port });constresourcesDir=`${sourceDir}/resources`;constfaviconsDir=`${sourceDir}/favicons`;return{
siteName,
sourceDir,fallbackSourceDir: fallbackDirPath,
faviconsDir,
resourcesDir,
rootUrl,targetDir: absolutePathToDirectory.toString()+"/docs",};};export{withoutUrl,formatUrl,makeFileResponse,getPageSettings};