@liascript/devserver
Version:
Run a development server for LiaScript locally
46 lines (40 loc) • 6.5 kB
JavaScript
var e,o=require("minimist"),t=require("path"),n=require("dotenv"),r=require("express"),i=require("fs"),s=require("cors"),l=require("express-handlebars"),a=require("ip"),c=require("open"),p=require("body-parser"),d=require("reloadsh.js"),h=t.resolve(__dirname,"../src");n.config();const _=r();_.use(p.json());var u="",g="",m="",f=[];const v=`<script>
if (!window.LIA) {
window.LIA = {}
}
var filename__ = document.location.search.replace("?"+document.location.origin, "")
window.LIA.lineGoto = function(linenumber) {
fetch("/lineGoto", {
method: "POST",
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({
"linenumber": linenumber,
"filename": filename__
})
}).then(res => {
console.log("Goto line", linenumber);
});
}
const events = new EventSource('/gotoLine');
events.onmessage = (event) => {
try {
const data = JSON.parse(event.data);
if (data.filename == filename__) {
console.log("goto line:", data.linenumber);
window.LIA.gotoLine(data.linenumber)
}
} catch (e) {
console.warn("gotoLine failed")
}
};
</script>`;function w(o,n){u=o||t.join(h,".."),e=n||t.join(u,"node_modules"),g=t.resolve(t.join(e,"reloadsh.js/reloader.browser.js")),m=t.resolve(t.join(e,"@liascript/editor/dist"))}function y(e,o,t){o.writeHead(200,{"Content-Type":"text/event-stream",Connection:"keep-alive","Cache-Control":"no-cache"});let n=`data:
`;o.write(n);let r=Date.now();f.push({id:r,response:o}),e.on("close",()=>{console.log(`${r} Connection closed`),f=f.filter(e=>e.id!==r)})}w(h);const j=o(process.argv.slice(2));function x(){console.log(" _ _ ____ _ _"),console.log("| | (_) __ _/ ___| ___ _ __(_)_ __ | |_"),console.log("| | | |/ _` \\___ \\ / __| '__| | '_ \\| __|"),console.log("| |___| | (_| |___) | (__| | | | |_) | |_ "),console.log("|_____|_|\\__,_|____/ \\___|_| |_| .__/ \\__|"),console.log(" |_|"),console.log()}(j.v||j.version)&&(console.log("DevServer: 1.1.37"),console.log("LiaScript: 0.16.12"),process.exit()),(j.h||j.help)&&(x(),console.log("-h --help show this help"),console.log("-v --version show version information"),console.log("-i --input input README.md file or folder (default: .)"),console.log("-n --hostname hostname of your server (default: localhost)"),console.log("-p --port used port number (default: 3000)"),console.log("-l --live do live reload on file change"),console.log("-o --open open in default browser"),console.log("-t --test test online on https://LiaScript.github.io"),console.log(),console.log("-r --responsiveVoice add optional responsiveVoice support,"),console.log(" or pass your own responsiveVoice key."),console.log(" Adding this feature might slow down"),console.log(" the reloading speed."),console.log(" For more information visit:"),console.log(" https://responsivevoice.org"),process.exit()),x(),w(j.node_modules);try{!function(e,o,n,r,p,h,f,w){e=e||3e3,o=o||"localhost",h=h||!1,p=p||!1,f=f||!1;var j={path:n=n||".",readme:void 0};if(n){let e=i.lstatSync(n);e.isDirectory()?j.path=n:e.isFile()&&(j.path=t.dirname(n),j.readme=t.basename(n))}_.set("view engine","hbs"),_.engine("hbs",l({layoutsDir:t.resolve(t.join(u,"views/layouts")),defaultLayout:"main",extname:"hbs"})),_.set("views",t.resolve(t.join(u,"views"))),_.get("/",function(e,o){o.redirect("/home")}),_.get("/gotoLine",y),_.get("/home*",function(t,n){let r=j.path+"/"+t.params[0],s=i.lstatSync(r);if(s.isDirectory()){let s=i.readdirSync(r).filter(e=>"."!==e[0]),l="/home",a=t.params[0].split("/").filter(e=>""!==e),c=[];for(let e=0;e<a.length;e++)l+="/"+a[e],c.push({name:a[e],href:l});n.render("main",{layout:"index",path:c,file:s.map(n=>({name:n,href:`http://${o}:${e}/home${t.params[0]}/${n}`,isDirectory:i.lstatSync(r+"/"+n).isDirectory()})).sort((e,o)=>e.isDirectory&&!o.isDirectory?-1:!e.isDirectory&&o.isDirectory?1:e.name.toLocaleLowerCase()<o.name.toLocaleLowerCase()?-1:1)})}else s.isFile()?t.params[0].toLocaleLowerCase().endsWith(".md")?f?n.redirect(`https://LiaScript.github.io/course/?http://${o}:${e}/${t.params[0]}`):n.redirect(`/liascript/index.html?http://${o}:${e}/${t.params[0]}`):n.sendFile(t.params[0],{root:j.path}):n.send("ups, something went wrong")}),_.get("/liascript/",function(e,o){o.redirect("/liascript/index.html")}),_.get("/liascript/index.html",function(e,o){p&&r?i.readFile(m+"/index.html","utf8",function(e,t){o.send(t.replace("</head>",`<script type='text/javascript' src='/reloader/reloader.js'></script>
<script type='text/javascript' src='https://code.responsivevoice.org/responsivevoice.js?key=${r}'></script>
${v}
</head>`))}):p?i.readFile(m+"/index.html","utf8",function(e,t){o.send(t.replace("</head>",`<script type='text/javascript' src='/reloader/reloader.js'></script>
${v}
</head>`))}):r?i.readFile(m+"/index.html","utf8",function(e,t){o.send(t.replace("</head>",`<script type='text/javascript' src='https://code.responsivevoice.org/responsivevoice.js?key=${r}'></script>
${v}
</head>`))}):i.readFile(m+"/index.html","utf8",function(e,t){o.send(t.replace("</head>",`${v}</head>`))})}),_.get("/liascript/*",function(e,o){o.sendFile(e.params[0],{root:m})}),_.get("/sw.js",function(e,o){}),_.get("/favicon.ico",function(e,o){}),_.get("/reloader/reloader.js",function(e,o){o.sendFile(g)}),_.post("/lineGoto",function(e,o){return o.json({})}),_.get("/*",s(),function(e,o){o.sendFile(e.originalUrl,{root:j.path})});let x="http://"+o+":"+e;j.path&&j.readme&&(x+="/liascript/index.html?http://"+o+":"+e+"/"+j.readme),f&&j.readme&&(x="https://LiaScript.github.io/course/?http://"+o+":"+e+"/"+j.readme);let $=d(_,p?[t.join(j.path,j.readme||"")]:[]);p&&console.log(`\u{2728} watching for changes on: "${t.join(j.path||"",j.readme||"")}"`),$.on("error",e=>{throw e}),$.listen(e),h&&c(x),console.log("\uD83D\uDCE1 starting server"),console.log(` - local: ${x}`),console.log(` - on your network: ${x.replace(o,a.address())}`)}(j.p||j.port,j.n||j.hostname,j.i||j.input,j.r||j.responsiveVoice||process.env.RESPONSIVE_VOICE_KEY,j.l||j.live,j.o||j.open,j.t||j.test)}catch(e){console.error("Error: ",e.message),process.exit()}console.log("✨ hit Ctrl-c to close the server");
//# sourceMappingURL=index.js.map