From 324f203d23e7f86529790ba304baf0d3f43c6cb0 Mon Sep 17 00:00:00 2001 From: MatMoul Date: Fri, 15 May 2026 18:56:22 +0200 Subject: [PATCH] chore: add portable local static file server script --- _serve.sh | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100755 _serve.sh diff --git a/_serve.sh b/_serve.sh new file mode 100755 index 0000000..d1b3576 --- /dev/null +++ b/_serve.sh @@ -0,0 +1,19 @@ +#!/usr/bin/env bash +set -euo pipefail + +PORT="${1:-8000}" +ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" + +cd "$ROOT_DIR" + +if command -v python3 >/dev/null 2>&1; then +exec python3 -m http.server "$PORT" --directory "$ROOT_DIR" +elif command -v python >/dev/null 2>&1; then + exec python -m SimpleHTTPServer "$PORT" +elif command -v node >/dev/null 2>&1; then +exec node -e "const http=require('http'); const fs=require('fs'); const path=require('path'); const root=process.argv[1]; const port=Number(process.argv[2]); const mime={'.html':'text/html','.js':'text/javascript','.css':'text/css','.json':'application/json','.png':'image/png','.jpg':'image/jpeg','.jpeg':'image/jpeg','.svg':'image/svg+xml','.ico':'image/x-icon'}; http.createServer((req,res)=>{let url=decodeURIComponent(req.url.split('?')[0]); if(url==='/' ) url='/'; let filePath=path.join(root,url); fs.stat(filePath,(err,stat)=>{if(!err&&stat.isDirectory()) filePath=path.join(filePath,'index.html'); fs.readFile(filePath,(err,data)=>{if(err){res.statusCode=404; return res.end('Not Found');} res.setHeader('Content-Type', mime[path.extname(filePath)]||'application/octet-stream'); res.end(data);});});}).listen(port,()=>console.log('Serving '+root+' on http://localhost:'+port));" "$ROOT_DIR" "$PORT" +else +echo "Error: python3, python, or node is required to serve the project root." >&2 +exit 1 +fi +