{
  "schema": 1,
  "id": "ext.liveserver",
  "name": "Live Server",
  "version": "1.0.1",
  "category": "tool",
  "description": "Starts a local server for HTML, PHP and static files in the project folder and reloads the page in the browser as soon as a file changes (CSS without a reload). PHP runs through php -S. Ships with program code.",
  "author": "Lumen",
  "icon": "Lv",
  "color": "#ff8a3d",
  "license": "MIT",
  "minAppVersion": "0.5.0",
  "keywords": [
    "live server",
    "liveserver",
    "live reload",
    "html",
    "php",
    "preview",
    "server",
    "browser"
  ],
  "readme": "# Live Server\n\nServes the project folder over HTTP (HTML, PHP and static files) and reloads\nthe page in the browser when a file changes.\n\n- **Go Live** in the status bar (or the command *Live Server: Start / Stop*) starts the server and opens the active file in the browser.\n- **HTML** gets a small script injected; changes to **CSS** only swap the styles, everything else reloads.\n- **PHP** runs through `php -S` (PHP must be installed); the output is routed through the live server so PHP pages reload too.\n- Folders without `index.html` show a file list. The port, the address, a subfolder (`dist`, `public`), the watched file types, ignored folders and the path to `php` are in the settings.\n\n## Commands\n\n| Command | What it does |\n|---|---|\n| Live Server: Start / Stop | toggles the server |\n| Live Server: Open active file in browser | opens the file at the server address |\n| Live Server: Open the site in the browser | opens the root of the server |\n",
  "settings": [
    {
      "key": "port",
      "label": "Port",
      "type": "number",
      "default": "5500",
      "min": 1024,
      "max": 65000,
      "hint": "If it is taken, the server uses the next free one.",
      "section": "Server"
    },
    {
      "key": "host",
      "label": "Address",
      "type": "text",
      "default": "127.0.0.1",
      "hint": "127.0.0.1 = this machine only; 0.0.0.0 = the network too (a phone, say).",
      "section": "Server"
    },
    {
      "key": "subfolder",
      "label": "Subfolder",
      "type": "text",
      "default": "",
      "placeholder": "e.g. public or dist",
      "hint": "Relative to the project folder. Empty = the project folder itself.",
      "section": "Server"
    },
    {
      "key": "openBrowser",
      "label": "Open the browser on start",
      "type": "toggle",
      "default": "true",
      "section": "Server"
    },
    {
      "key": "liveReload",
      "label": "Reload automatically",
      "type": "toggle",
      "default": "true",
      "hint": "Adds a small script to pages; CSS changes swap the styles only.",
      "section": "Reloading"
    },
    {
      "key": "reloadOn",
      "label": "File types that reload",
      "type": "text",
      "default": "html,htm,php,js,mjs,json,svg,xml,md,txt,twig,phtml",
      "hint": "Comma-separated, without the dot. CSS is always swapped live.",
      "section": "Reloading"
    },
    {
      "key": "ignore",
      "label": "Ignored folders",
      "type": "text",
      "default": "node_modules,.git,vendor,.idea,.vscode",
      "hint": "Comma-separated folder names that are not watched.",
      "section": "Reloading"
    },
    {
      "key": "php",
      "label": "Run PHP",
      "type": "toggle",
      "default": "true",
      "hint": "Starts php -S in the project folder when PHP is found.",
      "section": "PHP"
    },
    {
      "key": "phpPath",
      "label": "Path to php",
      "type": "path",
      "pathKind": "file",
      "default": "",
      "placeholder": "php from PATH",
      "hint": "Leave empty to use php from the PATH.",
      "section": "PHP",
      "when": "php"
    }
  ],
  "pages": [],
  "agents": [],
  "views": [],
  "commands": [
    {
      "id": "liveserver.toggle",
      "title": "Live Server: Start / Stop",
      "category": "Live Server",
      "icon": "radio"
    },
    {
      "id": "liveserver.start",
      "title": "Live Server: Start",
      "category": "Live Server",
      "icon": "radio"
    },
    {
      "id": "liveserver.stop",
      "title": "Live Server: Stop",
      "category": "Live Server",
      "icon": "square"
    },
    {
      "id": "liveserver.open",
      "title": "Live Server: Open active file in browser",
      "category": "Live Server",
      "icon": "globe"
    },
    {
      "id": "liveserver.open-root",
      "title": "Live Server: Open the site in the browser",
      "category": "Live Server",
      "icon": "globe"
    }
  ],
  "code": {
    "main": "import { createRequire as __lumenRequire } from 'node:module'; const require = __lumenRequire(import.meta.url);\nimport j from\"node:path\";import N from\"node:fs\";import S from\"node:fs/promises\";import F from\"node:http\";import X from\"node:net\";import h from\"node:path\";import{spawn as T}from\"node:child_process\";var A=\"/__lumen_livereload\",O=\"/__lumen_livereload.js\",_={\".html\":\"text/html; charset=utf-8\",\".htm\":\"text/html; charset=utf-8\",\".xhtml\":\"application/xhtml+xml; charset=utf-8\",\".css\":\"text/css; charset=utf-8\",\".js\":\"text/javascript; charset=utf-8\",\".mjs\":\"text/javascript; charset=utf-8\",\".json\":\"application/json; charset=utf-8\",\".map\":\"application/json; charset=utf-8\",\".xml\":\"application/xml; charset=utf-8\",\".svg\":\"image/svg+xml\",\".txt\":\"text/plain; charset=utf-8\",\".md\":\"text/plain; charset=utf-8\",\".png\":\"image/png\",\".jpg\":\"image/jpeg\",\".jpeg\":\"image/jpeg\",\".gif\":\"image/gif\",\".webp\":\"image/webp\",\".avif\":\"image/avif\",\".ico\":\"image/x-icon\",\".woff\":\"font/woff\",\".woff2\":\"font/woff2\",\".ttf\":\"font/ttf\",\".otf\":\"font/otf\",\".mp4\":\"video/mp4\",\".webm\":\"video/webm\",\".mp3\":\"audio/mpeg\",\".wav\":\"audio/wav\",\".pdf\":\"application/pdf\",\".wasm\":\"application/wasm\"},z=[\"index.html\",\"index.htm\",\"index.php\"],V=`(() => {\n  const source = new EventSource('${A}')\n  source.addEventListener('reload', () => location.reload())\n  source.addEventListener('css', () => {\n    for (const link of document.querySelectorAll('link[rel~=\"stylesheet\"][href]')) {\n      const url = new URL(link.href, location.href)\n      if (url.origin !== location.origin) continue\n      url.searchParams.set('__lumen', Date.now())\n      const fresh = link.cloneNode()\n      fresh.href = url.href\n      fresh.onload = () => link.remove()\n      link.after(fresh)\n    }\n  })\n})()\n`,C=`<script src=\"${O}\"></script>`;function I(t){let e=t.search(/<\\/body\\s*>/i);return e===-1?t+C:t.slice(0,e)+C+t.slice(e)}function J(t,e){let r=h.relative(t,e);return r===\"\"||!r.startsWith(\"..\")&&!h.isAbsolute(r)}function w(t){return t.replace(/[&<>\"]/g,e=>({\"&\":\"&amp;\",\"<\":\"&lt;\",\">\":\"&gt;\",'\"':\"&quot;\"})[e])}function v(t,e,r=200){let n=`<!doctype html><meta charset=\"utf-8\"><title>${w(t)}</title>\n<style>body{font:14px/1.5 system-ui,sans-serif;max-width:52rem;margin:2rem auto;padding:0 1rem;color:#222}a{color:#2563eb;text-decoration:none}a:hover{text-decoration:underline}li{list-style:none;padding:.1rem 0}code{background:#eee;padding:.1rem .3rem;border-radius:4px}</style>\n${e}`;return{status:r,html:I(n)}}async function K(t,e){return new Promise(r=>{let n=X.createServer();n.once(\"error\",()=>r(!1)),n.once(\"listening\",()=>n.close(()=>r(!0))),n.listen(t,e)})}async function B(t,e,r=20){for(let n=t;n<t+r;n+=1)if(await K(n,e))return n;throw new Error(`No free port from ${t} to ${t+r-1}`)}async function Q(t){return new Promise(e=>{let r=T(t,[\"-v\"],{stdio:\"ignore\"});r.once(\"error\",()=>e(!1)),r.once(\"exit\",n=>e(n===0))})}function b(t,e,r,n){t.writeHead(e,{\"cache-control\":\"no-store\",...r}),t.end(n)}function g(t,e){b(t,e.status,{\"content-type\":_[\".html\"]},e.html)}async function Y(t,e){let r=t.php&&await Q(t.phpPath),n=r?await B(t.port+1e3,\"127.0.0.1\"):0,i=r?T(t.phpPath,[\"-S\",`127.0.0.1:${n}`,\"-t\",t.root],{cwd:t.root,stdio:[\"ignore\",\"ignore\",\"pipe\"]}):null;return i?.stderr.on(\"data\",s=>{for(let o of String(s).split(`\n`))/(Warning|Fatal|Parse error|Notice|Deprecated)/i.test(o)&&e(o.trim())}),i?.on(\"error\",s=>e(`php: ${s.message}`)),t.php&&!r&&e(`php not found (${t.phpPath}) \\u2014 .php files are not run`),{php:i,phpOn:r,phpPort:n}}function Z(t,e,{phpPort:r,liveReload:n}){let i={...t.headers,\"accept-encoding\":\"identity\",host:`127.0.0.1:${r}`},s=F.request({host:\"127.0.0.1\",port:r,path:t.url,method:t.method,headers:i},o=>{let a=String(o.headers[\"content-type\"]??\"\");if(!n||!a.includes(\"text/html\")){e.writeHead(o.statusCode??502,o.headers),o.pipe(e);return}let d=[];o.on(\"data\",p=>d.push(p)),o.on(\"end\",()=>{let p=I(Buffer.concat(d).toString(\"utf8\")),{\"content-length\":P,...m}=o.headers;e.writeHead(o.statusCode??502,m),e.end(p)})});s.on(\"error\",o=>g(e,v(\"PHP\",`<h1>PHP</h1><p>${w(o.message)}</p>`,502))),t.pipe(s)}async function q(t,e,r){let i=(await S.readdir(e,{withFileTypes:!0})).filter(o=>!o.name.startsWith(\".\")).sort((o,a)=>Number(a.isDirectory())-Number(o.isDirectory())||o.name.localeCompare(a.name)).map(o=>{let a=o.name+(o.isDirectory()?\"/\":\"\");return`<li><a href=\"${encodeURI(h.posix.join(r,a))}\">${w(a)}</a></li>`}),s=r===\"/\"?\"\":'<li><a href=\"../\">../</a></li>';g(t,v(r,`<h1>${w(r)}</h1><ul>${s}${i.join(\"\")}</ul>`))}async function D(t,e,r,n){let i=h.extname(e).toLowerCase(),s=_[i]??\"application/octet-stream\";if(i===\".php\"){g(t,v(\"PHP\",`<h1>PHP is not available</h1><p>Install PHP or set its path in the extension's settings \\u2014 <code>${w(h.basename(e))}</code> was not run.</p>`,501));return}if(n&&(i===\".html\"||i===\".htm\"||i===\".xhtml\")){b(t,200,{\"content-type\":s},I(await S.readFile(e,\"utf8\")));return}let o=await S.stat(e);if(t.writeHead(200,{\"content-type\":s,\"content-length\":o.size,\"cache-control\":\"no-store\"}),r.method===\"HEAD\"){t.end();return}N.createReadStream(e).on(\"error\",()=>t.destroy()).pipe(t)}async function tt(t,e,r,n){for(let i of z){let s=h.join(e,i);if(await S.stat(s).then(a=>a.isFile(),()=>!1))return await D(t,s,r,n),!0}return!1}async function et(t,e,r,{root:n,liveReload:i}){let s;try{s=decodeURIComponent(r.pathname)}catch{g(e,v(\"400\",\"<h1>Bad request</h1>\",400));return}let o=h.resolve(n,`.${s}`);if(!J(n,o)){g(e,v(\"403\",\"<h1>Forbidden</h1>\",403));return}let a=await S.stat(o).catch(()=>null);if(!a){g(e,v(\"404\",`<h1>404</h1><p><code>${w(s)}</code> was not found.</p>`,404));return}if(!a.isDirectory()){await D(e,o,t,i);return}if(!s.endsWith(\"/\")){b(e,301,{location:`${r.pathname}/${r.search}`},\"\");return}await tt(e,o,t,i)||await q(e,o,s)}async function rt(t,e,r){let n=new URL(t.url??\"/\",\"http://localhost\");if(n.pathname===A){e.writeHead(200,{\"content-type\":\"text/event-stream\",\"cache-control\":\"no-store\",connection:\"keep-alive\"}),e.write(`retry: 800\n\n`),r.clients.add(e),t.on(\"close\",()=>r.clients.delete(e));return}if(n.pathname===O){b(e,200,{\"content-type\":_[\".js\"]},V);return}if(r.phpOn){Z(t,e,r);return}await et(t,e,n,r)}function nt({root:t,liveReload:e,ignore:r,reloadOn:n,clients:i,log:s}){let o=null,a=\"css\",d=c=>{e&&(c===\"reload\"&&(a=\"reload\"),!o&&(o=setTimeout(()=>{o=null;for(let u of i)u.write(`event: ${a}\ndata: ${Date.now()}\n\n`);a=\"css\"},80)))},p=new Set(r),P=c=>{if(c.split(/[\\\\/]/).some(f=>p.has(f)))return;let l=h.extname(c).toLowerCase();if(l===\".css\"){d(\"css\");return}n.has(l)&&d(\"reload\")},m=null;try{m=N.watch(t,{recursive:!0},(c,u)=>{u&&P(String(u))}),m.on(\"error\",()=>{})}catch(c){s(`Watching ${t} failed (${c.message}) \\u2014 pages reload on save only`)}let x=setInterval(()=>{for(let c of i)c.write(`: ping\n\n`)},2e4);return{changed:P,stop(){clearInterval(x),o&&clearTimeout(o),m?.close()}}}async function W(t){let{root:e,host:r,liveReload:n,ignore:i,log:s}=t,o=new Set(t.reloadOn),a=new Set,{php:d,phpOn:p,phpPort:P}=await Y(t,s),m={root:e,liveReload:n,phpOn:p,phpPort:P,clients:a},x=F.createServer((l,f)=>{rt(l,f,m).catch(H=>{if(f.headersSent){f.destroy();return}g(f,v(\"500\",`<h1>500</h1><pre>${w(String(H?.stack??H))}</pre>`,500))})}),c=await B(t.port,r);await new Promise((l,f)=>{x.once(\"error\",f),x.listen(c,r,l)});let u=nt({root:e,liveReload:n,ignore:i,reloadOn:o,clients:a,log:s});return{port:c,host:r,phpEnabled:p,changed:u.changed,async stop(){u.stop();for(let l of a)l.end();d?.kill(),await new Promise(l=>{x.close(l),x.closeAllConnections?.()})}}}var ot=new Set([\".html\",\".htm\",\".xhtml\",\".php\",\".phtml\",\".svg\",\".md\",\".xml\"]),U=t=>String(t??\"\").split(\",\").map(e=>e.trim()).filter(Boolean),y=(t,e,r=\"\")=>{let n=t.settings.get(e);return n==null||n===\"\"?r:String(n)},k=(t,e,r)=>{let n=t.settings.get(e);return n==null||n===\"\"?r:n===!0||n===\"true\"};function G({ctx:t,st:e}){if(!e.projectRoot)return null;let r=y(t,\"subfolder\");return r?j.resolve(e.projectRoot,r):e.projectRoot}function $({st:t}){return`http://${t.server.host===\"0.0.0.0\"?\"localhost\":t.server.host}:${t.server.port}`}function E(t){let{ctx:e,st:r}=t;if(r.starting){e.statusBar.set(\"live\",{text:\"Live\\u2026\",icon:\"loader\",tooltip:\"Live Server\",command:\"liveserver.toggle\",priority:50});return}if(!r.server){e.statusBar.set(\"live\",{text:\"Go Live\",icon:\"radio\",tooltip:\"Live Server starten\",command:\"liveserver.toggle\",priority:50});return}e.statusBar.set(\"live\",{text:`Port ${r.server.port}`,icon:\"radio-tower\",tone:\"success\",tooltip:`${$(t)} \\u2014 click to stop`,command:\"liveserver.toggle\",priority:50})}function M(t,e){let r=G(t);if(!t.st.server||!r)return null;if(!e)return`${$(t)}/`;let n=j.relative(r,e);if(n.startsWith(\"..\")||j.isAbsolute(n)||!ot.has(j.extname(e).toLowerCase()))return null;let i=n.split(j.sep).map(encodeURIComponent).join(\"/\");return`${$(t)}/${i}`}function it(t,e){return{root:e,port:Number(y(t,\"port\",\"5500\"))||5500,host:y(t,\"host\",\"127.0.0.1\"),liveReload:k(t,\"liveReload\",!0),php:k(t,\"php\",!0),phpPath:y(t,\"phpPath\",\"php\"),ignore:U(y(t,\"ignore\",\"node_modules,.git,vendor,.idea,.vscode\")),reloadOn:U(y(t,\"reloadOn\",\"html,htm,php,js,mjs,json,svg,xml,md,txt,twig,phtml\")).map(r=>`.${r.replace(/^\\./,\"\").toLowerCase()}`),log:r=>t.log(r)}}async function L(t,{open:e=k(t.ctx,\"openBrowser\",!0)}={}){let{ctx:r,st:n}=t;if(n.server||n.starting)return;let i=G(t);if(!i){r.ui.notify(\"Live Server: No project open.\",\"warning\");return}n.starting=!0,E(t);try{n.server=await W(it(r,i))}catch(o){n.server=null,r.ui.notify(`Live Server: ${o.message}`,\"error\");return}finally{n.starting=!1,E(t)}let s=n.server.phpEnabled?\" (with PHP)\":\"\";r.ui.notify(`Live Server running at ${$(t)}${s}`,\"success\"),e&&await r.openExternal(M(t,n.activePath)??`${$(t)}/`)}async function R(t,{quiet:e=!1}={}){let{ctx:r,st:n}=t;if(!n.server)return;let i=n.server;n.server=null,E(t),await i.stop().catch(()=>{}),e||r.ui.notify(\"Live Server gestoppt\",\"info\")}async function st(t){let{ctx:e,st:r}=t;if(r.server||await L(t,{open:!1}),!r.server)return;let n=M(t,r.activePath);if(!n){e.ui.notify(\"Live Server: The active file is not in the served folder or is not a page.\",\"warning\");return}await e.openExternal(n)}async function at(t){t.st.server||await L(t,{open:!1}),t.st.server&&await t.ctx.openExternal(`${$(t)}/`)}function ct(t){let e={\"liveserver.toggle\":()=>t.st.server?R(t):L(t),\"liveserver.start\":()=>L(t),\"liveserver.stop\":()=>R(t),\"liveserver.open\":()=>st(t),\"liveserver.open-root\":()=>at(t)};for(let[r,n]of Object.entries(e))t.ctx.commands.register(r,n)}function lt(t){let{ctx:e,st:r}=t;e.events.on(\"project\",n=>{(n.root??null)!==r.projectRoot&&(r.projectRoot=n.root??null,r.server&&R(t,{quiet:!0}))}),e.events.on(\"activeFile\",n=>{r.activePath=n.path??null}),e.events.on(\"fileSaved\",n=>r.server?.changed(n.path))}function xt(t){let e={server:null,starting:!1,projectRoot:t.events.last(\"project\")?.root??t.workspace.root()??null,activePath:t.events.last(\"activeFile\")?.path??null},r={ctx:t,st:e};return ct(r),lt(r),E(r),()=>{R(r,{quiet:!0}),t.statusBar.set(\"live\",null)}}export{xt as activate};\n"
  },
  "addon": {
    "schema": 1,
    "languages": [],
    "themes": [],
    "commands": [],
    "events": [],
    "templates": [],
    "projectKinds": [],
    "snippets": [],
    "id": "ext.liveserver",
    "name": "Live Server",
    "version": "1.0.1",
    "description": "Starts a local server for HTML, PHP and static files in the project folder and reloads the page in the browser as soon as a file changes (CSS without a reload). PHP runs through php -S. Ships with program code.",
    "author": "Lumen",
    "icon": "Lv",
    "color": "#ff8a3d",
    "category": "tool"
  }
}
