@liascript/devserver
Version:
Run a development server for LiaScript locally
48 lines (41 loc) • 10.6 kB
JavaScript
#!/usr/bin/env node
var $f14dee9e17222834$var$node_modules,$f14dee9e17222834$var$serverPointer,$aRKkE$minimist=require("minimist"),$aRKkE$path=require("path"),$aRKkE$express=require("express"),$aRKkE$fs=require("fs"),$aRKkE$cors=require("cors"),$aRKkE$expresshandlebars=require("express-handlebars"),$aRKkE$ip=require("ip"),$aRKkE$open=require("open"),$aRKkE$bodyparser=require("body-parser"),$aRKkE$reload=require("reload"),$aRKkE$chokidar=require("chokidar"),$f14dee9e17222834$var$$parcel$__dirname=$aRKkE$path.resolve(__dirname,"../src");const $f14dee9e17222834$var$app=$aRKkE$express();$f14dee9e17222834$var$app.use($aRKkE$bodyparser.json());var $f14dee9e17222834$var$dirname="",$f14dee9e17222834$var$liascriptPath="",$f14dee9e17222834$var$reloadInstance=null,$f14dee9e17222834$var$watcher=null,$f14dee9e17222834$var$clients=[];const $f14dee9e17222834$var$gotoScript=`<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 $f14dee9e17222834$export$2cd8252107eb640b(e,a){$f14dee9e17222834$var$dirname=e||$aRKkE$path.join($f14dee9e17222834$var$$parcel$__dirname,".."),$f14dee9e17222834$var$node_modules=a||$aRKkE$path.join($f14dee9e17222834$var$dirname,"node_modules"),$f14dee9e17222834$var$liascriptPath=$aRKkE$path.resolve($aRKkE$path.join($f14dee9e17222834$var$node_modules,"@liascript/editor/dist"))}async function $f14dee9e17222834$export$b3571188c770cc5a(e,a,r,o,t,$,n,i){e=e||3e3,a=a||"localhost",$=$||!1,t=t||!1,n=n||!1;var s={path:r=r||".",readme:void 0};if(r){let e=$aRKkE$fs.lstatSync(r);e.isDirectory()?s.path=r:e.isFile()&&(s.path=$aRKkE$path.dirname(r),s.readme=$aRKkE$path.basename(r))}if($f14dee9e17222834$var$app.set("view engine","hbs"),$f14dee9e17222834$var$app.engine("hbs",$aRKkE$expresshandlebars({layoutsDir:$aRKkE$path.resolve($aRKkE$path.join($f14dee9e17222834$var$dirname,"views/layouts")),defaultLayout:"main",extname:"hbs"})),$f14dee9e17222834$var$app.set("views",$aRKkE$path.resolve($aRKkE$path.join($f14dee9e17222834$var$dirname,"views"))),t){let e=$aRKkE$path.join(s.path,s.readme||"");console.log(`\u{2728} watching for changes on: "${e}"`);try{$f14dee9e17222834$var$reloadInstance=await $aRKkE$reload($f14dee9e17222834$var$app),($f14dee9e17222834$var$watcher=$aRKkE$chokidar.watch(e,{ignoreInitial:!0,awaitWriteFinish:{stabilityThreshold:100,pollInterval:50}})).on("change",e=>{console.log("📝 file changed, reloading..."),$f14dee9e17222834$var$reloadInstance.reload()}),$f14dee9e17222834$var$watcher.on("error",e=>{console.error("Watcher error:",e)})}catch(e){console.error("Reload could not start:",e)}}$f14dee9e17222834$var$app.get("/",function(e,a){a.redirect("/home")}),$f14dee9e17222834$var$app.get("/gotoLine",$f14dee9e17222834$var$eventsHandler),$f14dee9e17222834$var$app.get("/home*",function(r,o){let t=s.path+"/"+r.params[0],$=$aRKkE$fs.lstatSync(t);if($.isDirectory()){let $=$aRKkE$fs.readdirSync(t).filter(e=>"."!==e[0]),n="/home",i=r.params[0].split("/").filter(e=>""!==e),s=[];for(let e=0;e<i.length;e++)n+="/"+i[e],s.push({name:i[e],href:n});o.render("main",{layout:"index",path:s,file:$.map(o=>({name:o,href:`http://${a}:${e}/home${r.params[0]}/${o}`,isDirectory:$aRKkE$fs.lstatSync(t+"/"+o).isDirectory()})).sort((e,a)=>e.isDirectory&&!a.isDirectory?-1:!e.isDirectory&&a.isDirectory?1:e.name.toLocaleLowerCase()<a.name.toLocaleLowerCase()?-1:1)})}else $.isFile()?r.params[0].toLocaleLowerCase().endsWith(".md")?n?o.redirect(`https://LiaScript.github.io/course/?http://${a}:${e}/${r.params[0]}`):o.redirect(`/liascript/index.html?http://${a}:${e}/${r.params[0]}`):o.sendFile(r.params[0],{root:s.path}):o.send("ups, something went wrong")}),$f14dee9e17222834$var$app.get("/liascript/",function(e,a){a.redirect("/liascript/index.html")}),$f14dee9e17222834$var$app.get("/liascript/index.html",function(e,a){t&&o?$aRKkE$fs.readFile($f14dee9e17222834$var$liascriptPath+"/index.html","utf8",function(e,r){e||!r?a.status(500).send("index.html not found or could not be read"):a.send(r.replace("</head>",`<script type='text/javascript' src='/reload/reload.js'></script>
<script type='text/javascript' src='https://code.responsivevoice.org/responsivevoice.js?key=${o}'></script>
${$f14dee9e17222834$var$gotoScript}
</head>`))}):t?$aRKkE$fs.readFile($f14dee9e17222834$var$liascriptPath+"/index.html","utf8",function(e,r){e||!r?a.status(500).send("index.html not found or could not be read"):a.send(r.replace("</head>",`<script type='text/javascript' src='/reload/reload.js'></script>
${$f14dee9e17222834$var$gotoScript}
</head>`))}):o?$aRKkE$fs.readFile($f14dee9e17222834$var$liascriptPath+"/index.html","utf8",function(e,r){e||!r?a.status(500).send("index.html not found or could not be read"):a.send(r.replace("</head>",`<script type='text/javascript' src='https://code.responsivevoice.org/responsivevoice.js?key=${o}'></script>
${$f14dee9e17222834$var$gotoScript}
</head>`))}):$aRKkE$fs.readFile($f14dee9e17222834$var$liascriptPath+"/index.html","utf8",function(e,r){e||!r?a.status(500).send("index.html not found or could not be read"):a.send(r.replace("</head>",`${$f14dee9e17222834$var$gotoScript}</head>`))})}),$f14dee9e17222834$var$app.get("/liascript/*",function(e,a){a.sendFile(e.params[0],{root:$f14dee9e17222834$var$liascriptPath},r=>{if(r){let r=e.params[0];console.log(`File not found in liascriptPath, trying project.path: ${r}`,s.path),a.sendFile(r,{root:s.path})}})}),$f14dee9e17222834$var$app.get("/sw.js",function(e,a){}),$f14dee9e17222834$var$app.get("/favicon.ico",function(e,a){}),$f14dee9e17222834$var$app.post("/lineGoto",function(e,a){if(i)try{let a=e.body.linenumber,r=e.body.filename;i(a,r)}catch(e){console.warn("lineGoto event with wrong datatype, you have to provide {'linenumber': int, 'filename': string}")}return a.json({})}),$f14dee9e17222834$var$app.use("/*",$aRKkE$cors(),function(e,a,r){if(e.originalUrl.startsWith("/reload/")||e.originalUrl.startsWith("/socket.io"))return r();a.sendFile(e.originalUrl,{root:s.path})});let c="http://"+a+":"+e;s.path&&s.readme&&(c+="/liascript/index.html?http://"+a+":"+e+"/"+s.readme),n&&s.readme&&(c="https://LiaScript.github.io/course/?http://"+a+":"+e+"/"+s.readme);let l=$f14dee9e17222834$var$app.listen(e);l.on("error",e=>{throw e}),$&&$aRKkE$open(c),console.log("📡 starting server"),console.log(` - local: ${c}`),console.log(` - on your network: ${c.replace(a,$aRKkE$ip.address())}`),$f14dee9e17222834$var$serverPointer=l}function $f14dee9e17222834$export$fa6813432f753b0d(){$f14dee9e17222834$var$serverPointer&&$f14dee9e17222834$var$serverPointer.close(),$f14dee9e17222834$var$watcher&&($f14dee9e17222834$var$watcher.close(),$f14dee9e17222834$var$watcher=null),$f14dee9e17222834$var$reloadInstance&&($f14dee9e17222834$var$reloadInstance=null)}function $f14dee9e17222834$export$5d5b784f9a618ec3(e,a){$f14dee9e17222834$var$clients.forEach(r=>r.response.write(`data: ${JSON.stringify({linenumber:e,filename:a})}
`))}function $f14dee9e17222834$var$eventsHandler(e,a,r){a.writeHead(200,{"Content-Type":"text/event-stream",Connection:"keep-alive","Cache-Control":"no-cache"});let o=`data:
`;a.write(o);let t=Date.now();$f14dee9e17222834$var$clients.push({id:t,response:a}),e.on("close",()=>{$f14dee9e17222834$var$clients=$f14dee9e17222834$var$clients.filter(e=>e.id!==t)})}$f14dee9e17222834$export$2cd8252107eb640b($f14dee9e17222834$var$$parcel$__dirname);const $71fcc9ea54afc905$var$argv=$aRKkE$minimist(process.argv.slice(2));function $71fcc9ea54afc905$var$liascript(){console.log(" _ _ ____ _ _"),console.log("| | (_) __ _/ ___| ___ _ __(_)_ __ | |_"),console.log("| | | |/ _` \\___ \\ / __| '__| | '_ \\| __|"),console.log("| |___| | (_| |___) | (__| | | | |_) | |_ "),console.log("|_____|_|\\__,_|____/ \\___|_| |_| .__/ \\__|"),console.log(" |_|"),console.log()}($71fcc9ea54afc905$var$argv.v||$71fcc9ea54afc905$var$argv.version)&&(console.log("DevServer: 1.2.2"),console.log("LiaScript: 1.0.4"),process.exit()),($71fcc9ea54afc905$var$argv.h||$71fcc9ea54afc905$var$argv.help)&&($71fcc9ea54afc905$var$liascript(),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()),$71fcc9ea54afc905$var$liascript(),$f14dee9e17222834$export$2cd8252107eb640b($71fcc9ea54afc905$var$argv.node_modules),$f14dee9e17222834$export$b3571188c770cc5a($71fcc9ea54afc905$var$argv.p||$71fcc9ea54afc905$var$argv.port,$71fcc9ea54afc905$var$argv.n||$71fcc9ea54afc905$var$argv.hostname,$71fcc9ea54afc905$var$argv.i||$71fcc9ea54afc905$var$argv.input,$71fcc9ea54afc905$var$argv.r||$71fcc9ea54afc905$var$argv.responsiveVoice||process.env.RESPONSIVE_VOICE_KEY,$71fcc9ea54afc905$var$argv.l||$71fcc9ea54afc905$var$argv.live,$71fcc9ea54afc905$var$argv.o||$71fcc9ea54afc905$var$argv.open,$71fcc9ea54afc905$var$argv.t||$71fcc9ea54afc905$var$argv.test).catch(e=>{console.error("Error: ",e.message),process.exit()}),console.log("✨ hit Ctrl-c to close the server");
//# sourceMappingURL=index.js.map